Rev 69 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 69 | Rev 73 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CAN test program. |
2 | * CAN test program. |
| 3 | * |
3 | * |
| 4 | * @target AVR |
4 | * @target AVR |
| 5 | * @date 2006-10-29 |
5 | * @date 2006-10-29 |
| 6 | * @author Jimmy Myhrman |
6 | * @author Jimmy Myhrman |
| 7 | * |
7 | * |
| 8 | */ |
8 | */ |
| 9 | 9 | ||
| 10 | /*----------------------------------------------------------------------------- |
10 | /*----------------------------------------------------------------------------- |
| 11 | * Includes |
11 | * Includes |
| 12 | *---------------------------------------------------------------------------*/ |
12 | *---------------------------------------------------------------------------*/ |
| 13 | /* system files */ |
13 | /* system files */ |
| 14 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 15 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 17 | #include <stdio.h> |
17 | #include <stdio.h> |
| 18 | /* lib files */ |
18 | /* lib files */ |
| 19 | #include <can.h> |
19 | #include <can.h> |
| 20 | #include <uart.h> |
20 | #include <uart.h> |
| 21 | #include <timebase.h> |
21 | #include <timebase.h> |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | /*----------------------------------------------------------------------------- |
24 | /*----------------------------------------------------------------------------- |
| 25 | * Defines |
25 | * Defines |
| 26 | *---------------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------------*/ |
| 27 | #define UART_START_BYTE 253 |
27 | #define UART_START_BYTE 253 |
| 28 | #define UART_END_BYTE 250 |
28 | #define UART_END_BYTE 250 |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | /*----------------------------------------------------------------------------- |
31 | /*----------------------------------------------------------------------------- |
| 32 | * Functions |
32 | * Functions |
| 33 | *---------------------------------------------------------------------------*/ |
33 | *---------------------------------------------------------------------------*/ |
| 34 | /** |
34 | /** |
| 35 | * Parses an incoming UART byte by maintaining a state machine. The UART |
35 | * Parses an incoming UART byte by maintaining a state machine. The UART |
| 36 | * bytes will build up a CAN message according to the protocol described here: |
36 | * bytes will build up a CAN message according to the protocol described here: |
| 37 | * |
37 | * |
| 38 | * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara |
38 | * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara |
| 39 | * |
39 | * |
| 40 | * @param c |
40 | * @param c |
| 41 | * The received UART byte. |
41 | * The received UART byte. |
| 42 | */ |
42 | */ |
| 43 | void UartParseByte(uint8_t c) { |
43 | void UartParseByte(uint8_t c) { |
| 44 | static CanMessage_t cm; |
44 | static CanMessage_t cm; |
| 45 | static uint8_t waitingMessage = 0; |
45 | static uint8_t waitingMessage = 0; |
| 46 | static uint32_t startTime = 0; |
46 | static uint32_t startTime = 0; |
| 47 | static int8_t count = 0; |
47 | static int8_t count = 0; |
| 48 | 48 | ||
| 49 | /* 50ms timeout */ |
49 | /* 50ms timeout */ |
| 50 | if (waitingMessage && TimebasePassedTimeMS(startTime) > 50) { |
50 | if (waitingMessage && TimebasePassedTimeMS(startTime) > 50) { |
| 51 | waitingMessage = 0; |
51 | waitingMessage = 0; |
| 52 | } |
52 | } |
| 53 | 53 | ||
| 54 | if (waitingMessage) { |
54 | if (waitingMessage) { |
| 55 | /* save start time */ |
55 | /* save start time */ |
| 56 | startTime = TimebaseCurrentTime(); |
56 | startTime = TimebaseCurrentTime(); |
| 57 | /* UART END */ |
57 | /* UART END */ |
| 58 | if (count >= 15) { |
58 | if (count >= 15) { |
| 59 | if (c == UART_END_BYTE) { |
59 | if (c == UART_END_BYTE) { |
| 60 | PORTC ^= (1<<PC0); |
60 | PORTC ^= (1<<PC0); |
| 61 | CanSend(&cm); |
61 | CanSend(&cm); |
| 62 | } |
62 | } |
| 63 | waitingMessage = 0; |
63 | waitingMessage = 0; |
| 64 | return; |
64 | return; |
| 65 | } |
65 | } |
| 66 | /* data */ |
66 | /* data */ |
| 67 | else if (count >= 7) { |
67 | else if (count >= 7) { |
| 68 | cm.Data.bytes[count-7] = c; |
68 | cm.Data.bytes[count-7] = c; |
| 69 | count++; |
69 | count++; |
| 70 | return; |
70 | return; |
| 71 | } |
71 | } |
| 72 | /* data length */ |
72 | /* data length */ |
| 73 | else if (count >= 6) { |
73 | else if (count >= 6) { |
| 74 | cm.DataLength = c; |
74 | cm.DataLength = c; |
| 75 | count++; |
75 | count++; |
| 76 | return; |
76 | return; |
| 77 | } |
77 | } |
| 78 | /* remote request flag */ |
78 | /* remote request flag */ |
| 79 | else if (count >= 5) { |
79 | else if (count >= 5) { |
| 80 | cm.RemoteFlag = c; |
80 | cm.RemoteFlag = c; |
| 81 | count++; |
81 | count++; |
| 82 | return; |
82 | return; |
| 83 | } |
83 | } |
| 84 | /* extended */ |
84 | /* extended */ |
| 85 | else if (count >= 4) { |
85 | else if (count >= 4) { |
| 86 | cm.ExtendedFlag = c; |
86 | cm.ExtendedFlag = c; |
| 87 | count++; |
87 | count++; |
| 88 | return; |
88 | return; |
| 89 | } |
89 | } |
| 90 | /* ident */ |
90 | /* ident */ |
| 91 | else if (count >= 0) { |
91 | else if (count >= 0) { |
| 92 | cm.Id += ((uint32_t)c << (count*8)); |
92 | cm.Id += ((uint32_t)c << (count*8)); |
| 93 | count++; |
93 | count++; |
| 94 | return; |
94 | return; |
| 95 | } |
95 | } |
| 96 | } |
96 | } |
| 97 | 97 | ||
| 98 | if (c == UART_START_BYTE && !waitingMessage) { |
98 | if (c == UART_START_BYTE && !waitingMessage) { |
| 99 | waitingMessage = 1; |
99 | waitingMessage = 1; |
| 100 | startTime = TimebaseCurrentTime(); |
100 | startTime = TimebaseCurrentTime(); |
| 101 | count = 0; |
101 | count = 0; |
| 102 | cm.Id = 0; |
102 | cm.Id = 0; |
| 103 | return; |
103 | return; |
| 104 | } |
104 | } |
| 105 | } |
105 | } |
| 106 | 106 | ||
| 107 | 107 | ||
| 108 | /*----------------------------------------------------------------------------- |
108 | /*----------------------------------------------------------------------------- |
| 109 | * Main Program |
109 | * Main Program |
| 110 | *---------------------------------------------------------------------------*/ |
110 | *---------------------------------------------------------------------------*/ |
| 111 | int main(void) { |
111 | int main(void) { |
| 112 | TimebaseInit(); |
112 | TimebaseInit(); |
| 113 | UartInit(); |
113 | UartInit(); |
| 114 | CanInit(CAN_BITRATE_1M); |
114 | CanInit(CAN_BITRATE_1M); |
| 115 | 115 | ||
| 116 | DDRC = 1<<PC1 | 1<<PC0; |
116 | DDRC = 1<<PC1 | 1<<PC0; |
| 117 | PORTC = (1<<PC1) | (1<<PC0); |
117 | PORTC = (1<<PC1) | (1<<PC0); |
| 118 | 118 | ||
| 119 | sei(); |
119 | sei(); |
| 120 | 120 | ||
| 121 | CanMessage_t rxMsg; |
121 | CanMessage_t rxMsg; |
| 122 | uint16_t rxByte; |
122 | uint16_t rxByte; |
| 123 | uint8_t i = 0; |
123 | uint8_t i = 0; |
| 124 | 124 | ||
| 125 | /* main loop */ |
125 | /* main loop */ |
| 126 | while (1) { |
126 | while (1) { |
| 127 | /* any new CAN messages received? */ |
127 | /* any new CAN messages received? */ |
| 128 | if (CanReceive(&rxMsg) == CAN_OK) { |
128 | if (CanReceive(&rxMsg) == CAN_OK) { |
| 129 | PORTC ^= (1<<PC1); |
129 | PORTC ^= (1<<PC1); |
| 130 | /* send message to CanWatcher */ |
130 | /* send message to CanWatcher */ |
| 131 | uart_putc(UART_START_BYTE); |
131 | uart_putc(UART_START_BYTE); |
| 132 | uart_putc((uint8_t)rxMsg.Id); |
132 | uart_putc((uint8_t)rxMsg.Id); |
| 133 | uart_putc((uint8_t)(rxMsg.Id>>8)); |
133 | uart_putc((uint8_t)(rxMsg.Id>>8)); |
| 134 | uart_putc((uint8_t)(rxMsg.Id>>16)); |
134 | uart_putc((uint8_t)(rxMsg.Id>>16)); |
| 135 | uart_putc((uint8_t)(rxMsg.Id>>24)); |
135 | uart_putc((uint8_t)(rxMsg.Id>>24)); |
| 136 | uart_putc(rxMsg.ExtendedFlag); |
136 | uart_putc(rxMsg.ExtendedFlag); |
| 137 | uart_putc(rxMsg.RemoteFlag); |
137 | uart_putc(rxMsg.RemoteFlag); |
| 138 | uart_putc(rxMsg.DataLength); |
138 | uart_putc(rxMsg.DataLength); |
| 139 | for (i=0; i<8; i++) { |
139 | for (i=0; i<8; i++) { |
| 140 | uart_putc(rxMsg.Data.bytes[i]); |
140 | uart_putc(rxMsg.Data.bytes[i]); |
| 141 | } |
141 | } |
| 142 | uart_putc(UART_END_BYTE); |
142 | uart_putc(UART_END_BYTE); |
| 143 | } |
143 | } |
| 144 | 144 | ||
| 145 | /* any UART bytes received? */ |
145 | /* any UART bytes received? */ |
| 146 | rxByte = uart_getc(); |
146 | rxByte = uart_getc(); |
| 147 | while (rxByte != UART_NO_DATA) { |
147 | while (rxByte != UART_NO_DATA) { |
| 148 | /* parse byte! */ |
148 | /* parse byte! */ |
| 149 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
149 | UartParseByte((uint8_t)(rxByte & 0x00FF)); |
| 150 | /* receive next */ |
150 | /* receive next */ |
| 151 | rxByte = uart_getc(); |
151 | rxByte = uart_getc(); |
| 152 | } |
152 | } |
| 153 | } |
153 | } |
| 154 | 154 | ||
| 155 | return 0; |
155 | return 0; |
| 156 | } |
156 | } |
| 157 | 157 | ||