CH32V3xx Faster development by doing everything ram

 

Debugging the CH32V3xx (and V2xx)

The CH32V3xx chips are rather good, they are pretty fast, cheap with tons of peripherals.

Swindle supports the CH32V3xx chips (CH32V2XX as well but they are less interesting imho).

Debugging with them is not so great :

- They completely lack watchpoint (some revisions of WCH riscv cores do have them, but not the CH32V3x  it seems)

- Only 4 breakpoints

- Writing to flash is really slow compared to other similar chips ( from STM or Gigadevice)

Shadow Flash ?

The chip has 'shadow flash' i think. 
It is using slow flash ( or RRAM/MRAM)  and shadowing them with  RAM. 
At reset , it copies the flash to ram and execute from there. That way you get low cost and fast execution time.

The option bytes  allocates the physical ram between shadow flash area and ram area. 
But the total cannot exceed the physical amount of ram, ~ 256+64 in my case.

Debugging in bigger RAM

Similarly to what was done with the RP2040, we can debug things in RAM and when it works switch to the real location in flash.

The shopping list is the same :

- In flash, just loop in the reset vector

 _start:
    j _start
- Change the RAM/Flash layout so that we have more ram. On the CH32V303 i'm using,  it means going from 256 kb Flash / 64 kb Ram to 192 kB flash / 128 kB ram
This is done by writing the User part of the option byte :

Careful, bit 0 must be set to 1 and value 0b110 (6)  is not available on most chips.
Reset and you know have 128 kB of Ram.
Then : 
- Use a modified  linker script to put everything in ram
- When debugging don't do "run" but do "continue" with gdb (you may have to change the PC to the proper location of ram i.e. set $pc=0x200000000)


and bingo, you have now unlimited amount of breakpoints and very fast upload time.




I'll probably add that as "monitor" command to swindle so the user byte can easily be changed.

Nb: The above is generic enough but you'd want to use a debugger that supports so called software breakpoints.







Comments

Popular posts from this blog

Component tester with STM32 : Part 1 ADC, Resistor

Fixing the INA3221

INA3221, weird wiring