cleanups
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
|
||||
use crate::hal::peripherals::CAN1;
|
||||
use core::fmt::Write as _;
|
||||
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::peripherals::USBD;
|
||||
// use ch32_hal::delay::Delay;
|
||||
use embassy_executor::{Spawner, task};
|
||||
@@ -14,22 +19,24 @@ 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};
|
||||
use embassy_time::{Timer, Instant, Duration, Ticker};
|
||||
use embedded_can::StandardId;
|
||||
use heapless::String;
|
||||
use {ch32_hal as hal, panic_halt as _};
|
||||
|
||||
macro_rules! mk_static {
|
||||
($t:ty,$val:expr) => {{
|
||||
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
|
||||
#[deny(unused_attributes)]
|
||||
let x = STATIC_CELL.uninit().write(($val));
|
||||
x
|
||||
}};
|
||||
}
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
USB_LP_CAN1_RX0 => hal::usbd::InterruptHandler<hal::peripherals::USBD>;
|
||||
});
|
||||
|
||||
// 'static storage for USB descriptors and state so we can spawn tasks
|
||||
static mut USB_CONFIG_DESCRIPTOR: [u8; 256] = [0; 256];
|
||||
static mut USB_BOS_DESCRIPTOR: [u8; 256] = [0; 256];
|
||||
static mut USB_CONTROL_BUF: [u8; 64] = [0; 64];
|
||||
static mut CDC_STATE: core::mem::MaybeUninit<State<'static>> = core::mem::MaybeUninit::uninit();
|
||||
static mut USB_DEVICE: core::mem::MaybeUninit<UsbDevice<'static, Driver<'static, hal::peripherals::USBD>>> = core::mem::MaybeUninit::uninit();
|
||||
static mut CDC_CLASS: core::mem::MaybeUninit<CdcAcmClass<'static, Driver<'static, hal::peripherals::USBD>>> = core::mem::MaybeUninit::uninit();
|
||||
|
||||
static LOG_CH: Channel<CriticalSectionRawMutex, heapless::String<128>, 8> = Channel::new();
|
||||
|
||||
#[embassy_executor::main(entry = "qingke_rt::entry")]
|
||||
@@ -57,30 +64,25 @@ async fn main(spawner: Spawner) {
|
||||
config.device_protocol = 0x00;
|
||||
config.composite_with_iads = false;
|
||||
|
||||
let usb = unsafe {
|
||||
let mut builder = Builder::new(
|
||||
driver,
|
||||
config,
|
||||
&mut USB_CONFIG_DESCRIPTOR,
|
||||
&mut USB_BOS_DESCRIPTOR,
|
||||
&mut [], // no msos descriptors
|
||||
&mut USB_CONTROL_BUF,
|
||||
);
|
||||
let mut builder = Builder::new(
|
||||
driver,
|
||||
config,
|
||||
mk_static!([u8;256], [0; 256]),
|
||||
mk_static!([u8;256], [0; 256]),
|
||||
&mut [], // no msos descriptors
|
||||
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
|
||||
)
|
||||
);
|
||||
|
||||
// Initialize CDC state and create CDC-ACM class
|
||||
CDC_STATE.write(State::new());
|
||||
let class = {
|
||||
let state_ref: &mut State<'static> = CDC_STATE.assume_init_mut();
|
||||
CdcAcmClass::new(&mut builder, state_ref, 64)
|
||||
};
|
||||
CDC_CLASS.write(class);
|
||||
|
||||
// Build USB device
|
||||
let dev = builder.build();
|
||||
USB_DEVICE.write(dev);
|
||||
|
||||
USB_DEVICE.assume_init_mut()
|
||||
};
|
||||
// Build USB device
|
||||
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);
|
||||
@@ -91,15 +93,27 @@ 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<'static, CAN1 , Blocking> = Can::new_blocking(p.CAN1, p.PB8, p.PB9, CanFifo::Fifo1, CanMode::Normal, 500_000, config).expect("Valid");
|
||||
let mut filter = CanFilter::new_id_list();
|
||||
|
||||
filter
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.set(StandardId::new(0x580 | 0x42).unwrap().into(), Default::default());
|
||||
|
||||
can.add_filter(CanFilter::accept_all());
|
||||
|
||||
|
||||
// Spawn independent tasks using 'static references
|
||||
unsafe {
|
||||
let class = CDC_CLASS.assume_init_mut();
|
||||
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)).unwrap();
|
||||
spawner.spawn(worker(q_out, led, adc, ain, can)).unwrap();
|
||||
}
|
||||
|
||||
|
||||
// Prevent main from exiting
|
||||
core::future::pending::<()>().await;
|
||||
}
|
||||
@@ -110,6 +124,7 @@ async fn worker(
|
||||
mut led: Output<'static>,
|
||||
mut adc: Adc<'static, hal::peripherals::ADC1>,
|
||||
mut ain: hal::peripherals::PA1,
|
||||
mut can: Can<'static, CAN1, Blocking>,
|
||||
) {
|
||||
// 555 emulation state: Q initially Low
|
||||
let mut q_high = false;
|
||||
|
Reference in New Issue
Block a user