2025-01-03 16:15:22 +01:00
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
mod lsm6ds3tr;
|
|
|
|
|
|
|
|
use defmt::{info, panic};
|
|
|
|
use embassy_embedded_hal::shared_bus::asynch::i2c::I2cDevice;
|
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_futures::join::join;
|
|
|
|
use embassy_nrf::peripherals::TWISPI0;
|
|
|
|
use embassy_nrf::twim::Twim;
|
|
|
|
use embassy_nrf::usb::vbus_detect::{HardwareVbusDetect, VbusDetect};
|
|
|
|
use embassy_nrf::usb::{Driver, Instance};
|
|
|
|
use embassy_nrf::{bind_interrupts, pac, peripherals, twim, usb};
|
|
|
|
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
|
|
|
use embassy_sync::mutex::Mutex;
|
|
|
|
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
|
|
|
|
use embassy_usb::driver::EndpointError;
|
|
|
|
use embassy_usb::{Builder, Config};
|
|
|
|
|
|
|
|
use lsm6ds3tr::interface::i2c::I2cInterface;
|
|
|
|
// use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
|
|
|
|
use static_cell::StaticCell;
|
|
|
|
|
|
|
|
use {defmt_serial as _, panic_probe as _};
|
|
|
|
|
|
|
|
static I2C_BUS: StaticCell<Mutex<NoopRawMutex, Twim<TWISPI0>>> = StaticCell::new();
|
|
|
|
|
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
USBD => usb::InterruptHandler<peripherals::USBD>;
|
|
|
|
CLOCK_POWER => usb::vbus_detect::InterruptHandler;
|
|
|
|
});
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
|
|
|
|
|
|
|
info!("Enabling ext hfosc...");
|
|
|
|
pac::CLOCK.tasks_hfclkstart().write_value(1);
|
|
|
|
while pac::CLOCK.events_hfclkstarted().read() != 1 {}
|
|
|
|
|
|
|
|
// Create the driver, from the HAL.
|
|
|
|
let driver = Driver::new(p.USBD, Irqs, HardwareVbusDetect::new(Irqs));
|
|
|
|
|
|
|
|
// Create embassy-usb Config
|
|
|
|
let mut config = Config::new(0xc0de, 0xcafe);
|
|
|
|
config.manufacturer = Some("Embassy");
|
|
|
|
config.product = Some("USB-serial example");
|
|
|
|
config.serial_number = Some("12345678");
|
|
|
|
config.max_power = 100;
|
|
|
|
config.max_packet_size_0 = 64;
|
|
|
|
|
|
|
|
// Create embassy-usb DeviceBuilder using the driver and config.
|
|
|
|
// It needs some buffers for building the descriptors.
|
|
|
|
let mut config_descriptor = [0; 256];
|
|
|
|
let mut bos_descriptor = [0; 256];
|
|
|
|
let mut msos_descriptor = [0; 256];
|
|
|
|
let mut control_buf = [0; 64];
|
|
|
|
|
|
|
|
let mut state = State::new();
|
|
|
|
|
|
|
|
let mut builder = Builder::new(
|
|
|
|
driver,
|
|
|
|
config,
|
|
|
|
&mut config_descriptor,
|
|
|
|
&mut bos_descriptor,
|
|
|
|
&mut msos_descriptor,
|
|
|
|
&mut control_buf,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create classes on the builder.
|
|
|
|
let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
|
|
|
|
|
|
|
|
// Build the builder.
|
|
|
|
let mut usb = builder.build();
|
|
|
|
|
|
|
|
// Run the USB device.
|
|
|
|
let usb_fut = usb.run();
|
|
|
|
|
|
|
|
// Do stuff with the class!
|
|
|
|
let echo_fut = async {
|
|
|
|
loop {
|
|
|
|
class.wait_connection().await;
|
|
|
|
info!("Connected");
|
|
|
|
let _ = echo(&mut class).await;
|
|
|
|
info!("Disconnected");
|
|
|
|
}
|
2025-01-12 00:22:13 +01:00
|
|
|
};
|
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
// Run everything concurrently.
|
|
|
|
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
|
|
|
|
join(usb_fut, echo_fut).await;
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
// let config = twim::Config::default();
|
|
|
|
// let i2c = embassy_nrf::twim::Twim::new(p.TWISPI0, Irqs2, p.P0_07, p.P0_27, config);
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
let i2c_bus = {
|
|
|
|
use embassy_nrf::{
|
|
|
|
bind_interrupts,
|
|
|
|
peripherals::{self},
|
2025-01-03 16:15:22 +01:00
|
|
|
};
|
2025-01-12 19:39:49 +01:00
|
|
|
// bind_interrupts!(struct Irqs {
|
|
|
|
// SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twim::InterruptHandler<peripherals::TWISPI0>;
|
|
|
|
// });
|
|
|
|
bind_interrupts!(struct Irqs2 {
|
|
|
|
TWISPI0 => twim::InterruptHandler<peripherals::TWISPI0>;
|
|
|
|
});
|
|
|
|
let config = twim::Config::default();
|
|
|
|
let i2c = Twim::new(p.TWISPI0, Irqs2, p.P0_07, p.P0_27, config);
|
|
|
|
let i2c_bus = Mutex::<NoopRawMutex, _>::new(i2c);
|
|
|
|
I2C_BUS.init(i2c_bus)
|
|
|
|
};
|
2025-01-03 16:15:22 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
let i2c = I2cDevice::new(i2c_bus);
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
let interface = I2cInterface::new(i2c);
|
|
|
|
let imu = lsm6ds3tr::LSM6DS3TR::new(interface);
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
// let mut serial_port = usbd_serial::SerialPort::new(&usb);
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
loop {
|
|
|
|
info!("Hello, world!");
|
|
|
|
}
|
|
|
|
}
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
struct Disconnected {}
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
impl From<EndpointError> for Disconnected {
|
|
|
|
fn from(val: EndpointError) -> Self {
|
|
|
|
match val {
|
|
|
|
EndpointError::BufferOverflow => panic!("Buffer overflow"),
|
|
|
|
EndpointError::Disabled => Disconnected {},
|
2025-01-12 00:22:13 +01:00
|
|
|
}
|
2025-01-12 19:39:49 +01:00
|
|
|
}
|
|
|
|
}
|
2025-01-12 00:22:13 +01:00
|
|
|
|
2025-01-12 19:39:49 +01:00
|
|
|
async fn echo<'d, T: Instance + 'd, P: VbusDetect + 'd>(
|
|
|
|
class: &mut CdcAcmClass<'d, Driver<'d, T, P>>,
|
|
|
|
) -> Result<(), Disconnected> {
|
|
|
|
let mut buf = [0; 64];
|
|
|
|
loop {
|
|
|
|
let n = class.read_packet(&mut buf).await?;
|
|
|
|
let data = &buf[..n];
|
|
|
|
info!("data: {:x}", data);
|
|
|
|
class.write_packet(data).await?;
|
2025-01-03 16:15:22 +01:00
|
|
|
}
|
2025-01-12 00:22:13 +01:00
|
|
|
}
|