BeRTOS
|
00001 00043 #include "flash_avr.h" 00044 00045 #include "cfg/cfg_emb_flash.h" 00046 00047 #include <cfg/macros.h> // MIN() 00048 #include <cfg/compiler.h> 00049 #include <cfg/debug.h> 00050 #include <cpu/irq.h> 00051 00052 // Define log settings for cfg/log.h 00053 #define LOG_LEVEL CONFIG_FLASH_EMB_LOG_LEVEL 00054 #define LOG_FORMAT CONFIG_FLASH_EMB_LOG_FORMAT 00055 #include <cfg/log.h> 00056 00057 #include <drv/wdt.h> 00058 #include <drv/flash.h> 00059 00060 #include <io/kfile.h> 00061 #include <io/kfile_block.h> 00062 #include <io/kblock.h> 00063 00064 #include <avr/io.h> 00065 #include <avr/boot.h> 00066 #include <avr/pgmspace.h> 00067 00068 #include <string.h> 00069 00070 struct FlashHardware; 00071 00072 static size_t avr_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size) 00073 { 00074 memcpy_P(buf, (const void *)(uint16_t)(idx * blk->blk_size + offset), size); 00075 return blk->blk_size; 00076 } 00077 00078 static size_t avr_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size) 00079 { 00080 ASSERT(offset == 0); 00081 ASSERT(size == blk->blk_size); 00082 00083 uint32_t page_addr = idx * blk->blk_size; 00084 uint32_t addr = idx * blk->blk_size; 00085 const uint8_t *buf = (const uint8_t *)_buf; 00086 00087 /* Wait while the SPM instruction is busy. */ 00088 boot_spm_busy_wait(); 00089 00090 /* Fill the temporary buffer of the AVR */ 00091 while (size) 00092 { 00093 uint16_t data = ((*buf + 1) << 8) | *buf; 00094 ATOMIC(boot_page_fill(addr, data)); 00095 00096 buf += 2; 00097 size -= 2; 00098 addr += 2; 00099 } 00100 00101 wdt_reset(); 00102 00103 /* Page erase */ 00104 ATOMIC(boot_page_erase(page_addr)); 00105 00106 /* Wait until the memory is erased. */ 00107 boot_spm_busy_wait(); 00108 00109 /* Store buffer in flash page. */ 00110 ATOMIC(boot_page_write(page_addr)); 00111 00112 /* Wait while the SPM instruction is busy. */ 00113 boot_spm_busy_wait(); 00114 00115 /* 00116 * Reenable RWW-section again. We need this if we want to jump back 00117 * to the application after bootloading. 00118 */ 00119 ATOMIC(boot_rww_enable()); 00120 00121 return blk->blk_size; 00122 } 00123 00124 static int avr_flash_dummy(UNUSED_ARG(struct KBlock, *blk)) 00125 { 00126 return 0; 00127 } 00128 00129 static const KBlockVTable flash_avr_buffered_vt = 00130 { 00131 .readDirect = avr_flash_readDirect, 00132 .writeDirect = avr_flash_writeDirect, 00133 00134 .readBuf = kblock_swReadBuf, 00135 .writeBuf = kblock_swWriteBuf, 00136 .load = kblock_swLoad, 00137 .store = kblock_swStore, 00138 00139 .error = avr_flash_dummy, 00140 .clearerr = (kblock_clearerr_t)avr_flash_dummy, 00141 }; 00142 00143 static const KBlockVTable flash_avr_unbuffered_vt = 00144 { 00145 .readDirect = avr_flash_readDirect, 00146 .writeDirect = avr_flash_writeDirect, 00147 00148 .error = avr_flash_dummy, 00149 .clearerr = (kblock_clearerr_t)avr_flash_dummy, 00150 }; 00151 00152 static uint8_t flash_buf[SPM_PAGESIZE]; 00153 00154 static void common_init(Flash *fls) 00155 { 00156 memset(fls, 0, sizeof(*fls)); 00157 DB(fls->blk.priv.type = KBT_FLASH); 00158 00159 fls->blk.blk_size = SPM_PAGESIZE; 00160 fls->blk.blk_cnt = (FLASHEND + 1) / SPM_PAGESIZE; 00161 } 00162 00163 00164 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags)) 00165 { 00166 common_init(fls); 00167 fls->blk.priv.vt = &flash_avr_buffered_vt; 00168 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE; 00169 fls->blk.priv.buf = flash_buf; 00170 } 00171 00172 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags)) 00173 { 00174 common_init(fls); 00175 fls->blk.priv.vt = &flash_avr_unbuffered_vt; 00176 } 00177 00178