mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-23 16:09:47 +02:00
e54c756423
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
422 lines
14 KiB
Rust
422 lines
14 KiB
Rust
//! @file board.rs
|
|
//! @brief Board-level HAL helpers for the LCD 1602 driver
|
|
//! @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 pins.
|
|
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 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);
|
|
let mut i2c = init_i2c(pac.I2C1, pins.gpio2, pins.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); }
|
|
}
|
|
|
|
// End of file
|