Author Topic: CH32V003 - headers/project example for bare metal - no HAL, no Macros etc.  (Read 857 times)

0 Members and 1 Guest are viewing this topic.

Online mikeselectricstuffTopic starter

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
So I just want to be able to write stuff to the registers, ideally with names as defined in the datasheet. No HAL, no abstracted macros  etc.

I just want my source to look something  like

#include <whatever.h>
GPIOD_OUTDR=1;

etc.
Just like I can in MPLABX on a PIC

I'm still digging through all the bloated HAL BS that comes with mounriver to find where registers are actually defined. Not at all helped by Mounriver seeming to not have a simple way to search all files in a project.

Even the supposedly simplified ch32v003fun project abstracts simple GPIO access into Arduino-style macros.

I don't want to get into alternative IDEs as it's important that I can give a project file to a client and tell them to just install manufacturer tools and compile with minimmum dicking about.

How hard can it be ?





Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Offline gatk555

  • Newbie
  • Posts: 3
  • Country: us
« Last Edit: January 03, 2024, 05:32:03 pm by gatk555 »
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Registers are defined in EVT/EXAM/SRC/Peripheral/inc/ch32v00x.h (wherever that may live, I don't know). They're not defined in quite exactly the way you want, but instead using structs - so your example would be GPIOD->OUTDR. Also see EVT/EXAM/SRC/Core/core_riscv.h for register definitions for the PFIC/NVIC and SysTick (but ch32v00x.h includes it).

However, there is a caveat: ch32v00x.h is not 100% complete in terms of covering every single peripheral and their registers, and also has some errors. The problem I think is that the HAL code doesn't seem to make much use of this header file itself, but instead seems to prefer locally defining registers and masks, etc. within the code handling each peripheral, so they haven't noticed these problems.

I went my own way and made my own amalgamation of core_riscv.h and ch32v00x.h for standalone use, and also corrected any errors I've noticed and added stuff that was missing (e.g. vendor info and ESIG registers).

If you want to go even more standalone, you'll need to make a modification to their startup assembly code (startup_ch32v00x.S) to omit the jump to SystemInit() in the system_ch32v00x.c of a typical HAL project. Their linker script can be used as-is. Mainline GCC can be used (I recommend xPack distribution for Windows) if you forego use of 'fast' interrupts. All the compiler/linker options can probably be figured out from what the IDE calls them with; I figured those out from what random people on GitHub were doing in their makefiles - I've never touched the MounRiver IDE.
 

Online mikeselectricstuffTopic starter

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Registers are defined in EVT/EXAM/SRC/Peripheral/inc/ch32v00x.h (wherever that may live, I don't know). They're not defined in quite exactly the way you want, but instead using structs - so your example would be GPIOD->OUTDR. Also see EVT/EXAM/SRC/Core/core_riscv.h for register definitions for the PFIC/NVIC and SysTick (but ch32v00x.h includes it).

However, there is a caveat: ch32v00x.h is not 100% complete in terms of covering every single peripheral and their registers, and also has some errors. The problem I think is that the HAL code doesn't seem to make much use of this header file itself, but instead seems to prefer locally defining registers and masks, etc. within the code handling each peripheral, so they haven't noticed these problems.

I went my own way and made my own amalgamation of core_riscv.h and ch32v00x.h for standalone use, and also corrected any errors I've noticed and added stuff that was missing (e.g. vendor info and ESIG registers).

If you want to go even more standalone, you'll need to make a modification to their startup assembly code (startup_ch32v00x.S) to omit the jump to SystemInit() in the system_ch32v00x.c of a typical HAL project. Their linker script can be used as-is. Mainline GCC can be used (I recommend xPack distribution for Windows) if you forego use of 'fast' interrupts. All the compiler/linker options can probably be figured out from what the IDE calls them with; I figured those out from what random people on GitHub were doing in their makefiles - I've never touched the MounRiver IDE.

Thanks - I found the GPIOD->OUTDR syntax and now working as expected.

Any chance you could post your corrected headers?

I'm happy to leave all the startup code etc. as-is, I just want full control of IO and peripherals for fast bit-bashing etc., so hopefully  fairly sorted now.

BTW has anyone found a way to get Mounriver to do compile and then download (if compile OK) as a single keypress ?
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Any chance you could post your corrected headers?

Sure, see attached.
 
The following users thanked this post: IOsetting


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf