sensor sweep tester
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user