Subversion Repositories HomeAutomation

Rev

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

Rev 757 Rev 759
Line 1... Line 1...
1
/*
1
/*
2
 * $Id: bios.c 585 2007-08-06 17:44:46Z arune $
2
 * $Id: bios.c 759 2008-03-24 12:46:11Z arune $
3
 */
3
 */
4
 
4
 
5
#include <inttypes.h>
5
#include <inttypes.h>
6
#include <avr/io.h>
6
#include <avr/io.h>
7
#include <avr/interrupt.h>
7
#include <avr/interrupt.h>
Line 45... Line 45...
45
#endif
45
#endif
46
 
46
 
47
#define BIOS_APP    1
47
#define BIOS_APP        1
48
#define BIOS_NOAPP  2
48
#define BIOS_NOAPP      2
49
#define BIOS_PGM    3
49
#define BIOS_PGM        3
-
 
50
#define BIOS_END_PGM    4
50
 
51
 
51
//---------------------------------------------------------------------------
52
//---------------------------------------------------------------------------
52
// Private functions
53
// Private functions
53
 
54
 
54
void Can_Process(Can_Message_t* msg) {
55
void Can_Process(Can_Message_t* msg) {
Line 65... Line 66...
65
#if defined(AUTOSTART)
66
#if defined(AUTOSTART)
66
            autostart_cnt++;
67
            autostart_cnt++;
67
#endif
68
#endif
68
        }
69
        }
69
       
70
       
70
        if ((msg->Id & CAN_MASK_NMT_RID)>>CAN_SHIFT_NMT_RID == NODE_ID) {
71
        if (((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE) == CAN_NMT_RESET && msg->DataLength == 4 &&
71
 
-
 
72
            if ((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE == CAN_NMT_RESET) {
72
                msg->Data.bytes[0] == (NODE_HW_ID&0xff) && msg->Data.bytes[1] == ((NODE_HW_ID>>8)&0xff) &&
-
 
73
                msg->Data.bytes[2] == ((NODE_HW_ID>>16)&0xff) && msg->Data.bytes[3] == ((NODE_HW_ID>>24)&0xff) ) {
73
                BIOS_Reset();
74
            BIOS_Reset();
74
            }
75
        }
75
            // Copy message to bios buffer if bios is done with the previous message.
76
        // Copy message to bios buffer if bios is done with the previous message.
76
            // This is also a check to see if bios is still running. When app is
77
        // This is also a check to see if bios is still running. When app is
77
            // started, bios_msg_full is never reset to 0 so the check fails and all
78
        // started, bios_msg_full is never reset to 0 so the check fails and all
78
            // NMT communication except reset is ignored. 
79
        // NMT communication except reset is ignored. 
79
            if (!(bios_msg_full)) {
80
        if (!(bios_msg_full)) {
80
                memcpy(bios_msg_ptr, msg, sizeof(Can_Message_t));
81
            memcpy(bios_msg_ptr, msg, sizeof(Can_Message_t));
81
                bios_msg_full = 1;
82
            bios_msg_full = 1;
82
            }
-
 
83
        }      
83
        }
84
    } else if (BIOS_CanCallback) {
84
    } else if (BIOS_CanCallback) {
85
        //printf("Calling app callback.\n");
85
        //printf("Calling app callback.\n");
86
        BIOS_CanCallback(msg);
86
        BIOS_CanCallback(msg);
87
    }
87
    }
Line 119... Line 119...
119
    if (Can_Init() != CAN_OK) BIOS_Reset();
119
    if (Can_Init() != CAN_OK) BIOS_Reset();
120
   
120
   
121
    tx_msg.RemoteFlag = 0;
121
    tx_msg.RemoteFlag = 0;
122
    tx_msg.ExtendedFlag = 1;
122
    tx_msg.ExtendedFlag = 1;
123
    tx_msg.Id = CAN_ID_NMT_BIOS_START;
123
    tx_msg.Id = CAN_ID_NMT_BIOS_START;
124
    tx_msg.DataLength = 3;
124
    tx_msg.DataLength = 8;
125
    tx_msg.Data.words[0] = BIOS_VERSION;
125
    tx_msg.Data.bytes[0] = BIOS_VERSION&0xff;
-
 
126
    tx_msg.Data.bytes[1] = (BIOS_VERSION>>8)&0xff;
-
 
127
    tx_msg.Data.bytes[4] = NODE_HW_ID&0xff;
-
 
128
    tx_msg.Data.bytes[5] = (NODE_HW_ID>>8)&0xff;
-
 
129
    tx_msg.Data.bytes[6] = (NODE_HW_ID>>16)&0xff;
-
 
130
    tx_msg.Data.bytes[7] = (NODE_HW_ID>>24)&0xff;
126
 
131
 
127
    if (pgm_read_word(0) == 0xffff) {
132
    if (pgm_read_word(0) == 0xffff) {
128
        // No application in flash
133
        // No application in flash
129
        // Send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
134
        // Send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
130
        tx_msg.Data.bytes[2] = 0;
135
        tx_msg.Data.bytes[2] = 0;
Line 147... Line 152...
147
#if defined(AUTOSTART)
152
#if defined(AUTOSTART)
148
            if (bios_state == BIOS_APP && autostart_cnt == AUTOSTART) {
153
            if (bios_state == BIOS_APP && autostart_cnt == AUTOSTART) {
149
                app_reset();
154
                app_reset();
150
            }
155
            }
151
#endif
156
#endif
152
        }
157
        }
153
       
158
       
154
        nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE;
159
        nmt_type = (bios_msg.Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE;
155
       
160
       
156
        switch (bios_state) {
161
        switch (bios_state) {
157
        case BIOS_APP:
162
        case BIOS_APP:
158
            if (nmt_type == CAN_NMT_START_APP) {
163
            //if (nmt_type == CAN_NMT_START_APP) {
-
 
164
            if (nmt_type == CAN_NMT_START_APP && bios_msg.DataLength == 4 &&
-
 
165
                    bios_msg.Data.bytes[0] == (NODE_HW_ID&0xff) && bios_msg.Data.bytes[1] == ((NODE_HW_ID>>8)&0xff) &&
-
 
166
                    bios_msg.Data.bytes[2] == ((NODE_HW_ID>>16)&0xff) && bios_msg.Data.bytes[3] == ((NODE_HW_ID>>24)&0xff) ) {
159
                app_reset(); // Will never return
167
                app_reset(); // Will never return
160
            }
168
            }
161
            // Fall through
169
            // Fall through
162
        case BIOS_NOAPP:
170
        case BIOS_NOAPP:
163
            if (nmt_type == CAN_NMT_PGM_START) {
171
            //if (nmt_type == CAN_NMT_PGM_START) {
-
 
172
            if (nmt_type == CAN_NMT_PGM_START && bios_msg.DataLength == 8 &&
-
 
173
                    bios_msg.Data.bytes[4] == (NODE_HW_ID&0xff) && bios_msg.Data.bytes[5] == ((NODE_HW_ID>>8)&0xff) &&
-
 
174
                    bios_msg.Data.bytes[6] == ((NODE_HW_ID>>16)&0xff) && bios_msg.Data.bytes[7] == ((NODE_HW_ID>>24)&0xff) ) {
164
                // Set base address
175
                // Set base address
165
                base_addr = bios_msg.Data.words[0];
176
                base_addr = bios_msg.Data.words[0];
166
                flash_init(pagebuf);
177
                flash_init(pagebuf);
167
                //send CAN_NMT_PGM_ACK(offset)
178
                //send CAN_NMT_PGM_ACK(offset)
168
                tx_msg.Id = CAN_ID_NMT_PGM_ACK;
179
                tx_msg.Id = CAN_ID_NMT_PGM_ACK;
169
                tx_msg.Data.words[0] = base_addr;
180
                tx_msg.Data.words[0] = base_addr;
170
                send_msg = 1;
181
                send_msg = 1;
171
                bios_state = BIOS_PGM;
182
                bios_state = BIOS_PGM;
172
            }
-
 
173
            if (nmt_type == CAN_NMT_PGM_COPY) {
-
 
174
                // For BIOS updating over CAN. Upload bios to application area,
-
 
175
                // send this message with correct parameters to copy data from
-
 
176
                // application to bios area and pray it will come alive again.
-
 
177
                flash_copy_data(bios_msg.Data.words[0],
-
 
178
                                bios_msg.Data.words[1],
-
 
179
                                bios_msg.Data.words[2]);
-
 
180
                // flash_copy_data will never return.
-
 
181
            }
183
            }
182
            break;
184
            break;
183
        case BIOS_PGM:
185
        case BIOS_PGM:
184
            // Default to send CAN_NMT_PGM_ACK(offset)
186
            // Default to send CAN_NMT_PGM_ACK(offset)
185
            tx_msg.Id = CAN_ID_NMT_PGM_ACK;
187
            tx_msg.Id = CAN_ID_NMT_PGM_ACK;
Line 225... Line 227...
225
                    crc = _crc16_update(crc, pgm_read_byte(loc));
227
                    crc = _crc16_update(crc, pgm_read_byte(loc));
226
                }
228
                }
227
               
229
               
228
                tx_msg.Data.words[0] = crc;
230
                tx_msg.Data.words[0] = crc;
229
                send_msg = 1;
231
                send_msg = 1;
230
                bios_state = BIOS_NOAPP;
232
                bios_state = BIOS_END_PGM;
-
 
233
            }
-
 
234
            break;
-
 
235
        case BIOS_END_PGM:
-
 
236
            if (nmt_type == CAN_NMT_PGM_COPY) {
-
 
237
                // For BIOS updating over CAN. Upload bios to application area,
-
 
238
                // send this message with correct parameters to copy data from
-
 
239
                // application to bios area and pray it will come alive again.
-
 
240
                flash_copy_data(bios_msg.Data.words[0],
-
 
241
                                bios_msg.Data.words[1],
-
 
242
                                bios_msg.Data.words[2]);
-
 
243
                // flash_copy_data will never return.
231
            }
244
            }
232
        }
245
        }
233
       
246
       
234
        if (send_msg) {
247
        if (send_msg) {
235
            while (Can_Send(&tx_msg) != CAN_OK);
248
            while (Can_Send(&tx_msg) != CAN_OK);