CH32V003 - headers/project example for bare metal - no HAL, no Macros etc. - Page 1
Forum section: electronics / general

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: January 03, 2024, 02:57:32 pm
content: 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 PICI'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 ?

author: gatk555
author_rank: Newbie
timestamp: January 03, 2024, 05:26:28 pm
content: This?https://github.com/cnlohr/ch32v003fun/blob/master/ch32v003fun/ch32v003fun.hYou do not have to use any macros.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 03, 2024, 05:36:20 pm
content: 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 beGPIOD->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 toSystemInit()in the system_ch32v00x.c of a typical HAL project. Their linker script can be used as-is. Mainline GCC can be used (I recommendxPackdistribution 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.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: January 03, 2024, 06:41:35 pm
content: Quote from: HwAoRrDk on January 03, 2024, 05:36:20 pmThanks - 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 ?

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 03, 2024, 06:59:43 pm
content: Quote from: mikeselectricstuff on January 03, 2024, 06:41:35 pmSure, see attached.