Rev 233 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 233 | Rev 254 | ||
|---|---|---|---|
| 1 | #include <avr/interrupt.h> |
1 | #include <avr/interrupt.h> |
| 2 | #include <avr/boot.h> |
2 | #include <avr/boot.h> |
| 3 | #include <avr/pgmspace.h> |
3 | #include <avr/pgmspace.h> |
| 4 | #include "flash.h" |
4 | #include "flash.h" |
| 5 | 5 | ||
| 6 | static uint16_t flash_prev_addr; |
6 | static uint16_t flash_prev_addr; |
| 7 | static uint8_t flash_buffer_dirty = 0; |
7 | static uint8_t flash_buffer_dirty = 0; |
| 8 | 8 | ||
| 9 | void flash_flush_buffer() { |
9 | void flash_flush_buffer() { |
| 10 | uint8_t sreg; |
10 | uint8_t sreg; |
| 11 | 11 | ||
| 12 | if (!flash_buffer_dirty) return; // Nothing to flush |
12 | if (!flash_buffer_dirty) return; // Nothing to flush |
| 13 | 13 | ||
| 14 | // Disable interrupts. |
14 | // Disable interrupts. |
| 15 | 15 | ||
| 16 | sreg = SREG; |
16 | sreg = SREG; |
| 17 | cli(); |
17 | cli(); |
| 18 | 18 | ||
| 19 | eeprom_busy_wait(); |
19 | eeprom_busy_wait(); |
| 20 | 20 | ||
| 21 | boot_page_erase(flash_prev_addr); |
21 | boot_page_erase(flash_prev_addr); |
| 22 | boot_spm_busy_wait(); // Wait until the memory is erased. |
22 | boot_spm_busy_wait(); // Wait until the memory is erased. |
| 23 | 23 | ||
| 24 | boot_page_write(flash_prev_addr); // Store buffer in flash page. |
24 | boot_page_write(flash_prev_addr); // Store buffer in flash page. |
| 25 | boot_spm_busy_wait(); // Wait until the memory is written. |
25 | boot_spm_busy_wait(); // Wait until the memory is written. |
| 26 | 26 | ||
| 27 | flash_buffer_dirty = 0; |
27 | flash_buffer_dirty = 0; |
| 28 | 28 | ||
| 29 | // Reenable RWW-section again. We need this if we want to jump back |
29 | // Reenable RWW-section again. We need this if we want to jump back |
| 30 | // to the application after bootloading. |
30 | // to the application after bootloading. |
| 31 | 31 | ||
| 32 | boot_rww_enable (); |
32 | boot_rww_enable (); |
| 33 | // Re-enable interrupts (if they were ever enabled). |
33 | // Re-enable interrupts (if they were ever enabled). |
| 34 | 34 | ||
| 35 | SREG = sreg; |
35 | SREG = sreg; |
| 36 | } |
36 | } |
| 37 | 37 | ||
| - | 38 | #ifdef FLASH_LOAD_BUFFER |
|
| - | 39 | void flash_load_buffer(uint16_t addr) BOOTLOADER; |
|
| 38 | void flash_load_buffer(uint16_t addr) { |
40 | void flash_load_buffer(uint16_t addr) { |
| 39 | //TODO: Load current flash page contents into temporary buffer to implement |
41 | //TODO: Load current flash page contents into temporary buffer to implement |
| 40 | // flash read-modify-writes with word granularity. (If possible, the data |
42 | // flash read-modify-writes with word granularity. (If possible, the data |
| 41 | // sheet is a little fuzzy about this.) |
43 | // sheet is a little fuzzy about this.) |
| 42 | uint8_t i; |
44 | uint8_t i; |
| 43 | uint16_t data; |
45 | uint16_t data; |
| 44 | uint16_t page = (addr / SPM_PAGESIZE) * SPM_PAGESIZE; |
46 | uint16_t page = (addr / SPM_PAGESIZE) * SPM_PAGESIZE; |
| 45 | 47 | ||
| 46 | for (i = 0; i < SPM_PAGESIZE; i += 2) { |
48 | for (i = 0; i < SPM_PAGESIZE; i += 2) { |
| 47 | data = pgm_read_word(page + i); |
49 | data = pgm_read_word(page + i); |
| 48 | boot_page_fill(page + i, data); |
50 | boot_page_fill(page + i, data); |
| 49 | } |
51 | } |
| 50 | } |
52 | } |
| - | 53 | #endif |
|
| 51 | 54 | ||
| 52 | void flash_write_word(uint16_t addr, uint16_t word) { |
55 | void flash_write_word(uint16_t addr, uint16_t word) { |
| 53 | 56 | ||
| 54 | if ((addr / SPM_PAGESIZE) != (flash_prev_addr / SPM_PAGESIZE)) { |
57 | if ((addr / SPM_PAGESIZE) != (flash_prev_addr / SPM_PAGESIZE)) { |
| 55 | flash_flush_buffer(); |
58 | flash_flush_buffer(); |
| - | 59 | #ifdef FLASH_LOAD_BUFFER |
|
| 56 | flash_load_buffer(addr); |
60 | flash_load_buffer(addr); |
| - | 61 | #endif |
|
| 57 | } |
62 | } |
| 58 | 63 | ||
| 59 | boot_page_fill(addr, word); |
64 | boot_page_fill(addr, word); |
| 60 | flash_prev_addr = addr; |
65 | flash_prev_addr = addr; |
| 61 | flash_buffer_dirty = 1; |
66 | flash_buffer_dirty = 1; |
| 62 | } |
67 | } |
| 63 | 68 | ||
| 64 | void flash_init() { |
69 | void flash_init() { |
| 65 | flash_prev_addr = 0xffff; |
70 | flash_prev_addr = 0xffff; |
| 66 | flash_buffer_dirty = 0; |
71 | flash_buffer_dirty = 0; |
| 67 | } |
72 | } |
| - | 73 | ||
| - | 74 | #ifdef FLASH_COPY_DATA |
|
| - | 75 | void flash_copy_data(uint16_t src, uint16_t dst, uint16_t len) { |
|
| - | 76 | uint16_t i; |
|
| - | 77 | flash_init(); |
|
| - | 78 | for (i = 0; i < len; i+=2) { |
|
| - | 79 | flash_write_word(dst + i, pgm_read_word(src + i)); |
|
| - | 80 | } |
|
| - | 81 | flash_flush_buffer(); |
|
| - | 82 | } |
|
| - | 83 | #endif |
|
| 68 | 84 | ||