mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-07-07 13:08:09 +02:00
Initial commit
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
//! Implementation module
|
||||
//!
|
||||
//! **File:** `adc.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.
|
||||
|
||||
/// ADC reference voltage in millivolts.
|
||||
pub const ADC_VREF_MV: u32 = 3300;
|
||||
|
||||
/// ADC full-scale value for 12-bit resolution.
|
||||
pub const ADC_FULL_SCALE: u32 = 4095;
|
||||
|
||||
/// Convert a raw 12-bit ADC value to millivolts.
|
||||
///
|
||||
/// Scales the raw value linearly against the 3.3 V reference.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `raw` - 12-bit ADC conversion result (0–4095).
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Equivalent voltage in millivolts (0–3300).
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `raw` - The `raw` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A 32-bit unsigned integer value.
|
||||
#[inline]
|
||||
pub fn raw_to_mv(raw: u16) -> u32 {
|
||||
raw as u32 * ADC_VREF_MV / ADC_FULL_SCALE
|
||||
}
|
||||
|
||||
/// Convert a raw temperature-sensor ADC value to degrees Celsius.
|
||||
///
|
||||
/// Applies the RP2350 datasheet formula:
|
||||
/// T = 27 - (V - 0.706) / 0.001721
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `raw` - 12-bit ADC result from the internal temperature sensor (channel 4).
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Die temperature in degrees Celsius.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `raw` - The `raw` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `f32`.
|
||||
#[inline]
|
||||
pub fn raw_to_celsius(raw: u16) -> f32 {
|
||||
let voltage = raw as f32 * 3.3f32 / ADC_FULL_SCALE as f32;
|
||||
27.0f32 - (voltage - 0.706f32) / 0.001721f32
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// Import all parent module items
|
||||
use super::*;
|
||||
|
||||
/// Executes the raw to mv zero operation.
|
||||
#[test]
|
||||
fn raw_to_mv_zero() {
|
||||
assert_eq!(raw_to_mv(0), 0);
|
||||
}
|
||||
|
||||
/// Executes the raw to mv full scale operation.
|
||||
#[test]
|
||||
fn raw_to_mv_full_scale() {
|
||||
assert_eq!(raw_to_mv(4095), 3300);
|
||||
}
|
||||
|
||||
/// Executes the raw to mv half operation.
|
||||
#[test]
|
||||
fn raw_to_mv_half() {
|
||||
let mv = raw_to_mv(2048);
|
||||
assert!(mv >= 1649 && mv <= 1651);
|
||||
}
|
||||
|
||||
/// Executes the raw to mv quarter operation.
|
||||
#[test]
|
||||
fn raw_to_mv_quarter() {
|
||||
let mv = raw_to_mv(1024);
|
||||
assert!(mv >= 824 && mv <= 826);
|
||||
}
|
||||
|
||||
/// Executes the raw to celsius room temp operation.
|
||||
#[test]
|
||||
fn raw_to_celsius_room_temp() {
|
||||
let temp = raw_to_celsius(876);
|
||||
assert!(temp > 20.0 && temp < 35.0);
|
||||
}
|
||||
|
||||
/// Executes the raw to celsius known voltage operation.
|
||||
#[test]
|
||||
fn raw_to_celsius_known_voltage() {
|
||||
let raw = (0.706f32 / 3.3f32 * ADC_FULL_SCALE as f32 + 0.5f32) as u16;
|
||||
let temp = raw_to_celsius(raw);
|
||||
assert!((temp - 27.0).abs() < 1.0);
|
||||
}
|
||||
|
||||
/// Executes the raw to celsius higher voltage operation.
|
||||
#[test]
|
||||
fn raw_to_celsius_higher_voltage() {
|
||||
let temp_low = raw_to_celsius(1000);
|
||||
let temp_high = raw_to_celsius(800);
|
||||
assert!(temp_high > temp_low);
|
||||
}
|
||||
|
||||
/// Executes the raw to mv one count operation.
|
||||
#[test]
|
||||
fn raw_to_mv_one_count() {
|
||||
assert_eq!(raw_to_mv(1), 0);
|
||||
}
|
||||
|
||||
/// Executes the raw to mv ten counts operation.
|
||||
#[test]
|
||||
fn raw_to_mv_ten_counts() {
|
||||
assert_eq!(raw_to_mv(10), 8);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,424 @@
|
||||
//! 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.
|
||||
|
||||
// Rate extension trait for .Hz() baud rate construction
|
||||
use fugit::RateExtU32;
|
||||
// ADC one-shot trait for .read()
|
||||
use cortex_m::prelude::_embedded_hal_adc_OneShot;
|
||||
// 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;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// Main-loop polling interval in milliseconds.
|
||||
pub(crate) const POLL_MS: u32 = 500;
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// # 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())
|
||||
}
|
||||
|
||||
/// Write a conditional digit into `buf` if `val` meets the threshold.
|
||||
fn write_conditional_digit(
|
||||
buf: &mut [u8],
|
||||
pos: &mut usize,
|
||||
val: u32,
|
||||
threshold: u32,
|
||||
divisor: u32,
|
||||
) {
|
||||
if val >= threshold {
|
||||
buf[*pos] = b'0' + ((val / divisor) % 10) as u8;
|
||||
*pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a u32 with minimum digits (no leading zeros).
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `val` - Value to use.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `usize`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `val` - Value to use.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `usize`.
|
||||
fn write_min_digits(buf: &mut [u8], val: u32) -> usize {
|
||||
let mut pos = 0;
|
||||
write_conditional_digit(buf, &mut pos, val, 100, 100);
|
||||
write_conditional_digit(buf, &mut pos, val, 10, 10);
|
||||
buf[pos] = b'0' + (val % 10) as u8;
|
||||
pos + 1
|
||||
}
|
||||
|
||||
/// Write 4-digit millivolt value into `buf`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `mv` - The `mv` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `usize`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `mv` - The `mv` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `usize`.
|
||||
fn write_mv_digits(buf: &mut [u8], mv: u32) -> usize {
|
||||
buf[0] = b'0' + ((mv / 1000) % 10) as u8; buf[1] = b'0' + ((mv / 100) % 10) as u8;
|
||||
buf[2] = b'0' + ((mv / 10) % 10) as u8; buf[3] = b'0' + (mv % 10) as u8;
|
||||
4
|
||||
}
|
||||
|
||||
/// Write a negative sign if needed and return the absolute temperature value.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `pos` - The `pos` parameter.
|
||||
/// * `temp_int` - The `temp_int` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A 32-bit unsigned integer value.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `pos` - The `pos` parameter.
|
||||
/// * `temp_int` - The `temp_int` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A 32-bit unsigned integer value.
|
||||
fn write_sign(buf: &mut [u8], pos: &mut usize, temp_int: i32) -> u32 {
|
||||
if temp_int >= 0 { return temp_int as u32; }
|
||||
buf[*pos] = b'-'; *pos += 1;
|
||||
(-temp_int) as u32
|
||||
}
|
||||
|
||||
/// Write temperature as "[-]NN.F" into `buf`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `temp_int` - The `temp_int` parameter.
|
||||
/// * `temp_frac` - The `temp_frac` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `usize`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `temp_int` - The `temp_int` parameter.
|
||||
/// * `temp_frac` - The `temp_frac` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `usize`.
|
||||
fn write_temp(buf: &mut [u8], temp_int: i32, temp_frac: u8) -> usize {
|
||||
let mut pos = 0; let abs_temp = write_sign(buf, &mut pos, temp_int);
|
||||
pos += write_min_digits(&mut buf[pos..], abs_temp);
|
||||
buf[pos] = b'.'; buf[pos + 1] = b'0' + temp_frac;
|
||||
pos + 2
|
||||
}
|
||||
|
||||
/// Format a millivolt value into "ADC0: NNNN mV | Chip temp: NN.N C\r\n".
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - Mutable byte slice (must be at least 48 bytes).
|
||||
/// * `mv` - Voltage in millivolts.
|
||||
/// * `temp_int` - Integer part of temperature.
|
||||
/// * `temp_frac` - Single decimal digit of temperature fraction.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Number of bytes written into the buffer.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `buf` - The `buf` parameter.
|
||||
/// * `mv` - The `mv` parameter.
|
||||
/// * `temp_int` - The `temp_int` parameter.
|
||||
/// * `temp_frac` - The `temp_frac` parameter.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A value of type `usize`.
|
||||
pub(crate) fn format_adc_line(buf: &mut [u8], mv: u32, temp_int: i32, temp_frac: u8) -> usize {
|
||||
buf[..6].copy_from_slice(b"ADC0: ");
|
||||
let p1 = 6 + write_mv_digits(&mut buf[6..], mv);
|
||||
buf[p1..p1 + 19].copy_from_slice(b" mV | Chip temp: ");
|
||||
let p2 = p1 + 19 + write_temp(&mut buf[p1 + 19..], temp_int, temp_frac);
|
||||
buf[p2..p2 + 4].copy_from_slice(b" C\r\n"); p2 + 4
|
||||
}
|
||||
|
||||
/// Type alias for the ADC input pin on GPIO 26.
|
||||
type Gpio26Adc = hal::adc::AdcPin<Pin<hal::gpio::bank0::Gpio26, FunctionNull, PullDown>>;
|
||||
|
||||
/// Initialise all peripherals and run the ADC 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 p: hal::pac::Peripherals) -> ! {
|
||||
let mut w = hal::Watchdog::new(p.WATCHDOG);
|
||||
let c = init_clocks(p.XOSC, p.CLOCKS, p.PLL_SYS, p.PLL_USB, &mut p.RESETS, &mut w);
|
||||
let pins = init_pins(p.IO_BANK0, p.PADS_BANK0, p.SIO, &mut p.RESETS);
|
||||
let u = init_uart(p.UART0, pins.gpio0, pins.gpio1, &mut p.RESETS, &c);
|
||||
let (mut adc, mut adc_pin, mut temp) = init_adc(p.ADC, pins.gpio26, &mut p.RESETS);
|
||||
u.write_full_blocking(b"ADC driver initialized: GPIO26 (channel 0)\r\n");
|
||||
adc_loop(&u, &mut adc, &mut adc_pin, &mut temp, &mut init_delay(&c))
|
||||
}
|
||||
|
||||
/// Create the ADC peripheral, GPIO 26 input channel, and temperature sensor.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `adc_pac` - PAC ADC peripheral singleton.
|
||||
/// * `gpio26` - Default GPIO 26 pin to use as ADC input.
|
||||
/// * `resets` - Mutable reference to the RESETS peripheral.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Tuple of (ADC driver, ADC pin channel, temperature sensor channel).
|
||||
fn init_adc(adc: hal::pac::ADC, p26: Pin<hal::gpio::bank0::Gpio26, FunctionNull, PullDown>, r: &mut hal::pac::RESETS) -> (hal::Adc, Gpio26Adc, hal::adc::TempSense) {
|
||||
let mut a = hal::Adc::new(adc, r);
|
||||
let t = a.take_temp_sensor().unwrap();
|
||||
(a, hal::adc::AdcPin::new(p26).unwrap(), t)
|
||||
}
|
||||
|
||||
/// Sample voltage and temperature, format, and print in a loop.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `uart` - Reference to the enabled UART peripheral for serial output.
|
||||
/// * `adc` - Mutable reference to the ADC driver.
|
||||
/// * `adc_pin` - Mutable reference to the GPIO 26 ADC channel.
|
||||
/// * `temp` - Mutable reference to the temperature sensor channel.
|
||||
/// * `delay` - Mutable reference to the blocking delay provider.
|
||||
fn adc_loop(u: &EnabledUart, a: &mut hal::Adc, ap: &mut Gpio26Adc, t: &mut hal::adc::TempSense, d: &mut cortex_m::delay::Delay) -> ! {
|
||||
let mut buf = [0u8; 48];
|
||||
loop {
|
||||
let (mv, temp_int, temp_frac) = read_adc(a, ap, t);
|
||||
let n = format_adc_line(&mut buf, mv, temp_int, temp_frac);
|
||||
u.write_full_blocking(&buf[..n]); d.delay_ms(POLL_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/// Read voltage and temperature from the ADC.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `adc` - Mutable reference to the ADC driver.
|
||||
/// * `adc_pin` - Mutable reference to the GPIO 26 ADC channel.
|
||||
/// * `temp` - Mutable reference to the temperature sensor channel.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Tuple of (millivolts, integer temperature, fractional temperature digit).
|
||||
fn read_adc(a: &mut hal::Adc, ap: &mut Gpio26Adc, t: &mut hal::adc::TempSense) -> (u32, i32, u8) {
|
||||
let mv = adc_lib::adc::raw_to_mv(a.read(ap).unwrap());
|
||||
let c = adc_lib::adc::raw_to_celsius(a.read(t).unwrap());
|
||||
(mv, c as i32, (((c - (c as i32) as f32) * 10.0) as u8).min(9))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
//! Driver crate
|
||||
|
||||
#![deny(missing_docs)]
|
||||
#![deny(clippy::missing_docs_in_private_items)]
|
||||
#![cfg_attr(not(test), no_std)]
|
||||
pub mod adc;
|
||||
@@ -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;
|
||||
// ADC 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 ADC voltage and temperature 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"ADC Voltage and Temperature 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::*;
|
||||
}
|
||||
Reference in New Issue
Block a user