Added usb serial communication

This commit is contained in:
Sebastian 2024-10-03 18:22:37 +02:00
parent bc557ccdeb
commit 8cf75ac70d
4 changed files with 245 additions and 17 deletions

View file

@ -21,13 +21,19 @@ mod app {
use as5048a::AS5048A;
use heapless::Vec;
use postcard::{from_bytes_cobs, to_vec_cobs};
use stm32f4xx_hal::{
gpio::{gpioa, gpiob, gpioc, Output, PushPull},
i2c,
otg_fs::{UsbBus, UsbBusType, USB},
pac::{I2C1, SPI1},
prelude::*,
spi,
};
use usb_device::class_prelude::UsbBusAllocator;
use usb_device::prelude::*;
use usbd_serial::SerialPort;
use num_traits::{Float, FloatConst};
@ -35,10 +41,14 @@ mod app {
use qmc5883l::{self, QMC5883L};
use radomctl_protocol::*;
use rtic_monotonics::systick::prelude::*;
systick_monotonic!(Mono, 4000);
const USB_BUFFER_SIZE: usize = 64;
// Shared resources go here
#[shared]
struct Shared {
@ -58,13 +68,17 @@ mod app {
spi_cs3: gpiob::PB15<Output<PushPull>>,
spi1: spi::Spi<SPI1>,
az_enable: gpioa::PA12<Output<PushPull>>,
az_enable: gpioa::PA10<Output<PushPull>>,
az_dir: gpioa::PA15<Output<PushPull>>,
az_step: gpiob::PB3<Output<PushPull>>,
el_enable: gpiob::PB4<Output<PushPull>>,
el_dir: gpioa::PA8<Output<PushPull>>,
el_step: gpioa::PA9<Output<PushPull>>,
usb_dev: UsbDevice<'static, UsbBusType>,
usb_serial: SerialPort<'static, UsbBusType>,
usb_buffer: Vec<u8, USB_BUFFER_SIZE>,
}
#[init]
@ -89,8 +103,6 @@ mod app {
defmt::info!("Clock Setup done");
defmt::info!("Clock Setup done");
// Acquire the GPIO peripherials
let gpioa = cx.device.GPIOA.split();
let gpiob = cx.device.GPIOB.split();
@ -98,6 +110,40 @@ mod app {
let board_led = gpioc.pc13.into_push_pull_output();
defmt::info!("Basic gpio setup done");
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
static mut USB_BUS: Option<usb_device::bus::UsbBusAllocator<UsbBusType>> = None;
let usb = USB::new(
(
cx.device.OTG_FS_GLOBAL,
cx.device.OTG_FS_DEVICE,
cx.device.OTG_FS_PWRCLK,
),
(gpioa.pa11, gpioa.pa12),
&clocks,
);
unsafe {
USB_BUS.replace(UsbBus::new(usb, &mut EP_MEMORY));
}
let usb_serial = usbd_serial::SerialPort::new(unsafe { USB_BUS.as_ref().unwrap() });
let usb_dev = UsbDeviceBuilder::new(
unsafe { USB_BUS.as_ref().unwrap() },
UsbVidPid(0x16c0, 0x27dd),
)
.device_class(usbd_serial::USB_CLASS_CDC)
.strings(&[StringDescriptors::default()
.manufacturer("Amteurfunk Forschungs Gruppe")
.product("Radom Controler")])
.unwrap()
.build();
defmt::info!("USB Setup done");
// Todo: Check if internal pullups work here
let scl = gpiob.pb6.into_alternate_open_drain();
let sda = gpiob.pb7.into_alternate_open_drain();
@ -142,7 +188,7 @@ mod app {
defmt::info!("SPI Setup done");
let mut az_enable = gpioa.pa12.into_push_pull_output();
let mut az_enable = gpioa.pa10.into_push_pull_output();
az_enable.set_high();
let az_dir = gpioa.pa15.into_push_pull_output();
let az_step = gpiob.pb3.into_push_pull_output();
@ -179,6 +225,10 @@ mod app {
el_enable,
el_dir,
el_step,
usb_dev,
usb_serial,
usb_buffer: Vec::new(),
},
)
}
@ -317,4 +367,64 @@ mod app {
}
}
}
#[task(binds=OTG_FS, local=[usb_dev, usb_serial, usb_buffer])]
fn usb_fs(cx: usb_fs::Context) {
let usb_dev = cx.local.usb_dev;
let serial = cx.local.usb_serial;
let buffer = cx.local.usb_buffer;
if !usb_dev.poll(&mut [serial]) {
return;
}
let mut tmp = [0u8; 16];
match serial.read(&mut tmp) {
Ok(count) if count > 0 => {
if buffer.extend_from_slice(&tmp[0..count]).is_err() {
buffer.clear();
defmt::error!("Buffer overflow while waiting for the end of the packet");
}
}
_ => {}
}
loop {
if let Some(idx) = buffer.iter().position(|&x| x == 0) {
let (msg, rest) = buffer.split_at(idx + 1);
let mut message = [0u8; 128];
message[0..msg.len()].clone_from_slice(msg);
let host_msg = from_bytes_cobs::<HostMessage>(&mut message);
match host_msg {
Ok(host_msg) => match host_msg {
HostMessage::RequestStatus => {
let status = StatusMessage {
position: Position {
az: 42.0,
el: 23.0,
az_endcoder: 0.0,
el_encoder: 0.0,
az_magnetic: 0.0,
el_magnetic: 0.0,
},
alarms: Vec::new(),
};
let device_msg = RadomMessage::Status(status);
let bytes =
to_vec_cobs::<RadomMessage, USB_BUFFER_SIZE>(&device_msg).unwrap();
serial.write(bytes.as_slice()).unwrap();
}
HostMessage::TriggerDFUBootloader => todo!(),
},
Err(err) => defmt::error!("Unable to parse host message: {}", err),
};
*buffer = Vec::<u8, USB_BUFFER_SIZE>::from_slice(rest).unwrap();
} else {
break;
}
}
}
}