mirror of
https://github.com/italicsjenga/rp-hal-boards.git
synced 2025-01-11 04:51:31 +11:00
Merge pull request #471 from jannic/uart-config-constructor
Implement UartConfig::new constructor method
This commit is contained in:
commit
de673fb192
|
@ -38,6 +38,9 @@ use rp_pico::hal::pac;
|
||||||
// higher-level drivers.
|
// higher-level drivers.
|
||||||
use rp_pico::hal;
|
use rp_pico::hal;
|
||||||
|
|
||||||
|
// UART related types
|
||||||
|
use hal::uart::{DataBits, StopBits, UartConfig};
|
||||||
|
|
||||||
/// Prints the temperature received from the sensor
|
/// Prints the temperature received from the sensor
|
||||||
fn print_temperature(serial: &mut impl FmtWrite, temp: [u8; 2]) {
|
fn print_temperature(serial: &mut impl FmtWrite, temp: [u8; 2]) {
|
||||||
let temp_i16 = i16::from_be_bytes(temp) >> 5;
|
let temp_i16 = i16::from_be_bytes(temp) >> 5;
|
||||||
|
@ -97,7 +100,7 @@ fn main() -> ! {
|
||||||
|
|
||||||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||||
.enable(
|
.enable(
|
||||||
hal::uart::common_configs::_115200_8_N_1,
|
UartConfig::new(115_200.Hz(), DataBits::Eight, None, StopBits::One),
|
||||||
clocks.peripheral_clock.freq(),
|
clocks.peripheral_clock.freq(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -30,6 +30,9 @@ use rp2040_hal::Clock;
|
||||||
// The macro for our start-up function
|
// The macro for our start-up function
|
||||||
use rp_pico::entry;
|
use rp_pico::entry;
|
||||||
|
|
||||||
|
// Time handling traits
|
||||||
|
use fugit::RateExtU32;
|
||||||
|
|
||||||
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
||||||
// be linked)
|
// be linked)
|
||||||
use panic_halt as _;
|
use panic_halt as _;
|
||||||
|
@ -52,6 +55,9 @@ use heapless::spsc::Queue;
|
||||||
/// Import the GPIO pins we use
|
/// Import the GPIO pins we use
|
||||||
use hal::gpio::pin::bank0::{Gpio0, Gpio1};
|
use hal::gpio::pin::bank0::{Gpio0, Gpio1};
|
||||||
|
|
||||||
|
// UART related types
|
||||||
|
use hal::uart::{DataBits, StopBits, UartConfig};
|
||||||
|
|
||||||
/// Alias the type for our UART pins to make things clearer.
|
/// Alias the type for our UART pins to make things clearer.
|
||||||
type UartPins = (
|
type UartPins = (
|
||||||
hal::gpio::Pin<Gpio0, hal::gpio::Function<hal::gpio::Uart>>,
|
hal::gpio::Pin<Gpio0, hal::gpio::Function<hal::gpio::Uart>>,
|
||||||
|
@ -134,7 +140,7 @@ fn main() -> ! {
|
||||||
// Make a UART on the given pins
|
// Make a UART on the given pins
|
||||||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||||
.enable(
|
.enable(
|
||||||
hal::uart::common_configs::_9600_8_N_1,
|
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
||||||
clocks.peripheral_clock.freq(),
|
clocks.peripheral_clock.freq(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -28,6 +28,9 @@ use rp2040_hal::Clock;
|
||||||
// The macro for our start-up function
|
// The macro for our start-up function
|
||||||
use rp_pico::entry;
|
use rp_pico::entry;
|
||||||
|
|
||||||
|
// Time handling traits
|
||||||
|
use fugit::RateExtU32;
|
||||||
|
|
||||||
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
||||||
// be linked)
|
// be linked)
|
||||||
use panic_halt as _;
|
use panic_halt as _;
|
||||||
|
@ -49,6 +52,9 @@ use critical_section::Mutex;
|
||||||
/// Import the GPIO pins we use
|
/// Import the GPIO pins we use
|
||||||
use hal::gpio::pin::bank0::{Gpio0, Gpio1};
|
use hal::gpio::pin::bank0::{Gpio0, Gpio1};
|
||||||
|
|
||||||
|
// UART related types
|
||||||
|
use hal::uart::{DataBits, StopBits, UartConfig};
|
||||||
|
|
||||||
/// Alias the type for our UART pins to make things clearer.
|
/// Alias the type for our UART pins to make things clearer.
|
||||||
type UartPins = (
|
type UartPins = (
|
||||||
hal::gpio::Pin<Gpio0, hal::gpio::Function<hal::gpio::Uart>>,
|
hal::gpio::Pin<Gpio0, hal::gpio::Function<hal::gpio::Uart>>,
|
||||||
|
@ -118,7 +124,7 @@ fn main() -> ! {
|
||||||
// Make a UART on the given pins
|
// Make a UART on the given pins
|
||||||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||||
.enable(
|
.enable(
|
||||||
hal::uart::common_configs::_9600_8_N_1,
|
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
||||||
clocks.peripheral_clock.freq(),
|
clocks.peripheral_clock.freq(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
- Update embedded-hal alpha support to version 1.0.0-alpha.9 - @jannic
|
- Update embedded-hal alpha support to version 1.0.0-alpha.9 - @jannic
|
||||||
(Non-blocking traits were moved to embedded-hal-nb, which is not yet supported)
|
(Non-blocking traits were moved to embedded-hal-nb, which is not yet supported)
|
||||||
|
- Implement UartConfig::new constructor method - @jannic
|
||||||
|
- Deprecate uart::common_configs - @jannic
|
||||||
|
|
||||||
## [0.6.0] - 2022-08-26
|
## [0.6.0] - 2022-08-26
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,12 @@ use rp2040_hal as hal;
|
||||||
// Some traits we need
|
// Some traits we need
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
use embedded_hal::adc::OneShot;
|
use embedded_hal::adc::OneShot;
|
||||||
|
use fugit::RateExtU32;
|
||||||
use rp2040_hal::Clock;
|
use rp2040_hal::Clock;
|
||||||
|
|
||||||
|
// UART related types
|
||||||
|
use hal::uart::{DataBits, StopBits, UartConfig};
|
||||||
|
|
||||||
// A shorter alias for the Peripheral Access Crate, which provides low-level
|
// A shorter alias for the Peripheral Access Crate, which provides low-level
|
||||||
// register access
|
// register access
|
||||||
use hal::pac;
|
use hal::pac;
|
||||||
|
@ -91,7 +95,7 @@ fn main() -> ! {
|
||||||
// Create a UART driver
|
// Create a UART driver
|
||||||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||||
.enable(
|
.enable(
|
||||||
hal::uart::common_configs::_9600_8_N_1,
|
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
||||||
clocks.peripheral_clock.freq(),
|
clocks.peripheral_clock.freq(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -22,8 +22,12 @@ use hal::pac;
|
||||||
|
|
||||||
// Some traits we need
|
// Some traits we need
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
use fugit::RateExtU32;
|
||||||
use hal::Clock;
|
use hal::Clock;
|
||||||
|
|
||||||
|
// UART related types
|
||||||
|
use hal::uart::{DataBits, StopBits, UartConfig};
|
||||||
|
|
||||||
/// The linker will place this boot block at the start of our program image. We
|
/// The linker will place this boot block at the start of our program image. We
|
||||||
/// need this to help the ROM bootloader get our code up and running.
|
/// need this to help the ROM bootloader get our code up and running.
|
||||||
/// Note: This boot block is not necessary when using a rp-hal based BSP
|
/// Note: This boot block is not necessary when using a rp-hal based BSP
|
||||||
|
@ -88,7 +92,7 @@ fn main() -> ! {
|
||||||
);
|
);
|
||||||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||||
.enable(
|
.enable(
|
||||||
hal::uart::common_configs::_9600_8_N_1,
|
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
||||||
clocks.peripheral_clock.freq(),
|
clocks.peripheral_clock.freq(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -24,8 +24,12 @@ use hal::pac;
|
||||||
|
|
||||||
// Some traits we need
|
// Some traits we need
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
use fugit::RateExtU32;
|
||||||
use rp2040_hal::clocks::Clock;
|
use rp2040_hal::clocks::Clock;
|
||||||
|
|
||||||
|
// UART related types
|
||||||
|
use hal::uart::{DataBits, StopBits, UartConfig};
|
||||||
|
|
||||||
/// The linker will place this boot block at the start of our program image. We
|
/// The linker will place this boot block at the start of our program image. We
|
||||||
/// need this to help the ROM bootloader get our code up and running.
|
/// need this to help the ROM bootloader get our code up and running.
|
||||||
/// Note: This boot block is not necessary when using a rp-hal based BSP
|
/// Note: This boot block is not necessary when using a rp-hal based BSP
|
||||||
|
@ -88,7 +92,7 @@ fn main() -> ! {
|
||||||
);
|
);
|
||||||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||||
.enable(
|
.enable(
|
||||||
hal::uart::common_configs::_9600_8_N_1,
|
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
||||||
clocks.peripheral_clock.freq(),
|
clocks.peripheral_clock.freq(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -6,7 +6,8 @@
|
||||||
//!
|
//!
|
||||||
//! See [examples/uart.rs](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal/examples/uart.rs) for a more complete example
|
//! See [examples/uart.rs](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal/examples/uart.rs) for a more complete example
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use rp2040_hal::{Clock, clocks::init_clocks_and_plls, gpio::{Pins, FunctionUart}, pac, sio::Sio, uart::{self, UartPeripheral}, watchdog::Watchdog};
|
//! use rp2040_hal::{Clock, clocks::init_clocks_and_plls, gpio::{Pins, FunctionUart}, pac, sio::Sio, uart::{self, DataBits, StopBits, UartConfig, UartPeripheral}, watchdog::Watchdog};
|
||||||
|
//! use fugit::RateExtU32;
|
||||||
//!
|
//!
|
||||||
//! const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // Typically found in BSP crates
|
//! const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // Typically found in BSP crates
|
||||||
//!
|
//!
|
||||||
|
@ -24,7 +25,7 @@
|
||||||
//! // Need to perform clock init before using UART or it will freeze.
|
//! // Need to perform clock init before using UART or it will freeze.
|
||||||
//! let uart = UartPeripheral::new(peripherals.UART0, pins, &mut peripherals.RESETS)
|
//! let uart = UartPeripheral::new(peripherals.UART0, pins, &mut peripherals.RESETS)
|
||||||
//! .enable(
|
//! .enable(
|
||||||
//! uart::common_configs::_9600_8_N_1,
|
//! UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
||||||
//! clocks.peripheral_clock.freq(),
|
//! clocks.peripheral_clock.freq(),
|
||||||
//! ).unwrap();
|
//! ).unwrap();
|
||||||
//!
|
//!
|
||||||
|
@ -44,4 +45,5 @@ pub use self::utils::*;
|
||||||
pub use self::writer::Writer;
|
pub use self::writer::Writer;
|
||||||
|
|
||||||
/// Common configurations for UART.
|
/// Common configurations for UART.
|
||||||
|
#[deprecated(note = "Use UartConfig::new(...) instead.")]
|
||||||
pub mod common_configs;
|
pub mod common_configs;
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use fugit::HertzU32;
|
|
||||||
|
|
||||||
use crate::pac::{uart0::RegisterBlock, UART0, UART1};
|
use crate::pac::{uart0::RegisterBlock, UART0, UART1};
|
||||||
use crate::resets::SubsystemReset;
|
use crate::resets::SubsystemReset;
|
||||||
use core::ops::Deref;
|
use core::ops::Deref;
|
||||||
|
use fugit::HertzU32;
|
||||||
|
|
||||||
/// Error type for UART operations.
|
/// Error type for UART operations.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -84,6 +83,23 @@ pub struct UartConfig {
|
||||||
pub parity: Option<Parity>,
|
pub parity: Option<Parity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UartConfig {
|
||||||
|
/// Create a new instance of UartConfig
|
||||||
|
pub const fn new(
|
||||||
|
baudrate: HertzU32,
|
||||||
|
data_bits: DataBits,
|
||||||
|
parity: Option<Parity>,
|
||||||
|
stop_bits: StopBits,
|
||||||
|
) -> UartConfig {
|
||||||
|
UartConfig {
|
||||||
|
baudrate,
|
||||||
|
data_bits,
|
||||||
|
stop_bits,
|
||||||
|
parity,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Rx/Tx FIFO Watermark
|
/// Rx/Tx FIFO Watermark
|
||||||
///
|
///
|
||||||
/// Determine the FIFO level that trigger DMA/Interrupt
|
/// Determine the FIFO level that trigger DMA/Interrupt
|
||||||
|
|
Loading…
Reference in a new issue