Subversion Repositories HomeAutomation

Rev

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

Rev 253 Rev 254
Line 3... Line 3...
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
#include <stdio.h>
9
#include <stdio.h>
-
 
10
#endif
9
#include <string.h>
11
#include <string.h>
10
#include "flash.h"
12
#include "flash.h"
11
#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()
12
#include "mcp2515/mcp2515.h"
14
#include "mcp2515/mcp2515.h"
13
 
15
 
14
//---------------------------------------------------------------------------
16
//---------------------------------------------------------------------------
15
// Globals
17
// Globals
16
 
18
 
17
volatile long gMilliSecTick;
19
volatile uint32_t gMilliSecTick;
18
volatile uint8_t bios_msg_full;
20
volatile uint8_t bios_msg_full;
19
Can_Message_t* bios_msg_ptr; // only a pointer to main-local structure to save .bss space
21
Can_Message_t* bios_msg_ptr; // only a pointer to main-local structure to save .bss space
20
 
22
 
21
//---------------------------------------------------------------------------
23
//---------------------------------------------------------------------------
22
// Function definitions for the exported interface
24
// Function definitions for the exported interface
Line 25... Line 27...
25
    //Just loop and let the watchdog reset
27
    //Just loop and let the watchdog reset
26
    cli();
28
    cli();
27
    while(1);
29
    while(1);
28
}
30
}
29
 
31
 
-
 
32
#ifdef BIOS_UART
30
void bios_putchar(char c) {
33
void bios_putchar(char c) {
31
    UART_PUTCHAR(c);
34
    UART_PUTCHAR(c);
32
}
35
}
33
 
36
 
34
char bios_getchar(void) {
37
char bios_getchar(void) {
35
    char c;
38
    char c;
36
    UART_GETCHAR(c)
39
    UART_GETCHAR(c)
37
    return c;
40
    return c;
38
}
41
}
-
 
42
#endif
39
 
43
 
40
long timebase_get(void) {
44
unsigned long timebase_get(void) {
41
    uint8_t sreg;
45
    uint8_t sreg;
42
    long res;
46
    unsigned long res;
43
   
47
   
44
    //Disable interrupts while reading tick count
48
    //Disable interrupts while reading tick count
45
    sreg=SREG;
49
    sreg=SREG;
46
    cli();
50
    cli();
47
   
51
   
Line 58... Line 62...
58
}
62
}
59
 
63
 
60
//---------------------------------------------------------------------------
64
//---------------------------------------------------------------------------
61
// Function definitions for the private functions
65
// Function definitions for the private functions
62
 
66
 
-
 
67
#ifdef BIOS_UART
63
static int console_getchar(FILE *stream) {
68
static int console_getchar(FILE *stream) {
64
    return bios_getchar();
69
    return bios_getchar();
65
}
70
}
66
 
71
 
67
static int console_putchar(char c, FILE *stream) {
72
static int console_putchar(char c, FILE *stream) {
Line 69... Line 74...
69
    return 0;
74
    return 0;
70
}
75
}
71
 
76
 
72
static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
77
static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
73
                                         _FDEV_SETUP_RW);
78
                                         _FDEV_SETUP_RW);
74
   
79
#endif
75
 
80
 
76
//---------------------------------------------------------------------------
81
//---------------------------------------------------------------------------
77
// Interrupt Service Routines
82
// Interrupt Service Routines
78
// Read app_vectors.h!
83
// Read app_vectors.h!
79
 
84
 
Line 123... Line 128...
123
    uint16_t data;
128
    uint16_t data;
124
    Can_Message_t tx_msg;
129
    Can_Message_t tx_msg;
125
    Can_Message_t bios_msg;
130
    Can_Message_t bios_msg;
126
    bios_msg_ptr = &bios_msg;
131
    bios_msg_ptr = &bios_msg;
127
   
132
   
-
 
133
#ifdef BIOS_UART
128
    // Setup USART for debug channel
134
    // Setup USART for debug channel
129
    UART_INIT();
135
    UART_INIT();
130
    stdout = &bios_stdio;
136
    stdout = &bios_stdio;
131
    stdin = &bios_stdio;
137
    stdin = &bios_stdio;
-
 
138
#endif
132
   
139
   
133
    // Initialize a suitable hardware timer to create a 1KHz interrupt.
140
    // Initialize a suitable hardware timer to create a 1KHz interrupt.
134
    TIMEBASE_INIT();
141
    TIMEBASE_INIT();
135
   
142
   
136
    // Enable watchdog timer to protect against an application locking the
143
    // Enable watchdog timer to protect against an application locking the
Line 181... Line 188...
181
        case BIOS_NOAPP:
188
        case BIOS_NOAPP:
182
            if (nmt_type == CAN_NMT_PGM_START) {
189
            if (nmt_type == CAN_NMT_PGM_START) {
183
                // Set base address
190
                // Set base address
184
                base_addr = bios_msg.Data.words[0];
191
                base_addr = bios_msg.Data.words[0];
185
                flash_init();
192
                flash_init();
-
 
193
                //send CAN_NMT_PGM_ACK(offset)
-
 
194
                tx_msg.Id = CAN_ID_NMT_PGM_ACK;
-
 
195
                tx_msg.Data.words[0] = base_addr;
186
                bios_state = BIOS_PGM;
196
                bios_state = BIOS_PGM;
187
            }
197
            }
188
            break;
198
            break;
189
        case BIOS_PGM:
199
        case BIOS_PGM:
190
            if (nmt_type == CAN_NMT_PGM_DATA) {
200
            if (nmt_type == CAN_NMT_PGM_DATA) {
Line 197... Line 207...
197
                    //flash data at address
207
                    //flash data at address
198
                    len = (bios_msg.DataLength+1)/2; // Number of words in message, rounded up.
208
                    len = (bios_msg.DataLength+1)/2; // Number of words in message, rounded up.
199
                    for (i=1; i<len; i++) { // Skip first word (offset)
209
                    for (i=1; i<len; i++) { // Skip first word (offset)
200
                        data = bios_msg.Data.words[i];
210
                        data = bios_msg.Data.words[i];
201
                        flash_write_word(addr, data);
211
                        flash_write_word(addr, data);
-
 
212
                        addr += 2;
202
                    }
213
                    }
203
                    //update crc
214
                    //update crc
204
                    //send CAN_NMT_PGM_ACK(offset)
215
                    //send CAN_NMT_PGM_ACK(offset)
205
                    tx_msg.Id = CAN_ID_NMT_PGM_ACK;
216
                    tx_msg.Id = CAN_ID_NMT_PGM_ACK;
206
                } else {
217
                } else {