add debugging efforts

This commit is contained in:
Naxdy 2024-03-04 22:48:03 +01:00
parent bed51bf8e3
commit 33234c6257
No known key found for this signature in database
GPG key ID: C0437AAE9755550F
3 changed files with 44 additions and 16 deletions

View file

@ -83,9 +83,12 @@
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
nativeBuildInputs = builtins.attrValues { nativeBuildInputs = builtins.attrValues {
inherit rustToolchain; 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; inherit RUSTFLAGS CARGO_BUILD_TARGET;
}; };
})); }));

View file

@ -1,6 +1,6 @@
use core::default::Default; use core::default::Default;
use defmt::unwrap; use defmt::{error, info, unwrap};
use packed_struct::{derive::PackedStruct, prelude::packed_bits, types::Integer, PackedStruct}; use packed_struct::{derive::PackedStruct, prelude::packed_bits, types::Integer, PackedStruct};
use usb_device::bus::{UsbBus, UsbBusAllocator}; use usb_device::bus::{UsbBus, UsbBusAllocator};
use usbd_human_interface_device::{ use usbd_human_interface_device::{
@ -169,10 +169,16 @@ pub struct GcController<'a, B: UsbBus> {
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(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"); let data = input_state.pack().expect("Failed to pack GC input data");
info!("Packed data: {:08b}", data);
if unsafe { !GC_FIRST } { if unsafe { !GC_FIRST } {
buffer[1] |= 0x04; buffer[1] |= 0x04;
buffer[10] |= 0x04; buffer[10] |= 0x04;
@ -207,7 +215,7 @@ fn get_gcinput_hid_report(input_state: &GcReport) -> [u8; 37] {
unsafe { GC_FIRST = true }; unsafe { GC_FIRST = true };
} else { } else {
// controller in "port 1" // controller in "port 1"
buffer[2..=10].copy_from_slice(&data[0..=8]); buffer[2..=9].copy_from_slice(&data[0..=7]);
} }
buffer buffer

View file

@ -10,6 +10,8 @@
#![no_main] #![no_main]
mod gcc_hid; mod gcc_hid;
use core::fmt::Write;
use defmt::{error, info, Debug2Format};
use gcc_hid::{GcConfig, GcReport}; use gcc_hid::{GcConfig, GcReport};
use fugit::ExtU32; use fugit::ExtU32;
@ -24,10 +26,15 @@ use rp2040_hal as hal;
// A shorter alias for the Peripheral Access Crate, which provides low-level // A shorter alias for the Peripheral Access Crate, which provides low-level
// register access // register access
use hal::pac; use hal::{
gpio::FunctionUart,
pac,
uart::{UartConfig, UartPeripheral},
};
// Some traits we need // 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::{ use usb_device::{
bus::UsbBusAllocator, bus::UsbBusAllocator,
device::{UsbDeviceBuilder, UsbVidPid}, device::{UsbDeviceBuilder, UsbVidPid},
@ -74,7 +81,7 @@ fn main() -> ! {
.ok() .ok()
.unwrap(); .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(); let mut poll_timer = timer.count_down();
poll_timer.start(10.millis()); poll_timer.start(10.millis());
@ -108,6 +115,7 @@ fn main() -> ! {
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x057e, 0x0337)) let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x057e, 0x0337))
.manufacturer("Naxdy") .manufacturer("Naxdy")
.product("NaxGCC") .product("NaxGCC")
.serial_number("fleeb")
.device_class(0) .device_class(0)
.device_protocol(0) .device_protocol(0)
.device_sub_class(0) .device_sub_class(0)
@ -116,25 +124,34 @@ fn main() -> ! {
.max_packet_size_0(64) .max_packet_size_0(64)
.build(); .build();
let mut uart = UartPeripheral::new(
pac.UART0,
(
pins.gpio0.into_mode::<FunctionUart>(),
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 // Configure GPIO25 as an output
let mut led_pin = pins.gpio25.into_push_pull_output(); let mut led_pin = pins.gpio25.into_push_pull_output();
info!("Bleg");
let _ = uart.write_str("FLAR");
loop { loop {
if poll_timer.wait().is_ok() { if poll_timer.wait().is_ok() {
match gcc.device().write_report(&gcc_state) { match gcc.device().write_report(&gcc_state) {
Err(UsbHidError::WouldBlock) => {} Err(UsbHidError::WouldBlock) => {}
Ok(_) => {} Ok(_) => {}
Err(e) => { 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]) {} if usb_dev.poll(&mut [&mut gcc]) {}