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
+33 -1
View File
@@ -216,4 +216,36 @@ pub(crate) fn poll_receiver(
uart.write_full_blocking(&buf[..len]);
}
delay.delay_ms(POLL_MS);
}
}
/// Initialise all peripherals and run the NEC IR receiver 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.gpio5.into_pull_up_input();
announce_ir(&uart);
loop { poll_receiver(&uart, &timer, &mut delay); }
}
/// Print the IR driver initialisation banner over UART.
///
/// # Arguments
///
/// * `uart` - Reference to the enabled UART peripheral for serial output.
fn announce_ir(uart: &EnabledUart) {
uart.write_full_blocking(b"NEC IR driver initialized on GPIO 5\r\n");
uart.write_full_blocking(b"Press a button on your NEC remote...\r\n");
}
// End of file
+24 -25
View File
@@ -98,39 +98,38 @@ pub fn validate_nec_frame(data: &[u8; 4]) -> Option<u8> {
/// Format the decoded command as hexadecimal and decimal followed by CRLF.
pub fn format_command(buf: &mut [u8], command: u8) -> usize {
let mut pos = 0;
let prefix = b"NEC command: 0x";
buf[pos..pos + prefix.len()].copy_from_slice(prefix);
pos += prefix.len();
let mut pos = copy_slice(buf, 0, prefix);
pos += format_hex_u8(buf, pos, command);
let middle = b" (";
buf[pos..pos + middle.len()].copy_from_slice(middle);
pos += middle.len();
pos += copy_slice(buf, pos, b" (");
pos += format_u8(buf, pos, command);
buf[pos] = b')';
pos += 1;
buf[pos] = b'\r';
pos += 1;
buf[pos] = b'\n';
pos += 1;
pos += copy_slice(buf, pos, b")\r\n");
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 an unsigned 8-bit integer at the given buffer offset.
fn format_u8(buf: &mut [u8], pos: usize, value: u8) -> usize {
if value >= 100 {
buf[pos] = b'0' + value / 100;
buf[pos + 1] = b'0' + (value / 10) % 10;
buf[pos + 2] = b'0' + value % 10;
3
} else if value >= 10 {
buf[pos] = b'0' + value / 10;
buf[pos + 1] = b'0' + value % 10;
2
} else {
buf[pos] = b'0' + value;
1
}
let n = u8_digit_count(value);
write_u8_digits(buf, pos, value, n);
n
}
/// Return the number of decimal digits in a u8.
fn u8_digit_count(value: u8) -> usize {
if value >= 100 { 3 } else if value >= 10 { 2 } else { 1 }
}
/// Write the decimal digits of a u8 into `buf` at `pos`.
fn write_u8_digits(buf: &mut [u8], pos: usize, value: u8, n: usize) {
if n >= 3 { buf[pos] = b'0' + value / 100; }
if n >= 2 { buf[pos + n - 2] = b'0' + (value / 10) % 10; }
buf[pos + n - 1] = b'0' + value % 10;
}
/// Format an unsigned 8-bit integer as two uppercase hexadecimal digits.
+1 -18
View File
@@ -79,24 +79,7 @@ pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe();
/// Application entry point for the NEC IR receiver demo.
#[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 _ir_pin = pins.gpio5.into_pull_up_input();
uart.write_full_blocking(b"NEC IR driver initialized on GPIO 5\r\n");
uart.write_full_blocking(b"Press a button on your NEC remote...\r\n");
loop {
board::poll_receiver(&uart, &timer, &mut delay);
}
board::run(hal::pac::Peripherals::take().unwrap())
}
// Picotool binary info metadata