mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-17 21:44:45 +02:00
refactor: enforce max 8 code lines, add docstrings, fix warnings across all Rust and C SDK projects
Rust (all 15 projects):
- Refactored overlength functions: format_counter, format_u8, format_f32_1,
format_u32_minimal, gpio_drive, read_sensor, poll_sensor, format_round_trip,
format_u32, prepare_write_buf, write_min_digits, write_temp, UartDriver::init,
init_spi, angle_to_pulse_us, compute_servo_level
- Added 200+ docstrings to test functions, mock structs, impl blocks
- Fixed pub static comments (//) to doc comments (///) in all main.rs files
- Fixed helper function ordering (helpers above callers)
- Fixed Fn(u32) -> FnMut(u32) bound in button poll_button
- Moved OneShot trait import from main.rs to board.rs in adc project
- Added unsafe {} blocks in flash unsafe fn bodies (Rust 2024 edition)
- Removed unused hal::Clock imports from pwm/servo main.rs
- All 15 projects build with zero errors and zero warnings
C Pico SDK (all 15 projects):
- Added docstrings to all public functions, macros, and static variables
- All 15 projects rebuilt with zero errors
Cleanup:
- Removed build/ and target/ directories from git tracking
- Added target/ to .gitignore
- Deleted temporary fix_rust_docs.py script
This commit is contained in:
@@ -32,15 +32,18 @@ use fugit::RateExtU32;
|
||||
// Clock trait for accessing system clock frequency
|
||||
use hal::Clock;
|
||||
// GPIO pin types and function selectors
|
||||
use hal::gpio::{FunctionNull, FunctionSioInput, FunctionSioOutput, FunctionUart, Pin, PullDown, PullNone, PullUp};
|
||||
use hal::gpio::{
|
||||
FunctionNull, FunctionSioInput, FunctionSioOutput, FunctionUart, Pin, PullDown, PullNone,
|
||||
PullUp,
|
||||
};
|
||||
// UART configuration and peripheral types
|
||||
use hal::uart::{DataBits, Enabled, StopBits, UartConfig, UartPeripheral};
|
||||
|
||||
// Alias our HAL crate
|
||||
#[cfg(rp2350)]
|
||||
use rp235x_hal as hal;
|
||||
#[cfg(rp2040)]
|
||||
use rp2040_hal as hal;
|
||||
#[cfg(rp2350)]
|
||||
use rp235x_hal as hal;
|
||||
|
||||
/// External crystal frequency in Hz (12 MHz).
|
||||
pub(crate) const XTAL_FREQ_HZ: u32 = 12_000_000u32;
|
||||
@@ -96,7 +99,13 @@ pub(crate) fn init_clocks(
|
||||
watchdog: &mut hal::Watchdog,
|
||||
) -> hal::clocks::ClocksManager {
|
||||
hal::clocks::init_clocks_and_plls(
|
||||
XTAL_FREQ_HZ, xosc, clocks, pll_sys, pll_usb, resets, watchdog,
|
||||
XTAL_FREQ_HZ,
|
||||
xosc,
|
||||
clocks,
|
||||
pll_sys,
|
||||
pll_usb,
|
||||
resets,
|
||||
watchdog,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
@@ -188,7 +197,14 @@ type LedPin = Pin<hal::gpio::bank0::Gpio25, FunctionSioOutput, PullDown>;
|
||||
/// * `pac` - PAC Peripherals singleton (consumed).
|
||||
pub(crate) fn run(mut pac: hal::pac::Peripherals) -> ! {
|
||||
let mut wd = hal::Watchdog::new(pac.WATCHDOG);
|
||||
let clocks = init_clocks(pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS, &mut wd);
|
||||
let clocks = init_clocks(
|
||||
pac.XOSC,
|
||||
pac.CLOCKS,
|
||||
pac.PLL_SYS,
|
||||
pac.PLL_USB,
|
||||
&mut pac.RESETS,
|
||||
&mut wd,
|
||||
);
|
||||
let pins = init_pins(pac.IO_BANK0, pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
|
||||
let uart = init_uart(pac.UART0, pins.gpio0, pins.gpio1, &mut pac.RESETS, &clocks);
|
||||
let delay = RefCell::new(init_delay(&clocks));
|
||||
@@ -210,12 +226,15 @@ fn button_demo(
|
||||
led_pin: Pin<hal::gpio::bank0::Gpio25, FunctionNull, PullDown>,
|
||||
delay: &RefCell<cortex_m::delay::Delay>,
|
||||
) -> ! {
|
||||
let mut btn = crate::button::ButtonDriver::init(
|
||||
btn_pin.into_pull_up_input(), DEBOUNCE_MS, |ms| delay.borrow_mut().delay_ms(ms),
|
||||
);
|
||||
let mut btn =
|
||||
crate::button::ButtonDriver::init(btn_pin.into_pull_up_input(), DEBOUNCE_MS, |ms| {
|
||||
delay.borrow_mut().delay_ms(ms)
|
||||
});
|
||||
let mut led = crate::button::ButtonLed::init(led_pin.into_push_pull_output());
|
||||
let mut last = false;
|
||||
loop { poll_button(uart, &mut btn, &mut led, &mut last, delay); }
|
||||
loop {
|
||||
poll_button(uart, &mut btn, &mut led, &mut last, delay);
|
||||
}
|
||||
}
|
||||
|
||||
/// Poll button state, update LED, and report edge transitions.
|
||||
@@ -227,7 +246,7 @@ fn button_demo(
|
||||
/// * `led` - Mutable reference to the LED driver.
|
||||
/// * `last` - Mutable reference to the previous button state.
|
||||
/// * `delay` - Shared reference to the delay provider.
|
||||
fn poll_button<F: Fn(u32)>(
|
||||
fn poll_button<F: FnMut(u32)>(
|
||||
uart: &EnabledUart,
|
||||
btn: &mut crate::button::ButtonDriver<BtnPin, F>,
|
||||
led: &mut crate::button::ButtonLed<LedPin>,
|
||||
@@ -236,7 +255,10 @@ fn poll_button<F: Fn(u32)>(
|
||||
) {
|
||||
let pressed = btn.is_pressed();
|
||||
led.set(pressed);
|
||||
if pressed != *last { report_edge(uart, pressed); *last = pressed; }
|
||||
if pressed != *last {
|
||||
report_edge(uart, pressed);
|
||||
*last = pressed;
|
||||
}
|
||||
delay.borrow_mut().delay_ms(POLL_MS);
|
||||
}
|
||||
|
||||
@@ -247,7 +269,11 @@ fn poll_button<F: Fn(u32)>(
|
||||
/// * `uart` - Reference to the enabled UART peripheral for serial output.
|
||||
/// * `pressed` - `true` if the button is pressed, `false` if released.
|
||||
fn report_edge(uart: &EnabledUart, pressed: bool) {
|
||||
let msg = if pressed { b"Button: PRESSED\r\n" as &[u8] } else { b"Button: RELEASED\r\n" };
|
||||
let msg = if pressed {
|
||||
b"Button: PRESSED\r\n" as &[u8]
|
||||
} else {
|
||||
b"Button: RELEASED\r\n"
|
||||
};
|
||||
uart.write_full_blocking(msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -131,69 +131,87 @@ mod tests {
|
||||
// Error type trait for mock pin implementations
|
||||
use embedded_hal::digital::ErrorType;
|
||||
|
||||
/// Mock input pin for testing.
|
||||
struct MockInputPin {
|
||||
low: bool,
|
||||
}
|
||||
|
||||
impl MockInputPin {
|
||||
/// New.
|
||||
fn new(low: bool) -> Self {
|
||||
Self { low }
|
||||
}
|
||||
}
|
||||
|
||||
/// ErrorType implementation for MockInputPin.
|
||||
impl ErrorType for MockInputPin {
|
||||
type Error = Infallible;
|
||||
}
|
||||
|
||||
/// InputPin implementation for MockInputPin.
|
||||
impl InputPin for MockInputPin {
|
||||
/// Is high.
|
||||
fn is_high(&mut self) -> Result<bool, Self::Error> {
|
||||
Ok(!self.low)
|
||||
}
|
||||
/// Is low.
|
||||
fn is_low(&mut self) -> Result<bool, Self::Error> {
|
||||
Ok(self.low)
|
||||
}
|
||||
}
|
||||
|
||||
/// Mock output pin for testing.
|
||||
struct MockOutputPin {
|
||||
state: bool,
|
||||
}
|
||||
|
||||
impl MockOutputPin {
|
||||
/// New.
|
||||
fn new() -> Self {
|
||||
Self { state: false }
|
||||
}
|
||||
}
|
||||
|
||||
/// ErrorType implementation for MockOutputPin.
|
||||
impl ErrorType for MockOutputPin {
|
||||
type Error = Infallible;
|
||||
}
|
||||
|
||||
/// OutputPin implementation for MockOutputPin.
|
||||
impl OutputPin for MockOutputPin {
|
||||
/// Set low.
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
self.state = false;
|
||||
Ok(())
|
||||
}
|
||||
/// Set high.
|
||||
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||
self.state = true;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// StatefulOutputPin implementation for MockOutputPin.
|
||||
impl StatefulOutputPin for MockOutputPin {
|
||||
/// Is set high.
|
||||
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
|
||||
Ok(self.state)
|
||||
}
|
||||
/// Is set low.
|
||||
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
|
||||
Ok(!self.state)
|
||||
}
|
||||
/// Toggle.
|
||||
fn toggle(&mut self) -> Result<(), Self::Error> {
|
||||
self.state = !self.state;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Noop delay.
|
||||
fn noop_delay(_ms: u32) {}
|
||||
|
||||
/// Is pressed when low and confirmed.
|
||||
#[test]
|
||||
fn is_pressed_when_low_and_confirmed() {
|
||||
let pin = MockInputPin::new(true);
|
||||
@@ -201,6 +219,7 @@ mod tests {
|
||||
assert!(drv.is_pressed());
|
||||
}
|
||||
|
||||
/// Is not pressed when high.
|
||||
#[test]
|
||||
fn is_not_pressed_when_high() {
|
||||
let pin = MockInputPin::new(false);
|
||||
@@ -208,6 +227,7 @@ mod tests {
|
||||
assert!(!drv.is_pressed());
|
||||
}
|
||||
|
||||
/// Debounce ms stored.
|
||||
#[test]
|
||||
fn debounce_ms_stored() {
|
||||
let pin = MockInputPin::new(false);
|
||||
@@ -215,6 +235,7 @@ mod tests {
|
||||
assert_eq!(drv.debounce_ms, 50);
|
||||
}
|
||||
|
||||
/// Debounce calls delay.
|
||||
#[test]
|
||||
fn debounce_calls_delay() {
|
||||
let mut called_with: u32 = 0;
|
||||
@@ -224,12 +245,14 @@ mod tests {
|
||||
assert_eq!(called_with, 25);
|
||||
}
|
||||
|
||||
/// Led init starts off.
|
||||
#[test]
|
||||
fn led_init_starts_off() {
|
||||
let led = ButtonLed::init(MockOutputPin::new());
|
||||
assert!(!led.led_pin.state);
|
||||
}
|
||||
|
||||
/// Led set on.
|
||||
#[test]
|
||||
fn led_set_on() {
|
||||
let mut led = ButtonLed::init(MockOutputPin::new());
|
||||
@@ -237,6 +260,7 @@ mod tests {
|
||||
assert!(led.led_pin.state);
|
||||
}
|
||||
|
||||
/// Led set off.
|
||||
#[test]
|
||||
fn led_set_off() {
|
||||
let mut led = ButtonLed::init(MockOutputPin::new());
|
||||
@@ -245,6 +269,7 @@ mod tests {
|
||||
assert!(!led.led_pin.state);
|
||||
}
|
||||
|
||||
/// Led set on then off then on.
|
||||
#[test]
|
||||
fn led_set_on_then_off_then_on() {
|
||||
let mut led = ButtonLed::init(MockOutputPin::new());
|
||||
|
||||
@@ -63,13 +63,13 @@ use rp235x_hal as hal;
|
||||
#[cfg(rp2040)]
|
||||
use rp2040_hal as hal;
|
||||
|
||||
// Second-stage boot loader for RP2040
|
||||
/// Second-stage boot loader for RP2040
|
||||
#[unsafe(link_section = ".boot2")]
|
||||
#[used]
|
||||
#[cfg(rp2040)]
|
||||
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
|
||||
|
||||
// Boot metadata for the RP2350 Boot ROM
|
||||
/// Boot metadata for the RP2350 Boot ROM
|
||||
#[unsafe(link_section = ".start_block")]
|
||||
#[used]
|
||||
#[cfg(rp2350)]
|
||||
@@ -81,7 +81,7 @@ fn main() -> ! {
|
||||
board::run(hal::pac::Peripherals::take().unwrap())
|
||||
}
|
||||
|
||||
// Picotool binary info metadata
|
||||
/// Picotool binary info metadata
|
||||
#[unsafe(link_section = ".bi_entries")]
|
||||
#[used]
|
||||
pub static PICOTOOL_ENTRIES: [hal::binary_info::EntryAddr; 5] = [
|
||||
|
||||
Reference in New Issue
Block a user