mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-06-10 08:13:57 +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)]
|
||||
@@ -1 +1 @@
|
||||
{"rustc_fingerprint":3018370877978686052,"outputs":{"692057488268926967":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\assem.KEVINTHOMAS\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"5409910182631311548":{"success":true,"status":"","code":0,"stdout":"rustc 1.91.1 (ed61e7d7e 2025-11-07)\nbinary: rustc\ncommit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb\ncommit-date: 2025-11-07\nhost: x86_64-pc-windows-msvc\nrelease: 1.91.1\nLLVM version: 21.1.2\n","stderr":""},"7671865365644980443":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.a\nC:\\Users\\assem.KEVINTHOMAS\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\noff\n___\ndebug_assertions\npanic=\"abort\"\nproc_macro\ntarget_abi=\"eabihf\"\ntarget_arch=\"arm\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"none\"\ntarget_pointer_width=\"32\"\ntarget_vendor=\"unknown\"\n","stderr":"warning: dropping unsupported crate type `dylib` for target `thumbv8m.main-none-eabihf`\n\nwarning: dropping unsupported crate type `cdylib` for target `thumbv8m.main-none-eabihf`\n\nwarning: dropping unsupported crate type `proc-macro` for target `thumbv8m.main-none-eabihf`\n\nwarning: 3 warnings emitted\n\n"}},"successes":{}}
|
||||
{"rustc_fingerprint":3018370877978686052,"outputs":{"6257262133114560740":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\assem.KEVINTHOMAS\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"5409910182631311548":{"success":true,"status":"","code":0,"stdout":"rustc 1.91.1 (ed61e7d7e 2025-11-07)\nbinary: rustc\ncommit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb\ncommit-date: 2025-11-07\nhost: x86_64-pc-windows-msvc\nrelease: 1.91.1\nLLVM version: 21.1.2\n","stderr":""},"692057488268926967":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\assem.KEVINTHOMAS\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"7671865365644980443":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.a\nC:\\Users\\assem.KEVINTHOMAS\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\noff\n___\ndebug_assertions\npanic=\"abort\"\nproc_macro\ntarget_abi=\"eabihf\"\ntarget_arch=\"arm\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"none\"\ntarget_pointer_width=\"32\"\ntarget_vendor=\"unknown\"\n","stderr":"warning: dropping unsupported crate type `dylib` for target `thumbv8m.main-none-eabihf`\n\nwarning: dropping unsupported crate type `cdylib` for target `thumbv8m.main-none-eabihf`\n\nwarning: dropping unsupported crate type `proc-macro` for target `thumbv8m.main-none-eabihf`\n\nwarning: 3 warnings emitted\n\n"}},"successes":{}}
|
||||
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user