mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-18 14:04:48 +02:00
127 lines
4.1 KiB
Rust
127 lines
4.1 KiB
Rust
//! @file board.rs
|
|
//! @brief Board-level HAL helpers for the UART driver
|
|
//! @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.
|
|
|
|
// Alias our HAL crate
|
|
#[cfg(rp2350)]
|
|
use rp235x_hal as hal;
|
|
#[cfg(rp2040)]
|
|
use rp2040_hal as hal;
|
|
|
|
// UART driver for echo demo
|
|
use crate::uart;
|
|
|
|
/// 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;
|
|
|
|
/// 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 all peripherals and run the UART echo 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 mut drv = uart::UartDriver::init(pac.UART0, pins.gpio0, pins.gpio1, UART_BAUD, &mut pac.RESETS, &clocks);
|
|
drv.puts(b"UART driver ready (115200 8N1)\r\n");
|
|
drv.puts(b"Type characters to echo them back in UPPERCASE:\r\n");
|
|
echo_loop(&mut drv)
|
|
}
|
|
|
|
/// Run the uppercase echo loop forever.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `drv` - Mutable reference to the UART driver.
|
|
fn echo_loop(drv: &mut uart::UartDriver) -> ! {
|
|
loop {
|
|
if drv.is_readable() {
|
|
let c = drv.getchar();
|
|
drv.putchar(uart::UartDriver::to_upper(c));
|
|
}
|
|
}
|
|
}
|
|
|
|
// End of file
|