scoutos / scout_avr / bom / bootloader.c @ e92b8d00
History | View | Annotate | Download (1.6 KB)
1 |
#include <inttypes.h> |
---|---|
2 |
#include <avr/boot.h> |
3 |
#include <avr/interrupt.h> |
4 |
#include <avr/pgmspace.h> |
5 |
#include "tiny-twi-sync.h" |
6 |
#include "bootloader.h" |
7 |
|
8 |
uint32_t current_page; |
9 |
|
10 |
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 |
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 |
buf[1] = (w >> 8) & 0xFF; |
48 |
} |
49 |
boot_program_page(current_page, buf); |
50 |
current_page += SPM_PAGESIZE; |
51 |
} |
52 |
} |
53 |
|
54 |
static void slave_rx(uint8_t *buf, int len) { |
55 |
if (len >= 1) { |
56 |
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 |
static void timer_init(void) { |
68 |
TCNT0 = 0;
|
69 |
|
70 |
OCR0A = 200;
|
71 |
|
72 |
// clear on compare match A
|
73 |
TCCR0A |= _BV(WGM01); |
74 |
// 1024 prescaler
|
75 |
TCCR0B |= _BV(CS02) | _BV(CS00); |
76 |
} |
77 |
|
78 |
int main() {
|
79 |
smb_init(slave_rx); |
80 |
smb_set_address(3); // TODO parameterize address |
81 |
while (1) { |
82 |
smb_poll(); |
83 |
} |
84 |
return 0; |
85 |
} |