diff --git a/src/flash_mem.rs b/src/flash_mem.rs index b89a19e..4c4e442 100644 --- a/src/flash_mem.rs +++ b/src/flash_mem.rs @@ -1,13 +1,10 @@ use core::slice::from_raw_parts; -use defmt::info; use rp2040_flash::flash::{flash_range_erase, flash_range_program}; const XIP_BASE: u32 = 0x10000000; const FLASH_PAGE_SIZE: usize = 1usize << 8; const FLASH_SECTOR_SIZE: u32 = 1u32 << 12; -const FLASH_BLOCK_SIZE: u32 = 1u32 << 16; -const FLASH_BLOCK_ERASE_CMD: u8 = 0xd8; const FLASH_TARGET_OFFSET: u32 = 256 * 1024; @@ -22,15 +19,7 @@ pub fn read_from_flash() -> u8 { pub unsafe fn write_to_flash(b: u8) { let mut data = [0u8; FLASH_PAGE_SIZE]; data[0] = b; - info!("About to write with XIP_BASE: 0x{:x}", XIP_BASE); - info!("FLASH_PAGE_SIZE is {}", FLASH_PAGE_SIZE); - info!("FLASH_SECTOR_SIZE is {}", FLASH_SECTOR_SIZE); - info!("FLASH_BLOCK_SIZE is {}", FLASH_BLOCK_SIZE); - info!("FLASH_BLOCK_ERASE_CMD is 0x{:x}", FLASH_BLOCK_ERASE_CMD); - info!("FLASH_TARGET_OFFSET is {}", FLASH_TARGET_OFFSET); flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE, true); - info!("Erased flash"); flash_range_program(FLASH_TARGET_OFFSET, &data, true); - info!("Wrote to flash"); } diff --git a/src/gcc_hid.rs b/src/gcc_hid.rs index cd990c0..d21f74b 100644 --- a/src/gcc_hid.rs +++ b/src/gcc_hid.rs @@ -7,7 +7,7 @@ use usbd_human_interface_device::{ descriptor::InterfaceProtocol, device::DeviceClass, interface::{ - InBytes64, InBytes8, Interface, InterfaceBuilder, InterfaceConfig, OutNone, ReportSingle, + InBytes64, Interface, InterfaceBuilder, InterfaceConfig, OutBytes64, ReportSingle, UsbAllocatable, }, UsbHidError, @@ -129,13 +129,24 @@ pub struct GcReport { pub trigger_r: u8, } +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[repr(C, align(8))] +pub struct RawConsoleReport { + pub packet: [u8; 64], +} + +impl Default for RawConsoleReport { + fn default() -> Self { + Self { packet: [0u8; 64] } + } +} pub struct GcConfig<'a> { - interface: InterfaceConfig<'a, InBytes64, OutNone, ReportSingle>, + interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>, } impl<'a> GcConfig<'a> { #[must_use] - pub fn new(interface: InterfaceConfig<'a, InBytes64, OutNone, ReportSingle>) -> Self { + pub fn new(interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>) -> Self { Self { interface } } } @@ -143,12 +154,15 @@ impl<'a> GcConfig<'a> { impl<'a> Default for GcConfig<'a> { #[must_use] fn default() -> Self { - let i = unwrap!(unwrap!(InterfaceBuilder::new(GCC_REPORT_DESCRIPTOR)) - .boot_device(InterfaceProtocol::None) - .description("NaxGCC") - .in_endpoint(1.millis())); + let i = unwrap!( + unwrap!(unwrap!(InterfaceBuilder::new(GCC_REPORT_DESCRIPTOR)) + .boot_device(InterfaceProtocol::None) + .description("NaxGCC") + .in_endpoint(1.millis())) + .with_out_endpoint(1.millis()) + ); - Self::new(i.without_out_endpoint().build()) + Self::new(i.build()) } } @@ -163,25 +177,30 @@ impl<'a, B: UsbBus + 'a> UsbAllocatable<'a, B> for GcConfig<'a> { } pub struct GcController<'a, B: UsbBus> { - interface: Interface<'a, B, InBytes64, OutNone, ReportSingle>, + interface: Interface<'a, B, InBytes64, OutBytes64, ReportSingle>, } impl<'a, B: UsbBus> GcController<'a, B> { pub fn write_report(&mut self, report: &GcReport) -> Result<(), UsbHidError> { let report = get_gcinput_hid_report(report); - // print report as binary - - info!("Report: {:08b}", report); self.interface .write_report(&report) .map(|_| ()) .map_err(|e| UsbHidError::from(e)) } + + pub fn read_report(&mut self) -> Result { + let mut report = RawConsoleReport::default(); + match self.interface.read_report(&mut report.packet) { + Err(e) => Err(UsbHidError::from(e)), + Ok(_) => Ok(report), + } + } } impl<'a, B: UsbBus> DeviceClass<'a> for GcController<'a, B> { - type I = Interface<'a, B, InBytes64, OutNone, ReportSingle>; + type I = Interface<'a, B, InBytes64, OutBytes64, ReportSingle>; fn interface(&mut self) -> &mut Self::I { &mut self.interface diff --git a/src/main.rs b/src/main.rs index 8cf1fa3..53a34d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -164,8 +164,22 @@ fn main() -> ! { } } } - if usb_dev.poll(&mut [&mut gcc]) {} + if usb_dev.poll(&mut [&mut gcc]) { + match gcc.device().read_report() { + Err(UsbHidError::WouldBlock) => {} + Err(e) => { + error!("Failed to read report: {:?}", Debug2Format(&e)); + } + Ok(report) => { + info!("Received report: {:08x}", report.packet); + // rumble packet + if report.packet[0] == 0x11 { + info!("Received rumble info: Controller1 ({:08x}) Controller2 ({:08x}) Controller3 ({:08x}) Controller4 ({:08x})", report.packet[1], report.packet[2], report.packet[3], report.packet[4]); + } + } + } + } - gcc_state.buttons_2.button_start = btn_pin.is_low().unwrap(); + gcc_state.buttons_2.button_z = btn_pin.is_low().unwrap(); } }