Skip to content

Basic examples for ch32v003 from scratch in Zig. Just using `zig build` 🚀

License

Notifications You must be signed in to change notification settings

ghostiam/ch32v003_basic_zig

Repository files navigation

CH32V003 basic examples

Basic examples are based on the use of registers, without abstractions.
All examples have been tested for functionality on ch32v003, but since the pin to which the LED is connected may differ for you, you will need to change the port and pin in the code.
In these examples, the LED is connected to the PC0 pin.

Note

If you are looking for more production-ready solutions, you should check out the repository ch32_zig, which contains HAL and utilities for working with WCH CH32 chips.

Blinking LED with minimal code in ZIG.
No linker script, no startup code, no standard library, only read/write registers!

This example uses a global variable that needs to be placed in memory.
For this, a linker script and a function that initializes the registers and copies data from FLASH to RAM are required.
The data copying function will be implemented in pure ZIG.

The difference from the previous example is that the data copying function will be implemented in assembly, which will greatly reduce the firmware size when compiled in a mode other than ReleaseSmall.
This function will be used in all subsequent examples.

Now we get to interrupts.
In this example, we will blink the LED using a system timer interrupt.

We will do a small code refactoring by moving the startup code to a separate file.
We will add UART initialization and output a counter that increments with each LED blink.

We raise the stakes and connect std.log to UART!
Now we can use string formatting and output panic messages to UART!
To demonstrate this, the code will panic when the counter reaches 10.

As an additional example, we will add an SPI master that will send Test.

About

Basic examples for ch32v003 from scratch in Zig. Just using `zig build` 🚀

Topics

Resources

License

Stars

Watchers

Forks