Crates.io | gd32c1x3-hal |
lib.rs | gd32c1x3-hal |
version | 0.1.1 |
source | src |
created_at | 2024-08-04 03:42:14.397109 |
updated_at | 2024-08-23 11:10:27.847974 |
description | HAL for the GD32C1x3 family of microcontrollers |
homepage | |
repository | https://github.com/guineawheek/gd32c1x3-hal |
max_upload_size | |
id | 1324698 |
size | 281,029 |
gd32c1x3-hal
HAL for the GD32C1x3 family of microcontrollers
Embedded Rust development requires a bit more setup than ordinary development.
You will also need a debug probe, for example an ST-Link V3 mini for programming and debugging. (There are many different STLink probes out there, all of them should work fine with the instructions given here, other JTAG or SWD debug probes will work as well but will need different software or configuration).
To program your microcontroller, you need to install:
gdb-multiarch
(on some platforms you may need to use gdb-arm-none-eabi
instead, make sure to
update .cargo/config
to reflect this change)Finally, you need to install arm target support for the Rust compiler. To do so, run
rustup target install thumbv7em-none-eabihf
Create a new Rust project as you usually do with cargo init
. The hello world
of embedded development is usually to blink an LED and code to do so is
available in examples/blinky.rs. Copy that file to the
main.rs
of your project.
You also need to add some dependencies to your Cargo.toml
:
[dependencies]
embedded-hal = "1.0.0"
nb = "1.1.0"
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7.3"
# Panic behaviour, see https://crates.io/keywords/panic-impl for alternatives
panic-halt = "0.2.0"
gd32c1x3-hal = { version = "0.1.0", features = ["rt", "gd32c103"] }
If you build your project now, you should get a single error:
error: language item required, but not found: eh_personality
. This unhelpful error message is
fixed by compiling for the right target.
We also need to tell Rust how to link our executable, and how to lay out the result in memory. To accomplish all this, copy .cargo/config and memory.x from the gd32c1x3-hal repo to your project.
cargo build
If everything went well, your project should have built without errors.
It is now time to actually run the code on the hardware. To do so plug your
debug probe into your board and start openocd
using
openocd -f interface/stlink-v3.cfg -f target/stm32f1x.cfg
If you are not using an stlink V3, change the interface accordingly. For more information, see the embeddonomicon.
If all went well, it should detect your microcontroller and say
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
. Keep it running in the background.
We will use gdb for uploading the compiled binary to the microcontroller and
for debugging. Cargo will automatically start gdb
thanks to the
.cargo/config you added earlier. gdb
also needs to be told
to connect to openocd which is done by copying .gdbinit to the root
of your project.
You may also need to tell gdb
that it is safe to load .gdbinit
from the
working directory.
echo "set auto-load safe-path $(pwd)" >> ~/.gdbinit
echo set auto-load safe-path %CD% >> %USERPROFILE%\.gdbinit
If everything was successful, cargo should compile your project, start GDB, load your program and
give you a prompt. If you type continue
in the GDB prompt, your program should start and an LED
attached to PC13 should start blinking.
From here on, you can start adding more code to your project to make it do something more interesting. For crate documentation, see docs.rs/gd32c1x3-hal. There are also a lot more examples available. If something is unclear in the docs or examples, please, open an issue and we will try to improve it.
This crate supports multiple microcontrollers in the gd32c1x3 family. Which specific microcontroller
you want to build for has to be specified with a feature, for example gd32c103
.
If no microcontroller is specified, the crate will not compile.
gd32c103xx
(e.g. GD32C103TB, GD32C103CB, GD32C103RB, GD32C103VB)gd32c113xx
(e.g. GD32C113TB, GD32C113CB, GD32C113RB, GD32C113VB)You may need to give cargo
permission to call gdb
from the working directory.
echo "set auto-load safe-path $(pwd)" >> ~/.gdbinit
echo set auto-load safe-path %CD% >> %USERPROFILE%\.gdbinit
Compile, load, and launch the hardware debugger.
$ rustup target add thumbv7em-none-eabihf
# on another terminal
$ openocd -f interface/$INTERFACE.cfg -f target/stm32f1x.cfg
# flash and debug the "Hello, world" example. Change gd32c103 to match your hardware
$ cargo run --features gd32c103 --example hello
$INTERFACE
should be set based on your debugging hardware. If you are using
an stlink V2, use stlink-v2.cfg
. For more information, see the
embeddonomicon.
When using this crate as a dependency in your project, the microcontroller can
be specified as part of the Cargo.toml
definition.
gd32c1x3-hal = { version = "0.1.0", features = ["gd32c103", "rt"] }
The documentation can be found at docs.rs.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See the contributing guide for more details.