From 9ab135672be4a55bcf51753f78a068fbc79aa2be Mon Sep 17 00:00:00 2001 From: ju6ge Date: Sun, 13 Jul 2025 19:57:20 +0200 Subject: [PATCH] minimal working project with instruct step debugging infrastructure in place --- .doomrc | 47 +++++------ .gdbinit | 7 ++ Cargo.lock | 167 +++++++--------------------------------- Cargo.toml | 22 +++--- README.md | 35 ++++++--- bin/gdb | 1 + bin/openocd | 3 +- openocd.cfg | 2 +- src/main.rs | 49 ++---------- wch-tools.Containerfile | 2 + 10 files changed, 99 insertions(+), 236 deletions(-) create mode 100644 .gdbinit diff --git a/.doomrc b/.doomrc index 7299146..8acb61c 100644 --- a/.doomrc +++ b/.doomrc @@ -7,33 +7,26 @@ (add-to-list 'dape-configs `(gdb-dap-openocd - ,@(alist-get 'gdb-dap dape-configs) - fn (lambda (config) - (let* ((default-directory (dape--guess-root config)) - (openocd - ;; Run openocd with config files from `openocd-files' property - (start-process-shell-command - "openocd" "*openocd*" - (concat "openocd" - (mapconcat (lambda (file) - (concat " -f " (shell-quote-argument file))) - (append (dape-config-get config 'openocd-files) nil))))) - probe) - ;; Wait until port is open - (while (and (process-live-p openocd) - (not (setq probe (ignore-errors - (make-network-process :name "openocd port probe" - :service 3333))))) - (sleep-for 0.1)) - ;; Close probe connection - (when probe - (delete-process probe))) - config) - openocd-files ["openocd.cfg"] - :initCommands ["target remote 10.128.5.10:3333" - "monitor reset halt"] - :exitCommands ["monitor shutdown"] - :terminateCommands ["monitor shutdown"])) + ensure (lambda (config) + (dape-ensure-command config) + (let* ((default-directory + (or (dape-config-get config 'command-cwd) + default-directory)) + (command (dape-config-get config 'command)) + (output (shell-command-to-string (format "%s --version" command))) + (version (save-match-data + (when (string-match "GNU gdb \\(?:(.*) \\)?\\([0-9.]+\\)" output) + (string-to-number (match-string 1 output)))))) + (unless (>= version 14.1) + (user-error "Requires gdb version >= 14.1")))) + modes () + command-cwd dape-command-cwd + command "gdb" + command-args ("--interpreter=dap") + :request nil + :program nil + :args [] + :stopAtBeginningOfMainSubprogram nil)) ) ;;; .doomrc ends here diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..3263f19 --- /dev/null +++ b/.gdbinit @@ -0,0 +1,7 @@ +target extended-remote :3333 +set remotetimeout 2000 + +#symbol-file target/riscv32imc-unknown-none-elf/release/ch32v203-examples +file target/riscv32imc-unknown-none-elf/release/bms + +monitor reset halt diff --git a/Cargo.lock b/Cargo.lock index 2be28c6..320d3f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,12 +20,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" -[[package]] -name = "az" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" - [[package]] name = "bit_field" version = "0.10.2" @@ -39,16 +33,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac" [[package]] -name = "byte-slice-cast" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0a5e3906bcbf133e33c1d4d95afc664ad37fbdb9f6568d8043e7ea8c27d93d3" - -[[package]] -name = "byte-slice-cast" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7575182f7272186991736b70173b0ea045398f984bf5ebbb3804736ce1330c9d" +name = "bms" +version = "0.1.0" +dependencies = [ + "ch32-hal", + "embassy-executor", + "embassy-futures", + "embassy-time", + "embassy-usb", + "embedded-hal 1.0.0", + "heapless", + "micromath", + "panic-halt", + "qingke", + "qingke-rt", +] [[package]] name = "byteorder" @@ -65,6 +64,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "ch32-hal" version = "0.1.0" +source = "git+https://github.com/ch32-rs/ch32-hal#2b8e1c864ba5545ee65b1c77dcb17c86a471b70c" dependencies = [ "ch32-metapac", "critical-section", @@ -73,6 +73,7 @@ dependencies = [ "embassy-sync", "embassy-time", "embassy-time-driver", + "embassy-time-queue-utils", "embassy-usb-driver", "embedded-hal 0.2.7", "embedded-hal 1.0.0", @@ -97,26 +98,6 @@ dependencies = [ "vcell", ] -[[package]] -name = "ch32v203-examples" -version = "0.1.0" -dependencies = [ - "ch32-hal", - "display-interface-spi 0.5.0", - "embassy-executor", - "embassy-futures", - "embassy-time", - "embassy-usb", - "embedded-graphics", - "embedded-hal 1.0.0", - "heapless", - "micromath", - "panic-halt", - "qingke", - "qingke-rt", - "ssd1306", -] - [[package]] name = "critical-section" version = "1.2.0" @@ -158,51 +139,6 @@ dependencies = [ "syn 2.0.101", ] -[[package]] -name = "display-interface" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7517c040926d7b02b111884aa089177db80878533127f7c1b480d852c5fb4112" - -[[package]] -name = "display-interface" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba2aab1ef3793e6f7804162debb5ac5edb93b3d650fbcc5aeb72fcd0e6c03a0" - -[[package]] -name = "display-interface-i2c" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4895cd4e54e5536ef370d7f1eec787aad8275dd8ad15815aebfa71dd847b4ebf" -dependencies = [ - "display-interface 0.4.1", - "embedded-hal 0.2.7", -] - -[[package]] -name = "display-interface-spi" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "489378ad054862146fbd1f09f51d585ccbe4bd1e2feadcda2a13ac33f840e1a5" -dependencies = [ - "byte-slice-cast 0.3.5", - "display-interface 0.4.1", - "embedded-hal 0.2.7", -] - -[[package]] -name = "display-interface-spi" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f86b9ec30048b1955da2038fcc3c017f419ab21bb0001879d16c0a3749dc6b7a" -dependencies = [ - "byte-slice-cast 1.2.3", - "display-interface 0.5.0", - "embedded-hal 1.0.0", - "embedded-hal-async", -] - [[package]] name = "document-features" version = "0.2.11" @@ -214,15 +150,13 @@ dependencies = [ [[package]] name = "embassy-executor" -version = "0.6.3" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f64f84599b0f4296b92a4b6ac2109bc02340094bda47b9766c5f9ec6a318ebf8" +checksum = "90327bcc66333a507f89ecc4e2d911b265c45f5c9bc241f98eee076752d35ac6" dependencies = [ "critical-section", "document-features", "embassy-executor-macros", - "embassy-time-driver", - "embassy-time-queue-driver", ] [[package]] @@ -285,36 +219,38 @@ dependencies = [ [[package]] name = "embassy-time" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "158080d48f824fad101d7b2fae2d83ac39e3f7a6fa01811034f7ab8ffc6e7309" +checksum = "f820157f198ada183ad62e0a66f554c610cdcd1a9f27d4b316358103ced7a1f8" dependencies = [ "cfg-if", "critical-section", "document-features", "embassy-time-driver", - "embassy-time-queue-driver", "embedded-hal 0.2.7", "embedded-hal 1.0.0", "embedded-hal-async", "futures-util", - "heapless", ] [[package]] name = "embassy-time-driver" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e0c214077aaa9206958b16411c157961fb7990d4ea628120a78d1a5a28aed24" +checksum = "8d45f5d833b6d98bd2aab0c2de70b18bfaa10faf661a1578fd8e5dfb15eb7eba" dependencies = [ "document-features", ] [[package]] -name = "embassy-time-queue-driver" +name = "embassy-time-queue-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1177859559ebf42cd24ae7ba8fe6ee707489b01d0bf471f8827b7b12dcb0bc0" +checksum = "dc55c748d16908a65b166d09ce976575fb8852cf60ccd06174092b41064d8f83" +dependencies = [ + "embassy-executor", + "heapless", +] [[package]] name = "embassy-usb" @@ -337,29 +273,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fc247028eae04174b6635104a35b1ed336aabef4654f5e87a8f32327d231970" -[[package]] -name = "embedded-graphics" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0649998afacf6d575d126d83e68b78c0ab0e00ca2ac7e9b3db11b4cbe8274ef0" -dependencies = [ - "az", - "byteorder", - "embedded-graphics-core", - "float-cmp", - "micromath", -] - -[[package]] -name = "embedded-graphics-core" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba9ecd261f991856250d2207f6d8376946cd9f412a2165d3b75bc87a0bc7a044" -dependencies = [ - "az", - "byteorder", -] - [[package]] name = "embedded-hal" version = "0.2.7" @@ -416,15 +329,6 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" -[[package]] -name = "float-cmp" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" -dependencies = [ - "num-traits", -] - [[package]] name = "fnv" version = "1.0.7" @@ -766,19 +670,6 @@ dependencies = [ "syn 2.0.101", ] -[[package]] -name = "ssd1306" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b37918f6137f2b58427181c3e10a731bef1061a9756b13ffbade64f21892acc6" -dependencies = [ - "display-interface 0.4.1", - "display-interface-i2c", - "display-interface-spi 0.4.1", - "embedded-graphics-core", - "embedded-hal 0.2.7", -] - [[package]] name = "ssmarshal" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 79c7fb9..8551842 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "ch32v203-examples" +name = "bms" version = "0.1.0" edition = "2021" [dependencies] -ch32-hal = { path = "../ch32-hal", features = [ +ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal", features = [ "ch32v203c8t6", "memory-x", "embassy", @@ -12,16 +12,14 @@ ch32-hal = { path = "../ch32-hal", features = [ "time-driver-tim2", ], default-features = false } -embassy-executor = { version = "0.6.0", features = [ - "integrated-timers", +embassy-executor = { version = "0.7.0", features = [ "arch-riscv32", "executor-thread", ] } -embassy-time = { version = "0.3.2" } +embassy-time = { version = "0.4.0" } embassy-usb = { version = "0.3.0" } embassy-futures = { version = "0.1.0" } -#embassy-rp = "0.2.0" # This is okay because we should automatically use whatever ch32-hal uses qingke-rt = "*" @@ -29,18 +27,16 @@ qingke = "*" panic-halt = "1.0" -display-interface-spi = "0.5.0" embedded-hal = "1.0.0" -embedded-graphics = "0.8.1" -ssd1306 = "0.8.4" heapless = "0.8.0" micromath = { version = "2.1.0", features = ["num-traits"] } -# mipidsi = "0.7.1" -# embedded-hal-bus = "0.1.0" - +[profile.dev] +#lto = true +opt-level = 1 [profile.release] strip = false # symbols are not flashed to the microcontroller, so don't strip them. -lto = true +#lto = true +debug = true opt-level = "z" # Optimize for size. diff --git a/README.md b/README.md index 98c4ded..0e12557 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,31 @@ -# ch32v203-examples +# ch32v203-bms -- SWDIO: PA13 -- SWCLK: PA14 +A simple battery management controller software. -## Boards +## Building -### FlappyBoard - CH32V203G6 +``` sh +cargo build --release +``` -Link: +## Flash -### nanoCH32V203 - CH32V203C8T6 +``` sh +wchip wchisp flash target/riscv32imc-unknown-none-elf/release/bms +``` -Link: +## Debugging -- HSE: 8MHz -- LSE: 32.768kHz -- LED: PA15 +For debugging purposes a container file is provided together with wrapper scripts to start the containerized `openocd` and `riscv-gdb` transparently. The wrapper scripts assume that `podman` is setup. + +Starting Debug server + +``` +./bin/openocd +``` + +Connecting with gdb for interactive debugging + +``` +./bin/gdb -f target/riscv32imc-unknown-none-elf/release/bms +``` diff --git a/bin/gdb b/bin/gdb index e138cd4..3677232 100755 --- a/bin/gdb +++ b/bin/gdb @@ -13,6 +13,7 @@ function _fatal { declare -a PODMAN_ARGS=( "--rm" "-i" "--log-driver=none" "--network=host" + "--pid=host" "-v" "$PWD:$PWD:rw" "-w" "$PWD" ) diff --git a/bin/openocd b/bin/openocd index 21f136b..2ca3b44 100755 --- a/bin/openocd +++ b/bin/openocd @@ -12,8 +12,7 @@ function _fatal { declare -a PODMAN_ARGS=( "--rm" "-i" "--log-driver=none" - "-p" "3333:3333" - "-p" "4444:4444" + "--network=host" "-v" "$PWD:$PWD:rw" "-w" "$PWD" ) diff --git a/openocd.cfg b/openocd.cfg index b4b2dc5..fd3d973 100644 --- a/openocd.cfg +++ b/openocd.cfg @@ -1,7 +1,7 @@ set _CHIPNAME ch32v203 set _TARGETNAME $_CHIPNAME.cpu -bindto 0.0.0.0 +#bindto 0.0.0.0 adapter driver wlinke adapter speed 6000 diff --git a/src/main.rs b/src/main.rs index 369fa9f..d26fd42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,50 +28,11 @@ async fn main(spawner: Spawner) -> ! { let mut led = Output::new(p.PB2, Level::Low, Default::default()); - let mut cfg = usart::Config::default(); - let mut uart = UartTx::new_blocking(p.USART1, p.PA9, cfg).unwrap(); + loop { + Timer::after_millis(1000).await; - let usb_driver = Driver::new(p.USBD, Irqs, p.PA12, p.PA11); - // Create embassy-usb Config - let mut config = Config::new(0xc0de, 0xcafe); - config.manufacturer = Some("Embassy"); - config.product = Some("USB-serial example"); - config.serial_number = Some("12345678"); - config.max_power = 100; - config.max_packet_size_0 = 64; + //class.write_packet(b"hello world from embassy main\r\n").await; - // Create embassy-usb DeviceBuilder using the driver and config. - // It needs some buffers for building the descriptors. - let mut config_descriptor = [0; 256]; - let mut bos_descriptor = [0; 256]; - let mut control_buf = [0; 64]; - - let mut state = State::new(); - - let mut builder = Builder::new( - usb_driver, - config, - &mut config_descriptor, - &mut bos_descriptor, - &mut [], // no msos descriptors - &mut control_buf, - ); - - // Create classes on the builder. - let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); - - let mut usb = builder.build(); - let usb_task = usb.run(); - - let worker_task = async { - loop { - Timer::after_millis(1000).await; - - //class.write_packet(b"hello world from embassy main\r\n").await; - - led.toggle(); - } - }; - - join(usb_task, worker_task).await.1 + led.toggle(); + } } diff --git a/wch-tools.Containerfile b/wch-tools.Containerfile index e32a18b..e48a92f 100644 --- a/wch-tools.Containerfile +++ b/wch-tools.Containerfile @@ -22,4 +22,6 @@ RUN cd /root && \ mv xpack-toolchain/riscv-none-elf /usr/local && \ rm -rf xpack-toolchain xpack-riscv-toolchain.tar.gz +RUN mkdir -p /root/.config/gdb && echo "set auto-load safe-path /" >> /root/.config/gdb/gdbinit + ENTRYPOINT [ "/usr/bin/bash" ]