mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-27 01:32:25 +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;
|
||||
|
||||
/// Timer device type for the HAL timer peripheral.
|
||||
#[cfg(rp2350)]
|
||||
@@ -103,7 +103,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()
|
||||
}
|
||||
@@ -182,6 +188,17 @@ 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())
|
||||
}
|
||||
|
||||
/// Set or clear the DHT11 GPIO output level via SIO registers.
|
||||
fn sio_set_level(sio: &hal::pac::sio::RegisterBlock, high: bool) {
|
||||
if high {
|
||||
sio.gpio_out_set()
|
||||
.write(|w| unsafe { w.bits(1u32 << DHT11_GPIO) });
|
||||
} else {
|
||||
sio.gpio_out_clr()
|
||||
.write(|w| unsafe { w.bits(1u32 << DHT11_GPIO) });
|
||||
}
|
||||
}
|
||||
|
||||
/// Drive the DHT11 data pin to a given level (enables output).
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -190,11 +207,7 @@ pub(crate) fn init_delay(clocks: &hal::clocks::ClocksManager) -> cortex_m::delay
|
||||
fn gpio_drive(high: bool) {
|
||||
unsafe {
|
||||
let sio = &*hal::pac::SIO::PTR;
|
||||
if high {
|
||||
sio.gpio_out_set().write(|w| w.bits(1u32 << DHT11_GPIO));
|
||||
} else {
|
||||
sio.gpio_out_clr().write(|w| w.bits(1u32 << DHT11_GPIO));
|
||||
}
|
||||
sio_set_level(sio, high);
|
||||
sio.gpio_oe_set().write(|w| w.bits(1u32 << DHT11_GPIO));
|
||||
}
|
||||
}
|
||||
@@ -277,6 +290,26 @@ fn wait_response() -> bool {
|
||||
wait_for_level(true) && wait_for_level(false) && wait_for_level(true)
|
||||
}
|
||||
|
||||
/// Measure the high-period duration for a single DHT11 bit.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `timer` - Reference to the HAL timer for microsecond measurement.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Duration in microseconds, or `None` on timeout.
|
||||
fn measure_bit_duration(timer: &HalTimer) -> Option<u32> {
|
||||
if !wait_for_level(false) {
|
||||
return None;
|
||||
}
|
||||
let start = time_us_32(timer);
|
||||
if !wait_for_level(true) {
|
||||
return None;
|
||||
}
|
||||
Some(time_us_32(timer).wrapping_sub(start))
|
||||
}
|
||||
|
||||
/// Read a single bit from the DHT11 data stream.
|
||||
///
|
||||
/// Waits for the low-period to end, measures the high-period duration,
|
||||
@@ -293,14 +326,9 @@ fn wait_response() -> bool {
|
||||
///
|
||||
/// `true` on success, `false` on timeout.
|
||||
fn read_bit(data: &mut [u8; 5], i: usize, timer: &HalTimer) -> bool {
|
||||
if !wait_for_level(false) {
|
||||
let Some(duration) = measure_bit_duration(timer) else {
|
||||
return false;
|
||||
}
|
||||
let start = time_us_32(timer);
|
||||
if !wait_for_level(true) {
|
||||
return false;
|
||||
}
|
||||
let duration = time_us_32(timer).wrapping_sub(start);
|
||||
};
|
||||
dht11::accumulate_bit(data, i, duration);
|
||||
true
|
||||
}
|
||||
@@ -324,6 +352,12 @@ fn read_40_bits(data: &mut [u8; 5], timer: &HalTimer) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// Run the full DHT11 acquisition sequence (start, response, 40 bits, checksum).
|
||||
fn acquire_data(timer: &HalTimer, delay: &mut cortex_m::delay::Delay, data: &mut [u8; 5]) -> bool {
|
||||
send_start_signal(delay);
|
||||
wait_response() && read_40_bits(data, timer) && dht11::validate_checksum(data)
|
||||
}
|
||||
|
||||
/// Execute the full DHT11 read protocol.
|
||||
///
|
||||
/// Sends the start signal, waits for the sensor response, reads 40 bits
|
||||
@@ -342,17 +376,24 @@ pub(crate) fn read_sensor(
|
||||
delay: &mut cortex_m::delay::Delay,
|
||||
) -> Option<(f32, f32)> {
|
||||
let mut data = [0u8; 5];
|
||||
send_start_signal(delay);
|
||||
if !wait_response() {
|
||||
if !acquire_data(timer, delay, &mut data) {
|
||||
return None;
|
||||
}
|
||||
if !read_40_bits(&mut data, timer) {
|
||||
return None;
|
||||
}
|
||||
if !dht11::validate_checksum(&data) {
|
||||
return None;
|
||||
}
|
||||
Some((dht11::parse_humidity(&data), dht11::parse_temperature(&data)))
|
||||
Some((
|
||||
dht11::parse_humidity(&data),
|
||||
dht11::parse_temperature(&data),
|
||||
))
|
||||
}
|
||||
|
||||
/// Format the sensor result (or error) with CRLF into `buf`.
|
||||
fn format_sensor_output(buf: &mut [u8], result: Option<(f32, f32)>) -> usize {
|
||||
let n = match result {
|
||||
Some((h, t)) => dht11::format_reading(buf, h, t),
|
||||
None => dht11::format_error(buf, DHT11_GPIO),
|
||||
};
|
||||
buf[n] = b'\r';
|
||||
buf[n + 1] = b'\n';
|
||||
n + 2
|
||||
}
|
||||
|
||||
/// Read the sensor, format the result, write it over UART, and wait.
|
||||
@@ -372,13 +413,9 @@ pub(crate) fn poll_sensor(
|
||||
delay: &mut cortex_m::delay::Delay,
|
||||
) {
|
||||
let mut buf = [0u8; 64];
|
||||
let n = match read_sensor(timer, delay) {
|
||||
Some((h, t)) => dht11::format_reading(&mut buf, h, t),
|
||||
None => dht11::format_error(&mut buf, DHT11_GPIO),
|
||||
};
|
||||
buf[n] = b'\r';
|
||||
buf[n + 1] = b'\n';
|
||||
uart.write_full_blocking(&buf[..n + 2]);
|
||||
let result = read_sensor(timer, delay);
|
||||
let n = format_sensor_output(&mut buf, result);
|
||||
uart.write_full_blocking(&buf[..n]);
|
||||
delay.delay_ms(POLL_MS);
|
||||
}
|
||||
|
||||
@@ -389,7 +426,14 @@ pub(crate) fn poll_sensor(
|
||||
/// * `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);
|
||||
@@ -399,7 +443,9 @@ pub(crate) fn run(mut pac: hal::pac::Peripherals) -> ! {
|
||||
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS);
|
||||
let _ = pins.gpio4.into_pull_up_input();
|
||||
uart.write_full_blocking(b"DHT11 driver initialized on GPIO 4\r\n");
|
||||
loop { poll_sensor(&uart, &timer, &mut delay); }
|
||||
loop {
|
||||
poll_sensor(&uart, &timer, &mut delay);
|
||||
}
|
||||
}
|
||||
|
||||
// End of file
|
||||
|
||||
@@ -144,6 +144,18 @@ pub fn format_error(buf: &mut [u8], gpio: u8) -> usize {
|
||||
pos
|
||||
}
|
||||
|
||||
/// Return the number of decimal digits needed for a `u8`.
|
||||
fn u8_digit_count(val: u8) -> usize {
|
||||
if val >= 100 { 3 } else if val >= 10 { 2 } else { 1 }
|
||||
}
|
||||
|
||||
/// Write `count` decimal digits of `val` into `buf`.
|
||||
fn write_u8_digits(buf: &mut [u8], val: u8, count: usize) {
|
||||
if count >= 3 { buf[0] = b'0' + val / 100; }
|
||||
if count >= 2 { buf[count - 2] = b'0' + (val / 10) % 10; }
|
||||
buf[count - 1] = b'0' + val % 10;
|
||||
}
|
||||
|
||||
/// Format a `u8` as decimal ASCII digits.
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -155,19 +167,9 @@ pub fn format_error(buf: &mut [u8], gpio: u8) -> usize {
|
||||
///
|
||||
/// Number of bytes written.
|
||||
fn format_u8(buf: &mut [u8], val: u8) -> usize {
|
||||
if val >= 100 {
|
||||
buf[0] = b'0' + val / 100;
|
||||
buf[1] = b'0' + (val / 10) % 10;
|
||||
buf[2] = b'0' + val % 10;
|
||||
3
|
||||
} else if val >= 10 {
|
||||
buf[0] = b'0' + val / 10;
|
||||
buf[1] = b'0' + val % 10;
|
||||
2
|
||||
} else {
|
||||
buf[0] = b'0' + val;
|
||||
1
|
||||
}
|
||||
let n = u8_digit_count(val);
|
||||
write_u8_digits(buf, val, n);
|
||||
n
|
||||
}
|
||||
|
||||
/// Format an `f32` with one decimal place (e.g. `"25.3"`).
|
||||
@@ -182,27 +184,26 @@ fn format_u8(buf: &mut [u8], val: u8) -> usize {
|
||||
/// Number of bytes written.
|
||||
fn format_f32_1(buf: &mut [u8], val: f32) -> usize {
|
||||
let scaled = (val * 10.0) as u32;
|
||||
let integer = scaled / 10;
|
||||
let frac = (scaled % 10) as u8;
|
||||
let mut pos = format_u32_minimal(buf, integer);
|
||||
let pos = format_u32_minimal(buf, scaled / 10);
|
||||
buf[pos] = b'.';
|
||||
pos += 1;
|
||||
buf[pos] = b'0' + frac;
|
||||
pos += 1;
|
||||
pos
|
||||
buf[pos + 1] = b'0' + frac;
|
||||
pos + 2
|
||||
}
|
||||
|
||||
/// Write a conditional digit into `buf` if `val` meets the threshold.
|
||||
fn write_digit_if(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;
|
||||
}
|
||||
}
|
||||
|
||||
/// Format a u32 as minimal decimal digits (no leading zeros).
|
||||
fn format_u32_minimal(buf: &mut [u8], value: u32) -> usize {
|
||||
let mut pos = 0;
|
||||
if value >= 100 {
|
||||
buf[pos] = b'0' + (value / 100) as u8;
|
||||
pos += 1;
|
||||
}
|
||||
if value >= 10 {
|
||||
buf[pos] = b'0' + ((value / 10) % 10) as u8;
|
||||
pos += 1;
|
||||
}
|
||||
write_digit_if(buf, &mut pos, value, 100, 100);
|
||||
write_digit_if(buf, &mut pos, value, 10, 10);
|
||||
buf[pos] = b'0' + (value % 10) as u8;
|
||||
pos + 1
|
||||
}
|
||||
@@ -212,6 +213,7 @@ mod tests {
|
||||
// Import all parent module items
|
||||
use super::*;
|
||||
|
||||
/// Accumulate bit zero short pulse.
|
||||
#[test]
|
||||
fn accumulate_bit_zero_short_pulse() {
|
||||
let mut data = [0u8; 5];
|
||||
@@ -219,6 +221,7 @@ mod tests {
|
||||
assert_eq!(data[0], 0);
|
||||
}
|
||||
|
||||
/// Accumulate bit one long pulse.
|
||||
#[test]
|
||||
fn accumulate_bit_one_long_pulse() {
|
||||
let mut data = [0u8; 5];
|
||||
@@ -226,6 +229,7 @@ mod tests {
|
||||
assert_eq!(data[0], 1);
|
||||
}
|
||||
|
||||
/// Accumulate bit threshold exact.
|
||||
#[test]
|
||||
fn accumulate_bit_threshold_exact() {
|
||||
let mut data = [0u8; 5];
|
||||
@@ -233,6 +237,7 @@ mod tests {
|
||||
assert_eq!(data[0], 0);
|
||||
}
|
||||
|
||||
/// Accumulate bit two bits.
|
||||
#[test]
|
||||
fn accumulate_bit_two_bits() {
|
||||
let mut data = [0u8; 5];
|
||||
@@ -241,6 +246,7 @@ mod tests {
|
||||
assert_eq!(data[0], 0b10);
|
||||
}
|
||||
|
||||
/// Accumulate bit crosses byte.
|
||||
#[test]
|
||||
fn accumulate_bit_crosses_byte() {
|
||||
let mut data = [0u8; 5];
|
||||
@@ -250,24 +256,28 @@ mod tests {
|
||||
assert_eq!(data[1], 1);
|
||||
}
|
||||
|
||||
/// Validate checksum valid.
|
||||
#[test]
|
||||
fn validate_checksum_valid() {
|
||||
let data = [0x28, 0x00, 0x1A, 0x00, 0x42];
|
||||
assert!(validate_checksum(&data));
|
||||
}
|
||||
|
||||
/// Validate checksum invalid.
|
||||
#[test]
|
||||
fn validate_checksum_invalid() {
|
||||
let data = [0x28, 0x00, 0x1A, 0x00, 0xFF];
|
||||
assert!(!validate_checksum(&data));
|
||||
}
|
||||
|
||||
/// Validate checksum wraps.
|
||||
#[test]
|
||||
fn validate_checksum_wraps() {
|
||||
let data = [0xFF, 0x01, 0x00, 0x00, 0x00];
|
||||
assert!(validate_checksum(&data));
|
||||
}
|
||||
|
||||
/// Parse humidity integer only.
|
||||
#[test]
|
||||
fn parse_humidity_integer_only() {
|
||||
let data = [0x41, 0x00, 0x00, 0x00, 0x41];
|
||||
@@ -275,6 +285,7 @@ mod tests {
|
||||
assert!((h - 65.0).abs() < 0.01);
|
||||
}
|
||||
|
||||
/// Parse humidity with fraction.
|
||||
#[test]
|
||||
fn parse_humidity_with_fraction() {
|
||||
let data = [0x41, 0x03, 0x00, 0x00, 0x44];
|
||||
@@ -282,6 +293,7 @@ mod tests {
|
||||
assert!((h - 65.3).abs() < 0.01);
|
||||
}
|
||||
|
||||
/// Parse temperature integer only.
|
||||
#[test]
|
||||
fn parse_temperature_integer_only() {
|
||||
let data = [0x00, 0x00, 0x19, 0x00, 0x19];
|
||||
@@ -289,6 +301,7 @@ mod tests {
|
||||
assert!((t - 25.0).abs() < 0.01);
|
||||
}
|
||||
|
||||
/// Parse temperature with fraction.
|
||||
#[test]
|
||||
fn parse_temperature_with_fraction() {
|
||||
let data = [0x00, 0x00, 0x19, 0x05, 0x1E];
|
||||
@@ -296,6 +309,7 @@ mod tests {
|
||||
assert!((t - 25.5).abs() < 0.01);
|
||||
}
|
||||
|
||||
/// Format reading typical.
|
||||
#[test]
|
||||
fn format_reading_typical() {
|
||||
let mut buf = [0u8; 48];
|
||||
@@ -303,6 +317,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"Humidity: 65.0% Temperature: 25.0 C");
|
||||
}
|
||||
|
||||
/// Format reading with fractions.
|
||||
#[test]
|
||||
fn format_reading_with_fractions() {
|
||||
let mut buf = [0u8; 48];
|
||||
@@ -310,6 +325,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"Humidity: 65.3% Temperature: 25.5 C");
|
||||
}
|
||||
|
||||
/// Format error gpio4.
|
||||
#[test]
|
||||
fn format_error_gpio4() {
|
||||
let mut buf = [0u8; 48];
|
||||
@@ -317,6 +333,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"DHT11 read failed - check wiring on GPIO 4");
|
||||
}
|
||||
|
||||
/// Format error gpio15.
|
||||
#[test]
|
||||
fn format_error_gpio15() {
|
||||
let mut buf = [0u8; 48];
|
||||
@@ -324,6 +341,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"DHT11 read failed - check wiring on GPIO 15");
|
||||
}
|
||||
|
||||
/// Format u8 single digit.
|
||||
#[test]
|
||||
fn format_u8_single_digit() {
|
||||
let mut buf = [0u8; 4];
|
||||
@@ -331,6 +349,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"4");
|
||||
}
|
||||
|
||||
/// Format u8 two digits.
|
||||
#[test]
|
||||
fn format_u8_two_digits() {
|
||||
let mut buf = [0u8; 4];
|
||||
@@ -338,6 +357,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"15");
|
||||
}
|
||||
|
||||
/// Format u8 three digits.
|
||||
#[test]
|
||||
fn format_u8_three_digits() {
|
||||
let mut buf = [0u8; 4];
|
||||
|
||||
@@ -65,13 +65,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)]
|
||||
@@ -83,7 +83,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