mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-23 16:09:47 +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:
@@ -68,34 +68,40 @@ mod tests {
|
||||
// Import all parent module items
|
||||
use super::*;
|
||||
|
||||
/// Raw to mv zero.
|
||||
#[test]
|
||||
fn raw_to_mv_zero() {
|
||||
assert_eq!(raw_to_mv(0), 0);
|
||||
}
|
||||
|
||||
/// Raw to mv full scale.
|
||||
#[test]
|
||||
fn raw_to_mv_full_scale() {
|
||||
assert_eq!(raw_to_mv(4095), 3300);
|
||||
}
|
||||
|
||||
/// Raw to mv half.
|
||||
#[test]
|
||||
fn raw_to_mv_half() {
|
||||
let mv = raw_to_mv(2048);
|
||||
assert!(mv >= 1649 && mv <= 1651);
|
||||
}
|
||||
|
||||
/// Raw to mv quarter.
|
||||
#[test]
|
||||
fn raw_to_mv_quarter() {
|
||||
let mv = raw_to_mv(1024);
|
||||
assert!(mv >= 824 && mv <= 826);
|
||||
}
|
||||
|
||||
/// Raw to celsius room temp.
|
||||
#[test]
|
||||
fn raw_to_celsius_room_temp() {
|
||||
let temp = raw_to_celsius(876);
|
||||
assert!(temp > 20.0 && temp < 35.0);
|
||||
}
|
||||
|
||||
/// Raw to celsius known voltage.
|
||||
#[test]
|
||||
fn raw_to_celsius_known_voltage() {
|
||||
let raw = (0.706f32 / 3.3f32 * ADC_FULL_SCALE as f32 + 0.5f32) as u16;
|
||||
@@ -103,6 +109,7 @@ mod tests {
|
||||
assert!((temp - 27.0).abs() < 1.0);
|
||||
}
|
||||
|
||||
/// Raw to celsius higher voltage.
|
||||
#[test]
|
||||
fn raw_to_celsius_higher_voltage() {
|
||||
let temp_low = raw_to_celsius(1000);
|
||||
@@ -110,11 +117,13 @@ mod tests {
|
||||
assert!(temp_high > temp_low);
|
||||
}
|
||||
|
||||
/// Raw to mv one count.
|
||||
#[test]
|
||||
fn raw_to_mv_one_count() {
|
||||
assert_eq!(raw_to_mv(1), 0);
|
||||
}
|
||||
|
||||
/// Raw to mv ten counts.
|
||||
#[test]
|
||||
fn raw_to_mv_ten_counts() {
|
||||
assert_eq!(raw_to_mv(10), 8);
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
|
||||
// Rate extension trait for .Hz() baud rate construction
|
||||
use fugit::RateExtU32;
|
||||
// ADC one-shot trait for .read()
|
||||
use cortex_m::prelude::_embedded_hal_adc_OneShot;
|
||||
// Clock trait for accessing system clock frequency
|
||||
use hal::Clock;
|
||||
// GPIO pin types and function selectors
|
||||
@@ -35,10 +37,10 @@ use hal::gpio::{FunctionNull, FunctionUart, Pin, PullDown, PullNone};
|
||||
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;
|
||||
@@ -91,7 +93,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()
|
||||
}
|
||||
@@ -170,6 +178,59 @@ pub(crate) fn init_delay(clocks: &hal::clocks::ClocksManager) -> cortex_m::delay
|
||||
cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz())
|
||||
}
|
||||
|
||||
/// Write a conditional digit into `buf` if `val` meets the threshold.
|
||||
fn write_conditional_digit(
|
||||
buf: &mut [u8],
|
||||
pos: &mut usize,
|
||||
val: u32,
|
||||
threshold: u32,
|
||||
divisor: u32,
|
||||
) {
|
||||
if val >= threshold {
|
||||
buf[*pos] = b'0' + ((val / divisor) % 10) as u8;
|
||||
*pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a u32 with minimum digits (no leading zeros).
|
||||
fn write_min_digits(buf: &mut [u8], val: u32) -> usize {
|
||||
let mut pos = 0;
|
||||
write_conditional_digit(buf, &mut pos, val, 100, 100);
|
||||
write_conditional_digit(buf, &mut pos, val, 10, 10);
|
||||
buf[pos] = b'0' + (val % 10) as u8;
|
||||
pos + 1
|
||||
}
|
||||
|
||||
/// Write 4-digit millivolt value into `buf`.
|
||||
fn write_mv_digits(buf: &mut [u8], mv: u32) -> usize {
|
||||
buf[0] = b'0' + ((mv / 1000) % 10) as u8;
|
||||
buf[1] = b'0' + ((mv / 100) % 10) as u8;
|
||||
buf[2] = b'0' + ((mv / 10) % 10) as u8;
|
||||
buf[3] = b'0' + (mv % 10) as u8;
|
||||
4
|
||||
}
|
||||
|
||||
/// Write a negative sign if needed and return the absolute temperature value.
|
||||
fn write_sign(buf: &mut [u8], pos: &mut usize, temp_int: i32) -> u32 {
|
||||
if temp_int < 0 {
|
||||
buf[*pos] = b'-';
|
||||
*pos += 1;
|
||||
(-temp_int) as u32
|
||||
} else {
|
||||
temp_int as u32
|
||||
}
|
||||
}
|
||||
|
||||
/// Write temperature as "[-]NN.F" into `buf`.
|
||||
fn write_temp(buf: &mut [u8], temp_int: i32, temp_frac: u8) -> usize {
|
||||
let mut pos = 0;
|
||||
let abs_temp = write_sign(buf, &mut pos, temp_int);
|
||||
pos += write_min_digits(&mut buf[pos..], abs_temp);
|
||||
buf[pos] = b'.';
|
||||
buf[pos + 1] = b'0' + temp_frac;
|
||||
pos + 2
|
||||
}
|
||||
|
||||
/// Format a millivolt value into "ADC0: NNNN mV | Chip temp: NN.N C\r\n".
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -193,34 +254,6 @@ pub(crate) fn format_adc_line(buf: &mut [u8], mv: u32, temp_int: i32, temp_frac:
|
||||
pos + 4
|
||||
}
|
||||
|
||||
/// Write 4-digit millivolt value into `buf`.
|
||||
fn write_mv_digits(buf: &mut [u8], mv: u32) -> usize {
|
||||
buf[0] = b'0' + ((mv / 1000) % 10) as u8;
|
||||
buf[1] = b'0' + ((mv / 100) % 10) as u8;
|
||||
buf[2] = b'0' + ((mv / 10) % 10) as u8;
|
||||
buf[3] = b'0' + (mv % 10) as u8;
|
||||
4
|
||||
}
|
||||
|
||||
/// Write temperature as "[-]NN.F" into `buf`.
|
||||
fn write_temp(buf: &mut [u8], temp_int: i32, temp_frac: u8) -> usize {
|
||||
let mut pos = 0;
|
||||
let abs_temp = if temp_int < 0 { buf[pos] = b'-'; pos += 1; -temp_int } else { temp_int } as u32;
|
||||
pos += write_min_digits(&mut buf[pos..], abs_temp);
|
||||
buf[pos] = b'.'; pos += 1;
|
||||
buf[pos] = b'0' + temp_frac; pos += 1;
|
||||
pos
|
||||
}
|
||||
|
||||
/// Write a u32 with minimum digits (no leading zeros).
|
||||
fn write_min_digits(buf: &mut [u8], val: u32) -> usize {
|
||||
let mut pos = 0;
|
||||
if val >= 100 { buf[pos] = b'0' + ((val / 100) % 10) as u8; pos += 1; }
|
||||
if val >= 10 { buf[pos] = b'0' + ((val / 10) % 10) as u8; pos += 1; }
|
||||
buf[pos] = b'0' + (val % 10) as u8;
|
||||
pos + 1
|
||||
}
|
||||
|
||||
/// Type alias for the ADC input pin on GPIO 26.
|
||||
type Gpio26Adc = hal::adc::AdcPin<Pin<hal::gpio::bank0::Gpio26, FunctionNull, PullDown>>;
|
||||
|
||||
@@ -231,7 +264,14 @@ type Gpio26Adc = hal::adc::AdcPin<Pin<hal::gpio::bank0::Gpio26, FunctionNull, Pu
|
||||
/// * `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 mut delay = init_delay(&clocks);
|
||||
|
||||
@@ -54,24 +54,22 @@ use panic_halt as _;
|
||||
#[cfg(target_arch = "arm")]
|
||||
use panic_probe as _;
|
||||
|
||||
// Legacy embedded-hal 0.2 ADC OneShot trait for .read()
|
||||
use cortex_m::prelude::_embedded_hal_adc_OneShot;
|
||||
// HAL entry-point macro
|
||||
use hal::entry;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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)]
|
||||
@@ -83,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