Gotcha with CH32V003 GPIO config lock feature - Page 1
Forum section: electronics / general

author: HwAoRrDk
author_rank: Super Contributor
timestamp: August 25, 2023, 05:00:45 pm
content: Dear reader, don't be like me. Don't spend ages chasing your own tail, chasing a red herring down a rabbit hole, all because of an incorrect assumption.When you use the GPIO configuration locking feature of the CH32V003 - a feature that allows you to make the mode and direction of selected GPIO pins unchangeable once locked, guarding against accidental modification - when the manual says that the configuration is lockeduntil a reset occurs, they're not wrong.I added an input pin to my code, but was extremely puzzled when that line was not being pulled up by the configured internal pull-up as it should have been. I double, triple, quadruple checked my code, went off on tangents, down dead ends, and generally wasted a lot of time. Not once did it occur to me to actuallyresetthe microcontroller.You see, when flashing new code to the MCU (at least with the WCH-LinkUtility software), it doesn't get reset. And so because I was using the GPIO config locking, and had lockedallpins, my new code for the new GPIO input never took effect.In hindsight, it should have been apparent that no reset occurs when programming flash, because you don't actually need to connect the reset line to the Link-E programmer to do so (SWDIO only is sufficient). I assume it does something like re-initialise execution at zero instead.

author: harerod
author_rank: Frequent Contributor
timestamp: August 25, 2023, 06:53:04 pm
content: Caveat: I don't know that chip - yet.So you're saying that this feature requires an external hardware reset, i.e. some external source pulling the nRST-line?Different examples for resets:- rewinding PC to reset address and proceeding from there - this is a jump, not a reset, no effect on hardware- the STM32 has a feature where you trigger a reset via software. This pretty much resets CPU and peripherals. Still not a complete reset, but close- something pulls the external reset line, that would also reset external components- a power cycle. Some features require this. Keep an eye on buffer-energized regions on chip and on the DUTIf you could find the time, please check if your programming tool allows for any of these features. An J-Link, when used to power the DUT, could do all of those. Maybe all you need is to ask your debug tool for an external reset. A good example why a development debug interface should include the reset line.

author: rhodges
author_rank: Frequent Contributor
timestamp: August 25, 2023, 09:39:14 pm
content: Thanks for the reminder. I am using the wlink utility and LinkE USB adapter. It says it is doing a reset, but maybe I need to verify that.Quote

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 25, 2023, 10:20:06 pm
content: Yeah sometimes a conventional reset doesn't refresh everything, requiring power cyling.Before configuring GPIO (But after enabling GPIO clock), check ifGPIOx->LCKRLCKKbit is set (Meaning it's locked).In that case try issuing "NVIC_SystemReset();".I don't know the extend of that reset, theorically it internally pulls nRST low so it shoud be the same as a physical pin reset.I've noticed some debuggers use a "GOTO" jump instead resetting the cpu.For example, the py32f002a I've been recently working on.Setting BOOT0=1 should enter the system bootloader, and it normally does.But when programming with J-LINK it jumps right to the application, ignoring the BOOT0 state.This tells me it's not doing a proper reset!

author: HwAoRrDk
author_rank: Super Contributor
timestamp: August 26, 2023, 10:15:43 am
content: Quote from: rhodges on August 25, 2023, 09:39:14 pmI should try out that wlink utility. I remember seeing it when the project was first started, but it was immature at that stage. Need something to do programming from the command line, because for some reason OpenOCD is terribly flaky for me. It just refuses to work sometimes, and I have no idea why.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: August 26, 2023, 10:22:05 am
content: Quote from: DavidAlfa on August 25, 2023, 10:20:06 pmThat's an interesting idea, thanks. I might try that.

author: harerod
author_rank: Frequent Contributor
timestamp: August 26, 2023, 11:46:27 am
content: Where can one find information about that chip, which goes beyond the datasheet?https://www.wch-ic.com/products/CH32V003.htmlWhat I am looking for is something similar to the basic set of information ST provides for their chips:- data sheet (found on manufacturer website)- errata sheet- reference manual (all the bits and registers in the periphery)- CPU core programming manual (NVIC!)I also wouldn't mind seeing some appnotes. And I also would strongly prefer docu in English.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: August 26, 2023, 12:09:18 pm
content: Well, you've answered your own question (sort of); all of those things, with the exception of errata (they don't provide any), are available on the manufacturer's website. The Reference Manual is right there at the link you already gave. The CPU manual can be found here:https://www.wch-ic.com/downloads/QingKeV2_Processor_Manual_PDF.htmlIt's disappointing WCH don't provide errata, but they at least have been updating the documentation. The datasheet is on its 4th or 5th revision now, and the Reference Manual was recently updated too. But it's also annoying they don't keep a summary of changes - you have to figure out yourself what's changed.

author: harerod
author_rank: Frequent Contributor
timestamp: August 28, 2023, 10:19:19 am
content: A case of selective blindness. I must have taken those couple of links for one. Feeble excuse, I know. Thanks for the RM, I wanted to see if it holds the required information. I seems so:->7.2.5 Locking MechanismThe locking mechanism locks the configuration of the IO port. After a specific write sequence, the selected IOpin configuration will be locked and cannot be changed until the next reset.<-A bit sparse, IMHO. The Chinese original doesn't seem to hold additional info:->7.2.5 锁定 机制锁定机制可以锁定 IO 口的配置。经过特定的一个写序列后，选定的 IO 引脚配置将被锁定，在下一个复位前无法更改。<-Looking into the ch3 - Reset:->3.2.2 System ResetWhen a system Reset occurs, it will reset the reset flag in addition to the control/status registerRCC_RSTSCKR and all the registers. The source of the reset event is identified by looking at the reset statusflag bit in the RCC_RSTSCKR register.A system Reset is generated when one of the following events occurs:- Low signal on NRST pin (external reset)- Window watchdog count termination (WWDG reset)- Independent watchdog count termination (IWDG reset)- Software reset (SW reset)- Low-power management reset<-This should give enough ways to unlock those GPIO locks.Side note: Ilovehow that manufacturer's website resets on changing languages. Navigating the Chinese version feels a bit like a maze. Especially since the structure slightly varies between the languages. Maybe open two browser windows and Google Translator for more thorough searches?

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 28, 2023, 03:51:37 pm
content: How are you complaning about the manufacturer's website, when all it takes is googling "CH32V003 datasheet" First 3 results contain it all?But wait, how were you programming it without any manual then?