This commit is contained in:
Naxdy 2024-03-11 13:39:24 +01:00
parent 6c7202bed2
commit 32fff25889
No known key found for this signature in database
GPG key ID: C0437AAE9755550F
3 changed files with 48 additions and 26 deletions

View file

@ -1,13 +1,10 @@
use core::slice::from_raw_parts; use core::slice::from_raw_parts;
use defmt::info;
use rp2040_flash::flash::{flash_range_erase, flash_range_program}; use rp2040_flash::flash::{flash_range_erase, flash_range_program};
const XIP_BASE: u32 = 0x10000000; const XIP_BASE: u32 = 0x10000000;
const FLASH_PAGE_SIZE: usize = 1usize << 8; const FLASH_PAGE_SIZE: usize = 1usize << 8;
const FLASH_SECTOR_SIZE: u32 = 1u32 << 12; 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; 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) { pub unsafe fn write_to_flash(b: u8) {
let mut data = [0u8; FLASH_PAGE_SIZE]; let mut data = [0u8; FLASH_PAGE_SIZE];
data[0] = b; 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); flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE, true);
info!("Erased flash");
flash_range_program(FLASH_TARGET_OFFSET, &data, true); flash_range_program(FLASH_TARGET_OFFSET, &data, true);
info!("Wrote to flash");
} }

View file

@ -7,7 +7,7 @@ use usbd_human_interface_device::{
descriptor::InterfaceProtocol, descriptor::InterfaceProtocol,
device::DeviceClass, device::DeviceClass,
interface::{ interface::{
InBytes64, InBytes8, Interface, InterfaceBuilder, InterfaceConfig, OutNone, ReportSingle, InBytes64, Interface, InterfaceBuilder, InterfaceConfig, OutBytes64, ReportSingle,
UsbAllocatable, UsbAllocatable,
}, },
UsbHidError, UsbHidError,
@ -129,13 +129,24 @@ pub struct GcReport {
pub trigger_r: u8, 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> { pub struct GcConfig<'a> {
interface: InterfaceConfig<'a, InBytes64, OutNone, ReportSingle>, interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>,
} }
impl<'a> GcConfig<'a> { impl<'a> GcConfig<'a> {
#[must_use] #[must_use]
pub fn new(interface: InterfaceConfig<'a, InBytes64, OutNone, ReportSingle>) -> Self { pub fn new(interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>) -> Self {
Self { interface } Self { interface }
} }
} }
@ -143,12 +154,15 @@ impl<'a> GcConfig<'a> {
impl<'a> Default for GcConfig<'a> { impl<'a> Default for GcConfig<'a> {
#[must_use] #[must_use]
fn default() -> Self { fn default() -> Self {
let i = unwrap!(unwrap!(InterfaceBuilder::new(GCC_REPORT_DESCRIPTOR)) let i = unwrap!(
.boot_device(InterfaceProtocol::None) unwrap!(unwrap!(InterfaceBuilder::new(GCC_REPORT_DESCRIPTOR))
.description("NaxGCC") .boot_device(InterfaceProtocol::None)
.in_endpoint(1.millis())); .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> { 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> { impl<'a, B: UsbBus> GcController<'a, B> {
pub fn write_report(&mut self, report: &GcReport) -> Result<(), UsbHidError> { pub fn write_report(&mut self, report: &GcReport) -> Result<(), UsbHidError> {
let report = get_gcinput_hid_report(report); let report = get_gcinput_hid_report(report);
// print report as binary
info!("Report: {:08b}", report);
self.interface self.interface
.write_report(&report) .write_report(&report)
.map(|_| ()) .map(|_| ())
.map_err(|e| UsbHidError::from(e)) .map_err(|e| UsbHidError::from(e))
} }
pub fn read_report(&mut self) -> Result<RawConsoleReport, UsbHidError> {
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> { 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 { fn interface(&mut self) -> &mut Self::I {
&mut self.interface &mut self.interface

View file

@ -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();
} }
} }