49 #include "dev/watchdog.h"
52 #define PRINTF(...) printf(__VA_ARGS__)
54 #define PRINTF(...) do {} while (0)
57 #define SPI_FLASH_INS_WREN 0x06
58 #define SPI_FLASH_INS_WRDI 0x04
59 #define SPI_FLASH_INS_RDSR 0x05
60 #define SPI_FLASH_INS_WRSR 0x01
61 #define SPI_FLASH_INS_READ 0x03
62 #define SPI_FLASH_INS_FAST_READ 0x0b
63 #define SPI_FLASH_INS_PP 0x02
64 #define SPI_FLASH_INS_SE 0xd8
65 #define SPI_FLASH_INS_BE 0xc7
66 #define SPI_FLASH_INS_DP 0xb9
67 #define SPI_FLASH_INS_RES 0xab
77 SPI_WRITE(SPI_FLASH_INS_WREN);
84 read_status_register(
void)
93 SPI_WRITE(SPI_FLASH_INS_RDSR);
112 u = read_status_register();
122 erase_sector(
unsigned long offset)
132 SPI_WRITE_FAST(SPI_FLASH_INS_SE);
133 SPI_WRITE_FAST(offset >> 16);
134 SPI_WRITE_FAST(offset >> 8);
135 SPI_WRITE_FAST(offset >> 0);
136 SPI_WAITFORTx_ENDED();
151 P4DIR |= BV(FLASH_CS) | BV(FLASH_HOLD) | BV(FLASH_PWR);
152 P4OUT |= BV(FLASH_PWR);
157 SPI_WRITE_FAST(SPI_FLASH_INS_RES);
158 SPI_WAITFORTx_ENDED();
166 xmem_pread(
void *_p,
int size,
unsigned long offset)
168 unsigned char *p = _p;
169 const unsigned char *end = p + size;
174 ENERGEST_ON(ENERGEST_TYPE_FLASH_READ);
179 SPI_WRITE_FAST(SPI_FLASH_INS_READ);
180 SPI_WRITE_FAST(offset >> 16);
181 SPI_WRITE_FAST(offset >> 8);
182 SPI_WRITE_FAST(offset >> 0);
183 SPI_WAITFORTx_ENDED();
186 for(; p < end; p++) {
195 ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ);
200 static const unsigned char *
201 program_page(
unsigned long offset,
const unsigned char *p,
int nbytes)
203 const unsigned char *end = p + nbytes;
212 SPI_WRITE_FAST(SPI_FLASH_INS_PP);
213 SPI_WRITE_FAST(offset >> 16);
214 SPI_WRITE_FAST(offset >> 8);
215 SPI_WRITE_FAST(offset >> 0);
217 for(; p < end; p++) {
220 SPI_WAITFORTx_ENDED();
229 xmem_pwrite(
const void *_buf,
int size,
unsigned long addr)
231 const unsigned char *p = _buf;
232 const unsigned long end = addr + size;
233 unsigned long i, next_page;
235 ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
237 for(i = addr; i < end;) {
238 next_page = (i | 0xff) + 1;
239 if(next_page > end) {
242 p = program_page(i, p, next_page - i);
246 ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
252 xmem_erase(
long size,
unsigned long addr)
254 unsigned long end = addr + size;
256 if(size % XMEM_ERASE_UNIT_SIZE != 0) {
257 PRINTF(
"xmem_erase: bad size\n");
261 if(addr % XMEM_ERASE_UNIT_SIZE != 0) {
262 PRINTF(
"xmem_erase: bad offset\n");
266 for (; addr < end; addr += XMEM_ERASE_UNIT_SIZE) {