Skip to content

Commit

Permalink
Use lib/examples structure
Browse files Browse the repository at this point in the history
  • Loading branch information
KoviRobi committed Dec 21, 2023
1 parent ec907f9 commit 0017735
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
104 changes: 104 additions & 0 deletions examples/minimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#![no_std]
#![no_main]

use rp_pico as bsp;

use bsp::entry;
use bsp::hal::pac::interrupt;
use bsp::hal::{clocks::init_clocks_and_plls, pac, usb::UsbBus, watchdog::Watchdog};

// USB Device support
use usb_device::class_prelude::UsbBusAllocator;
use usb_device::prelude::*;

/// The USB Device Driver (shared with the interrupt).
static mut USB_DEVICE: Option<UsbDevice<UsbBus>> = None;

/// The USB Bus Driver (shared with the interrupt).
static mut USB_BUS: Option<UsbBusAllocator<UsbBus>> = None;

use panic_probe as _;

use rp_selfdebug::{dap_execute_command, dap_setup, CmsisDap};

/// The USB CMSIS-DAP Device Driver (shared with the interrupt).
static mut USB_DAP: Option<CmsisDap<UsbBus, 64>> = None;

#[entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);
let clocks = init_clocks_and_plls(
bsp::XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();

let usb_bus = UsbBusAllocator::new(UsbBus::new(
pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM,
clocks.usb_clock,
true,
&mut pac.RESETS,
));
unsafe {
USB_BUS = Some(usb_bus);
}
let bus_ref = unsafe { USB_BUS.as_ref().unwrap() };

let usb_dap = CmsisDap::new(bus_ref);
unsafe {
USB_DAP = Some(usb_dap);
}

let usb_dev = UsbDeviceBuilder::new(bus_ref, UsbVidPid(0x04b4, 0xf138))
.manufacturer("KoviRobi")
.product("CMSIS-DAP")
.serial_number("Test")
.device_class(2)
.composite_with_iads()
.max_packet_size_0(64)
.build();

unsafe {
USB_DEVICE = Some(usb_dev);
};

dap_setup(&pac.SYSCFG.dbgforce);

// Enable the USB interrupt
unsafe {
pac::NVIC::unmask(bsp::hal::pac::Interrupt::USBCTRL_IRQ);
};

loop {
cortex_m::asm::wfe();
}
}

#[allow(non_snake_case)]
#[interrupt]
fn USBCTRL_IRQ() {
let usb_dev = unsafe { USB_DEVICE.as_mut().unwrap() };
let dap = unsafe { USB_DAP.as_mut().unwrap() };

if usb_dev.poll(&mut [dap]) {
let mut buf = [0u8; 64];

match dap.read(&mut buf) {
Err(_e) => { /* Do nothing */ }
Ok(0) => { /* Do nothing */ }
Ok(_) => {
let mut out = [0; 64];
let (_in_size, out_size) = dap_execute_command(&buf, &mut out);
let _ = dap.write(&out[..out_size as usize]);
}
}
}
}
7 changes: 3 additions & 4 deletions src/main.rs → examples/pico_blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ static mut USB_SERIAL: Option<SerialPort<UsbBus>> = None;

use panic_probe as _;

pub mod cmsis_dap;
use cmsis_dap::CmsisDap;
use rp_selfdebug::{dap_execute_command, dap_setup, CmsisDap};

/// The USB CMSIS-DAP Device Driver (shared with the interrupt).
static mut USB_DAP: Option<CmsisDap<UsbBus, 64>> = None;
Expand Down Expand Up @@ -123,7 +122,7 @@ fn main() -> ! {
USB_DEVICE = Some(usb_dev);
};

cmsis_dap::dap_setup(&pac.SYSCFG.dbgforce);
dap_setup(&pac.SYSCFG.dbgforce);

// Enable the USB interrupt
unsafe {
Expand Down Expand Up @@ -200,7 +199,7 @@ fn USBCTRL_IRQ() {
}
Ok(_) => {
let mut out = [0; 64];
let (_in_size, out_size) = cmsis_dap::dap_execute_command(&buf, &mut out);
let (_in_size, out_size) = dap_execute_command(&buf, &mut out);
let _ = dap.write(&out[..out_size as usize]);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![no_std]
mod cmsis_dap;
pub use cmsis_dap::*;

0 comments on commit 0017735

Please sign in to comment.