Update! Add good optionbytes

This commit is contained in:
cnlohr
2023-04-23 23:18:41 -04:00
parent dd3539706a
commit 51469453e8
3 changed files with 183 additions and 0 deletions

View File

@@ -10,6 +10,8 @@
// Note: Before running this, you should run the optionbytes folder script
// in order configure RESET as a GPIO so you can use the AUX input as well
// as forcing the watchdog on by default.
//
// TODO: Spare "neon" channel should be independent.
#define SYSTEM_CORE_CLOCK 48000000

View File

@@ -0,0 +1,13 @@
all : flash monitor
TARGET:=optionbytes
CFLAGS+=-DTINYVECTOR
CH32V003FUN:=../../ch32v003fun/ch32v003fun
MINICHLINK:=../../ch32v003fun/minichlink
include ../../ch32v003fun/ch32v003fun/ch32v003fun.mk
flash : cv_flash
clean : cv_clean

View File

@@ -0,0 +1,168 @@
/* This shows how to use the option bytes. I.e. how do you disable NRST?
WARNING Portions of this code are under the following copyright.
*/
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v00x_flash.c
* Author : WCH
* Version : V1.0.0
* Date : 2022/08/08
* Description : This file provides all the FLASH firmware functions.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
// Could be defined here, or in the processor defines.
#define SYSTEM_CORE_CLOCK 48000000
#include "ch32v003fun.h"
#include <stdio.h>
/* Notes from flash document:
* @param OB_IWDG - Selects the IWDG mode
* OB_IWDG_SW - Software IWDG selected
* OB_IWDG_HW - Hardware IWDG selected
* OB_STOP - Reset event when entering STOP mode.
* OB_STOP_NoRST - No reset generated when entering in STOP
* OB_STOP_RST - Reset generated when entering in STOP
* OB_STDBY - Reset event when entering Standby mode.
* OB_STDBY_NoRST - No reset generated when entering in STANDBY
* OB_STDBY_RST - Reset generated when entering in STANDBY
* OB_RST - Selects the reset IO mode and Ignore delay time
* OB_RST_NoEN - Reset IO disable (PD7)
* OB_RST_EN_DT12ms - Reset IO enable (PD7) and Ignore delay time 12ms
* OB_RST_EN_DT1ms - Reset IO enable (PD7) and Ignore delay time 1ms
* OB_RST_EN_DT128ms - Reset IO enable (PD7) and Ignore delay time 128ms
*/
uint16_t OB_STOP = OB_STOP_NoRST;
uint16_t OB_IWDG = OB_IWDG_SW;
uint16_t OB_STDBY = OB_STDBY_NoRST;
uint16_t OB_RST = OB_RST_NoEN;
uint32_t count;
int FLASH_WaitForLastOperation(uint32_t Timeout);
int main()
{
SystemInit48HSI();
SetupDebugPrintf();
FLASH->OBKEYR = FLASH_KEY1;
FLASH->OBKEYR = FLASH_KEY2;
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_KEY2;
FLASH->MODEKEYR = FLASH_KEY1;
FLASH->MODEKEYR = FLASH_KEY2;
printf( "Option bytes started as:%04x\n", OB->USER );
uint16_t rdptmp = RDP_Key;
int status = FLASH_WaitForLastOperation(EraseTimeout);
if(status == FLASH_COMPLETE)
{
FLASH->OBKEYR = FLASH_KEY1;
FLASH->OBKEYR = FLASH_KEY2;
FLASH->CTLR |= CR_OPTER_Set;
FLASH->CTLR |= CR_STRT_Set;
status = FLASH_WaitForLastOperation(EraseTimeout);
if(status == FLASH_COMPLETE)
{
FLASH->CTLR &= CR_OPTER_Reset;
FLASH->CTLR |= CR_OPTPG_Set;
OB->RDPR = (uint16_t)rdptmp;
status = FLASH_WaitForLastOperation(ProgramTimeout);
if(status != FLASH_TIMEOUT)
{
FLASH->CTLR &= CR_OPTPG_Reset;
}
}
else
{
if(status != FLASH_TIMEOUT)
{
FLASH->CTLR &= CR_OPTPG_Reset;
}
}
}
printf( "After Clear:%04x\n", OB->USER );
FLASH->OBKEYR = FLASH_KEY1;
FLASH->OBKEYR = FLASH_KEY2;
status = FLASH_WaitForLastOperation(10000);
if(status == FLASH_COMPLETE)
{
FLASH->CTLR |= CR_OPTPG_Set;
OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | (uint16_t)(OB_RST | (uint16_t)0xE0)));
status = FLASH_WaitForLastOperation(10000);
if(status != FLASH_TIMEOUT)
{
FLASH->CTLR &= CR_OPTPG_Reset;
}
}
printf( "After Write:%04x\n", OB->USER );
printf( "Done\n" );
while(1);
}
int FLASH_GetBank1Status(void)
{
int flashstatus = FLASH_COMPLETE;
if((FLASH->STATR & FLASH_FLAG_BANK1_BSY) == FLASH_FLAG_BSY)
{
flashstatus = FLASH_BUSY;
}
else
{
if((FLASH->STATR & FLASH_FLAG_BANK1_WRPRTERR) != 0)
{
flashstatus = FLASH_ERROR_WRP;
}
else
{
flashstatus = FLASH_COMPLETE;
}
}
return flashstatus;
}
int FLASH_WaitForLastOperation(uint32_t Timeout)
{
int status = FLASH_COMPLETE;
status = FLASH_GetBank1Status();
while((status == FLASH_BUSY) && (Timeout != 0x00))
{
status = FLASH_GetBank1Status();
Timeout--;
}
if(Timeout == 0x00)
{
status = FLASH_TIMEOUT;
}
return status;
}