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