Fixed Rust drivers

This commit is contained in:
Kevin Thomas
2026-04-02 11:36:06 -04:00
parent 2792583302
commit 9834d5c96c
245 changed files with 1110 additions and 650 deletions
+44 -4
View File
@@ -26,7 +26,7 @@
//! SOFTWARE.
// Flash driver pure-logic functions and constants
use crate::flash_driver;
use crate::flash;
// Rate extension trait for .Hz() baud rate construction
use fugit::RateExtU32;
// Clock trait for accessing system clock frequency
@@ -176,8 +176,8 @@ pub(crate) fn flash_write(flash_offset: u32, data: &[u8]) {
rom_data::flash_exit_xip();
rom_data::flash_range_erase(
flash_offset,
flash_driver::FLASH_SECTOR_SIZE as usize,
flash_driver::FLASH_SECTOR_SIZE,
flash::FLASH_SECTOR_SIZE as usize,
flash::FLASH_SECTOR_SIZE,
0x20,
);
rom_data::flash_range_program(flash_offset, data.as_ptr(), len);
@@ -198,10 +198,50 @@ pub(crate) fn flash_write(flash_offset: u32, data: &[u8]) {
/// * `flash_offset` - Byte offset from the start of flash.
/// * `out` - Destination buffer.
pub(crate) fn flash_read(flash_offset: u32, out: &mut [u8]) {
let addr = (flash_driver::XIP_BASE + flash_offset) as *const u8;
let addr = (flash::XIP_BASE + flash_offset) as *const u8;
for (i, byte) in out.iter_mut().enumerate() {
*byte = unsafe { core::ptr::read_volatile(addr.add(i)) };
}
}
/// Initialise all peripherals and run the flash 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);
flash_demo(&uart);
loop { cortex_m::asm::wfe(); }
}
/// Execute the flash write / read-back / report sequence.
///
/// # Arguments
///
/// * `uart` - Reference to the enabled UART peripheral for serial output.
fn flash_demo(uart: &EnabledUart) {
let mut write_buf = [0u8; flash::FLASH_WRITE_LEN];
flash::prepare_write_buf(&mut write_buf);
flash_write(flash::FLASH_TARGET_OFFSET, &write_buf);
let mut read_buf = [0u8; flash::FLASH_WRITE_LEN];
flash_read(flash::FLASH_TARGET_OFFSET, &mut read_buf);
report_readback(uart, &read_buf);
}
/// Format and print the flash read-back result over UART.
///
/// # Arguments
///
/// * `uart` - Reference to the enabled UART peripheral for serial output.
/// * `read_buf` - Buffer containing the data read back from flash.
fn report_readback(uart: &EnabledUart, read_buf: &[u8]) {
let mut out = [0u8; 128];
let n = flash::format_readback(&mut out, read_buf);
uart.write_full_blocking(&out[..n]);
}
// End of file
@@ -1,4 +1,4 @@
//! @file flash_driver.rs
//! @file flash.rs
//! @brief Pure-logic flash driver (host-testable, no HAL)
//! @author Kevin Thomas
//! @date 2025
@@ -89,23 +89,27 @@ pub fn prepare_write_buf(buf: &mut [u8]) -> usize {
pub fn format_readback(buf: &mut [u8], read_data: &[u8]) -> usize {
let prefix = b"Flash readback: ";
let suffix = b"\r\n";
let mut pos = 0usize;
// Write prefix
let n = prefix.len().min(buf.len());
buf[..n].copy_from_slice(&prefix[..n]);
pos += n;
// Find NUL terminator in read_data
let str_len = read_data.iter().position(|&b| b == 0).unwrap_or(read_data.len());
let copy_len = str_len.min(buf.len().saturating_sub(pos));
buf[pos..pos + copy_len].copy_from_slice(&read_data[..copy_len]);
pos += copy_len;
// Write suffix
let n = suffix.len().min(buf.len().saturating_sub(pos));
buf[pos..pos + n].copy_from_slice(&suffix[..n]);
pos += n;
let mut pos = copy_slice(buf, 0, prefix);
pos += copy_c_string(&mut buf[pos..], read_data);
pos += copy_slice(buf, pos, suffix);
pos
}
/// Copy a byte slice into `buf` at the given offset, returning bytes written.
fn copy_slice(buf: &mut [u8], offset: usize, src: &[u8]) -> usize {
let n = src.len().min(buf.len().saturating_sub(offset));
buf[offset..offset + n].copy_from_slice(&src[..n]);
n
}
/// Copy bytes from `data` up to the first NUL into `buf`.
fn copy_c_string(buf: &mut [u8], data: &[u8]) -> usize {
let str_len = data.iter().position(|&b| b == 0).unwrap_or(data.len());
let n = str_len.min(buf.len());
buf[..n].copy_from_slice(&data[..n]);
n
}
#[cfg(test)]
mod tests {
// Import all parent module items
+1 -1
View File
@@ -6,4 +6,4 @@
#![no_std]
// Flash driver module
pub mod flash_driver;
pub mod flash;
+4 -21
View File
@@ -1,5 +1,5 @@
//! @file main.rs
//! @brief On-chip flash write/read demo using flash_driver.rs
//! @brief On-chip flash write/read demo using flash.rs
//! @author Kevin Thomas
//! @date 2025
//!
@@ -28,7 +28,7 @@
//! -----------------------------------------------------------------------------
//!
//! Demonstrates on-chip flash read/write using the flash driver
//! (flash_driver.rs). A string is written to the last sector of flash
//! (flash.rs). A string is written to the last sector of flash
//! and then read back to verify. The result is printed over UART.
//!
//! Wiring:
@@ -41,7 +41,7 @@
mod board;
// Flash driver module — suppress warnings for unused public API functions
#[allow(dead_code)]
mod flash_driver;
mod flash;
// 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 on-chip flash 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 write_buf = [0u8; flash_driver::FLASH_WRITE_LEN];
flash_driver::prepare_write_buf(&mut write_buf);
board::flash_write(flash_driver::FLASH_TARGET_OFFSET, &write_buf);
let mut read_buf = [0u8; flash_driver::FLASH_WRITE_LEN];
board::flash_read(flash_driver::FLASH_TARGET_OFFSET, &mut read_buf);
let mut out = [0u8; 128];
let n = flash_driver::format_readback(&mut out, &read_buf);
uart.write_full_blocking(&out[..n]);
loop {
cortex_m::asm::wfe();
}
board::run(hal::pac::Peripherals::take().unwrap())
}
// Picotool binary info metadata