Implemented postcard-rpc.

This commit is contained in:
2025-08-04 16:58:34 +02:00
parent 18220e4be9
commit 4cd3beba73
6 changed files with 537 additions and 36 deletions

125
src/communication.rs Normal file
View File

@@ -0,0 +1,125 @@
use defmt::{info, error};
use embassy_executor::Spawner;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_stm32::usb::Driver;
use embassy_stm32::{peripherals, usb};
use postcard_rpc::{
define_dispatch,
header::VarHeader,
server::{
impls::embassy_usb_v0_4::{
dispatch_impl::{WireRxBuf, WireRxImpl, WireSpawnImpl, WireStorage, WireTxImpl},
PacketBuffers,
},
Dispatch, Server,
},
};
use bioz_icd::{PingEndpoint, GetUniqueIdEndpoint, ENDPOINT_LIST, TOPICS_IN_LIST, TOPICS_OUT_LIST};
pub struct Context;
type AppDriver = usb::Driver<'static, peripherals::USB>;
type AppStorage = WireStorage<ThreadModeRawMutex, AppDriver, 256, 256, 64, 256>;
type BufStorage = PacketBuffers<1024, 1024>;
type AppTx = WireTxImpl<ThreadModeRawMutex, AppDriver>;
type AppRx = WireRxImpl<AppDriver>;
type AppServer = Server<AppTx, AppRx, WireRxBuf, MyApp>;
use static_cell::ConstStaticCell;
static PBUFS: ConstStaticCell<BufStorage> = ConstStaticCell::new(BufStorage::new());
static STORAGE: AppStorage = AppStorage::new();
define_dispatch! {
app: MyApp;
spawn_fn: spawn_fn;
tx_impl: AppTx;
spawn_impl: WireSpawnImpl;
context: Context;
endpoints: {
list: ENDPOINT_LIST;
| EndpointTy | kind | handler |
| ---------- | ---- | ------- |
| PingEndpoint | blocking | ping_handler |
| GetUniqueIdEndpoint | blocking | get_unique_id_handler |
};
topics_in: {
list: TOPICS_IN_LIST;
| TopicTy | kind | handler |
| ---------- | ---- | ------- |
};
topics_out: {
list: TOPICS_OUT_LIST;
};
}
fn usb_config() -> embassy_usb::Config<'static> {
let mut config = embassy_usb::Config::new(0x16c0, 0x27DD);
config.manufacturer = Some("Hubald Verzijl");
config.product = Some("bioz-amplifier");
config.serial_number = Some("12345678");
// Required for windows compatibility.
// https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
config.device_class = 0xEF;
config.device_sub_class = 0x02;
config.device_protocol = 0x01;
config.composite_with_iads = true;
config
}
/// This handles the low level USB management
#[embassy_executor::task]
pub async fn usb_task(mut usb: embassy_usb::UsbDevice<'static, AppDriver>) {
usb.run().await;
}
#[embassy_executor::task]
async fn server_run(mut server: AppServer) {
loop {
// If the host disconnects, we'll return an error here.
// If this happens, just wait until the host reconnects
let _ = server.run().await;
}
}
// ---
pub fn init_communication(usb_driver: Driver<'static, peripherals::USB>, spawner: Spawner) {
// Initialize communication peripherals
let pbufs = PBUFS.take();
let config = usb_config();
let context = Context;
let (device, tx_impl, rx_impl) = STORAGE.init(usb_driver, config, pbufs.tx_buf.as_mut_slice());
let dispatcher = MyApp::new(context, spawner.into());
let vkk = dispatcher.min_key_len();
let server: AppServer = Server::new(
tx_impl,
rx_impl,
pbufs.rx_buf.as_mut_slice(),
dispatcher,
vkk,
);
spawner.must_spawn(usb_task(device));
spawner.must_spawn(server_run(server));
}
fn ping_handler(_context: &mut Context, _header: VarHeader, rqst: u32) -> u32 {
info!("ping");
rqst
}
fn get_unique_id_handler(_context: &mut Context, _header: VarHeader, _rqst: ()) -> u64 {
error!("get_unique_id");
200
}

View File

@@ -1,7 +1,7 @@
#![no_std]
#![no_main]
use defmt::*;
use defmt::info;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::{i2c, spi, Config};
@@ -11,19 +11,29 @@ use crate::ad5940_registers::{AFECON, AFEGENINTSTA};
use {defmt_rtt as _, panic_probe as _};
mod ad5940;
use ad5940::AD5940;
// mod ad5940;
// use ad5940::AD5940;
mod adg2128;
use adg2128::State;
// mod adg2128;
// use adg2128::State;
mod electrodes;
use electrodes::{Electrodes, Electrode, AD5940Pin};
// mod electrodes;
// use electrodes::{Electrodes, Electrode, AD5940Pin};
mod ad5940_registers;
use embassy_stm32::usb::Driver;
use embassy_stm32::{bind_interrupts, peripherals, usb};
mod communication;
use communication::init_communication;
bind_interrupts!(struct Irqs {
USB_DRD_FS => usb::InterruptHandler<peripherals::USB>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
async fn main(spawner: Spawner) {
let mut config = Config::default();
{
@@ -49,7 +59,7 @@ async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(config);
info!("Hello World!");
// let mut led = Output::new(p.PA5, Level::High, Speed::Low);
let mut led = Output::new(p.PA5, Level::High, Speed::Low);
// // Set up SPI for AD5940
// let cs = Output::new(p.PC9, Level::High, Speed::Low);
@@ -108,6 +118,10 @@ async fn main(_spawner: Spawner) {
// ad5940.sequencer_info_configure(0, ad5940.seq_len, start_address).await;
// info!("{}", ad5940.seq_len);
// Create USB driver and start postcard-rpc server
let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11);
init_communication(driver, spawner);
loop {
// Read chip id
// let chip_id = ad5940.get_chipid().await.unwrap();
@@ -121,10 +135,16 @@ async fn main(_spawner: Spawner) {
// info!("Electrodes states: {:?}", result);
// info!("high");
// led.set_high();
led.set_high();
Timer::after_millis(1000).await;
led.set_low();
Timer::after_millis(1000).await;
info!("Toggle!");
// ad5940.sequencer_trigger().await;
}
}