dc32-peppercon9-addon/firmware/user/src/flash.c

43 lines
846 B
C

/*
* Created on: Jul 29, 2024
*
* routines for fucking around with built in flash memory.
*/
#include <stdint.h>
#include <ch32v20x.h>
// reads from flash in 32-bit mode
uint8_t flash_read(uint32_t *flash_addr, uint32_t *data, uint32_t len)
{
uint32_t *addr = (uint32_t *)flash_addr;
while (len >= 4) {
*data++ = *addr++;
len -= 4;
}
return 0;
}
// erases flash page, then writes 256-byte data buffer to flash page
// flash page must be at 256-byte boundary
uint8_t flash_write256(uint32_t *flash_addr, uint32_t *data)
{
FLASH_Status s;
// erase flash page
s = FLASH_ROM_ERASE((uint32_t)flash_addr, 256);
if (s != FLASH_COMPLETE) {
return s;
}
s = FLASH_ROM_WRITE((uint32_t)flash_addr, data, 256);
if (s != FLASH_COMPLETE) {
return s;
}
return 0;
}