Rev 292 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 292 | Rev 294 | ||
|---|---|---|---|
| 1 | #include "bios_funs.h" |
1 | #include "bios_funs.h" |
| 2 | #include "avr_cfg.h" |
2 | #include "avr_cfg.h" |
| 3 | #include <inttypes.h> |
3 | #include <inttypes.h> |
| 4 | #include <avr/io.h> |
4 | #include <avr/io.h> |
| 5 | #include <avr/interrupt.h> |
5 | #include <avr/interrupt.h> |
| 6 | #include <avr/wdt.h> |
6 | #include <avr/wdt.h> |
| 7 | #include <avr/pgmspace.h> |
7 | #include <avr/pgmspace.h> |
| 8 | #ifdef BIOS_UART |
8 | #ifdef BIOS_UART |
| 9 | #include <stdio.h> |
9 | #include <stdio.h> |
| 10 | #endif |
10 | #endif |
| 11 | #include <string.h> |
11 | #include <string.h> |
| 12 | #include <util/crc16.h> |
12 | #include <util/crc16.h> |
| 13 | #include "flash.h" |
13 | #include "flash.h" |
| 14 | #include "vectors.h" // Must be included after sfr_defs.h but before any ISR() |
14 | #include "vectors.h" // Must be included after sfr_defs.h but before any ISR() |
| 15 | #include "mcp2515/mcp2515.h" |
15 | #include "mcp2515/mcp2515.h" |
| 16 | 16 | ||
| 17 | //--------------------------------------------------------------------------- |
17 | //--------------------------------------------------------------------------- |
| 18 | // Globals |
18 | // Globals |
| 19 | 19 | ||
| 20 | volatile uint32_t gMilliSecTick; |
20 | volatile uint32_t gMilliSecTick; |
| 21 | volatile uint8_t bios_msg_full; |
21 | volatile uint8_t bios_msg_full; |
| 22 | Can_Message_t* bios_msg_ptr; // only a pointer to main-local structure to save .bss space |
22 | Can_Message_t* bios_msg_ptr; // only a pointer to main-local structure to save .bss space |
| 23 | 23 | ||
| 24 | //--------------------------------------------------------------------------- |
24 | //--------------------------------------------------------------------------- |
| 25 | // Function definitions for the exported interface |
25 | // Function definitions for the exported interface |
| 26 | 26 | ||
| 27 | void reset(void) { |
27 | void reset(void) { |
| 28 | //Just loop and let the watchdog reset |
28 | //Just loop and let the watchdog reset |
| 29 | cli(); |
29 | cli(); |
| 30 | while(1); |
30 | while(1); |
| 31 | } |
31 | } |
| 32 | 32 | ||
| 33 | #ifdef BIOS_UART |
33 | #ifdef BIOS_UART |
| 34 | void bios_putchar(char c) { |
34 | void bios_putchar(char c) { |
| 35 | UART_PUTCHAR(c); |
35 | UART_PUTCHAR(c); |
| 36 | } |
36 | } |
| 37 | 37 | ||
| 38 | char bios_getchar(void) { |
38 | char bios_getchar(void) { |
| 39 | char c; |
39 | char c; |
| 40 | UART_GETCHAR(c) |
40 | UART_GETCHAR(c) |
| 41 | return c; |
41 | return c; |
| 42 | } |
42 | } |
| 43 | #endif |
43 | #endif |
| 44 | 44 | ||
| 45 | unsigned long timebase_get(void) { |
45 | unsigned long timebase_get(void) { |
| 46 | uint8_t sreg; |
46 | uint8_t sreg; |
| 47 | unsigned long res; |
47 | unsigned long res; |
| 48 | 48 | ||
| 49 | //Disable interrupts while reading tick count |
49 | //Disable interrupts while reading tick count |
| 50 | sreg=SREG; |
50 | sreg=SREG; |
| 51 | cli(); |
51 | cli(); |
| 52 | 52 | ||
| 53 | res = gMilliSecTick; |
53 | res = gMilliSecTick; |
| 54 | 54 | ||
| 55 | SREG=sreg; |
55 | SREG=sreg; |
| 56 | 56 | ||
| 57 | return res; |
57 | return res; |
| 58 | } |
58 | } |
| 59 | 59 | ||
| 60 | // TODO: Insert can driver's Can_Send directly in bios interface. |
60 | // TODO: Insert can driver's Can_Send directly in bios interface. |
| 61 | Can_Return_t can_send(Can_Message_t* msg) { |
61 | Can_Return_t can_send(Can_Message_t* msg) { |
| 62 | return Can_Send(msg); |
62 | return Can_Send(msg); |
| 63 | } |
63 | } |
| 64 | 64 | ||
| 65 | //--------------------------------------------------------------------------- |
65 | //--------------------------------------------------------------------------- |
| 66 | // Function definitions for the private functions |
66 | // Function definitions for the private functions |
| 67 | 67 | ||
| 68 | #ifdef BIOS_UART |
68 | #ifdef BIOS_UART |
| 69 | static int console_getchar(FILE *stream) { |
69 | static int console_getchar(FILE *stream) { |
| 70 | return bios_getchar(); |
70 | return bios_getchar(); |
| 71 | } |
71 | } |
| 72 | 72 | ||
| 73 | static int console_putchar(char c, FILE *stream) { |
73 | static int console_putchar(char c, FILE *stream) { |
| 74 | bios_putchar(c); |
74 | bios_putchar(c); |
| 75 | return 0; |
75 | return 0; |
| 76 | } |
76 | } |
| 77 | 77 | ||
| 78 | static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar, |
78 | static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar, |
| 79 | _FDEV_SETUP_RW); |
79 | _FDEV_SETUP_RW); |
| 80 | #endif |
80 | #endif |
| 81 | 81 | ||
| 82 | //--------------------------------------------------------------------------- |
82 | //--------------------------------------------------------------------------- |
| 83 | // Interrupt Service Routines |
83 | // Interrupt Service Routines |
| 84 | // Read app_vectors.h! |
84 | // Read app_vectors.h! |
| 85 | 85 | ||
| 86 | ISR(TIMEBASE_VECTOR) { |
86 | ISR(TIMEBASE_VECTOR) { |
| 87 | TIMEBASE_COUNTER -= TIMEBASE_RELOAD; |
87 | TIMEBASE_COUNTER -= TIMEBASE_RELOAD; |
| 88 | gMilliSecTick++; |
88 | gMilliSecTick++; |
| 89 | wdt_reset(); |
89 | wdt_reset(); |
| 90 | } |
90 | } |
| 91 | 91 | ||
| 92 | void Can_Process(Can_Message_t* msg) { |
92 | void Can_Process(Can_Message_t* msg) { |
| 93 | if (!(msg->ExtendedFlag)) return; |
93 | if (!(msg->ExtendedFlag)) return; |
| 94 | 94 | ||
| 95 | if ((msg->Id & CAN_MASK_CLASS) == ((uint32_t)CAN_NMT << CAN_SHIFT_CLASS)) { |
95 | if ((msg->Id & CAN_MASK_CLASS) == ((uint32_t)CAN_NMT << CAN_SHIFT_CLASS)) { |
| 96 | if ((msg->Id & CAN_MASK_NMT_RID) == ((uint32_t)NODE_ID << CAN_SHIFT_NMT_RID)) { |
96 | if ((msg->Id & CAN_MASK_NMT_RID) == ((uint32_t)NODE_ID << CAN_SHIFT_NMT_RID)) { |
| 97 | //printf("BIOS received msg!\n"); |
97 | //printf("BIOS received msg!\n"); |
| 98 | if ((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE == CAN_NMT_RESET) { |
98 | if ((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE == CAN_NMT_RESET) { |
| 99 | reset(); |
99 | reset(); |
| 100 | } |
100 | } |
| 101 | // Copy message to bios buffer if bios is done with the previous message. |
101 | // Copy message to bios buffer if bios is done with the previous message. |
| 102 | // This is also a check to see if bios is still running. When app is |
102 | // This is also a check to see if bios is still running. When app is |
| 103 | // started, bios_msg_full is never reset to 0 so the check fails and all |
103 | // started, bios_msg_full is never reset to 0 so the check fails and all |
| 104 | // NMT communication except reset is ignored. |
104 | // NMT communication except reset is ignored. |
| 105 | if (!(bios_msg_full)) { |
105 | if (!(bios_msg_full)) { |
| 106 | memcpy(bios_msg_ptr, msg, sizeof(Can_Message_t)); |
106 | memcpy(bios_msg_ptr, msg, sizeof(Can_Message_t)); |
| 107 | bios_msg_full = 1; |
107 | bios_msg_full = 1; |
| 108 | } |
108 | } |
| 109 | } |
109 | } |
| 110 | } else if (bios->can_callback) { |
110 | } else if (bios->can_callback) { |
| 111 | //printf("Calling app callback.\n"); |
111 | //printf("Calling app callback.\n"); |
| 112 | bios->can_callback(msg); |
112 | bios->can_callback(msg); |
| 113 | } |
113 | } |
| 114 | } |
114 | } |
| 115 | 115 | ||
| 116 | #define BIOS_APP 1 |
116 | #define BIOS_APP 1 |
| 117 | #define BIOS_NOAPP 2 |
117 | #define BIOS_NOAPP 2 |
| 118 | #define BIOS_PGM 3 |
118 | #define BIOS_PGM 3 |
| 119 | 119 | ||
| 120 | int main() { |
120 | int main() { |
| 121 | void (*app_reset)(void) = 0; |
121 | void (*app_reset)(void) = 0; |
| 122 | uint8_t bios_state; |
122 | uint8_t bios_state; |
| 123 | uint8_t nmt_type; |
123 | uint8_t nmt_type; |
| 124 | // This unneccessary initialization uses 8 bytes flash, just to make the compiler happy! |
124 | // This unneccessary initialization uses 8 bytes flash, just to make the compiler happy! |
| 125 | uint16_t base_addr = 0; |
125 | uint16_t base_addr = 0; |
| 126 | uint16_t offset = 0; |
126 | uint16_t offset = 0; |
| 127 | uint16_t addr; |
127 | uint16_t addr; |
| 128 | uint8_t len; |
128 | uint8_t len; |
| 129 | uint16_t i; |
129 | uint16_t i; |
| 130 | uint16_t data; |
130 | uint16_t data; |
| - | 131 | uint8_t send_msg = 0; |
|
| 131 | Can_Message_t tx_msg; |
132 | Can_Message_t tx_msg; |
| 132 | Can_Message_t bios_msg; |
133 | Can_Message_t bios_msg; |
| 133 | bios_msg_ptr = &bios_msg; |
134 | bios_msg_ptr = &bios_msg; |
| 134 | 135 | ||
| 135 | #ifdef BIOS_UART |
136 | #ifdef BIOS_UART |
| 136 | // Setup USART for debug channel |
137 | // Setup USART for debug channel |
| 137 | UART_INIT(); |
138 | UART_INIT(); |
| 138 | stdout = &bios_stdio; |
139 | stdout = &bios_stdio; |
| 139 | stdin = &bios_stdio; |
140 | stdin = &bios_stdio; |
| 140 | #endif |
141 | #endif |
| 141 | 142 | ||
| 142 | // Initialize a suitable hardware timer to create a 1KHz interrupt. |
143 | // Initialize a suitable hardware timer to create a 1KHz interrupt. |
| 143 | TIMEBASE_INIT(); |
144 | TIMEBASE_INIT(); |
| 144 | 145 | ||
| 145 | // Enable watchdog timer to protect against an application locking the |
146 | // Enable watchdog timer to protect against an application locking the |
| 146 | // node by leaving interrupts disabled. |
147 | // node by leaving interrupts disabled. |
| 147 | wdt_enable(WDTO_500MS); |
148 | wdt_enable(WDTO_500MS); |
| 148 | 149 | ||
| 149 | // Move interrupt vectors to start of bootloader section and enable interrupts |
150 | // Move interrupt vectors to start of bootloader section and enable interrupts |
| 150 | IVSEL_REG = _BV(IVCE); |
151 | IVSEL_REG = _BV(IVCE); |
| 151 | IVSEL_REG = _BV(IVSEL); |
152 | IVSEL_REG = _BV(IVSEL); |
| 152 | sei(); |
153 | sei(); |
| 153 | 154 | ||
| 154 | if (Can_Init() != CAN_OK) reset(); |
155 | if (Can_Init() != CAN_OK) reset(); |
| 155 | 156 | ||
| 156 | tx_msg.RemoteFlag = 0; |
157 | tx_msg.RemoteFlag = 0; |
| 157 | tx_msg.ExtendedFlag = 1; |
158 | tx_msg.ExtendedFlag = 1; |
| 158 | tx_msg.Id = CAN_ID_NMT_BIOS_START; |
159 | tx_msg.Id = CAN_ID_NMT_BIOS_START; |
| 159 | tx_msg.DataLength = 3; |
160 | tx_msg.DataLength = 3; |
| 160 | tx_msg.Data.words[0] = BIOS_VERSION; |
161 | tx_msg.Data.words[0] = BIOS_VERSION; |
| 161 | #ifdef BIOS_UART |
162 | #ifdef BIOS_UART |
| 162 | printf("BIOS start\n"); |
163 | printf("BIOS start\n"); |
| 163 | #endif |
164 | #endif |
| 164 | if (pgm_read_word(0) == 0xffff) { |
165 | if (pgm_read_word(0) == 0xffff) { |
| 165 | // No application in flash |
166 | // No application in flash |
| 166 | //send CAN_NMT_BIOS_START(BIOS_VERSION, 0) |
167 | //send CAN_NMT_BIOS_START(BIOS_VERSION, 0) |
| 167 | tx_msg.Data.bytes[2] = 0; |
168 | tx_msg.Data.bytes[2] = 0; |
| 168 | bios_state = BIOS_NOAPP; |
169 | bios_state = BIOS_NOAPP; |
| 169 | } else { |
170 | } else { |
| 170 | // Application exists |
171 | // Application exists |
| 171 | //send CAN_NMT_BIOS_START(BIOS_VERSION, 1) |
172 | //send CAN_NMT_BIOS_START(BIOS_VERSION, 1) |
| 172 | tx_msg.Data.bytes[2] = 1; |
173 | tx_msg.Data.bytes[2] = 1; |
| 173 | bios_state = BIOS_APP; |
174 | bios_state = BIOS_APP; |
| 174 | } |
175 | } |
| 175 | 176 | ||
| 176 | while (Can_Send(&tx_msg) != CAN_OK); |
177 | while (Can_Send(&tx_msg) != CAN_OK); |
| 177 | 178 | ||
| 178 | tx_msg.DataLength = 2; |
179 | tx_msg.DataLength = 2; |
| 179 | 180 | ||
| 180 | // main loop |
181 | // main loop |
| 181 | while (1) { |
182 | while (1) { |
| 182 | // Wait for message |
183 | // Wait for message |
| 183 | while (!bios_msg_full); |
184 | while (!bios_msg_full); |
| 184 | nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE; |
185 | nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE; |
| 185 | 186 | ||
| 186 | switch (bios_state) { |
187 | switch (bios_state) { |
| 187 | case BIOS_APP: |
188 | case BIOS_APP: |
| 188 | if (nmt_type == CAN_NMT_START_APP) { |
189 | if (nmt_type == CAN_NMT_START_APP) { |
| 189 | app_reset(); // Will never return |
190 | app_reset(); // Will never return |
| 190 | } |
191 | } |
| 191 | // Fall through |
192 | // Fall through |
| 192 | case BIOS_NOAPP: |
193 | case BIOS_NOAPP: |
| 193 | if (nmt_type == CAN_NMT_PGM_START) { |
194 | if (nmt_type == CAN_NMT_PGM_START) { |
| 194 | // Set base address |
195 | // Set base address |
| 195 | base_addr = bios_msg.Data.words[0]; |
196 | base_addr = bios_msg.Data.words[0]; |
| 196 | flash_init(); |
197 | flash_init(); |
| 197 | //send CAN_NMT_PGM_ACK(offset) |
198 | //send CAN_NMT_PGM_ACK(offset) |
| 198 | tx_msg.Id = CAN_ID_NMT_PGM_ACK; |
199 | tx_msg.Id = CAN_ID_NMT_PGM_ACK; |
| 199 | tx_msg.Data.words[0] = base_addr; |
200 | tx_msg.Data.words[0] = base_addr; |
| - | 201 | send_msg = 1; |
|
| 200 | bios_state = BIOS_PGM; |
202 | bios_state = BIOS_PGM; |
| 201 | } |
203 | } |
| 202 | if (nmt_type == CAN_NMT_PGM_COPY) { |
204 | if (nmt_type == CAN_NMT_PGM_COPY) { |
| 203 | // For BIOS updating over CAN. Upload bios to application area, |
205 | // For BIOS updating over CAN. Upload bios to application area, |
| 204 | // send this message with correct parameters to copy data from |
206 | // send this message with correct parameters to copy data from |
| 205 | // application to bios area and pray it will come alive again. |
207 | // application to bios area and pray it will come alive again. |
| 206 | // Make extra really super sure that the .bootloader section is |
208 | // Make extra really super sure that the .bootloader section is |
| 207 | // EXACTLY the same, except for the vector table (which MUST be |
209 | // EXACTLY the same, except for the vector table (which MUST be |
| 208 | // updated) or go get your ISP. |
210 | // updated) or go get your ISP. |
| 209 | flash_copy_data(bios_msg.Data.words[0], |
211 | flash_copy_data(bios_msg.Data.words[0], |
| 210 | bios_msg.Data.words[1], |
212 | bios_msg.Data.words[1], |
| 211 | bios_msg.Data.words[2]); |
213 | bios_msg.Data.words[2]); |
| 212 | // flash_copy_data will never return. |
214 | // flash_copy_data will never return. |
| 213 | } |
215 | } |
| 214 | break; |
216 | break; |
| 215 | case BIOS_PGM: |
217 | case BIOS_PGM: |
| 216 | if (nmt_type == CAN_NMT_PGM_DATA) { |
218 | if (nmt_type == CAN_NMT_PGM_DATA) { |
| 217 | // Set address = base address + offset |
219 | // Set address = base address + offset |
| 218 | offset = bios_msg.Data.words[0]; |
220 | offset = bios_msg.Data.words[0]; |
| 219 | addr = base_addr + offset; |
221 | addr = base_addr + offset; |
| 220 | tx_msg.Data.words[0] = offset; |
222 | tx_msg.Data.words[0] = offset; |
| 221 | // TODO: Check (addr + dlc) valid instead |
223 | // TODO: Check (addr + dlc) valid instead |
| 222 | if (addr < (uint16_t)&__bios_start) { //address valid |
224 | if (addr < (uint16_t)&__bios_start) { //address valid |
| 223 | //flash data at address |
225 | //flash data at address |
| 224 | len = (bios_msg.DataLength+1)/2; // Number of words in message, rounded up. |
226 | len = (bios_msg.DataLength+1)/2; // Number of words in message, rounded up. |
| 225 | for (i=1; i<len; i++) { // Skip first word (offset) |
227 | for (i=1; i<len; i++) { // Skip first word (offset) |
| 226 | data = bios_msg.Data.words[i]; |
228 | data = bios_msg.Data.words[i]; |
| 227 | flash_write_word(addr, data); |
229 | flash_write_word(addr, data); |
| 228 | addr += 2; |
230 | addr += 2; |
| 229 | } |
231 | } |
| 230 | //update crc //cannot update crc here, data not written yet /arune |
232 | //update crc //cannot update crc here, data not written yet /arune |
| 231 | offset += bios_msg.DataLength - 2; //let offset be last address to simplify crc calc later |
233 | offset += bios_msg.DataLength - 2; //let offset be last address to simplify crc calc later |
| 232 | //send CAN_NMT_PGM_ACK(offset) |
234 | //send CAN_NMT_PGM_ACK(offset) |
| 233 | tx_msg.Id = CAN_ID_NMT_PGM_ACK; |
235 | tx_msg.Id = CAN_ID_NMT_PGM_ACK; |
| 234 | } else { |
236 | } else { |
| 235 | //send CAN_NMT_PGM_NACK(offset) |
237 | //send CAN_NMT_PGM_NACK(offset) |
| 236 | tx_msg.Id = CAN_ID_NMT_PGM_NACK; |
238 | tx_msg.Id = CAN_ID_NMT_PGM_NACK; |
| 237 | } |
239 | } |
| - | 240 | send_msg = 1; |
|
| 238 | } |
241 | } |
| 239 | if (nmt_type == CAN_NMT_PGM_END) { |
242 | if (nmt_type == CAN_NMT_PGM_END) { |
| 240 | flash_flush_buffer(); |
243 | flash_flush_buffer(); |
| 241 | //check crc |
244 | //check crc |
| 242 | uint16_t calccrc = 0; |
245 | uint16_t calccrc = 0; |
| 243 | for (i=0; i < offset; i++) { |
246 | for (i=0; i < offset; i++) { |
| 244 | calccrc = _crc16_update(calccrc, pgm_read_byte(base_addr+i)); |
247 | calccrc = _crc16_update(calccrc, pgm_read_byte(base_addr+i)); |
| 245 | } |
248 | } |
| 246 | tx_msg.Data.words[0] = calccrc; |
249 | tx_msg.Data.words[0] = calccrc; |
| 247 | tx_msg.Id = CAN_ID_NMT_PGM_ACK; |
250 | tx_msg.Id = CAN_ID_NMT_PGM_ACK; |
| - | 251 | send_msg = 1; |
|
| 248 | bios_state = BIOS_NOAPP; |
252 | bios_state = BIOS_NOAPP; |
| 249 | } |
253 | } |
| 250 | } |
254 | } |
| - | 255 | ||
| - | 256 | if (send_msg) { |
|
| 251 | while (Can_Send(&tx_msg) != CAN_OK); |
257 | while (Can_Send(&tx_msg) != CAN_OK); |
| - | 258 | send_msg = 0; |
|
| - | 259 | } |
|
| 252 | 260 | ||
| 253 | bios_msg_full = 0; // We're done with this message, ready for the next. |
261 | bios_msg_full = 0; // We're done with this message, ready for the next. |
| 254 | } |
262 | } |
| 255 | 263 | ||
| 256 | return 0; |
264 | return 0; |
| 257 | } |
265 | } |
| 258 | 266 | ||