Subversion Repositories HomeAutomation

Rev

Rev 254 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 254 Rev 283
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 <avr/wdt.h>
4
#include "flash.h"
5
#include "flash.h"
5
 
6
 
6
static uint16_t flash_prev_addr;
7
static uint16_t flash_prev_addr;
7
static uint8_t flash_buffer_dirty = 0;
8
static uint8_t flash_buffer_dirty = 0;
8
 
9
 
9
void flash_flush_buffer() {
10
void flash_flush_buffer() {
10
    uint8_t sreg;
11
    uint8_t sreg;
11
   
12
   
12
    if (!flash_buffer_dirty) return; // Nothing to flush
13
    if (!flash_buffer_dirty) return; // Nothing to flush
13
   
14
   
14
    // Disable interrupts.
15
    // Disable interrupts.
15
   
16
   
16
    sreg = SREG;
17
    sreg = SREG;
17
    cli();
18
    cli();
18
   
19
   
19
    eeprom_busy_wait();
20
    eeprom_busy_wait();
20
   
21
   
21
    boot_page_erase(flash_prev_addr);
22
    boot_page_erase(flash_prev_addr);
22
    boot_spm_busy_wait();      // Wait until the memory is erased.
23
    boot_spm_busy_wait();      // Wait until the memory is erased.
23
   
24
   
24
    boot_page_write(flash_prev_addr);     // Store buffer in flash page.
25
    boot_page_write(flash_prev_addr);     // Store buffer in flash page.
25
    boot_spm_busy_wait();       // Wait until the memory is written.
26
    boot_spm_busy_wait();       // Wait until the memory is written.
26
 
27
 
27
    flash_buffer_dirty = 0;
28
    flash_buffer_dirty = 0;
28
 
29
 
29
    // Reenable RWW-section again. We need this if we want to jump back
30
    // Reenable RWW-section again. We need this if we want to jump back
30
    // to the application after bootloading.
31
    // to the application after bootloading.
31
 
32
 
32
    boot_rww_enable ();
33
    boot_rww_enable ();
33
    // Re-enable interrupts (if they were ever enabled).
34
    // Re-enable interrupts (if they were ever enabled).
34
 
35
 
35
    SREG = sreg;
36
    SREG = sreg;
36
}
37
}
37
 
38
 
38
#ifdef FLASH_LOAD_BUFFER
39
#ifdef FLASH_LOAD_BUFFER
39
void flash_load_buffer(uint16_t addr) BOOTLOADER;
40
void flash_load_buffer(uint16_t addr) BOOTLOADER;
40
void flash_load_buffer(uint16_t addr) {
41
void flash_load_buffer(uint16_t addr) {
41
    //TODO: Load current flash page contents into temporary buffer to implement
42
    //TODO: Load current flash page contents into temporary buffer to implement
42
    // flash read-modify-writes with word granularity. (If possible, the data
43
    // flash read-modify-writes with word granularity. (If possible, the data
43
    // sheet is a little fuzzy about this.)
44
    // sheet is a little fuzzy about this.)
-
 
45
    // Update: It seems it isn't possible to write any location in the temporary
-
 
46
    // buffer more than once without destroying the buffer. To implement this
-
 
47
    // functionality, a sram based buffer is probably neccessary. Problem is, 
-
 
48
    // how should it be allocated? A global buffer uses valuable .bss space.
-
 
49
    // All functions should probably take a pointer to a buffer allocated by
-
 
50
    // main code.
44
    uint8_t i;
51
    uint8_t i;
45
    uint16_t data;
52
    uint16_t data;
46
    uint16_t page = (addr / SPM_PAGESIZE) * SPM_PAGESIZE;
53
    uint16_t page = (addr / SPM_PAGESIZE) * SPM_PAGESIZE;
47
   
54
   
48
    for (i = 0; i < SPM_PAGESIZE; i += 2) {
55
    for (i = 0; i < SPM_PAGESIZE; i += 2) {
49
        data = pgm_read_word(page + i);
56
        data = pgm_read_word(page + i);
50
        boot_page_fill(page + i, data);
57
        boot_page_fill(page + i, data);
51
    }
58
    }
52
}
59
}
53
#endif
60
#endif
54
 
61
 
55
void flash_write_word(uint16_t addr, uint16_t word) {
62
void flash_write_word(uint16_t addr, uint16_t word) {
56
   
63
   
57
    if ((addr / SPM_PAGESIZE) != (flash_prev_addr / SPM_PAGESIZE)) {
64
    if ((addr / SPM_PAGESIZE) != (flash_prev_addr / SPM_PAGESIZE)) {
58
        flash_flush_buffer();
65
        flash_flush_buffer();
59
#ifdef FLASH_LOAD_BUFFER
66
#ifdef FLASH_LOAD_BUFFER
60
        flash_load_buffer(addr);
67
        flash_load_buffer(addr);
61
#endif
68
#endif
62
    }
69
    }
63
   
70
   
64
    boot_page_fill(addr, word);
71
    boot_page_fill(addr, word);
65
    flash_prev_addr = addr;
72
    flash_prev_addr = addr;
66
    flash_buffer_dirty = 1;
73
    flash_buffer_dirty = 1;
67
}    
74
}    
68
 
75
 
69
void flash_init() {
76
void flash_init() {
70
    flash_prev_addr = 0xffff;
77
    flash_prev_addr = 0xffff;
71
    flash_buffer_dirty = 0;
78
    flash_buffer_dirty = 0;
72
}
79
}
73
 
80
 
74
#ifdef FLASH_COPY_DATA
81
#ifdef FLASH_COPY_DATA
75
void flash_copy_data(uint16_t src, uint16_t dst, uint16_t len) {
82
void flash_copy_data(uint16_t src, uint16_t dst, uint16_t len) {
76
    uint16_t i;
83
    uint16_t i;
77
    flash_init();
84
    flash_init();
-
 
85
    cli(); // From this point we're on our own
78
    for (i = 0; i < len; i+=2) {
86
    for (i = 0; i < len; i+=2) {
79
        flash_write_word(dst + i, pgm_read_word(src + i));
87
        flash_write_word(dst + i, pgm_read_word(src + i));
-
 
88
        wdt_reset();
80
    }
89
    }
81
    flash_flush_buffer();
90
    flash_flush_buffer();
-
 
91
    while (1); // Nothing more we can do, hopefully there is a watchdog active
82
}
92
}
83
#endif
93
#endif
84
 
94