Subversion Repositories HomeAutomation

Rev

Rev 671 | Rev 697 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 671 Rev 693
Line 8... Line 8...
8
 */
8
 */
9
 
9
 
10
/*-----------------------------------------------------------------------------
10
/*-----------------------------------------------------------------------------
11
 * Includes
11
 * Includes
12
 *---------------------------------------------------------------------------*/
12
 *---------------------------------------------------------------------------*/
-
 
13
 
13
/* system files */
14
/* system files */
14
#include <avr/io.h>
15
#include <avr/io.h>
15
#include <avr/interrupt.h>
16
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
17
#include <avr/wdt.h>
17
#include <avr/eeprom.h>
18
#include <avr/eeprom.h>
18
#include <stdio.h>
19
#include <stdio.h>
19
#include <string.h>
20
#include <string.h>
-
 
21
 
-
 
22
#include <config.h>
20
/* lib files */
23
/* lib files */
-
 
24
#ifdef USE_STDCAN
-
 
25
#include <stdcan.h>
-
 
26
#else
21
#include <mcp2515.h>
27
#include <mcp2515.h>
-
 
28
#endif
22
#include <serial.h>
29
#include <serial.h>
23
#include <uart.h>
30
#include <uart.h>
24
#include <timebase.h>
31
#include <timebase.h>
-
 
32
 
-
 
33
#ifdef USE_STDCAN
-
 
34
#if SENDTIMESTAMP == 1
-
 
35
#error SENDTIMESTAMP not implemented with StdCan yet.
-
 
36
#endif
-
 
37
#endif
25
 
38
 
26
 
39
 
27
/*-----------------------------------------------------------------------------
40
/*-----------------------------------------------------------------------------
28
 * Defines
41
 * Defines
29
 *---------------------------------------------------------------------------*/
42
 *---------------------------------------------------------------------------*/
30
#define UART_START_BYTE 253
43
#define UART_START_BYTE 253
31
#define UART_END_BYTE 250
44
#define UART_END_BYTE 250
32
 
45
 
-
 
46
#ifndef USE_STDCAN
33
volatile Can_Message_t rxMsg; // Message storage
47
volatile Can_Message_t rxMsg; // Message storage
34
volatile uint8_t rxMsgFull;   // Synchronization flag
48
volatile uint8_t rxMsgFull;   // Synchronization flag
-
 
49
#endif
35
 
50
 
36
const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
51
const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
37
 
52
 
38
union {
53
union {
39
    uint32_t packed;
54
    uint32_t packed;
Line 85... Line 100...
85
 *
100
 *
86
 * @param c
101
 * @param c
87
 *      The received UART byte.
102
 *      The received UART byte.
88
 */
103
 */
89
void UartParseByte(uint8_t c) {
104
void UartParseByte(uint8_t c) {
-
 
105
#ifndef USE_STDCAN
90
    static Can_Message_t cm;
106
    static Can_Message_t cm;
-
 
107
#else
-
 
108
    static StdCan_Msg_t cm;
-
 
109
#endif
91
    static uint8_t waitingMessage = 0;
110
    static uint8_t waitingMessage = 0;
92
    static uint32_t startTime = 0;
111
    static uint32_t startTime = 0;
93
    static int8_t count = 0;
112
    static int8_t count = 0;
94
   
113
   
95
    /* 50ms timeout */
114
    /* 50ms timeout */
Line 102... Line 121...
102
        startTime = Timebase_CurrentTime();
121
        startTime = Timebase_CurrentTime();
103
        /* UART END */
122
        /* UART END */
104
        if (count >= 15) {
123
        if (count >= 15) {
105
            if (c == UART_END_BYTE) {
124
            if (c == UART_END_BYTE) {
106
                PORTC ^= (1<<PC0);
125
                PORTC ^= (1<<PC0);
-
 
126
#ifndef USE_STDCAN
107
                Can_Send(&cm);
127
                Can_Send(&cm);
-
 
128
#else
-
 
129
                StdCan_Put(&cm);
-
 
130
#endif
108
            }
131
            }
109
            waitingMessage = 0;
132
            waitingMessage = 0;
110
            return;
133
            return;
111
        }
134
        }
112
        /* data */
135
        /* data */
113
        else if (count >= 7) {
136
        else if (count >= 7) {
-
 
137
#ifndef USE_STDCAN
114
            cm.Data.bytes[count-7] = c;
138
            cm.Data.bytes[count-7] = c;
-
 
139
#else
-
 
140
            cm.Data[count-7] = c;
-
 
141
#endif
115
            count++;
142
            count++;
116
            return;
143
            return;
117
        }
144
        }
118
        /* data length */
145
        /* data length */
119
        else if (count >= 6) {
146
        else if (count >= 6) {
-
 
147
#ifndef USE_STDCAN
120
            cm.DataLength = c;
148
            cm.DataLength = c;
-
 
149
#else
-
 
150
            cm.Length = c;
-
 
151
#endif
121
            count++;
152
            count++;
122
            return;
153
            return;
123
        }
154
        }
124
        /* remote request flag */
155
        /* remote request flag */
125
        else if (count >= 5) {
156
        else if (count >= 5) {
-
 
157
#ifndef USE_STDCAN
126
            cm.RemoteFlag = c;
158
            cm.RemoteFlag = c;
-
 
159
#endif
127
            count++;
160
            count++;
128
            return;
161
            return;
129
        }
162
        }
130
        /* extended */
163
        /* extended */
131
        else if (count >= 4) {
164
        else if (count >= 4) {
-
 
165
#ifndef USE_STDCAN
132
            cm.ExtendedFlag = c;
166
            cm.ExtendedFlag = c;
-
 
167
#endif
133
            count++;
168
            count++;
134
            return;
169
            return;
135
        }
170
        }
136
        /* ident */
171
        /* ident */
137
        else if (count >= 0) {
172
        else if (count >= 0) {
Line 148... Line 183...
148
        cm.Id = 0;
183
        cm.Id = 0;
149
        return;
184
        return;
150
    }
185
    }
151
}
186
}
152
 
187
 
-
 
188
#ifndef USE_STDCAN
153
void Can_Process(Can_Message_t* msg) {
189
void Can_Process(Can_Message_t* msg) {
154
    if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames.
190
    if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames.
155
 
191
 
156
    if (!rxMsgFull) {
192
    if (!rxMsgFull) {
157
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
193
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
158
        rxMsgFull = 1;
194
        rxMsgFull = 1;
159
    }
195
    }
160
}
196
}
-
 
197
#endif
161
 
198
 
162
 
199
 
163
/*-----------------------------------------------------------------------------
200
/*-----------------------------------------------------------------------------
164
 * Main Program
201
 * Main Program
165
 *---------------------------------------------------------------------------*/
202
 *---------------------------------------------------------------------------*/
166
int main(void) {
203
int main(void) {
167
//  For calibrating internal oscillator
204
//  For calibrating internal oscillator
168
//  OSCCAL = eeprom_read_byte(0);
205
//  OSCCAL = eeprom_read_byte(0);
169
    Timebase_Init();
206
    Timebase_Init();
170
    Serial_Init();
207
    Serial_Init();
-
 
208
#ifndef USE_STDCAN
171
    Can_Init();
209
    Can_Init();
-
 
210
#else
-
 
211
    StdCan_Init(0);
-
 
212
#endif  
172
   
213
   
173
    DDRC = 1<<PC1 | 1<<PC0;
214
    DDRC = 1<<PC1 | 1<<PC0;
174
    PORTC = (1<<PC1) | (1<<PC0);
215
    PORTC = (1<<PC1) | (1<<PC0);
175
   
216
   
176
    sei();
217
    sei();
177
   
218
   
178
    Can_Message_t timeMsg;
-
 
179
#if SENDTIMESTAMP == 1
219
#if SENDTIMESTAMP == 1
-
 
220
    Can_Message_t timeMsg;
180
    uint32_t time = Timebase_CurrentTime();
221
    uint32_t time = Timebase_CurrentTime();
181
    uint32_t time1 = time;
222
    uint32_t time1 = time;
182
    uint32_t unixtime = 0;
223
    uint32_t unixtime = 0;
183
#endif
224
#endif
184
    uint16_t rxByte;
225
    uint16_t rxByte;
Line 188... Line 229...
188
    timeMsg.ExtendedFlag = 1;
229
    timeMsg.ExtendedFlag = 1;
189
    timeMsg.RemoteFlag = 0;
230
    timeMsg.RemoteFlag = 0;
190
    timeMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE);
231
    timeMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE);
191
    //timeMsg.Id = 0; //Same thing, and lib's can.h is not updated.
232
    //timeMsg.Id = 0; //Same thing, and lib's can.h is not updated.
192
    timeMsg.DataLength = 8;
233
    timeMsg.DataLength = 8;
-
 
234
#endif
-
 
235
   
-
 
236
#ifdef USE_STDCAN
-
 
237
    StdCan_Msg_t rxMsg;
193
#endif
238
#endif  
194
   
239
   
195
    /* main loop */
240
    /* main loop */
196
    while (1) {
241
    while (1) {
197
        /* any new CAN messages received? */
242
        /* any new CAN messages received? */
-
 
243
#ifndef USE_STDCAN
198
        if (rxMsgFull) {
244
        if (rxMsgFull) {
-
 
245
#else
-
 
246
        if (StdCan_Get(&rxMsg) == StdCan_Ret_OK) {
-
 
247
#endif
199
            // Toggle activity LED
248
            // Toggle activity LED
200
            PORTC ^= (1<<PC1);
249
            PORTC ^= (1<<PC1);
201
            /* send message to CanWatcher */
250
            /* send message to CanWatcher */
202
            uart_putc(UART_START_BYTE);
251
            uart_putc(UART_START_BYTE);
203
            uart_putc((uint8_t)rxMsg.Id);
252
            uart_putc((uint8_t)rxMsg.Id);
204
            uart_putc((uint8_t)(rxMsg.Id>>8));
253
            uart_putc((uint8_t)(rxMsg.Id>>8));
205
            uart_putc((uint8_t)(rxMsg.Id>>16));
254
            uart_putc((uint8_t)(rxMsg.Id>>16));
206
            uart_putc((uint8_t)(rxMsg.Id>>24));
255
            uart_putc((uint8_t)(rxMsg.Id>>24));
-
 
256
#ifndef USE_STDCAN
207
            uart_putc(rxMsg.ExtendedFlag);
257
            uart_putc(rxMsg.ExtendedFlag);
208
            uart_putc(rxMsg.RemoteFlag);
258
            uart_putc(rxMsg.RemoteFlag);
209
            uart_putc(rxMsg.DataLength);
259
            uart_putc(rxMsg.DataLength);
210
            for (i=0; i<8; i++) {
260
            for (i=0; i<8; i++) {
211
                uart_putc(rxMsg.Data.bytes[i]);
261
                uart_putc(rxMsg.Data.bytes[i]);
212
            }
262
            }
-
 
263
#else
-
 
264
            uart_putc(1);
-
 
265
            uart_putc(0);
-
 
266
            uart_putc(rxMsg.Length);
-
 
267
            for (i=0; i<8; i++) {
-
 
268
                uart_putc(rxMsg.Data[i]);
-
 
269
            }
-
 
270
#endif
213
            uart_putc(UART_END_BYTE);
271
            uart_putc(UART_END_BYTE);
214
           
272
           
-
 
273
#ifndef USE_STDCAN
215
            rxMsgFull = 0;
274
            rxMsgFull = 0;
-
 
275
#endif
216
        }
276
        }
217
       
277
       
218
        /* any UART bytes received? */
278
        /* any UART bytes received? */
219
        rxByte = uart_getc();
279
        rxByte = uart_getc();
220
        while (rxByte != UART_NO_DATA) {
280
        while (rxByte != UART_NO_DATA) {