chore: 📎 + fmt
This commit is contained in:
		@@ -3,25 +3,25 @@
 | 
			
		||||
extern crate alloc;
 | 
			
		||||
 | 
			
		||||
use crate::hal::peripherals::CAN1;
 | 
			
		||||
use core::fmt::Write as _;
 | 
			
		||||
use canapi::id::{classify, plant_id, MessageKind, IDENTIFY_CMD_OFFSET, MOISTURE_DATA_OFFSET};
 | 
			
		||||
use canapi::SensorSlot;
 | 
			
		||||
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::{NonBlocking};
 | 
			
		||||
use ch32_hal::gpio::{Level, Output, Speed};
 | 
			
		||||
use ch32_hal::mode::NonBlocking;
 | 
			
		||||
use ch32_hal::peripherals::USBD;
 | 
			
		||||
use embassy_executor::{Spawner, task};
 | 
			
		||||
use core::fmt::Write as _;
 | 
			
		||||
use embassy_executor::{task, Spawner};
 | 
			
		||||
use embassy_futures::yield_now;
 | 
			
		||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
 | 
			
		||||
use embassy_sync::channel::Channel;
 | 
			
		||||
use embassy_time::{Delay, Duration, Instant, Timer};
 | 
			
		||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
 | 
			
		||||
use embassy_usb::{Builder, UsbDevice};
 | 
			
		||||
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};
 | 
			
		||||
use embassy_time::{Instant, Duration, Delay, Timer};
 | 
			
		||||
use embedded_can::{Id, StandardId};
 | 
			
		||||
use hal::bind_interrupts;
 | 
			
		||||
use hal::usbd::Driver;
 | 
			
		||||
use {ch32_hal as hal, panic_halt as _};
 | 
			
		||||
 | 
			
		||||
macro_rules! mk_static {
 | 
			
		||||
@@ -33,20 +33,18 @@ macro_rules! mk_static {
 | 
			
		||||
    }};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 bind_interrupts!(struct Irqs {
 | 
			
		||||
     USB_LP_CAN1_RX0 => hal::usbd::InterruptHandler<hal::peripherals::USBD>;
 | 
			
		||||
 });
 | 
			
		||||
bind_interrupts!(struct Irqs {
 | 
			
		||||
    USB_LP_CAN1_RX0 => hal::usbd::InterruptHandler<hal::peripherals::USBD>;
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 use embedded_alloc::LlffHeap as Heap;
 | 
			
		||||
use embedded_alloc::LlffHeap as Heap;
 | 
			
		||||
use embedded_can::nb::Can as nb_can;
 | 
			
		||||
use qingke::riscv::asm::delay;
 | 
			
		||||
use log::log;
 | 
			
		||||
use qingke::riscv::asm::delay;
 | 
			
		||||
 | 
			
		||||
#[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")]
 | 
			
		||||
@@ -63,8 +61,6 @@ async fn main(spawner: Spawner) {
 | 
			
		||||
        ..Default::default()
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // Build driver and USB stack using 'static buffers
 | 
			
		||||
    let driver = Driver::new(p.USBD, Irqs, p.PA12, p.PA11);
 | 
			
		||||
 | 
			
		||||
@@ -84,22 +80,19 @@ async fn main(spawner: Spawner) {
 | 
			
		||||
    let mut builder = Builder::new(
 | 
			
		||||
        driver,
 | 
			
		||||
        config,
 | 
			
		||||
        mk_static!([u8;256], [0; 256]),
 | 
			
		||||
        mk_static!([u8;256], [0; 256]),
 | 
			
		||||
        mk_static!([u8; 256], [0; 256]),
 | 
			
		||||
        mk_static!([u8; 256], [0; 256]),
 | 
			
		||||
        &mut [], // no msos descriptors
 | 
			
		||||
        mk_static!([u8;64], [0; 64]),
 | 
			
		||||
        mk_static!([u8; 64], [0; 64]),
 | 
			
		||||
    );
 | 
			
		||||
    // Initialize CDC state and create CDC-ACM class
 | 
			
		||||
    let class = mk_static!(CdcAcmClass<'static, Driver<'static, hal::peripherals::USBD>>,
 | 
			
		||||
            CdcAcmClass::new(
 | 
			
		||||
            &mut builder,
 | 
			
		||||
            mk_static!(State, State::new()),
 | 
			
		||||
            64
 | 
			
		||||
        )
 | 
			
		||||
    let class = mk_static!(
 | 
			
		||||
        CdcAcmClass<'static, Driver<'static, hal::peripherals::USBD>>,
 | 
			
		||||
        CdcAcmClass::new(&mut builder, mk_static!(State, State::new()), 64)
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Build USB device
 | 
			
		||||
    let usb = mk_static!(UsbDevice<Driver<USBD>>, builder.build()) ;
 | 
			
		||||
    let usb = mk_static!(UsbDevice<Driver<USBD>>, builder.build());
 | 
			
		||||
 | 
			
		||||
    // Create GPIO for 555 Q output (PB0)
 | 
			
		||||
    let q_out = Output::new(p.PB0, Level::Low, Speed::Low);
 | 
			
		||||
@@ -112,7 +105,16 @@ async fn main(spawner: Spawner) {
 | 
			
		||||
    let adc = Adc::new(p.ADC1, Default::default());
 | 
			
		||||
    let ain = p.PA1;
 | 
			
		||||
    let config = can::can::Config::default();
 | 
			
		||||
    let can: Can<CAN1, NonBlocking> = Can::new_nb(p.CAN1, p.PB8, p.PB9, CanFifo::Fifo0, CanMode::Normal, 125_000, config).expect("Valid");
 | 
			
		||||
    let can: Can<CAN1, NonBlocking> = Can::new_nb(
 | 
			
		||||
        p.CAN1,
 | 
			
		||||
        p.PB8,
 | 
			
		||||
        p.PB9,
 | 
			
		||||
        CanFifo::Fifo0,
 | 
			
		||||
        CanMode::Normal,
 | 
			
		||||
        125_000,
 | 
			
		||||
        config,
 | 
			
		||||
    )
 | 
			
		||||
    .expect("Valid");
 | 
			
		||||
    ch32_hal::pac::AFIO.pcfr1().write(|w| w.set_can1_rm(2));
 | 
			
		||||
 | 
			
		||||
    spawner.spawn(usb_task(usb)).unwrap();
 | 
			
		||||
@@ -120,8 +122,6 @@ async fn main(spawner: Spawner) {
 | 
			
		||||
    // 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
 | 
			
		||||
    core::future::pending::<()>().await;
 | 
			
		||||
}
 | 
			
		||||
@@ -136,12 +136,13 @@ async fn worker(
 | 
			
		||||
) {
 | 
			
		||||
    // 555 emulation state: Q initially Low
 | 
			
		||||
    let mut q_high = false;
 | 
			
		||||
    let low_th: u16 = (ADC_MAX as u16) / 3;       // ~1/3 Vref
 | 
			
		||||
    let low_th: u16 = (ADC_MAX as u16) / 3; // ~1/3 Vref
 | 
			
		||||
    let high_th: u16 = ((ADC_MAX as u32 * 2) / 3) as u16; // ~2/3 Vref
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    let moisture_address = StandardId::new(plant_id(MOISTURE_DATA_OFFSET, SensorSlot::A,  0)).unwrap();
 | 
			
		||||
    let identity_address = StandardId::new(plant_id(IDENTIFY_CMD_OFFSET, SensorSlot::A,  0)).unwrap();
 | 
			
		||||
    let moisture_address =
 | 
			
		||||
        StandardId::new(plant_id(MOISTURE_DATA_OFFSET, SensorSlot::A, 0)).unwrap();
 | 
			
		||||
    let identity_address =
 | 
			
		||||
        StandardId::new(plant_id(IDENTIFY_CMD_OFFSET, SensorSlot::A, 0)).unwrap();
 | 
			
		||||
 | 
			
		||||
    let mut filter = CanFilter::new_id_list();
 | 
			
		||||
 | 
			
		||||
@@ -153,14 +154,15 @@ async fn worker(
 | 
			
		||||
    can.add_filter(filter);
 | 
			
		||||
    //can.add_filter(CanFilter::accept_all());
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    loop {
 | 
			
		||||
        // Count rising edges of Q in a 100 ms window
 | 
			
		||||
        let start = Instant::now();
 | 
			
		||||
        let mut pulses: u32 = 0;
 | 
			
		||||
        let mut last_q = q_high;
 | 
			
		||||
 | 
			
		||||
        while Instant::now().checked_duration_since(start).unwrap_or(Duration::from_millis(0))
 | 
			
		||||
        while Instant::now()
 | 
			
		||||
            .checked_duration_since(start)
 | 
			
		||||
            .unwrap_or(Duration::from_millis(0))
 | 
			
		||||
            < Duration::from_millis(1000)
 | 
			
		||||
        {
 | 
			
		||||
            // Sample the analog input (Threshold/Trigger on A1)
 | 
			
		||||
@@ -204,24 +206,16 @@ async fn worker(
 | 
			
		||||
        );
 | 
			
		||||
        log(msg);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        let mut moisture = CanFrame::new(moisture_address, &[freq_hz as u8]).unwrap();
 | 
			
		||||
        match can.transmit(&mut moisture){
 | 
			
		||||
        match can.transmit(&mut moisture) {
 | 
			
		||||
            Ok(..) => {
 | 
			
		||||
                let mut msg: heapless::String<128> = heapless::String::new();
 | 
			
		||||
                let _ = write!(
 | 
			
		||||
                    &mut msg,
 | 
			
		||||
                    "Send to canbus"
 | 
			
		||||
                );
 | 
			
		||||
                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
 | 
			
		||||
                );
 | 
			
		||||
                let _ = write!(&mut msg, "err {:?}", err);
 | 
			
		||||
                log(msg);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -229,35 +223,31 @@ async fn worker(
 | 
			
		||||
        loop {
 | 
			
		||||
            yield_now().await;
 | 
			
		||||
            match can.receive() {
 | 
			
		||||
                Ok(frame) => {
 | 
			
		||||
                    match frame.id() {
 | 
			
		||||
                        Id::Standard(s_frame) => {
 | 
			
		||||
                            let mut msg: heapless::String<128> = heapless::String::new();
 | 
			
		||||
                            let _ = write!(
 | 
			
		||||
                                &mut msg,
 | 
			
		||||
                                "Received from canbus: {:?} ident is {:?} \r\n",
 | 
			
		||||
                                s_frame.as_raw(),
 | 
			
		||||
                                identity_address.as_raw()
 | 
			
		||||
                            );
 | 
			
		||||
                            log(msg);
 | 
			
		||||
                            if s_frame.as_raw() == identity_address.as_raw() {
 | 
			
		||||
                                for _ in 0..10 {
 | 
			
		||||
                                    Timer::after_millis(250).await;
 | 
			
		||||
                                    led.toggle();
 | 
			
		||||
                                }
 | 
			
		||||
                                led.set_low();
 | 
			
		||||
                Ok(frame) => match frame.id() {
 | 
			
		||||
                    Id::Standard(s_frame) => {
 | 
			
		||||
                        let mut msg: heapless::String<128> = heapless::String::new();
 | 
			
		||||
                        let _ = write!(
 | 
			
		||||
                            &mut msg,
 | 
			
		||||
                            "Received from canbus: {:?} ident is {:?} \r\n",
 | 
			
		||||
                            s_frame.as_raw(),
 | 
			
		||||
                            identity_address.as_raw()
 | 
			
		||||
                        );
 | 
			
		||||
                        log(msg);
 | 
			
		||||
                        if s_frame.as_raw() == identity_address.as_raw() {
 | 
			
		||||
                            for _ in 0..10 {
 | 
			
		||||
                                Timer::after_millis(250).await;
 | 
			
		||||
                                led.toggle();
 | 
			
		||||
                            }
 | 
			
		||||
                            led.set_low();
 | 
			
		||||
                        }
 | 
			
		||||
                        Id::Extended(_) => {}
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                    Id::Extended(_) => {}
 | 
			
		||||
                },
 | 
			
		||||
                _ => {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -275,10 +265,9 @@ async fn usb_task(usb: &'static mut UsbDevice<'static, Driver<'static, hal::peri
 | 
			
		||||
 | 
			
		||||
#[task]
 | 
			
		||||
async fn usb_writer(
 | 
			
		||||
    class: &'static mut CdcAcmClass<'static, Driver<'static, hal::peripherals::USBD>>
 | 
			
		||||
    class: &'static mut CdcAcmClass<'static, Driver<'static, hal::peripherals::USBD>>,
 | 
			
		||||
) {
 | 
			
		||||
    loop {
 | 
			
		||||
 | 
			
		||||
        class.wait_connection().await;
 | 
			
		||||
        printer(class).await;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user