scoutos / scout_avr / bom / bootloader.c @ 6030b995
History | View | Annotate | Download (1.41 KB)
1 |
#include <inttypes.h> |
---|---|
2 |
#include <avr/interrupt.h> |
3 |
#include <avr/pgmspace.h> |
4 |
#include "tiny-twi-sync.h" |
5 |
|
6 |
uint32_t current_page; |
7 |
|
8 |
void boot_program_page (uint32_t page, uint8_t *buf)
|
9 |
{ |
10 |
uint16_t i; |
11 |
uint8_t sreg; |
12 |
|
13 |
// Disable interrupts.
|
14 |
|
15 |
sreg = SREG; |
16 |
cli(); |
17 |
|
18 |
eeprom_busy_wait (); |
19 |
|
20 |
boot_page_erase (page); |
21 |
boot_spm_busy_wait (); // Wait until the memory is erased.
|
22 |
|
23 |
for (i=0; i<SPM_PAGESIZE; i+=2) |
24 |
{ |
25 |
// Set up little-endian word.
|
26 |
uint16_t w = *buf++; |
27 |
w += (*buf++) << 8;
|
28 |
|
29 |
boot_page_fill (page + i, w); |
30 |
} |
31 |
|
32 |
boot_page_write (page); // Store buffer in flash page.
|
33 |
boot_spm_busy_wait(); // Wait until the memory is written.
|
34 |
|
35 |
// Reenable RWW-section again. We need this if we want to jump back
|
36 |
// to the application after bootloading.
|
37 |
|
38 |
boot_rww_enable (); |
39 |
|
40 |
// Re-enable interrupts (if they were ever enabled).
|
41 |
|
42 |
SREG = sreg; |
43 |
} |
44 |
|
45 |
static void bom_pgrm(uint8_t *buf, int len) { |
46 |
if (len == SPM_PAGESIZE) {
|
47 |
if (current_page == 0) { |
48 |
uint16_t w = 0xC000 | ((START_ADDR >> 1) - 1); |
49 |
buf[0] = w & 0xFF; |
50 |
buf[1] = (w >> 16) & 0xFF; |
51 |
} |
52 |
boot_program_page(current_page, buf); |
53 |
current_page++; |
54 |
} |
55 |
} |
56 |
|
57 |
static void slave_rx(uint8_t *buf, int len) { |
58 |
if (len > 1) { |
59 |
switch (buf[0]) { |
60 |
case BOM_I2C_PGRM:
|
61 |
bom_pgrm(buf+1, len-1); |
62 |
break;
|
63 |
case BOM_I2C_STRT:
|
64 |
strt_pgrm(); //yolo
|
65 |
break;
|
66 |
} |
67 |
} |
68 |
} |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
int main() {
|
74 |
return 0; |
75 |
} |