Subversion Repositories HomeAutomation

Rev

Rev 264 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 264 Rev 278
Line 7... Line 7...
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 "flash.h"
13
#include "flash.h"
13
#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()
14
#include "mcp2515/mcp2515.h"
15
#include "mcp2515/mcp2515.h"
15
 
16
 
16
//---------------------------------------------------------------------------
17
//---------------------------------------------------------------------------
Line 154... Line 155...
154
    tx_msg.RemoteFlag = 0;
155
    tx_msg.RemoteFlag = 0;
155
    tx_msg.ExtendedFlag = 1;
156
    tx_msg.ExtendedFlag = 1;
156
    tx_msg.Id = CAN_ID_NMT_BIOS_START;
157
    tx_msg.Id = CAN_ID_NMT_BIOS_START;
157
    tx_msg.DataLength = 3;
158
    tx_msg.DataLength = 3;
158
    tx_msg.Data.words[0] = BIOS_VERSION;
159
    tx_msg.Data.words[0] = BIOS_VERSION;
159
           
160
    printf("BOIS start\n");
160
    if (pgm_read_word(0) == 0xffff) {
161
    if (pgm_read_word(0) == 0xffff) {
161
        // No application in flash
162
        // No application in flash
162
        //send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
163
        //send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
163
        tx_msg.Data.bytes[2] = 0;
164
        tx_msg.Data.bytes[2] = 0;
164
        bios_state = BIOS_NOAPP;
165
        bios_state = BIOS_NOAPP;
Line 170... Line 171...
170
    }
171
    }
171
   
172
   
172
    while (Can_Send(&tx_msg) != CAN_OK);
173
    while (Can_Send(&tx_msg) != CAN_OK);
173
 
174
 
174
    tx_msg.DataLength = 2;
175
    tx_msg.DataLength = 2;
175
   
176
   
176
    // main loop
177
    // main loop
177
    while (1) {
178
    while (1) {
178
        // Wait for message
179
        // Wait for message
179
        while (!bios_msg_full);
180
        while (!bios_msg_full);
180
        nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE;
181
        nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE;
Line 209... Line 210...
209
                    for (i=1; i<len; i++) { // Skip first word (offset)
210
                    for (i=1; i<len; i++) { // Skip first word (offset)
210
                        data = bios_msg.Data.words[i];
211
                        data = bios_msg.Data.words[i];
211
                        flash_write_word(addr, data);
212
                        flash_write_word(addr, data);
212
                        addr += 2;
213
                        addr += 2;
213
                    }
214
                    }
214
                    //update crc
215
                    //update crc //cannot update crc here, data not written yet /arune
215
                    //send CAN_NMT_PGM_ACK(offset)
216
                    //send CAN_NMT_PGM_ACK(offset)
216
                    tx_msg.Id = CAN_ID_NMT_PGM_ACK;
217
                    tx_msg.Id = CAN_ID_NMT_PGM_ACK;
217
                } else {
218
                } else {
218
                    //send CAN_NMT_PGM_NACK(offset)
219
                    //send CAN_NMT_PGM_NACK(offset)
219
                    tx_msg.Id = CAN_ID_NMT_PGM_NACK;
220
                    tx_msg.Id = CAN_ID_NMT_PGM_NACK;
220
                }
221
                }
221
            }
222
            }
222
            if (nmt_type == CAN_NMT_PGM_END) {
223
            if (nmt_type == CAN_NMT_PGM_END) {
223
                flash_flush_buffer();
224
                flash_flush_buffer();
224
                //check crc
225
                //check crc
-
 
226
                data = bios_msg.Data.words[0];
-
 
227
                uint16_t calccrc = 0;
-
 
228
                for (i=0; i < offset; i++) {
-
 
229
                    calccrc = _crc16_update(calccrc, pgm_read_byte(base_addr+i));
-
 
230
                }
225
                //tx_msg.Data.words[0] = crc;
231
                //tx_msg.Data.words[0] = crc;
226
                if (1) { //crc ok
232
                if (data == calccrc) { //crc ok
227
                    //send CAN_NMT_PGM_ACK(crc)
233
                    //send CAN_NMT_PGM_ACK(crc)
228
                    tx_msg.Id = CAN_ID_NMT_PGM_ACK;
234
                    tx_msg.Id = CAN_ID_NMT_PGM_ACK;
229
                } else {
235
                } else {
230
                    //send CAN_NMT_PGM_NACK(crc)
236
                    //send CAN_NMT_PGM_NACK(crc)
231
                    tx_msg.Id = CAN_ID_NMT_PGM_NACK;
237
                    tx_msg.Id = CAN_ID_NMT_PGM_NACK;