diff --git a/examples/optiondata/Makefile b/examples/optiondata/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..5ad2b8e5ed62673531a142e2a44fa1e7ebf78b08 --- /dev/null +++ b/examples/optiondata/Makefile @@ -0,0 +1,8 @@ +all : flash + +TARGET:=optiondata + +include ../../ch32v003fun/ch32v003fun.mk + +flash : cv_flash +clean : cv_clean diff --git a/examples/optiondata/README.md b/examples/optiondata/README.md new file mode 100644 index 0000000000000000000000000000000000000000..840aee6057895eb3696b9e18407e37190a9f87cf --- /dev/null +++ b/examples/optiondata/README.md @@ -0,0 +1,11 @@ +# Write to the user fields in the Option Data flash + +This is a simple example code for writing to the two 8-bit data0/data1 bytes that are a part of the Flash "User-selected words" (aka Option bytes). + +To reduce the code footpint no timeouts are implemented in the code. It simply waits for the busy-flag to be cleaed. The two wait-loops could be replaced with a Delay_Ms(10) if speed is not an issue. + +The two bytes could be used for remembering user settings between power-offs. + +After flashing you can test it with the command +`../../minichlink/minichlink -a -b -T` +that will Halt, Reboot and enter Terminal mode. For each run the count will increment by one. diff --git a/examples/optiondata/optiondata.c b/examples/optiondata/optiondata.c new file mode 100644 index 0000000000000000000000000000000000000000..a3c8f273c0a07d9416ff30963cacea91651b6b17 --- /dev/null +++ b/examples/optiondata/optiondata.c @@ -0,0 +1,74 @@ +// +// Example code for writing to the two 8-bit data0/data1 bytes that are +// a part of the Flash "User-selected words" (aka Option bytes). +// To reduce the code footpint no timeouts are implemented in the code. It simply +// waits for the busy-flag to be cleaed. The two wait-loops could be replaced +// with a Delay_Ms(10) if speed is not an issue. +// +// June 13, 2023 Mats Engstrom (github.com/mengstr) +// + +#define SYSTEM_CORE_CLOCK 48000000 + +#include "ch32v003fun.h" +#include <stdio.h> +void FlashOptionData(uint8_t data0, uint8_t data1) { + volatile uint16_t hold[6]; // array to hold current values while erasing + + // The entire 64 byte data block of the "User-selected words" will be erased + // so we need to keep a copy of the content for re-writing after erase. + // Save a few (20) bytes code space by moving 32 bits at a time. + // hold[0]=OB->RDPR; + // hold[1]=OB->USER; + // hold[2]=data0; + // hold[3]=data1; + // hold[4]=OB->WRPR0; + // hold[5]=OB->WRPR1; + uint32_t *hold32p=(uint32_t *)hold; + uint32_t *ob32p=(uint32_t *)OB_BASE; + hold32p[0]=ob32p[0]; // Copy RDPR and USER + hold32p[1]=data0+(data1<<16); // Copy in the two Data values to be written + hold32p[2]=ob32p[2]; // Copy WRPR0 and WEPR1 + + // Unlock both the general Flash and the User-selected words + FLASH->KEYR = FLASH_KEY1; + FLASH->KEYR = FLASH_KEY2; + FLASH->OBKEYR = FLASH_KEY1; + FLASH->OBKEYR = FLASH_KEY2; + + FLASH->CTLR |= CR_OPTER_Set; // OBER RW Perform user-selected word erasure + FLASH->CTLR |= CR_STRT_Set; // STRT RW1 Start. Set 1 to start an erase action,hw automatically clears to 0 + while (FLASH->STATR & FLASH_BUSY); // Wait for flash operation to be done + FLASH->CTLR &= CR_OPTER_Reset; // Disable erasure mode + + // Write the held values back one-by-one + FLASH->CTLR |= CR_OPTPG_Set; // OBG RW Perform user-selected word programming + uint16_t *ob16p=(uint16_t *)OB_BASE; + for (int i=0;i<sizeof(hold)/sizeof(hold[0]); i++) { + ob16p[i]=hold[i]; + while (FLASH->STATR & FLASH_BUSY); // Wait for flash operation to be done + } + FLASH->CTLR &= CR_OPTPG_Reset; // Disable programming mode + + FLASH->CTLR|=CR_LOCK_Set; // Lock flash memories again + + return; +} + + + +// +// +// +int main() { + SystemInit48HSI(); + SetupDebugPrintf(); + Delay_Ms(250); + + uint8_t bootcnt=OB->Data0; + bootcnt++; + FlashOptionData(bootcnt,0); + printf("Boot count is %d\n",bootcnt); + + for(;;); +}