mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-06-05 22:06:39 +02:00
Fixed Rust drivers
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
//! SOFTWARE.
|
||||
|
||||
// Timer driver pure-logic functions and constants
|
||||
use crate::timer_driver;
|
||||
use crate::timer;
|
||||
// Rate extension trait for .Hz() baud rate construction
|
||||
use fugit::RateExtU32;
|
||||
// Clock trait for accessing system clock frequency
|
||||
@@ -193,23 +193,67 @@ pub(crate) fn heartbeat_loop(
|
||||
uart: &EnabledUart,
|
||||
timer: &HalTimer,
|
||||
delay: &mut cortex_m::delay::Delay,
|
||||
state: &mut timer_driver::TimerDriverState,
|
||||
state: &mut timer::TimerDriverState,
|
||||
) -> ! {
|
||||
let mut last_us = timer.get_counter().ticks() as u32;
|
||||
let period_us = state.period_ms() as u64 * 1_000;
|
||||
loop {
|
||||
let now_us = timer.get_counter().ticks() as u32;
|
||||
let elapsed = now_us.wrapping_sub(last_us) as u64;
|
||||
if elapsed >= period_us {
|
||||
last_us = now_us;
|
||||
if state.on_fire() {
|
||||
let mut buf = [0u8; 32];
|
||||
let n = timer_driver::format_heartbeat(&mut buf);
|
||||
uart.write_full_blocking(&buf[..n]);
|
||||
}
|
||||
}
|
||||
let (now, elapsed) = tick_elapsed(timer, last_us);
|
||||
if elapsed >= period_us { last_us = now; fire_heartbeat(uart, state); }
|
||||
delay.delay_us(100);
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute elapsed microseconds since last checkpoint.
|
||||
fn tick_elapsed(timer: &HalTimer, last_us: u32) -> (u32, u64) {
|
||||
let now_us = timer.get_counter().ticks() as u32;
|
||||
(now_us, now_us.wrapping_sub(last_us) as u64)
|
||||
}
|
||||
|
||||
/// Fire the heartbeat callback and print the message over UART.
|
||||
fn fire_heartbeat(uart: &EnabledUart, state: &mut timer::TimerDriverState) {
|
||||
if state.on_fire() {
|
||||
let mut buf = [0u8; 32];
|
||||
let n = timer::format_heartbeat(&mut buf);
|
||||
uart.write_full_blocking(&buf[..n]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialise all peripherals and run the repeating timer demo.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pac` - PAC Peripherals singleton (consumed).
|
||||
pub(crate) fn run(mut pac: hal::pac::Peripherals) -> ! {
|
||||
let mut wd = hal::Watchdog::new(pac.WATCHDOG);
|
||||
let clocks = init_clocks(pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS, &mut wd);
|
||||
let pins = init_pins(pac.IO_BANK0, pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
|
||||
let uart = init_uart(pac.UART0, pins.gpio0, pins.gpio1, &mut pac.RESETS, &clocks);
|
||||
let mut delay = init_delay(&clocks);
|
||||
#[cfg(rp2350)]
|
||||
let timer = hal::Timer::new_timer0(pac.TIMER0, &mut pac.RESETS, &clocks);
|
||||
#[cfg(rp2040)]
|
||||
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS);
|
||||
let mut state = start_timer(&uart);
|
||||
heartbeat_loop(&uart, &timer, &mut delay, &mut state)
|
||||
}
|
||||
|
||||
/// Create the timer driver state, start it, and report over UART.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `uart` - Reference to the enabled UART peripheral for serial output.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Initialised timer driver state.
|
||||
fn start_timer(uart: &EnabledUart) -> timer::TimerDriverState {
|
||||
let mut state = timer::TimerDriverState::new();
|
||||
state.start(timer::DEFAULT_PERIOD_MS);
|
||||
let mut buf = [0u8; 64];
|
||||
let n = timer::format_started(&mut buf, timer::DEFAULT_PERIOD_MS);
|
||||
uart.write_full_blocking(&buf[..n]);
|
||||
state
|
||||
}
|
||||
|
||||
// End of file
|
||||
|
||||
@@ -6,4 +6,4 @@
|
||||
#![no_std]
|
||||
|
||||
// Timer driver module
|
||||
pub mod timer_driver;
|
||||
pub mod timer;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! @file main.rs
|
||||
//! @brief Repeating timer demo using timer_driver.rs
|
||||
//! @brief Repeating timer demo using timer.rs
|
||||
//! @author Kevin Thomas
|
||||
//! @date 2025
|
||||
//!
|
||||
@@ -28,7 +28,7 @@
|
||||
//! -----------------------------------------------------------------------------
|
||||
//!
|
||||
//! Demonstrates repeating timer callbacks using the timer driver
|
||||
//! (timer_driver.rs). A one-second heartbeat timer prints a message
|
||||
//! (timer.rs). A one-second heartbeat timer prints a message
|
||||
//! over UART to confirm the timer is firing.
|
||||
//!
|
||||
//! Wiring:
|
||||
@@ -41,7 +41,7 @@
|
||||
mod board;
|
||||
// Timer driver module — suppress warnings for unused public API functions
|
||||
#[allow(dead_code)]
|
||||
mod timer_driver;
|
||||
mod timer;
|
||||
|
||||
// Debugging output over RTT
|
||||
use defmt_rtt as _;
|
||||
@@ -76,24 +76,7 @@ pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe();
|
||||
/// Application entry point for the repeating timer demo.
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let mut pac = hal::pac::Peripherals::take().unwrap();
|
||||
let clocks = board::init_clocks(
|
||||
pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS,
|
||||
&mut hal::Watchdog::new(pac.WATCHDOG),
|
||||
);
|
||||
let pins = board::init_pins(pac.IO_BANK0, pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
|
||||
let uart = board::init_uart(pac.UART0, pins.gpio0, pins.gpio1, &mut pac.RESETS, &clocks);
|
||||
let mut delay = board::init_delay(&clocks);
|
||||
#[cfg(rp2350)]
|
||||
let timer = hal::Timer::new_timer0(pac.TIMER0, &mut pac.RESETS, &clocks);
|
||||
#[cfg(rp2040)]
|
||||
let timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS);
|
||||
let mut state = timer_driver::TimerDriverState::new();
|
||||
state.start(timer_driver::DEFAULT_PERIOD_MS);
|
||||
let mut buf = [0u8; 64];
|
||||
let n = timer_driver::format_started(&mut buf, timer_driver::DEFAULT_PERIOD_MS);
|
||||
uart.write_full_blocking(&buf[..n]);
|
||||
board::heartbeat_loop(&uart, &timer, &mut delay, &mut state);
|
||||
board::run(hal::pac::Peripherals::take().unwrap())
|
||||
}
|
||||
|
||||
// Picotool binary info metadata
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! @file timer_driver.rs
|
||||
//! @file timer.rs
|
||||
//! @brief Implementation of the repeating timer driver
|
||||
//! @author Kevin Thomas
|
||||
//! @date 2025
|
||||
@@ -184,17 +184,27 @@ fn format_u32(buf: &mut [u8], value: u32) -> usize {
|
||||
return 1;
|
||||
}
|
||||
let mut tmp = [0u8; 10];
|
||||
let n = u32_to_digits_reversed(&mut tmp, value);
|
||||
reverse_copy(buf, &tmp, n);
|
||||
n
|
||||
}
|
||||
|
||||
/// Convert a u32 to reversed decimal digits in a temporary buffer.
|
||||
fn u32_to_digits_reversed(tmp: &mut [u8; 10], mut value: u32) -> usize {
|
||||
let mut n = 0usize;
|
||||
let mut v = value;
|
||||
while v > 0 {
|
||||
tmp[n] = b'0' + (v % 10) as u8;
|
||||
v /= 10;
|
||||
while value > 0 {
|
||||
tmp[n] = b'0' + (value % 10) as u8;
|
||||
value /= 10;
|
||||
n += 1;
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
/// Copy digits from a reversed temporary buffer into the output buffer.
|
||||
fn reverse_copy(buf: &mut [u8], tmp: &[u8], n: usize) {
|
||||
for i in 0..n {
|
||||
buf[i] = tmp[n - 1 - i];
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
Reference in New Issue
Block a user