Subversion Repositories HomeAutomation

Rev

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

Rev 759 Rev 766
Line 1... Line 1...
1
/*
1
/*
2
 * $Id: bios.c 759 2008-03-24 12:46:11Z arune $
2
 * $Id: bios.c 766 2008-04-10 18:33:30Z 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 13... Line 13...
13
#include <vectors.h> // Must be included after sfr_defs.h but before any ISR()
13
#include <vectors.h> // Must be included after sfr_defs.h but before any ISR()
14
#include <drivers/can/can.h>
14
#include <drivers/can/can.h>
15
#include <flash.h>
15
#include <flash.h>
16
#include <bios_export.h>
16
#include <bios_export.h>
17
#include CAN_DRIVER_H
17
#include CAN_DRIVER_H
-
 
18
 
-
 
19
#if !defined( NODE_HW_ID )
-
 
20
#error No NODE_HW_ID in bios.inc, set NODE_HW_ID=<GENERATE_HW> and Ill generate it for you
-
 
21
#endif
18
 
22
 
19
#if defined(__AVR_ATmega8__)
23
#if defined(__AVR_ATmega8__)
20
#define IVSEL_REG GICR
24
#define IVSEL_REG GICR
21
#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
25
#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
22
#define IVSEL_REG MCUCR
26
#define IVSEL_REG MCUCR
Line 25... Line 29...
25
#endif
29
#endif
26
 
30
 
27
#if defined(AUTOSTART)
31
#if defined(AUTOSTART)
28
#if AUTOSTART == 0 
32
#if AUTOSTART == 0 
29
#error Please choose a higher AUTOSTART value in bios.inc, the nod will cease to work, and you can only reflash node with ISP
33
#error Please choose a higher AUTOSTART value in bios.inc, the nod will cease to work, and you can only reflash node with ISP
30
#endif
34
#endif
31
#if AUTOSTART == 1
35
#if AUTOSTART == 1
32
#error Please choose a higher AUTOSTART value in bios.inc, there might be problems with updating software on node
36
#error Please choose a higher AUTOSTART value in bios.inc, there might be problems with updating software on node
33
#endif
37
#endif
34
#endif
38
#endif
35
 
39
 
36
//---------------------------------------------------------------------------
40
//---------------------------------------------------------------------------
37
// Private declarations
41
// Private declarations
38
 
42
 
39
volatile uint8_t bios_msg_full;
43
volatile uint8_t bios_msg_full;
40
Can_Message_t* bios_msg_ptr; // only a pointer to main-local structure to save .bss space
44
Can_Message_t* bios_msg_ptr; // only a pointer to main-local structure to save .bss space
41
extern void __bios_start; // Start of BIOS area in flash, from ld-script.
45
extern void __bios_start; // Start of BIOS area in flash, from ld-script.
-
 
46
 
-
 
47
prog_uint32_t hwid = NODE_HW_ID;
42
 
48
 
43
#if defined(AUTOSTART)
49
#if defined(AUTOSTART)
44
uint8_t autostart_cnt;
50
uint8_t autostart_cnt;
45
#endif
51
#endif
46
 
52
 
Line 67... Line 73...
67
            autostart_cnt++;
73
            autostart_cnt++;
68
#endif
74
#endif
69
        }
75
        }
70
       
76
       
71
        if (((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE) == CAN_NMT_RESET && msg->DataLength == 4 &&
77
        if (((msg->Id & CAN_MASK_NMT_TYPE)>>CAN_SHIFT_NMT_TYPE) == CAN_NMT_RESET && msg->DataLength == 4 &&
72
                msg->Data.bytes[0] == (NODE_HW_ID&0xff) && msg->Data.bytes[1] == ((NODE_HW_ID>>8)&0xff) &&
78
                msg->Data.bytes[0] == (hwid&0xff) && msg->Data.bytes[1] == ((hwid>>8)&0xff) &&
73
                msg->Data.bytes[2] == ((NODE_HW_ID>>16)&0xff) && msg->Data.bytes[3] == ((NODE_HW_ID>>24)&0xff) ) {
79
                msg->Data.bytes[2] == ((hwid>>16)&0xff) && msg->Data.bytes[3] == ((hwid>>24)&0xff) ) {
74
            BIOS_Reset();
80
            BIOS_Reset();
75
        }
81
        }
76
        // Copy message to bios buffer if bios is done with the previous message.
82
        // Copy message to bios buffer if bios is done with the previous message.
77
        // This is also a check to see if bios is still running. When app is
83
        // This is also a check to see if bios is still running. When app is
78
        // started, bios_msg_full is never reset to 0 so the check fails and all
84
        // started, bios_msg_full is never reset to 0 so the check fails and all
Line 122... Line 128...
122
    tx_msg.ExtendedFlag = 1;
128
    tx_msg.ExtendedFlag = 1;
123
    tx_msg.Id = CAN_ID_NMT_BIOS_START;
129
    tx_msg.Id = CAN_ID_NMT_BIOS_START;
124
    tx_msg.DataLength = 8;
130
    tx_msg.DataLength = 8;
125
    tx_msg.Data.bytes[0] = BIOS_VERSION&0xff;
131
    tx_msg.Data.bytes[0] = BIOS_VERSION&0xff;
126
    tx_msg.Data.bytes[1] = (BIOS_VERSION>>8)&0xff;
132
    tx_msg.Data.bytes[1] = (BIOS_VERSION>>8)&0xff;
127
    tx_msg.Data.bytes[4] = NODE_HW_ID&0xff;
133
    tx_msg.Data.bytes[4] = hwid&0xff;
128
    tx_msg.Data.bytes[5] = (NODE_HW_ID>>8)&0xff;
134
    tx_msg.Data.bytes[5] = (hwid>>8)&0xff;
129
    tx_msg.Data.bytes[6] = (NODE_HW_ID>>16)&0xff;
135
    tx_msg.Data.bytes[6] = (hwid>>16)&0xff;
130
    tx_msg.Data.bytes[7] = (NODE_HW_ID>>24)&0xff;
136
    tx_msg.Data.bytes[7] = (hwid>>24)&0xff;
131
 
137
 
132
    if (pgm_read_word(0) == 0xffff) {
138
    if (pgm_read_word(0) == 0xffff) {
133
        // No application in flash
139
        // No application in flash
134
        // Send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
140
        // Send CAN_NMT_BIOS_START(BIOS_VERSION, 0)
135
        tx_msg.Data.bytes[2] = 0;
141
        tx_msg.Data.bytes[2] = 0;
Line 160... Line 166...
160
       
166
       
161
        switch (bios_state) {
167
        switch (bios_state) {
162
        case BIOS_APP:
168
        case BIOS_APP:
163
            //if (nmt_type == CAN_NMT_START_APP) {
169
            //if (nmt_type == CAN_NMT_START_APP) {
164
            if (nmt_type == CAN_NMT_START_APP && bios_msg.DataLength == 4 &&
170
            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) &&
171
                    bios_msg.Data.bytes[0] == (hwid&0xff) && bios_msg.Data.bytes[1] == ((hwid>>8)&0xff) &&
166
                    bios_msg.Data.bytes[2] == ((NODE_HW_ID>>16)&0xff) && bios_msg.Data.bytes[3] == ((NODE_HW_ID>>24)&0xff) ) {
172
                    bios_msg.Data.bytes[2] == ((hwid>>16)&0xff) && bios_msg.Data.bytes[3] == ((hwid>>24)&0xff) ) {
167
                app_reset(); // Will never return
173
                app_reset(); // Will never return
168
            }
174
            }
169
            // Fall through
175
            // Fall through
170
        case BIOS_NOAPP:
176
        case BIOS_NOAPP:
171
            //if (nmt_type == CAN_NMT_PGM_START) {
177
            //if (nmt_type == CAN_NMT_PGM_START) {
172
            if (nmt_type == CAN_NMT_PGM_START && bios_msg.DataLength == 8 &&
178
            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) &&
179
                    bios_msg.Data.bytes[4] == (hwid&0xff) && bios_msg.Data.bytes[5] == ((hwid>>8)&0xff) &&
174
                    bios_msg.Data.bytes[6] == ((NODE_HW_ID>>16)&0xff) && bios_msg.Data.bytes[7] == ((NODE_HW_ID>>24)&0xff) ) {
180
                    bios_msg.Data.bytes[6] == ((hwid>>16)&0xff) && bios_msg.Data.bytes[7] == ((hwid>>24)&0xff) ) {
175
                // Set base address
181
                // Set base address
176
                base_addr = bios_msg.Data.words[0];
182
                base_addr = bios_msg.Data.words[0];
177
                flash_init(pagebuf);
183
                flash_init(pagebuf);
178
                //send CAN_NMT_PGM_ACK(offset)
184
                //send CAN_NMT_PGM_ACK(offset)
179
                tx_msg.Id = CAN_ID_NMT_PGM_ACK;
185
                tx_msg.Id = CAN_ID_NMT_PGM_ACK;