Author Topic: Gotcha with CH32V003 GPIO config lock feature  (Read 1377 times)

0 Members and 2 Guests are viewing this topic.

Online HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Gotcha with CH32V003 GPIO config lock feature
« on: August 25, 2023, 05:00:45 pm »
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 locked until 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 actually reset the 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 locked all pins, 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.
 
The following users thanked this post: thm_w, rhodges

Offline harerod

  • Frequent Contributor
  • **
  • Posts: 449
  • Country: de
  • ee - digital & analog
    • My services:
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #1 on: August 25, 2023, 06:53:04 pm »
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 DUT

If 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. ;)
 

Offline rhodges

  • Frequent Contributor
  • **
  • Posts: 306
  • Country: us
  • Available for embedded projects.
    • My public libraries, code samples, and projects for STM8.
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #2 on: August 25, 2023, 09:39:14 pm »
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
wlink flash --chip ch32v003 --address 0x08000000 blink.bin
21:35:56 [INFO] WCH-Link v2.7 (WCH-LinkE-CH32V305)
21:35:56 [INFO] Attached chip: CH32V003(0x00330500)
21:35:56 [INFO] Chip UID: cd-ab-3f-47-4a-bc-56-af
21:35:56 [INFO] Flash protected: false
21:35:56 [INFO] Read blink.bin as Binary format
21:35:56 [INFO] Flashing 816 bytes to 0x08000000
21:35:57 [INFO] Flash done
21:35:57 [INFO] Now reset...
21:35:58 [INFO] Resume executing...
Currently developing STM8 and STM32. Past includes 6809, Z80, 8086, PIC, MIPS, PNX1302, and some 8748 and 6805. Check out my public code on github. https://github.com/unfrozen
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #3 on: August 25, 2023, 10:20:06 pm »
Yeah sometimes a conventional reset doesn't refresh everything, requiring power cyling.
Before configuring GPIO (But after enabling GPIO clock), check if GPIOx->LCKR LCKK bit 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!
« Last Edit: August 25, 2023, 10:30:36 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #4 on: August 26, 2023, 10:15:43 am »
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.

I 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.
 

Online HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #5 on: August 26, 2023, 10:22:05 am »
Before configuring GPIO (But after enabling GPIO clock), check if GPIOx->LCKR LCKK bit is set (Meaning it's locked).
In that case try issuing "NVIC_SystemReset();".

That's an interesting idea, thanks. I might try that.
 

Offline harerod

  • Frequent Contributor
  • **
  • Posts: 449
  • Country: de
  • ee - digital & analog
    • My services:
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #6 on: August 26, 2023, 11:46:27 am »
Where can one find information about that chip, which goes beyond the datasheet?
https://www.wch-ic.com/products/CH32V003.html

What 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.
 

Online HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #7 on: August 26, 2023, 12:09:18 pm »
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.html

It'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.
 
The following users thanked this post: harerod

Offline harerod

  • Frequent Contributor
  • **
  • Posts: 449
  • Country: de
  • ee - digital & analog
    • My services:
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #8 on: August 28, 2023, 10:19:19 am »
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 Mechanism
The locking mechanism locks the configuration of the IO port. After a specific write sequence, the selected IO
pin 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 Reset
When a system Reset occurs, it will reset the reset flag in addition to the control/status register
RCC_RSTSCKR and all the registers. The source of the reset event is identified by looking at the reset status
flag 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: I love how 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?
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: Gotcha with CH32V003 GPIO config lock feature
« Reply #9 on: August 28, 2023, 03:51:37 pm »
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?
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf