Created
December 23, 2016 03:39
-
-
Save summivox/a82b4d4355e5ecd5717068a2b2c63331 to your computer and use it in GitHub Desktop.
behavior description of SPI flash in pseudo-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef uint8_t byte; | |
const int FLASH_SIZE = 0x100000; | |
const int PAGE_SIZE = 0x100; | |
byte flash[FLASH_SIZE]; | |
int read_addr = 0; | |
byte write_buf[PAGE_SIZE]; | |
int write_page = 0; | |
int write_offset = 0; | |
void read_start(int a) { | |
read_addr = a & 0xffffff; | |
} | |
byte read() { | |
byte d = flash[read_addr]; | |
read_addr = (read_addr + 1) % FLASH_SIZE; | |
return d; | |
} | |
void write_start(int a) { | |
memset(buf, 0xff, 0x100); | |
write_page = a / PAGE_SIZE * PAGE_SIZE; | |
write_offset = a % PAGE_SIZE; | |
} | |
void write(byte d) { | |
buf[write_offset] = d; | |
write_offset = (write_offset + 1) % PAGE_SIZE; | |
} | |
void write_end() { | |
for (int i = 0 ; i < PAGE_SIZE ; i++) { | |
flash[write_page + i] &= buf[i]; | |
} | |
} | |
void erase_4k(int a) { | |
memset(flash + (a & 0xfff000), 0, 0x1000); | |
} | |
void erase_32k(int a) { | |
memset(flash + (a & 0xff8000), 0, 0x8000); | |
} | |
void erase_64k(int a) { | |
memset(flash + (a & 0xff0000), 0, 0x10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment