Initial commit with strict idiomatic rust enforcement

This commit is contained in:
Kevin Thomas
2023-10-06 14:27:20 -04:00
commit 8b9d2d93a1
1232 changed files with 114453 additions and 0 deletions
+491
View File
@@ -0,0 +1,491 @@
//! Implementation module
//!
//! **File:** `board.rs`
//! **Author:** Kevin Thomas
//! **Date:** 2025
//!
//! MIT License
//!
//! Copyright (c) 2025 Kevin Thomas
//!
//! Permission is hereby granted, free of charge, to any person obtaining a copy
//! of this software and associated documentation files (the "Software"), to deal
//! in the Software without restriction, including without limitation the rights
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//! copies of the Software, and to permit persons to whom the Software is
//! furnished to do so, subject to the following conditions:
//!
//! The above copyright notice and this permission notice shall be included in
//! all copies or substantial portions of the Software.
//!
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//! SOFTWARE.
// IR pure-logic functions and timing constants
use ir_lib::ir;
// Rate extension trait for .Hz() baud rate construction
use fugit::RateExtU32;
// Clock trait for accessing system clock frequency
use hal::Clock;
// GPIO pin types and function selectors
use hal::gpio::{FunctionNull, FunctionUart, Pin, PullDown, PullNone};
// UART configuration and peripheral types
use hal::uart::{DataBits, Enabled, StopBits, UartConfig, UartPeripheral};
// Alias our HAL crate
#[cfg(rp2350)]
// Import rp235x_hal as hal
use rp235x_hal as hal;
#[cfg(rp2040)]
// Import rp2040_hal as hal
use rp2040_hal as hal;
/// Timer device type for the HAL timer peripheral.
#[cfg(rp2350)]
pub(crate) type HalTimer = hal::Timer<hal::timer::CopyableTimer0>;
/// Timer type alias for RP2040 (non-generic).
#[cfg(rp2040)]
pub(crate) type HalTimer = hal::Timer;
/// External crystal frequency in Hz (12 MHz).
pub(crate) const XTAL_FREQ_HZ: u32 = 12_000_000u32;
/// UART baud rate in bits per second.
pub(crate) const UART_BAUD: u32 = 115_200;
/// GPIO pin number connected to the IR receiver output.
pub(crate) const IR_GPIO: u8 = 5;
/// Delay between decode attempts in milliseconds.
pub(crate) const POLL_MS: u32 = 10;
/// Type alias for the configured TX pin (GPIO 0, UART function, no pull).
pub(crate) type TxPin = Pin<hal::gpio::bank0::Gpio0, FunctionUart, PullNone>;
/// Type alias for the configured RX pin (GPIO 1, UART function, no pull).
pub(crate) type RxPin = Pin<hal::gpio::bank0::Gpio1, FunctionUart, PullNone>;
/// Type alias for the default TX pin state from `Pins::new()`.
pub(crate) type TxPinDefault = Pin<hal::gpio::bank0::Gpio0, FunctionNull, PullDown>;
/// Type alias for the default RX pin state from `Pins::new()`.
pub(crate) type RxPinDefault = Pin<hal::gpio::bank0::Gpio1, FunctionNull, PullDown>;
/// Type alias for the fully-enabled UART0 peripheral with TX/RX p.
pub(crate) type EnabledUart = UartPeripheral<Enabled, hal::pac::UART0, (TxPin, RxPin)>;
/// Initialise system clocks and PLLs from the external 12 MHz crystal.
pub(crate) fn init_clocks(
xosc: hal::pac::XOSC,
clocks: hal::pac::CLOCKS,
pll_sys: hal::pac::PLL_SYS,
pll_usb: hal::pac::PLL_USB,
resets: &mut hal::pac::RESETS,
watchdog: &mut hal::Watchdog,
) -> hal::clocks::ClocksManager {
hal::clocks::init_clocks_and_plls(
XTAL_FREQ_HZ,
xosc,
clocks,
pll_sys,
pll_usb,
resets,
watchdog,
)
.unwrap()
}
/// Unlock the GPIO bank and return the pin set.
pub(crate) fn init_pins(
io_bank0: hal::pac::IO_BANK0,
pads_bank0: hal::pac::PADS_BANK0,
sio: hal::pac::SIO,
resets: &mut hal::pac::RESETS,
) -> hal::gpio::Pins {
let sio = hal::Sio::new(sio);
hal::gpio::Pins::new(io_bank0, pads_bank0, sio.gpio_bank0, resets)
}
/// Initialise UART0 for serial output.
pub(crate) fn init_uart(
uart0: hal::pac::UART0,
tx_pin: TxPinDefault,
rx_pin: RxPinDefault,
resets: &mut hal::pac::RESETS,
clocks: &hal::clocks::ClocksManager,
) -> EnabledUart {
let pins = (
tx_pin.reconfigure::<FunctionUart, PullNone>(),
rx_pin.reconfigure::<FunctionUart, PullNone>(),
);
let cfg = UartConfig::new(UART_BAUD.Hz(), DataBits::Eight, None, StopBits::One);
UartPeripheral::new(uart0, pins, resets)
.enable(cfg, clocks.peripheral_clock.freq())
.unwrap()
}
/// Create a blocking delay timer from the ARM SysTick peripheral.
///
/// # Arguments
///
/// * `clocks` - The `clocks` parameter.
///
/// # Returns
///
/// A value of type `cortex_m::delay::Delay`.
///
/// # Arguments
///
/// * `clocks` - The `clocks` parameter.
///
/// # Returns
///
/// A value of type `cortex_m::delay::Delay`.
pub(crate) fn init_delay(clocks: &hal::clocks::ClocksManager) -> cortex_m::delay::Delay {
let core = cortex_m::Peripherals::take().unwrap();
cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz())
}
/// Read the free-running microsecond timer (lower 32 bits).
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// A 32-bit unsigned integer value.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// A 32-bit unsigned integer value.
fn time_us_32(timer: &HalTimer) -> u32 {
timer.get_counter().ticks() as u32
}
/// Read the current logic level of the IR input pin through the SIO block.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
fn gpio_read() -> bool {
unsafe { (*hal::pac::SIO::PTR).gpio_in().read().bits() & (1u32 << IR_GPIO) != 0 }
}
/// Wait for the IR pin to reach the requested level or time out.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
/// * `level` - The `level` parameter.
/// * `timeout_us` - The `timeout_us` parameter.
///
/// # Returns
///
/// A value of type `Option<i64>`.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
/// * `level` - The `level` parameter.
/// * `timeout_us` - The `timeout_us` parameter.
///
/// # Returns
///
/// An Optional value.
fn wait_for_level(timer: &HalTimer, level: bool, timeout_us: u32) -> Option<i64> {
let start = time_us_32(timer);
while gpio_read() != level {
let elapsed = time_us_32(timer).wrapping_sub(start) as i64;
if elapsed > timeout_us as i64 {
return None;
}
}
Some(time_us_32(timer).wrapping_sub(start) as i64)
}
/// Wait for the IR receiver to go idle (LOW).
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
fn wait_for_idle(timer: &HalTimer) -> bool {
wait_for_level(timer, false, ir::LEADER_START_TIMEOUT_US).is_some()
}
/// Validate the NEC leader mark pulse width.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
fn validate_leader_mark(timer: &HalTimer) -> bool {
let Some(w) = wait_for_level(timer, true, ir::LEADER_MARK_TIMEOUT_US) else {
return false;
};
ir::is_valid_leader_mark(w)
}
/// Validate the NEC leader space width.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
fn validate_leader_space(timer: &HalTimer) -> bool {
let Some(w) = wait_for_level(timer, false, ir::LEADER_SPACE_TIMEOUT_US) else {
return false;
};
ir::is_valid_leader_space(w)
}
/// Wait for the NEC leader burst and space.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
fn wait_leader(timer: &HalTimer) -> bool {
wait_for_idle(timer) && validate_leader_mark(timer) && validate_leader_space(timer)
}
/// Wait for the bit mark and measure the bit space width.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// A value of type `Option<i64>`.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// An Optional value.
fn measure_bit_space(timer: &HalTimer) -> Option<i64> {
if wait_for_level(timer, true, ir::BIT_MARK_TIMEOUT_US).is_none() {
return None;
}
let w = wait_for_level(timer, false, ir::BIT_SPACE_TIMEOUT_US)?;
if !ir::is_valid_bit_space(w) {
return None;
}
Some(w)
}
/// Read one NEC bit and store it in the frame buffer.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
/// * `data` - Data to send/write.
/// * `bit_index` - The `bit_index` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
/// * `data` - Data to send/write.
/// * `bit_index` - The `bit_index` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
fn read_nec_bit(timer: &HalTimer, data: &mut [u8; 4], bit_index: usize) -> bool {
let Some(space_width) = measure_bit_space(timer) else {
return false;
};
ir::accumulate_nec_bit(data, bit_index, space_width);
true
}
/// Read a full 32-bit NEC frame.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
/// * `data` - Data to send/write.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
/// * `data` - Data to send/write.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
fn read_32_bits(timer: &HalTimer, data: &mut [u8; 4]) -> bool {
let mut bit_index = 0usize;
while bit_index < ir::FRAME_BITS {
if !read_nec_bit(timer, data, bit_index) {
return false;
}
bit_index += 1;
}
true
}
/// Block until an NEC key is decoded or return `None` on failure.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// A value of type `Option<u8>`.
///
/// # Arguments
///
/// * `timer` - The `timer` parameter.
///
/// # Returns
///
/// An Optional value.
pub(crate) fn ir_getkey(timer: &HalTimer) -> Option<u8> {
if !wait_leader(timer) {
return None;
}
let mut data = [0u8; 4];
if !read_32_bits(timer, &mut data) {
return None;
}
ir::validate_nec_frame(&data)
}
/// Poll the decoder and print the key code when a valid frame is received.
pub(crate) fn poll_receiver(
uart: &EnabledUart,
timer: &HalTimer,
delay: &mut cortex_m::delay::Delay,
) {
let mut buf = [0u8; 26];
if let Some(command) = ir_getkey(timer) {
let len = ir::format_command(&mut buf, command);
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).
///
/// # Returns
///
/// A value of type `!`.
///
/// # Arguments
///
/// * `pac` - The `pac` parameter.
///
/// # Returns
///
/// A value of type `!`.
pub(crate) fn run(mut pac: hal::pac::Peripherals) -> ! {
let clocks = init_clocks(
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut hal::Watchdog::new(pac.WATCHDOG),
);
let p = init_pins(pac.IO_BANK0, pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
let uart = init_uart(pac.UART0, p.gpio0, p.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 _ = p.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.
///
/// # Arguments
///
/// * `uart` - The `uart` parameter.
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");
}
+463
View File
@@ -0,0 +1,463 @@
//! Implementation module
//!
//! **File:** `ir.rs`
//! **Author:** Kevin Thomas
//! **Date:** 2025
//!
//! MIT License
//!
//! Copyright (c) 2025 Kevin Thomas
//!
//! Permission is hereby granted, free of charge, to any person obtaining a copy
//! of this software and associated documentation files (the "Software"), to deal
//! in the Software without restriction, including without limitation the rights
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//! copies of the Software, and to permit persons to whom the Software is
//! furnished to do so, subject to the following conditions:
//!
//! The above copyright notice and this permission notice shall be included in
//! all copies or substantial portions of the Software.
//!
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//! SOFTWARE.
/// Leader wait timeout in microseconds.
pub const LEADER_START_TIMEOUT_US: u32 = 150_000;
/// Maximum duration accepted for the NEC leader mark wait.
pub const LEADER_MARK_TIMEOUT_US: u32 = 12_000;
/// Minimum valid NEC leader mark width in microseconds.
pub const LEADER_MARK_MIN_US: i64 = 8_000;
/// Maximum valid NEC leader mark width in microseconds.
pub const LEADER_MARK_MAX_US: i64 = 10_000;
/// Maximum duration accepted for the NEC leader space wait.
pub const LEADER_SPACE_TIMEOUT_US: u32 = 7_000;
/// Minimum valid NEC leader space width in microseconds.
pub const LEADER_SPACE_MIN_US: i64 = 3_500;
/// Maximum valid NEC leader space width in microseconds.
pub const LEADER_SPACE_MAX_US: i64 = 5_000;
/// Maximum duration accepted while waiting for the bit mark to end.
pub const BIT_MARK_TIMEOUT_US: u32 = 1_000;
/// Maximum duration accepted while measuring the data space.
pub const BIT_SPACE_TIMEOUT_US: u32 = 2_500;
/// Minimum valid data space width in microseconds.
pub const BIT_SPACE_MIN_US: i64 = 200;
/// Space width above which a NEC bit is interpreted as logical 1.
pub const BIT_ONE_THRESHOLD_US: i64 = 1_200;
/// Total number of bits in an NEC frame.
pub const FRAME_BITS: usize = 32;
/// Return true if the measured NEC leader mark width is valid.
///
/// # Arguments
///
/// * `duration_us` - The `duration_us` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `duration_us` - The `duration_us` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
pub fn is_valid_leader_mark(duration_us: i64) -> bool {
(LEADER_MARK_MIN_US..=LEADER_MARK_MAX_US).contains(&duration_us)
}
/// Return true if the measured NEC leader space width is valid.
///
/// # Arguments
///
/// * `duration_us` - The `duration_us` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `duration_us` - The `duration_us` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
pub fn is_valid_leader_space(duration_us: i64) -> bool {
(LEADER_SPACE_MIN_US..=LEADER_SPACE_MAX_US).contains(&duration_us)
}
/// Return true if the measured NEC bit space width is valid.
///
/// # Arguments
///
/// * `duration_us` - The `duration_us` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
///
/// # Arguments
///
/// * `duration_us` - The `duration_us` parameter.
///
/// # Returns
///
/// `true` if successful or set, `false` otherwise.
pub fn is_valid_bit_space(duration_us: i64) -> bool {
duration_us >= BIT_SPACE_MIN_US
}
/// Accumulate a single NEC bit into the 4-byte frame buffer.
///
/// Matches the C implementation exactly: bytes are filled LSB-first.
///
/// # Arguments
///
/// * `data` - Data to send/write.
/// * `bit_index` - The `bit_index` parameter.
/// * `duration_us` - The `duration_us` parameter.
///
/// # Arguments
///
/// * `data` - Data to send/write.
/// * `bit_index` - The `bit_index` parameter.
/// * `duration_us` - The `duration_us` parameter.
pub fn accumulate_nec_bit(data: &mut [u8; 4], bit_index: usize, duration_us: i64) {
let byte_idx = bit_index / 8;
let bit_idx = bit_index % 8;
if duration_us > BIT_ONE_THRESHOLD_US {
data[byte_idx] |= 1u8 << bit_idx;
}
}
/// Validate an NEC frame and return the command byte on success.
///
/// # Arguments
///
/// * `data` - Data to send/write.
///
/// # Returns
///
/// A value of type `Option<u8>`.
///
/// # Arguments
///
/// * `data` - Data to send/write.
///
/// # Returns
///
/// An Optional value.
pub fn validate_nec_frame(data: &[u8; 4]) -> Option<u8> {
if data[0].wrapping_add(data[1]) == 0xFF && data[2].wrapping_add(data[3]) == 0xFF {
Some(data[2])
} else {
None
}
}
/// Format the decoded command as hexadecimal and decimal followed by CRLF.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `command` - The `command` parameter.
///
/// # Returns
///
/// A value of type `usize`.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `command` - The `command` parameter.
///
/// # Returns
///
/// A value of type `usize`.
pub fn format_command(buf: &mut [u8], command: u8) -> usize {
let prefix = b"NEC command: 0x";
let mut pos = copy_slice(buf, 0, prefix);
pos += format_hex_u8(buf, pos, command);
pos += copy_slice(buf, pos, b" (");
pos += format_u8(buf, pos, command);
pos += copy_slice(buf, pos, b")\r\n");
pos
}
/// Copy a byte slice into `buf` at the given offset, returning bytes written.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `offset` - The `offset` parameter.
/// * `src` - The `src` parameter.
///
/// # Returns
///
/// A value of type `usize`.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `offset` - The `offset` parameter.
/// * `src` - The `src` parameter.
///
/// # Returns
///
/// A value of type `usize`.
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.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `pos` - The `pos` parameter.
/// * `value` - Value to use.
///
/// # Returns
///
/// A value of type `usize`.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `pos` - The `pos` parameter.
/// * `value` - Value to use.
///
/// # Returns
///
/// A value of type `usize`.
fn format_u8(buf: &mut [u8], pos: usize, value: u8) -> usize {
let n = u8_digit_count(value);
write_u8_digits(buf, pos, value, n);
n
}
/// Return the number of decimal digits in a u8.
///
/// # Arguments
///
/// * `value` - Value to use.
///
/// # Returns
///
/// A value of type `usize`.
///
/// # Arguments
///
/// * `value` - Value to use.
///
/// # Returns
///
/// A value of type `usize`.
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`.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `pos` - The `pos` parameter.
/// * `value` - Value to use.
/// * `n` - Nibble or number.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `pos` - The `pos` parameter.
/// * `value` - Value to use.
/// * `n` - Nibble or number.
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.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `pos` - The `pos` parameter.
/// * `value` - Value to use.
///
/// # Returns
///
/// A value of type `usize`.
///
/// # Arguments
///
/// * `buf` - The `buf` parameter.
/// * `pos` - The `pos` parameter.
/// * `value` - Value to use.
///
/// # Returns
///
/// A value of type `usize`.
fn format_hex_u8(buf: &mut [u8], pos: usize, value: u8) -> usize {
buf[pos] = hex_digit((value >> 4) & 0x0F);
buf[pos + 1] = hex_digit(value & 0x0F);
2
}
/// Convert a 4-bit value to its uppercase ASCII hex digit.
///
/// # Arguments
///
/// * `value` - Value to use.
///
/// # Returns
///
/// An 8-bit unsigned integer value.
///
/// # Arguments
///
/// * `value` - Value to use.
///
/// # Returns
///
/// An 8-bit unsigned integer value.
fn hex_digit(value: u8) -> u8 {
if value < 10 {
b'0' + value
} else {
b'A' + (value - 10)
}
}
#[cfg(test)]
mod tests {
// Import all parent module items
use super::*;
/// Executes the leader mark accepts lower bound operation.
#[test]
fn leader_mark_accepts_lower_bound() {
assert!(is_valid_leader_mark(8_000));
}
/// Executes the leader mark rejects below lower bound operation.
#[test]
fn leader_mark_rejects_below_lower_bound() {
assert!(!is_valid_leader_mark(7_999));
}
/// Executes the leader space accepts upper bound operation.
#[test]
fn leader_space_accepts_upper_bound() {
assert!(is_valid_leader_space(5_000));
}
/// Executes the leader space rejects above upper bound operation.
#[test]
fn leader_space_rejects_above_upper_bound() {
assert!(!is_valid_leader_space(5_001));
}
/// Executes the bit space rejects short pulse operation.
#[test]
fn bit_space_rejects_short_pulse() {
assert!(!is_valid_bit_space(199));
}
/// Executes the bit space accepts threshold operation.
#[test]
fn bit_space_accepts_threshold() {
assert!(is_valid_bit_space(200));
}
/// Executes the accumulate zero bit leaves byte clear operation.
#[test]
fn accumulate_zero_bit_leaves_byte_clear() {
let mut data = [0u8; 4];
accumulate_nec_bit(&mut data, 0, 800);
assert_eq!(data[0], 0);
}
/// Executes the accumulate one bit sets lsb operation.
#[test]
fn accumulate_one_bit_sets_lsb() {
let mut data = [0u8; 4];
accumulate_nec_bit(&mut data, 0, 1_300);
assert_eq!(data[0], 1);
}
/// Executes the accumulate crosses into next byte operation.
#[test]
fn accumulate_crosses_into_next_byte() {
let mut data = [0u8; 4];
accumulate_nec_bit(&mut data, 8, 1_300);
assert_eq!(data[0], 0);
assert_eq!(data[1], 1);
}
/// Executes the validate frame returns command operation.
#[test]
fn validate_frame_returns_command() {
let data = [0x00, 0xFF, 0x45, 0xBA];
assert_eq!(validate_nec_frame(&data), Some(0x45));
}
/// Executes the validate frame rejects bad inverse operation.
#[test]
fn validate_frame_rejects_bad_inverse() {
let data = [0x00, 0xFE, 0x45, 0xBA];
assert_eq!(validate_nec_frame(&data), None);
}
/// Executes the format command single digit operation.
#[test]
fn format_command_single_digit() {
let mut buf = [0u8; 24];
let n = format_command(&mut buf, 7);
assert_eq!(&buf[..n], b"NEC command: 0x07 (7)\r\n");
}
/// Executes the format command three digits operation.
#[test]
fn format_command_three_digits() {
let mut buf = [0u8; 26];
let n = format_command(&mut buf, 255);
assert_eq!(&buf[..n], b"NEC command: 0xFF (255)\r\n");
}
/// Executes the format hex digit alpha operation.
#[test]
fn format_hex_digit_alpha() {
assert_eq!(hex_digit(0x0A), b'A');
}
}
+6
View File
@@ -0,0 +1,6 @@
//! Driver crate
#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]
#![cfg_attr(not(test), no_std)]
pub mod ir;
+102
View File
@@ -0,0 +1,102 @@
//! Driver crate
#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]
//! Implementation module
//!
//! **File:** `main.rs`
//! **Author:** Kevin Thomas
//! **Date:** 2025
//!
//! MIT License
//!
//! Copyright (c) 2025 Kevin Thomas
//!
//! Permission is hereby granted, free of charge, to any person obtaining a copy
//! of this software and associated documentation files (the "Software"), to deal
//! in the Software without restriction, including without limitation the rights
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//! copies of the Software, and to permit persons to whom the Software is
//! furnished to do so, subject to the following conditions:
//!
//! The above copyright notice and this permission notice shall be included in
//! all copies or substantial portions of the Software.
//!
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//! SOFTWARE.
#![no_std]
#![no_main]
// Board-level helpers: constants, type aliases, and init functions
mod board;
// IR driver module — suppress warnings for unused public API functions
// Debugging output over RTT
use defmt_rtt as _;
// Panic handler for RISC-V targets
#[cfg(target_arch = "riscv32")]
// Import panic_halt as _
use panic_halt as _;
// Panic handler for ARM targets
#[cfg(target_arch = "arm")]
// Import panic_probe as _
use panic_probe as _;
// HAL entry-point macro
use hal::entry;
// Alias our HAL crate
#[cfg(rp2350)]
// Import rp235x_hal as hal
use rp235x_hal as hal;
#[cfg(rp2040)]
// Import rp2040_hal as hal
use rp2040_hal as hal;
/// 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
#[unsafe(link_section = ".start_block")]
#[used]
#[cfg(rp2350)]
pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe();
/// Application entry point for the NEC IR receiver demo.
///
/// # Returns
///
/// A value of type `!`.
///
/// # Returns
///
/// A value of type `!`.
#[entry]
fn main() -> ! {
board::run(hal::pac::Peripherals::take().unwrap())
}
/// Picotool binary info metadata
#[unsafe(link_section = ".bi_entries")]
#[used]
pub static PICOTOOL_ENTRIES: [hal::binary_info::EntryAddr; 5] = [
hal::binary_info::rp_cargo_bin_name!(),
hal::binary_info::rp_cargo_version!(),
hal::binary_info::rp_program_description!(c"NEC IR Receiver Demo"),
hal::binary_info::rp_cargo_homepage_url!(),
hal::binary_info::rp_program_build_attribute!(),
];
#[cfg(test)]
mod tests {
// Import all parent module items
use super::*;
}