Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (2)
......@@ -28,27 +28,78 @@ async fn main(_spawner: Spawner) {
let mut cp = cortex_m::peripheral::Peripherals::take().unwrap();
// Create the USB driver, from the HAL.
let irq = interrupt::take!(USBCTRL_IRQ);
let driver = Driver::new(p.USB, irq);
// 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;
// Required for windows compatiblity.
// 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;
// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let mut state = State::new();
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut control_buf,
None,
);
// 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();
// XXX: Can we get the number from the pin?
let mut gpio = (Flex::new(p.PIN_6), 6);
// let mut gpio = (Flex::new(p.PIN_10), 10);
// let mut gpio = (Flex::new(p.PIN_6), 6);
let mut gpio = (Flex::new(p.PIN_10), 10);
// let mut gpio = (Flex::new(p.PIN_13), 13);
run(&mut gpio.0, gpio.1, &mut cp.SYST);
// // Do stuff with the class!
// let echo_fut = async {
// loop {
// class.wait_connection().await;
// info!("Connected");
// let _ = usb_echo(&mut class, &mut gpio6, &mut cp.SYST).await;
// info!("Disconnected");
// }
// };
let usb = false;
if usb {
// // Do stuff with the class!
let echo_fut = async {
loop {
class.wait_connection().await;
info!("Connected");
let _ = usb_run(&mut gpio.0, gpio.1, &mut cp.SYST, &mut class).await;
info!("Disconnected");
}
};
// // 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;
// 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;
} else {
run(&mut gpio.0, gpio.1, &mut cp.SYST);
// run2()
}
}
struct Disconnected {}
......@@ -89,6 +140,21 @@ impl core::fmt::Write for FmtBuf {
}
}
async fn usb_run<'d, T: Instance + 'd, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST,
class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
todo!("impl me")
}
fn run2() {
loop {
for i in 100_000..1_000_000 {
info!("{}", i);
cortex_m::asm::delay(i);
}
}
}
fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Result<(), Disconnected> {
info!("hello");
......@@ -97,9 +163,12 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
syst.set_clock_source(cortex_m::peripheral::syst::SystClkSource::Core);
syst.enable_counter();
unsafe{ pac::ROSC.ctrl().modify(|s| s.set_enable(pac::rosc::vals::Enable::DISABLE)) };
unsafe{ pac::PADS_BANK0.gpio(pin_num).modify(|s| s.set_schmitt(false)) };
// Seemed to reduce noise (for characterising capacitor jitter).
// XXX Somehow disabling ROSC breaks SWD, perhaps signal integrity issues?
// unsafe{ pac::ROSC.ctrl().modify(|s| s.set_enable(pac::rosc::vals::Enable::DISABLE)) };
// TODO: test with/without schmitt disable
unsafe{ pac::PADS_BANK0.gpio(pin_num).modify(|s| s.set_schmitt(false)) };
// get it near the threshold
let del = critical_section::with(|_cs| {
......@@ -190,8 +259,8 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
// const UPRANGE: u32 = 50000;
// const LOWRANGE: u32 = 15000;
// nice integers
const LOW_OVER: u32 = 15000;
const HIGH_OVER: u32 = LOW_OVER + 8192;
const LOW_OVER: u32 = 1000;
const HIGH_OVER: u32 = LOW_OVER + 16384;
// for debug printout
// BUF iterations from the start (or end?) will be printed. must be <LOOP
......@@ -233,26 +302,38 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
pin.set_pull(Pull::Up);
syst.clear_current();
let t1 = SYST::get_current();
let imm = pin.is_high();
while pin.is_low() {}
let t2 = SYST::get_current();
let t_up = t1 - t2;
if i % 2 == 0 {
down_x = step(down_x, t_up);
} else {
// TODO: keep `t_up` as our random output
if i > 4 {
info!("t_up {} {} from down_x {}", t_up, imm, down_x);
// TODO: keep `t_up` as our random output
}
// only for nodouble testing
// down_x = step(down_x, t_up);
}
cortex_m::asm::delay(up_x);
pin.set_pull(Pull::Down);
syst.clear_current();
let t3 = SYST::get_current();
let imm = pin.is_low();
while pin.is_high() {}
let t4 = SYST::get_current();
let t_down = t1 - t2;
let t_down = t3 - t4;
if i % 2 == 0 {
up_x = step(up_x, t_down);
} else {
// TODO: keep `t_down` as our random output
if i > 4 {
info!("t_down {} {} from up_x {}", t_down, imm, up_x);
// TODO: keep t_down
}
// only for nodouble testing
// up_x = step(up_x, t_down);
}
cortex_m::asm::delay(down_x);
......@@ -270,12 +351,12 @@ fn run<'d, P: Pin>(pin: &mut Flex<'d, P>, pin_num: usize, syst: &mut SYST) -> Re
pin.set_pull(Pull::None);
});
for i in 0..BUF-1 {
if i % 2 == 1 {
info!("U {},{} x {} D {},{} x {} use", ups[i], ups[i+1], upx[i], downs[i], downs[i+1], downx[i]);
} else {
}
}
info!("===");
// for i in 0..BUF-1 {
// if i % 2 == 1 {
// info!("U {},{} x {} D {},{} x {} use", ups[i], ups[i+1], upx[i], downs[i], downs[i+1], downx[i]);
// } else {
// }
// }
// info!("===");
}
}