Fixed Rust drivers

This commit is contained in:
Kevin Thomas
2026-04-02 11:36:06 -04:00
parent 2792583302
commit 9834d5c96c
245 changed files with 1110 additions and 650 deletions
+20
View File
@@ -382,4 +382,24 @@ pub(crate) fn poll_sensor(
delay.delay_ms(POLL_MS);
}
/// Initialise all peripherals and run the DHT11 sensor demo.
///
/// # Arguments
///
/// * `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 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);
#[cfg(rp2350)]
let timer = hal::Timer::new_timer0(pac.TIMER0, &mut pac.RESETS, &clocks);
#[cfg(rp2040)]
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); }
}
// End of file
+25 -21
View File
@@ -111,21 +111,20 @@ pub fn parse_temperature(data: &[u8; 5]) -> f32 {
///
/// Number of bytes written into the buffer.
pub fn format_reading(buf: &mut [u8], humidity: f32, temperature: f32) -> usize {
let mut pos = 0;
let prefix = b"Humidity: ";
buf[pos..pos + prefix.len()].copy_from_slice(prefix);
pos += prefix.len();
let mut pos = copy_slice(buf, 0, b"Humidity: ");
pos += format_f32_1(&mut buf[pos..], humidity);
let mid = b"% Temperature: ";
buf[pos..pos + mid.len()].copy_from_slice(mid);
pos += mid.len();
pos += copy_slice(buf, pos, b"% Temperature: ");
pos += format_f32_1(&mut buf[pos..], temperature);
let suffix = b" C";
buf[pos..pos + suffix.len()].copy_from_slice(suffix);
pos += suffix.len();
pos += copy_slice(buf, pos, b" C");
pos
}
/// Copy a byte slice into `buf` at the given offset, returning bytes written.
fn copy_slice(buf: &mut [u8], offset: usize, src: &[u8]) -> usize {
buf[offset..offset + src.len()].copy_from_slice(src);
src.len()
}
/// Format a failed-read error message.
///
/// # Arguments
@@ -185,17 +184,7 @@ 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 = 0;
if integer >= 100 {
buf[pos] = b'0' + (integer / 100) as u8;
pos += 1;
}
if integer >= 10 {
buf[pos] = b'0' + ((integer / 10) % 10) as u8;
pos += 1;
}
buf[pos] = b'0' + (integer % 10) as u8;
pos += 1;
let mut pos = format_u32_minimal(buf, integer);
buf[pos] = b'.';
pos += 1;
buf[pos] = b'0' + frac;
@@ -203,6 +192,21 @@ fn format_f32_1(buf: &mut [u8], val: f32) -> usize {
pos
}
/// 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;
}
buf[pos] = b'0' + (value % 10) as u8;
pos + 1
}
#[cfg(test)]
mod tests {
// Import all parent module items
+1 -24
View File
@@ -78,32 +78,9 @@ pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe();
/// Application entry point for the DHT11 sensor demo.
///
/// Initializes the DHT11 on GPIO4 and continuously reads temperature
/// and humidity, printing results over UART every 2 seconds.
///
/// # Returns
///
/// Does not return.
#[entry]
fn main() -> ! {
let mut pac = hal::pac::Peripherals::take().unwrap();
let clocks = board::init_clocks(
pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS,
&mut hal::Watchdog::new(pac.WATCHDOG),
);
let pins = board::init_pins(pac.IO_BANK0, pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
let uart = board::init_uart(pac.UART0, pins.gpio0, pins.gpio1, &mut pac.RESETS, &clocks);
let mut delay = board::init_delay(&clocks);
#[cfg(rp2350)]
let timer = hal::Timer::new_timer0(pac.TIMER0, &mut pac.RESETS, &clocks);
#[cfg(rp2040)]
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS);
let _dht_pin = pins.gpio4.into_pull_up_input();
uart.write_full_blocking(b"DHT11 driver initialized on GPIO 4\r\n");
loop {
board::poll_sensor(&uart, &timer, &mut delay);
}
board::run(hal::pac::Peripherals::take().unwrap())
}
// Picotool binary info metadata