sensor sweep tester

This commit is contained in:
2025-10-07 21:50:33 +02:00
parent 712e8c8b8f
commit 7f3910bcd0
12 changed files with 242 additions and 54 deletions

View File

@@ -26,6 +26,7 @@ embassy-usb = { version = "0.3.0" }
embassy-futures = { version = "0.1.0" }
embassy-sync = { version = "0.6.0" }
embedded-can = "0.4.1"
embedded-alloc = { version = "0.6.0", default-features = false, features = ["llff"] }
# This is okay because we should automatically use whatever ch32-hal uses
qingke-rt = "*"

View File

@@ -28,12 +28,6 @@ If you need to map a label to code, use the same letter+number as in the silkscr
cargo build --release
```
## USB CDC Console (optional)
This project includes an optional software USB CDC-ACM device stack using embassy-usb. It runs on the CH32V203s USB device peripheral but implements the protocol fully in software (no built-in USB class firmware is required).
How to enable:
- Build with the `usb-cdc` feature: `cargo build --release --features usb-cdc`
- Wire the MCUs USB pins to a USB connector:
- D+ (PA12)
- D (PA11)
@@ -46,12 +40,6 @@ Example:
- macOS: `screen /dev/tty.usbmodemXXXX 115200`
- Windows: Use PuTTY on the shown COM port.
Notes:
- The firmware currently implements an echo console: bytes you type are echoed back. You can extend it to print logs or interact with your application.
- If you dont see a device, ensure D+ (PA12) and D (PA11) are connected and the cable supports data.
## Flash
You can flash the built ELF using wchisp (WCH ISP tool):
``` sh

View File

@@ -1,6 +1,6 @@
#![no_std]
#![no_main]
extern crate alloc;
use crate::hal::peripherals::CAN1;
use core::fmt::Write as _;
@@ -8,7 +8,7 @@ use ch32_hal::gpio::{Level, Output, Speed};
use ch32_hal::adc::{Adc, SampleTime, ADC_MAX};
use ch32_hal::can;
use ch32_hal::can::{Can, CanFifo, CanFilter, CanFrame, CanMode};
use ch32_hal::mode::{Blocking, Mode};
use ch32_hal::mode::{Blocking};
use ch32_hal::peripherals::USBD;
// use ch32_hal::delay::Delay;
use embassy_executor::{Spawner, task};
@@ -18,10 +18,10 @@ use embassy_futures::yield_now;
use hal::usbd::{Driver};
use hal::{bind_interrupts};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::{Channel, TrySendError};
use embassy_time::{Timer, Instant, Duration, Ticker};
use embassy_sync::channel::{Channel};
use embassy_time::{Instant, Duration};
use embedded_can::blocking::Can as bcan;
use embedded_can::StandardId;
use heapless::String;
use {ch32_hal as hal, panic_halt as _};
macro_rules! mk_static {
@@ -37,10 +37,22 @@ bind_interrupts!(struct Irqs {
USB_LP_CAN1_RX0 => hal::usbd::InterruptHandler<hal::peripherals::USBD>;
});
use embedded_alloc::LlffHeap as Heap;
#[global_allocator]
static HEAP: Heap = Heap::empty();
static LOG_CH: Channel<CriticalSectionRawMutex, heapless::String<128>, 8> = Channel::new();
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) {
unsafe {
static mut HEAP_SPACE: [u8; 4096] = [0; 4096]; // 4 KiB heap, adjust as needed
HEAP.init(HEAP_SPACE.as_ptr() as usize, HEAP_SPACE.len());
}
let p = hal::init(hal::Config {
rcc: hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSI,
..Default::default()
@@ -105,13 +117,11 @@ async fn main(spawner: Spawner) {
can.add_filter(CanFilter::accept_all());
// Spawn independent tasks using 'static references
unsafe {
spawner.spawn(usb_task(usb)).unwrap();
spawner.spawn(usb_writer(class)).unwrap();
// move Q output, LED, ADC and analog input into worker task
spawner.spawn(worker(q_out, led, adc, ain, can)).unwrap();
}
spawner.spawn(usb_task(usb)).unwrap();
spawner.spawn(usb_writer(class)).unwrap();
// move Q output, LED, ADC and analog input into worker task
spawner.spawn(worker(q_out, led, adc, ain, can)).unwrap();
// Prevent main from exiting
@@ -182,6 +192,31 @@ async fn worker(
pulses, freq_hz
);
log(msg);
let address = StandardId::new(0x580 | 0x42).unwrap();
let moisture = CanFrame::new(address, &[freq_hz as u8]).unwrap();
match bcan::transmit(&mut can, &moisture) {
Ok(..) => {
let mut msg: heapless::String<128> = heapless::String::new();
let _ = write!(
&mut msg,
"Send to canbus"
);
log(msg);
}
Err(err) => {
let mut msg: heapless::String<128> = heapless::String::new();
let _ = write!(
&mut msg,
"err {}"
,err
);
log(msg);
}
}
}
}