diff --git a/flake.nix b/flake.nix index ffb5fa9..460486e 100644 --- a/flake.nix +++ b/flake.nix @@ -83,9 +83,12 @@ devShells.default = pkgs.mkShell { nativeBuildInputs = builtins.attrValues { inherit rustToolchain; - inherit (pkgs) gcc-arm-embedded flip-link elf2uf2-rs picotool; + inherit (pkgs) gcc-arm-embedded flip-link elf2uf2-rs picotool probe-rs; }; + CARGO_TARGET_THUMBV6M_NONE_EABI_RUNNER = "probe-rs run --chip RP2040 --protocol swd"; + DEFMT_LOG = "trace"; + inherit RUSTFLAGS CARGO_BUILD_TARGET; }; })); diff --git a/src/gcc_hid.rs b/src/gcc_hid.rs index 6f6e168..8730d4a 100644 --- a/src/gcc_hid.rs +++ b/src/gcc_hid.rs @@ -1,6 +1,6 @@ use core::default::Default; -use defmt::unwrap; +use defmt::{error, info, unwrap}; use packed_struct::{derive::PackedStruct, prelude::packed_bits, types::Integer, PackedStruct}; use usb_device::bus::{UsbBus, UsbBusAllocator}; use usbd_human_interface_device::{ @@ -169,10 +169,16 @@ pub struct GcController<'a, B: UsbBus> { 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(UsbHidError::from) + .map_err(|e| { + error!("Found an error: {:?}", e); + UsbHidError::from(e) + }) } } @@ -199,6 +205,8 @@ fn get_gcinput_hid_report(input_state: &GcReport) -> [u8; 37] { let data = input_state.pack().expect("Failed to pack GC input data"); + info!("Packed data: {:08b}", data); + if unsafe { !GC_FIRST } { buffer[1] |= 0x04; buffer[10] |= 0x04; @@ -207,7 +215,7 @@ fn get_gcinput_hid_report(input_state: &GcReport) -> [u8; 37] { unsafe { GC_FIRST = true }; } else { // controller in "port 1" - buffer[2..=10].copy_from_slice(&data[0..=8]); + buffer[2..=9].copy_from_slice(&data[0..=7]); } buffer diff --git a/src/main.rs b/src/main.rs index d46378f..b296edd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ #![no_main] mod gcc_hid; +use core::fmt::Write; +use defmt::{error, info, Debug2Format}; use gcc_hid::{GcConfig, GcReport}; use fugit::ExtU32; @@ -24,10 +26,15 @@ use rp2040_hal as hal; // A shorter alias for the Peripheral Access Crate, which provides low-level // register access -use hal::pac; +use hal::{ + gpio::FunctionUart, + pac, + uart::{UartConfig, UartPeripheral}, +}; // Some traits we need -use embedded_hal::{digital::v2::OutputPin, timer::CountDown}; +use embedded_hal::{blocking::delay::DelayMs, digital::v2::OutputPin, timer::CountDown}; +use rp2040_hal::Clock; use usb_device::{ bus::UsbBusAllocator, device::{UsbDeviceBuilder, UsbVidPid}, @@ -74,7 +81,7 @@ fn main() -> ! { .ok() .unwrap(); - let timer = rp2040_hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks); + let mut timer = rp2040_hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks); let mut poll_timer = timer.count_down(); poll_timer.start(10.millis()); @@ -108,6 +115,7 @@ fn main() -> ! { let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x057e, 0x0337)) .manufacturer("Naxdy") .product("NaxGCC") + .serial_number("fleeb") .device_class(0) .device_protocol(0) .device_sub_class(0) @@ -116,25 +124,34 @@ fn main() -> ! { .max_packet_size_0(64) .build(); + let mut uart = UartPeripheral::new( + pac.UART0, + ( + pins.gpio0.into_mode::(), + pins.gpio1.into_mode(), + ), + &mut pac.RESETS, + ) + .enable(UartConfig::default(), clocks.peripheral_clock.freq()) + .unwrap(); + + gcc_state.buttons_1.button_a = true; + // Configure GPIO25 as an output let mut led_pin = pins.gpio25.into_push_pull_output(); + info!("Bleg"); + let _ = uart.write_str("FLAR"); loop { if poll_timer.wait().is_ok() { match gcc.device().write_report(&gcc_state) { Err(UsbHidError::WouldBlock) => {} Ok(_) => {} Err(e) => { - panic!("Error: {:?}", e); + led_pin.set_high().unwrap(); + error!("Error: {:?}", Debug2Format(&e)); + panic!(); } } - - gcc_state.buttons_1.button_a = !gcc_state.buttons_1.button_a; - - if gcc_state.buttons_1.button_a { - led_pin.set_high().unwrap(); - } else { - led_pin.set_low().unwrap(); - } } if usb_dev.poll(&mut [&mut gcc]) {}