Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
63 jimmy 1
/**
2
 * CAN test program.
3
 *
4
 * @target  AVR
5
 * @date    2006-10-29
380 eqlazer 6
 * @author  Jimmy Myhrman, Andreas Fritiofsson
63 jimmy 7
 *  
8
 */
9
 
10
/*-----------------------------------------------------------------------------
11
 * Includes
12
 *---------------------------------------------------------------------------*/
13
/* system files */
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
356 andfri 17
#include <avr/eeprom.h>
63 jimmy 18
#include <stdio.h>
665 arune 19
#include <string.h>
63 jimmy 20
/* lib files */
665 arune 21
#include <mcp2515.h>
149 jimmy 22
#include <serial.h>
63 jimmy 23
#include <uart.h>
24
#include <timebase.h>
25
 
26
 
27
/*-----------------------------------------------------------------------------
28
 * Defines
29
 *---------------------------------------------------------------------------*/
30
#define UART_START_BYTE 253
31
#define UART_END_BYTE 250
32
 
665 arune 33
volatile Can_Message_t rxMsg; // Message storage
34
volatile uint8_t rxMsgFull;   // Synchronization flag
35
 
356 andfri 36
const uint8_t days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
63 jimmy 37
 
356 andfri 38
union {
39
    uint32_t packed;
40
    struct date_t {
41
        unsigned ss:6;
42
        unsigned mm:6;
43
        unsigned hh:5;
44
        unsigned DD:5;
45
        unsigned MM:4;
46
        unsigned YY:6;
47
    } unpacked;
48
} date;
49
 
665 arune 50
#if SENDTIMESTAMP == 1
356 andfri 51
void IncTime(void) {
52
    static uint8_t YY=07, MM=03, DD=02, hh=23, mm=58, ss=00;
53
 
54
    if (ss++ > 59) {
55
        ss=0;
56
        if (mm++ > 59) {
57
            mm=0;
58
            if (hh++ > 23) {
59
                hh=0;
380 eqlazer 60
                if (DD++ > days[MM-1] + ((YY%4) & (MM==2)) ? 1 : 0) {
356 andfri 61
                    DD=1;
62
                    if (MM++ > 12) {
63
                        MM=1; YY++;
64
                    }
65
                }
66
            }
67
        }
68
    }
69
    date.unpacked.YY = YY;
70
    date.unpacked.MM = MM;
71
    date.unpacked.DD = DD;
72
    date.unpacked.hh = hh;
73
    date.unpacked.mm = mm;
74
    date.unpacked.ss = ss;
75
}
665 arune 76
#endif
63 jimmy 77
/*-----------------------------------------------------------------------------
78
 * Functions
79
 *---------------------------------------------------------------------------*/
80
/**
81
 * Parses an incoming UART byte by maintaining a state machine. The UART
82
 * bytes will build up a CAN message according to the protocol described here:
83
 *
84
 * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara
85
 *
86
 * @param c
87
 *      The received UART byte.
88
 */
89
void UartParseByte(uint8_t c) {
127 jimmy 90
    static Can_Message_t cm;
63 jimmy 91
    static uint8_t waitingMessage = 0;
92
    static uint32_t startTime = 0;
93
    static int8_t count = 0;
94
 
95
    /* 50ms timeout */
127 jimmy 96
    if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) {
63 jimmy 97
        waitingMessage = 0;
69 jimmy 98
    }
63 jimmy 99
 
100
    if (waitingMessage) {
101
        /* save start time */
127 jimmy 102
        startTime = Timebase_CurrentTime();
63 jimmy 103
        /* UART END */
104
        if (count >= 15) {
69 jimmy 105
            if (c == UART_END_BYTE) {
106
                PORTC ^= (1<<PC0);
127 jimmy 107
                Can_Send(&cm);
69 jimmy 108
            }
63 jimmy 109
            waitingMessage = 0;
110
            return;
111
        }
112
        /* data */
113
        else if (count >= 7) {
114
            cm.Data.bytes[count-7] = c;
115
            count++;
116
            return;
117
        }
118
        /* data length */
119
        else if (count >= 6) {
120
            cm.DataLength = c;
121
            count++;
122
            return;
123
        }
124
        /* remote request flag */
125
        else if (count >= 5) {
126
            cm.RemoteFlag = c;
127
            count++;
128
            return;
129
        }
130
        /* extended */
131
        else if (count >= 4) {
132
            cm.ExtendedFlag = c;
133
            count++;
134
            return;
135
        }
136
        /* ident */
137
        else if (count >= 0) {
138
            cm.Id += ((uint32_t)c << (count*8));
139
            count++;
140
            return;
141
        }
142
    }
143
 
144
    if (c == UART_START_BYTE && !waitingMessage) {
145
        waitingMessage = 1;
127 jimmy 146
        startTime = Timebase_CurrentTime();
63 jimmy 147
        count = 0;
148
        cm.Id = 0;
149
        return;
150
    }
151
}
152
 
665 arune 153
void Can_Process(Can_Message_t* msg) {
154
    if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames.
63 jimmy 155
 
665 arune 156
    if (!rxMsgFull) {
157
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
158
        rxMsgFull = 1;
159
    }
160
}
161
 
162
 
63 jimmy 163
/*-----------------------------------------------------------------------------
164
 * Main Program
165
 *---------------------------------------------------------------------------*/
166
int main(void) {
380 eqlazer 167
//  For calibrating internal oscillator
168
//  OSCCAL = eeprom_read_byte(0);
127 jimmy 169
    Timebase_Init();
149 jimmy 170
    Serial_Init();
127 jimmy 171
    Can_Init();
63 jimmy 172
 
173
    DDRC = 1<<PC1 | 1<<PC0;
174
    PORTC = (1<<PC1) | (1<<PC0);
175
 
176
    sei();
177
 
356 andfri 178
    Can_Message_t timeMsg;
665 arune 179
#if SENDTIMESTAMP == 1
356 andfri 180
    uint32_t time = Timebase_CurrentTime();
181
    uint32_t time1 = time;
182
    uint32_t unixtime = 0;
665 arune 183
#endif
63 jimmy 184
    uint16_t rxByte;
185
    uint8_t i = 0;
186
 
665 arune 187
#if SENDTIMESTAMP == 1
356 andfri 188
    timeMsg.ExtendedFlag = 1;
189
    timeMsg.RemoteFlag = 0;
665 arune 190
    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.
356 andfri 192
    timeMsg.DataLength = 8;
665 arune 193
#endif
356 andfri 194
 
63 jimmy 195
    /* main loop */
196
    while (1) {
197
        /* any new CAN messages received? */
665 arune 198
        if (rxMsgFull) {
380 eqlazer 199
            // Toggle activity LED
63 jimmy 200
            PORTC ^= (1<<PC1);
201
            /* send message to CanWatcher */
202
            uart_putc(UART_START_BYTE);
203
            uart_putc((uint8_t)rxMsg.Id);
204
            uart_putc((uint8_t)(rxMsg.Id>>8));
205
            uart_putc((uint8_t)(rxMsg.Id>>16));
206
            uart_putc((uint8_t)(rxMsg.Id>>24));
207
            uart_putc(rxMsg.ExtendedFlag);
208
            uart_putc(rxMsg.RemoteFlag);
209
            uart_putc(rxMsg.DataLength);
210
            for (i=0; i<8; i++) {
211
                uart_putc(rxMsg.Data.bytes[i]);
212
            }
213
            uart_putc(UART_END_BYTE);
665 arune 214
 
215
            rxMsgFull = 0;
63 jimmy 216
        }
217
 
218
        /* any UART bytes received? */
219
        rxByte = uart_getc();
220
        while (rxByte != UART_NO_DATA) {
221
            /* parse byte! */
222
            UartParseByte((uint8_t)(rxByte & 0x00FF));
223
            /* receive next */
224
            rxByte = uart_getc();
225
        }
356 andfri 226
 
665 arune 227
#if SENDTIMESTAMP == 1
356 andfri 228
        time1 = Timebase_CurrentTime();
229
        if ((time1 - time) > 1000) {
230
            time = time1;
231
            timeMsg.Data.dwords[0] = ++unixtime;
232
            IncTime();
233
            timeMsg.Data.dwords[1] = date.packed;
234
            Can_Send(&timeMsg);
235
        }
665 arune 236
#endif
63 jimmy 237
    }
238
 
239
    return 0;
240
}