mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-25 08:54:05 +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:
@@ -37,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;
|
||||
@@ -96,7 +96,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()
|
||||
}
|
||||
@@ -189,20 +195,29 @@ pub(crate) fn format_duty(buf: &mut [u8], duty: u8) -> usize {
|
||||
buf[..6].copy_from_slice(b"Duty: ");
|
||||
let mut pos = 6;
|
||||
pos += write_duty_digits(&mut buf[pos..], duty);
|
||||
buf[pos] = b'%'; pos += 1;
|
||||
buf[pos] = b'\r'; pos += 1;
|
||||
buf[pos] = b'\n'; pos += 1;
|
||||
buf[pos] = b'%';
|
||||
pos += 1;
|
||||
buf[pos] = b'\r';
|
||||
pos += 1;
|
||||
buf[pos] = b'\n';
|
||||
pos += 1;
|
||||
pos
|
||||
}
|
||||
|
||||
/// Write the 3-character right-justified duty digits into `buf`.
|
||||
fn write_duty_digits(buf: &mut [u8], duty: u8) -> usize {
|
||||
if duty >= 100 {
|
||||
buf[0] = b'1'; buf[1] = b'0'; buf[2] = b'0';
|
||||
buf[0] = b'1';
|
||||
buf[1] = b'0';
|
||||
buf[2] = b'0';
|
||||
} else if duty >= 10 {
|
||||
buf[0] = b' '; buf[1] = b'0' + duty / 10; buf[2] = b'0' + duty % 10;
|
||||
buf[0] = b' ';
|
||||
buf[1] = b'0' + duty / 10;
|
||||
buf[2] = b'0' + duty % 10;
|
||||
} else {
|
||||
buf[0] = b' '; buf[1] = b' '; buf[2] = b'0' + duty;
|
||||
buf[0] = b' ';
|
||||
buf[1] = b' ';
|
||||
buf[2] = b'0' + duty;
|
||||
}
|
||||
3
|
||||
}
|
||||
@@ -274,7 +289,14 @@ type PwmSlice4 = hal::pwm::Slice<hal::pwm::Pwm4, hal::pwm::FreeRunning>;
|
||||
/// * `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);
|
||||
|
||||
@@ -55,24 +55,22 @@ use panic_halt as _;
|
||||
#[cfg(target_arch = "arm")]
|
||||
use panic_probe as _;
|
||||
|
||||
// Clock trait for accessing system clock frequency
|
||||
use hal::Clock;
|
||||
// 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)]
|
||||
@@ -84,7 +82,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] = [
|
||||
|
||||
@@ -82,58 +82,69 @@ mod tests {
|
||||
// Import all parent module items
|
||||
use super::*;
|
||||
|
||||
/// Calc clk div 1khz.
|
||||
#[test]
|
||||
fn calc_clk_div_1khz() {
|
||||
let div = calc_clk_div(150_000_000, 1000, 9999);
|
||||
assert!((div - 15.0).abs() < 0.01);
|
||||
}
|
||||
|
||||
/// Calc clk div 10khz.
|
||||
#[test]
|
||||
fn calc_clk_div_10khz() {
|
||||
let div = calc_clk_div(150_000_000, 10000, 9999);
|
||||
assert!((div - 1.5).abs() < 0.01);
|
||||
}
|
||||
|
||||
/// Clamp percent within range.
|
||||
#[test]
|
||||
fn clamp_percent_within_range() {
|
||||
assert_eq!(clamp_percent(50), 50);
|
||||
}
|
||||
|
||||
/// Clamp percent at 100.
|
||||
#[test]
|
||||
fn clamp_percent_at_100() {
|
||||
assert_eq!(clamp_percent(100), 100);
|
||||
}
|
||||
|
||||
/// Clamp percent above 100.
|
||||
#[test]
|
||||
fn clamp_percent_above_100() {
|
||||
assert_eq!(clamp_percent(255), 100);
|
||||
}
|
||||
|
||||
/// Clamp percent zero.
|
||||
#[test]
|
||||
fn clamp_percent_zero() {
|
||||
assert_eq!(clamp_percent(0), 0);
|
||||
}
|
||||
|
||||
/// Duty to level zero.
|
||||
#[test]
|
||||
fn duty_to_level_zero() {
|
||||
assert_eq!(duty_to_level(0, 9999), 0);
|
||||
}
|
||||
|
||||
/// Duty to level 100.
|
||||
#[test]
|
||||
fn duty_to_level_100() {
|
||||
assert_eq!(duty_to_level(100, 9999), 10000);
|
||||
}
|
||||
|
||||
/// Duty to level 50.
|
||||
#[test]
|
||||
fn duty_to_level_50() {
|
||||
assert_eq!(duty_to_level(50, 9999), 5000);
|
||||
}
|
||||
|
||||
/// Duty to level clamped.
|
||||
#[test]
|
||||
fn duty_to_level_clamped() {
|
||||
assert_eq!(duty_to_level(200, 9999), 10000);
|
||||
}
|
||||
|
||||
/// Duty to level 5.
|
||||
#[test]
|
||||
fn duty_to_level_5() {
|
||||
assert_eq!(duty_to_level(5, 9999), 500);
|
||||
|
||||
Reference in New Issue
Block a user