Contiki 2.5
flash.c
1 
2 #include "dev/flash.h"
3 
4 #include <avr/boot.h>
5 #include <inttypes.h>
6 #include <avr/interrupt.h>
7 #include <avr/pgmspace.h>
8 
9 /*---------------------------------------------------------------------------*/
10 /*
11  * The following code was taken from the avr-libc manual:
12  */
13 void
14 flash_write_page(uint32_t page, uint8_t *buf)
15 {
16  uint16_t i;
17  uint8_t sreg;
18 
19  /* Disable interrupts. */
20 
21  sreg = SREG;
22  cli();
23 
24  eeprom_busy_wait();
25 
26  boot_page_erase(page);
27  boot_spm_busy_wait(); /* Wait until the memory is erased. */
28 
29  for(i = 0; i < SPM_PAGESIZE; i += 2) {
30  /* Set up little-endian word. */
31 
32  uint16_t w = *buf++;
33  w += (*buf++) << 8;
34 
35  boot_page_fill(page + i, w);
36  }
37 
38  boot_page_write(page); /* Store buffer in flash page. */
39  boot_spm_busy_wait(); /* Wait until the memory is written. */
40 
41  /* Reenable RWW-section again. We need this if we want to jump back
42  * to the application after bootloading. */
43 
44  boot_rww_enable();
45 
46  /* Re-enable interrupts (if they were ever enabled). */
47 
48  SREG = sreg;
49 }
50 /*---------------------------------------------------------------------------*/