mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-07-24 21:00:52 +02:00
Fixed Rust drivers
This commit is contained in:
@@ -196,4 +196,24 @@ pub(crate) fn loopback_transfer(
|
||||
uart.write_full_blocking(&line_buf[..rx_len]);
|
||||
spi::clear_rx_buffer(&mut rx);
|
||||
delay.delay_ms(POLL_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialise all peripherals and run the SPI loopback 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 spi, mut cs) = init_spi(
|
||||
pac.SPI0, pins.gpio16, pins.gpio17, pins.gpio18, pins.gpio19, &mut pac.RESETS, &clocks,
|
||||
);
|
||||
uart.write_full_blocking(b"SPI driver initialized on SPI0 at 1000000 Hz\r\n");
|
||||
loop { loopback_transfer(&mut spi, &mut cs, &uart, &mut delay); }
|
||||
}
|
||||
|
||||
// End of file
|
||||
@@ -78,27 +78,7 @@ pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe();
|
||||
/// Application entry point for the SPI loopback 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);
|
||||
let (mut spi_dev, mut cs) = board::init_spi(
|
||||
pac.SPI0,
|
||||
pins.gpio16,
|
||||
pins.gpio17,
|
||||
pins.gpio18,
|
||||
pins.gpio19,
|
||||
&mut pac.RESETS,
|
||||
&clocks,
|
||||
);
|
||||
uart.write_full_blocking(b"SPI driver initialized on SPI0 at 1000000 Hz\r\n");
|
||||
loop {
|
||||
board::loopback_transfer(&mut spi_dev, &mut cs, &uart, &mut delay);
|
||||
}
|
||||
board::run(hal::pac::Peripherals::take().unwrap())
|
||||
}
|
||||
|
||||
// Picotool binary info metadata
|
||||
|
||||
@@ -77,22 +77,25 @@ fn format_line(buf: &mut [u8], prefix: &[u8], text: &[u8], extra_blank_line: boo
|
||||
let mut pos = 0;
|
||||
buf[pos..pos + prefix.len()].copy_from_slice(prefix);
|
||||
pos += prefix.len();
|
||||
let text_len = c_string_len(text);
|
||||
buf[pos..pos + text_len].copy_from_slice(&text[..text_len]);
|
||||
pos += text_len;
|
||||
buf[pos] = b'\r';
|
||||
pos += 1;
|
||||
buf[pos] = b'\n';
|
||||
pos += 1;
|
||||
if extra_blank_line {
|
||||
buf[pos] = b'\r';
|
||||
pos += 1;
|
||||
buf[pos] = b'\n';
|
||||
pos += 1;
|
||||
}
|
||||
pos += copy_c_string(&mut buf[pos..], text);
|
||||
pos += append_crlf(&mut buf[pos..], extra_blank_line);
|
||||
pos
|
||||
}
|
||||
|
||||
/// Copy bytes from `text` up to the first NUL into `buf`.
|
||||
fn copy_c_string(buf: &mut [u8], text: &[u8]) -> usize {
|
||||
let len = c_string_len(text);
|
||||
buf[..len].copy_from_slice(&text[..len]);
|
||||
len
|
||||
}
|
||||
|
||||
/// Append CRLF (and optionally a second blank CRLF) to `buf`.
|
||||
fn append_crlf(buf: &mut [u8], extra: bool) -> usize {
|
||||
buf[0] = b'\r';
|
||||
buf[1] = b'\n';
|
||||
if extra { buf[2] = b'\r'; buf[3] = b'\n'; 4 } else { 2 }
|
||||
}
|
||||
|
||||
/// Return the length up to the first NUL byte or full slice length.
|
||||
fn c_string_len(text: &[u8]) -> usize {
|
||||
let mut len = 0usize;
|
||||
|
||||
Reference in New Issue
Block a user