Refactor Rust drivers for strict idiomatic documentation and 8-line enforcement

This commit is contained in:
Kevin Thomas
2023-10-06 14:27:20 -04:00
commit 46e8b76762
1231 changed files with 110385 additions and 0 deletions
+419
View File
@@ -0,0 +1,419 @@
//! 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.
// I2C bus trait for LCD communication
use embedded_hal::i2c::I2c;
// 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::{FunctionI2C, FunctionNull, FunctionUart, Pin, PullDown, PullNone, PullUp};
// UART configuration and peripheral types
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;
/// 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;
/// I2C bus speed in Hz (100 kHz standard mode).
pub(crate) const I2C_BAUD: u32 = 100_000;
/// 7-bit I2C address of the PCF8574 LCD backpack.
pub(crate) const LCD_I2C_ADDR: u8 = 0x27;
/// Number of bit positions to shift a 4-bit nibble.
pub(crate) const NIBBLE_SHIFT: u8 = 4;
/// PCF8574 backlight enable mask.
pub(crate) const BACKLIGHT_MASK: u8 = 0x08;
/// Delay between counter updates in milliseconds.
pub(crate) const COUNTER_DELAY_MS: u32 = 1_000;
/// 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.
///
/// # Arguments
///
/// * `xosc` - XOSC peripheral singleton.
/// * `clocks` - CLOCKS peripheral singleton.
/// * `pll_sys` - PLL_SYS peripheral singleton.
/// * `pll_usb` - PLL_USB peripheral singleton.
/// * `resets` - Mutable reference to the RESETS peripheral.
/// * `watchdog` - Mutable reference to the watchdog timer.
///
/// # Returns
///
/// Configured clocks manager.
///
/// # Panics
///
/// Panics if clock initialisation fails.
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.
///
/// # Arguments
///
/// * `io_bank0` - IO_BANK0 peripheral singleton.
/// * `pads_bank0` - PADS_BANK0 peripheral singleton.
/// * `sio` - SIO peripheral singleton.
/// * `resets` - Mutable reference to the RESETS peripheral.
///
/// # Returns
///
/// GPIO pin set for the entire bank.
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 (stdio equivalent).
///
/// # Arguments
///
/// * `uart0` - PAC UART0 peripheral singleton.
/// * `tx_pin` - GPIO pin to use as UART0 TX (GPIO 0).
/// * `rx_pin` - GPIO pin to use as UART0 RX (GPIO 1).
/// * `resets` - Mutable reference to the RESETS peripheral.
/// * `clocks` - Reference to the initialised clock configuration.
///
/// # Returns
///
/// Enabled UART0 peripheral ready for blocking writes.
///
/// # Panics
///
/// Panics if the HAL cannot achieve the requested baud rate.
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` - Reference to the initialised clock configuration.
///
/// # Returns
///
/// Blocking delay provider.
///
/// # Panics
///
/// Panics if the cortex-m core peripherals have already been taken.
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())
}
/// Write one raw byte to the PCF8574 expander over I2C.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `data` - Byte to write.
fn pcf_write_byte(i2c: &mut impl I2c, addr: u8, data: u8) {
let _ = i2c.write(addr, &[data]);
}
/// Toggle EN to latch a nibble into the LCD controller.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `data` - Nibble byte without EN asserted.
/// * `delay` - Delay provider for timing.
fn pcf_pulse_enable(i2c: &mut impl I2c, addr: u8, data: u8, delay: &mut cortex_m::delay::Delay) {
pcf_write_byte(i2c, addr, crate::lcd1602::nibble_with_en(data));
delay.delay_us(1);
pcf_write_byte(i2c, addr, crate::lcd1602::nibble_without_en(data));
delay.delay_us(50);
}
/// Write one 4-bit nibble to the LCD.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `nibble` - 4-bit value to send.
/// * `mode` - Register select: 0 for command, 1 for data.
/// * `delay` - Delay provider for timing.
fn lcd_write4(i2c: &mut impl I2c, addr: u8, nibble: u8, mode: u8, delay: &mut cortex_m::delay::Delay) {
let data = crate::lcd1602::build_nibble(nibble, NIBBLE_SHIFT, mode, BACKLIGHT_MASK);
pcf_pulse_enable(i2c, addr, data, delay);
}
/// Send one full 8-bit command/data value as two nibbles.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `value` - 8-bit value to send.
/// * `mode` - Register select: 0 for command, 1 for data.
/// * `delay` - Delay provider for timing.
fn lcd_send(i2c: &mut impl I2c, addr: u8, value: u8, mode: u8, delay: &mut cortex_m::delay::Delay) {
lcd_write4(i2c, addr, (value >> 4) & 0x0F, mode, delay);
lcd_write4(i2c, addr, value & 0x0F, mode, delay);
}
/// Send three 0x03 nibbles with required power-on delays.
fn lcd_reset_pulse_3x(i2c: &mut impl I2c, addr: u8, delay: &mut cortex_m::delay::Delay) {
lcd_write4(i2c, addr, 0x03, 0, delay);
delay.delay_ms(5);
lcd_write4(i2c, addr, 0x03, 0, delay);
delay.delay_us(150);
lcd_write4(i2c, addr, 0x03, 0, delay);
delay.delay_us(150);
}
/// Execute the HD44780 4-bit mode power-on reset sequence.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `delay` - Delay provider for timing.
fn lcd_hd44780_reset(i2c: &mut impl I2c, addr: u8, delay: &mut cortex_m::delay::Delay) {
lcd_reset_pulse_3x(i2c, addr, delay);
lcd_write4(i2c, addr, 0x02, 0, delay);
delay.delay_us(150);
}
/// Send post-reset configuration commands to the HD44780.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `delay` - Delay provider for timing.
fn lcd_hd44780_configure(i2c: &mut impl I2c, addr: u8, delay: &mut cortex_m::delay::Delay) {
lcd_send(i2c, addr, 0x28, 0, delay);
lcd_send(i2c, addr, 0x0C, 0, delay);
lcd_send(i2c, addr, 0x01, 0, delay);
delay.delay_ms(2);
lcd_send(i2c, addr, 0x06, 0, delay);
}
/// Set the LCD cursor position.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `line` - Display row (0 or 1).
/// * `position` - Column offset.
/// * `delay` - Delay provider for timing.
fn lcd_set_cursor(i2c: &mut impl I2c, addr: u8, line: u8, position: u8, delay: &mut cortex_m::delay::Delay) {
lcd_send(i2c, addr, crate::lcd1602::cursor_address(line, position), 0, delay);
}
/// Write a byte slice as character data to the LCD.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `addr` - 7-bit I2C address of the PCF8574.
/// * `s` - Byte slice of ASCII characters to display.
/// * `delay` - Delay provider for timing.
fn lcd_puts(i2c: &mut impl I2c, addr: u8, s: &[u8], delay: &mut cortex_m::delay::Delay) {
for &ch in s {
lcd_send(i2c, addr, ch, 1, delay);
}
}
/// Initialize the LCD, display the title, and log over UART.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `uart` - UART peripheral for serial log output.
/// * `delay` - Delay provider for timing.
pub(crate) fn setup_display(
i2c: &mut impl I2c,
uart: &EnabledUart,
delay: &mut cortex_m::delay::Delay,
) {
lcd_hd44780_reset(i2c, LCD_I2C_ADDR, delay);
lcd_hd44780_configure(i2c, LCD_I2C_ADDR, delay);
lcd_show_title(i2c, delay);
uart.write_full_blocking(b"LCD 1602 driver initialized at I2C addr 0x27\r\n");
}
/// Write the title text on LCD row 0.
fn lcd_show_title(i2c: &mut impl I2c, delay: &mut cortex_m::delay::Delay) {
lcd_set_cursor(i2c, LCD_I2C_ADDR, 0, 0, delay);
lcd_puts(i2c, LCD_I2C_ADDR, b"Reverse Eng.", delay);
}
/// Format and display the next counter value on LCD line 1.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus.
/// * `uart` - UART peripheral for serial log output.
/// * `delay` - Delay provider for timing.
/// * `count` - Mutable reference to the counter state.
pub(crate) fn update_counter(
i2c: &mut impl I2c,
uart: &EnabledUart,
delay: &mut cortex_m::delay::Delay,
count: &mut u32,
) {
let mut buf = [0u8; 16];
let n = crate::lcd1602::format_counter(&mut buf, *count);
*count += 1;
lcd_display_counter(i2c, delay, &buf[..n]);
uart_log_counter(uart, &buf[..n]);
delay.delay_ms(COUNTER_DELAY_MS);
}
/// Write counter text to LCD line 1.
fn lcd_display_counter(i2c: &mut impl I2c, delay: &mut cortex_m::delay::Delay, text: &[u8]) {
lcd_set_cursor(i2c, LCD_I2C_ADDR, 1, 0, delay);
lcd_puts(i2c, LCD_I2C_ADDR, text, delay);
}
/// Log counter text over UART with trailing CRLF.
fn uart_log_counter(uart: &EnabledUart, text: &[u8]) {
uart.write_full_blocking(text);
uart.write_full_blocking(b"\r\n");
}
/// Initialise all peripherals and run the LCD 1602 counter demo.
///
/// # Arguments
///
/// * `pac` - PAC Peripherals singleton (consumed).
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);
let mut i2c = init_i2c(pac.I2C1, p.gpio2, p.gpio3, &mut pac.RESETS, &clocks);
setup_display(&mut i2c, &uart, &mut delay);
counter_loop(&mut i2c, &uart, &mut delay)
}
/// Initialise I2C1 on SDA=GPIO2 / SCL=GPIO3.
///
/// # Arguments
///
/// * `i2c1` - PAC I2C1 peripheral singleton.
/// * `sda` - Default GPIO 2 pin (will be reconfigured for I2C).
/// * `scl` - Default GPIO 3 pin (will be reconfigured for I2C).
/// * `resets` - Mutable reference to the RESETS peripheral.
/// * `clocks` - Reference to the initialised clock configuration.
///
/// # Returns
///
/// Configured I2C1 bus controller.
fn init_i2c(
i2c1: hal::pac::I2C1,
sda: Pin<hal::gpio::bank0::Gpio2, FunctionNull, PullDown>,
scl: Pin<hal::gpio::bank0::Gpio3, FunctionNull, PullDown>,
resets: &mut hal::pac::RESETS,
clocks: &hal::clocks::ClocksManager,
) -> impl I2c {
let sda = sda.reconfigure::<FunctionI2C, PullUp>();
let scl = scl.reconfigure::<FunctionI2C, PullUp>();
hal::I2C::i2c1(i2c1, sda, scl, I2C_BAUD.Hz(), resets, clocks.system_clock.freq())
}
/// Run the counter display loop forever.
///
/// # Arguments
///
/// * `i2c` - Mutable reference to the I2C bus controller.
/// * `uart` - Reference to the enabled UART peripheral for serial output.
/// * `delay` - Mutable reference to the blocking delay provider.
fn counter_loop(
i2c: &mut impl I2c,
uart: &EnabledUart,
delay: &mut cortex_m::delay::Delay,
) -> ! {
let mut count: u32 = 0;
loop { update_counter(i2c, uart, delay, &mut count); }
}
+221
View File
@@ -0,0 +1,221 @@
//! Implementation module
//!
//! **File:** `lcd1602.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.
/// PCF8574 -> LCD control pin: Register Select.
pub const PIN_RS: u8 = 0x01;
/// PCF8574 -> LCD control pin: Read/Write.
pub const PIN_RW: u8 = 0x02;
/// PCF8574 -> LCD control pin: Enable.
pub const PIN_EN: u8 = 0x04;
/// Build a PCF8574 output byte for a 4-bit LCD nibble.
///
/// # Arguments
///
/// * `nibble` - 4-bit data value (0x00..0x0F).
/// * `nibble_shift` - Number of bits to shift the nibble left.
/// * `mode` - Register select mode (0 = command, non-zero = data).
/// * `backlight_mask` - Bitmask to enable the backlight LED.
///
/// # Returns
///
/// Assembled PCF8574 output byte.
pub fn build_nibble(nibble: u8, nibble_shift: u8, mode: u8, backlight_mask: u8) -> u8 {
let mut data = (nibble & 0x0F) << nibble_shift;
if mode != 0 { data |= PIN_RS; }
data |= backlight_mask;
data
}
/// Build the PCF8574 byte with EN asserted.
///
/// # Arguments
///
/// * `nibble_byte` - PCF8574 output byte before EN assertion.
///
/// # Returns
///
/// Byte with the EN bit set.
pub fn nibble_with_en(nibble_byte: u8) -> u8 {
nibble_byte | PIN_EN
}
/// Build the PCF8574 byte with EN de-asserted.
///
/// # Arguments
///
/// * `nibble_byte` - PCF8574 output byte before EN de-assertion.
///
/// # Returns
///
/// Byte with the EN bit cleared.
pub fn nibble_without_en(nibble_byte: u8) -> u8 {
nibble_byte & !PIN_EN
}
/// HD44780 row-offset lookup.
const ROW_OFFSETS: [u8; 2] = [0x00, 0x40];
/// Compute the DDRAM address byte for `lcd_set_cursor`.
///
/// # Arguments
///
/// * `line` - Display row (0 or 1; values > 1 are clamped to 1).
/// * `position` - Column offset within the row.
///
/// # Returns
///
/// HD44780 set-DDRAM-address command byte (0x80 | offset).
pub fn cursor_address(line: u8, position: u8) -> u8 {
let row = if line > 1 { 1 } else { line as usize };
0x80 | (position + ROW_OFFSETS[row])
}
/// Extract the six decimal digits of a counter value into an array.
fn fill_digit_array(c: u32) -> [u8; 6] {
[
((c / 100000) % 10) as u8, ((c / 10000) % 10) as u8,
((c / 1000) % 10) as u8, ((c / 100) % 10) as u8,
((c / 10) % 10) as u8, (c % 10) as u8,
]
}
/// Write six counter digits into `buf` starting at `start`, suppressing leading zeros.
fn write_counter_digits(buf: &mut [u8], mut pos: usize, digits: &[u8; 6]) -> usize {
let mut leading = true;
for (i, &d) in digits.iter().enumerate() {
if i == 5 || d != 0 { leading = false; }
buf[pos] = if leading { b' ' } else { b'0' + d }; pos += 1;
}
pos
}
/// Format a counter value as `"Count: NNNNNN"` (right-justified, 6 digits).
///
/// # Arguments
///
/// * `buf` - Mutable byte slice (must be at least 13 bytes).
/// * `count` - Counter value to format (0..999999).
///
/// # Returns
///
/// Number of bytes written into the buffer.
pub fn format_counter(buf: &mut [u8], count: u32) -> usize {
buf[..7].copy_from_slice(b"Count: ");
let digits = fill_digit_array(count);
write_counter_digits(buf, 7, &digits)
}
#[cfg(test)]
mod tests {
// Import all parent module items
use super::*;
#[test]
fn build_nibble_command_mode() {
let b = build_nibble(0x03, 4, 0, 0x08);
assert_eq!(b, 0x38);
}
#[test]
fn build_nibble_data_mode() {
let b = build_nibble(0x04, 4, 1, 0x08);
assert_eq!(b, 0x49);
}
#[test]
fn build_nibble_no_backlight() {
let b = build_nibble(0x0F, 4, 0, 0x00);
assert_eq!(b, 0xF0);
}
#[test]
fn nibble_with_en_sets_bit() {
assert_eq!(nibble_with_en(0x38), 0x3C);
}
#[test]
fn nibble_without_en_clears_bit() {
assert_eq!(nibble_without_en(0x3C), 0x38);
}
#[test]
fn cursor_address_line0_col0() {
assert_eq!(cursor_address(0, 0), 0x80);
}
#[test]
fn cursor_address_line1_col0() {
assert_eq!(cursor_address(1, 0), 0xC0);
}
#[test]
fn cursor_address_line0_col5() {
assert_eq!(cursor_address(0, 5), 0x85);
}
#[test]
fn cursor_address_line1_col15() {
assert_eq!(cursor_address(1, 15), 0xCF);
}
#[test]
fn cursor_address_clamps_line() {
assert_eq!(cursor_address(5, 0), 0xC0);
}
#[test]
fn format_counter_zero() {
let mut buf = [0u8; 16];
let n = format_counter(&mut buf, 0);
assert_eq!(&buf[..n], b"Count: 0");
}
#[test]
fn format_counter_one() {
let mut buf = [0u8; 16];
let n = format_counter(&mut buf, 1);
assert_eq!(&buf[..n], b"Count: 1");
}
#[test]
fn format_counter_large() {
let mut buf = [0u8; 16];
let n = format_counter(&mut buf, 123456);
assert_eq!(&buf[..n], b"Count: 123456");
}
#[test]
fn format_counter_six_digits() {
let mut buf = [0u8; 16];
let n = format_counter(&mut buf, 999999);
assert_eq!(&buf[..n], b"Count: 999999");
}
}
+32
View File
@@ -0,0 +1,32 @@
//! Implementation module
//!
//! **File:** `lib.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.
#![cfg_attr(not(test), no_std)]
// LCD 1602 driver module
pub mod lcd1602;
+84
View File
@@ -0,0 +1,84 @@
//! 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;
// LCD1602 driver module — suppress warnings for unused public API functions
#[allow(dead_code)]
mod lcd1602;
// Debugging output over RTT
use defmt_rtt as _;
// Panic handler for RISC-V targets
#[cfg(target_arch = "riscv32")]
use panic_halt as _;
// Panic handler for ARM targets
#[cfg(target_arch = "arm")]
use panic_probe as _;
// 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;
/// 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 LCD 1602 counter demo.
#[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"LCD 1602 Counter Demo"),
hal::binary_info::rp_cargo_homepage_url!(),
hal::binary_info::rp_program_build_attribute!(),
];