flash.c

00001 
00002 #include "dev/flash.h"
00003 
00004 #include <avr/boot.h>
00005 #include <inttypes.h>
00006 #include <avr/interrupt.h>
00007 #include <avr/pgmspace.h>
00008 
00009 /*---------------------------------------------------------------------------*/
00010 /*
00011  * The following code was taken from the avr-libc manual:
00012  */
00013 void
00014 flash_write_page(uint32_t page, uint8_t *buf)
00015 {
00016   uint16_t i;
00017   uint8_t sreg;
00018 
00019   /* Disable interrupts. */
00020 
00021   sreg = SREG;
00022   cli();
00023     
00024   eeprom_busy_wait();
00025 
00026   boot_page_erase(page);
00027   boot_spm_busy_wait();      /* Wait until the memory is erased. */
00028 
00029   for(i = 0; i < SPM_PAGESIZE; i += 2) {
00030     /* Set up little-endian word. */
00031     
00032     uint16_t w = *buf++;
00033     w += (*buf++) << 8;
00034     
00035     boot_page_fill(page + i, w);
00036   }
00037   
00038   boot_page_write(page);      /* Store buffer in flash page. */
00039   boot_spm_busy_wait();       /* Wait until the memory is written. */
00040   
00041   /* Reenable RWW-section again. We need this if we want to jump back
00042    * to the application after bootloading. */
00043   
00044   boot_rww_enable();
00045 
00046   /* Re-enable interrupts (if they were ever enabled). */
00047 
00048   SREG = sreg;
00049 }
00050 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:36 2011 for Contiki 2.5 by  doxygen 1.6.1