From 0afda7192c30a16ca14dc15939b0a0ccb9fceecc Mon Sep 17 00:00:00 2001 From: Adam Macdonald <72780006+twokilohertz@users.noreply.github.com> Date: Thu, 20 Feb 2025 14:06:46 +0000 Subject: [PATCH] Hello, UART --- Cargo.lock | 1 + Cargo.toml | 1 + src/constants.rs | 2 ++ src/main.rs | 36 +++++++++++++++++++++++------------- 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/constants.rs diff --git a/Cargo.lock b/Cargo.lock index 10985ee..a6b49fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,6 +263,7 @@ name = "pico-enviro-sensor" version = "0.1.0" dependencies = [ "embedded-hal 1.0.0", + "fugit", "panic-halt", "rp235x-hal", ] diff --git a/Cargo.toml b/Cargo.toml index b470140..a84cab0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" [dependencies] embedded-hal = "1.0.0" +fugit = "0.3.7" panic-halt = "1.0.0" rp235x-hal = { version = "0.2.0", features = [ "critical-section-impl", diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..3141208 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,2 @@ +/// Pico 2 W on-board crystal oscillator frequency (AEL 12.0) +pub const XTAL_FREQ_HZ: u32 = 12_000_000u32; diff --git a/src/main.rs b/src/main.rs index 5e33c9c..cd5cd11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,26 @@ #![no_std] #![no_main] -use panic_halt as _; +use embedded_hal::delay::DelayNs; +use fugit::RateExtU32; +use panic_halt as _; // Needed so its built & linked correctly // Alias for our HAL crate use rp235x_hal as hal; -// Some things we need -use embedded_hal::delay::DelayNs; -use embedded_hal::digital::OutputPin; +// RP235x HAL +use hal::uart::DataBits; +use hal::uart::StopBits; +use hal::uart::UartConfig; +use hal::uart::UartPeripheral; +use hal::Clock; + +mod constants; #[link_section = ".start_block"] #[used] pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe(); -/// Pico 2 W on-board crystal oscillator frequency (AEL 12.0) -const XTAL_FREQ_HZ: u32 = 12_000_000u32; - #[hal::entry] fn main() -> ! { let mut peripherals = hal::pac::Peripherals::take().unwrap(); @@ -24,7 +28,7 @@ fn main() -> ! { let mut watchdog = hal::Watchdog::new(peripherals.WATCHDOG); let clocks = hal::clocks::init_clocks_and_plls( - XTAL_FREQ_HZ, + constants::XTAL_FREQ_HZ, peripherals.XOSC, peripherals.CLOCKS, peripherals.PLL_SYS, @@ -45,12 +49,18 @@ fn main() -> ! { &mut peripherals.RESETS, ); - let mut led_pin = pins.gpio19.into_push_pull_output(); + let uart0_pins = (pins.gpio0.into_function(), pins.gpio1.into_function()); + + let uart = UartPeripheral::new(peripherals.UART0, uart0_pins, &mut peripherals.RESETS) + .enable( + UartConfig::new(9600_u32.Hz(), DataBits::Eight, None, StopBits::One), + clocks.peripheral_clock.freq(), + ) + .unwrap(); + loop { - led_pin.set_high().unwrap(); - timer.delay_ms(1000); - led_pin.set_low().unwrap(); - timer.delay_ms(1000); + uart.write_full_blocking(b"hello, world!\r\n"); + timer.delay_ms(500); } }