Use flash key defines/constants when writing to flash

This commit is contained in:
Imbus 2025-02-18 00:34:48 +01:00
parent bfcbb77570
commit 8745c22268

9
rand.c
View file

@ -1,5 +1,6 @@
#include "rand.h" #include "rand.h"
#include "ch32fun.h" #include "ch32fun.h"
#include "ch32v003hw.h"
#include <stdint.h> #include <stdint.h>
#define BUILD_SEED \ #define BUILD_SEED \
@ -51,11 +52,11 @@ void rand_reseed() {
// See: // See:
// https://github.com/cnlohr/ch32v003fun/blob/2ac62072272f2ccd2122e688a9e0566de3976a94/examples/flashtest/flashtest.c // https://github.com/cnlohr/ch32v003fun/blob/2ac62072272f2ccd2122e688a9e0566de3976a94/examples/flashtest/flashtest.c
void rand_save_to_flash() { void rand_save_to_flash() {
FLASH->KEYR = 0x45670123; // Unlock flash FLASH->KEYR = FLASH_KEY1; // Unlock flash
FLASH->KEYR = 0xCDEF89AB; FLASH->KEYR = FLASH_KEY2;
FLASH->MODEKEYR = 0x45670123; // Unlock programming mode FLASH->MODEKEYR = FLASH_KEY1; // Unlock programming mode
FLASH->MODEKEYR = 0xCDEF89AB; FLASH->MODEKEYR = FLASH_KEY2;
// Erase the flash page // Erase the flash page
FLASH->CTLR = CR_PAGE_ER; FLASH->CTLR = CR_PAGE_ER;