diff options
| author | JanKomarekNXP <[email protected]> | 2025-12-01 18:07:58 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-01 18:07:58 +0100 |
| commit | 717346f21a4cf4993c2a1f046e26b6bf647d89cb (patch) | |
| tree | 81dcbf726769eac5726796b340542d661debe407 /examples | |
| parent | 3b239cb6de22b7bb8c2d87defb3205294653be7a (diff) | |
Refactor LPUART driver constructors (#51)
* Refactor LPUART driver constructors
- Add new_with_rtscts(), new_with_rts(), new_with_cts() methods
- Store RTS/CTS pins in TX/RX structs
- Remove enable_tx, enable_rx, enable_rx_rts, enable_tx_cts config fields
- Fix typo: msb_firs -> msb_first
- Fix write_byte() return type to Result<()>
* Update hello example for LPUART driver changes
* Refactor buffered LPUART initialization logic
- Split new_inner() into init_common() and separate helpers for full-duplex, TX-only, and RX-only
- Replace assert!() with proper Error::InvalidArgument returns for empty buffers
- Simplify constructor implementations by removing expect() calls
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/src/bin/hello.rs | 14 | ||||
| -rw-r--r-- | examples/src/bin/lpuart_buffered.rs | 2 | ||||
| -rw-r--r-- | examples/src/bin/lpuart_polling.rs | 2 |
3 files changed, 8 insertions, 10 deletions
diff --git a/examples/src/bin/hello.rs b/examples/src/bin/hello.rs index f426d1898..e371d9413 100644 --- a/examples/src/bin/hello.rs +++ b/examples/src/bin/hello.rs | |||
| @@ -2,27 +2,29 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use embassy_executor::Spawner; | 4 | use embassy_executor::Spawner; |
| 5 | use embassy_mcxa::clocks::config::Div8; | ||
| 5 | use hal::lpuart::{Blocking, Config, Lpuart}; | 6 | use hal::lpuart::{Blocking, Config, Lpuart}; |
| 6 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | 7 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; |
| 7 | 8 | ||
| 8 | /// Simple helper to write a byte as hex to UART | 9 | /// Simple helper to write a byte as hex to UART |
| 9 | fn write_hex_byte(uart: &mut Lpuart<'_, Blocking>, byte: u8) { | 10 | fn write_hex_byte(uart: &mut Lpuart<'_, Blocking>, byte: u8) { |
| 10 | const HEX_DIGITS: &[u8] = b"0123456789ABCDEF"; | 11 | const HEX_DIGITS: &[u8] = b"0123456789ABCDEF"; |
| 11 | uart.write_byte(HEX_DIGITS[(byte >> 4) as usize]); | 12 | let _ = uart.write_byte(HEX_DIGITS[(byte >> 4) as usize]); |
| 12 | uart.write_byte(HEX_DIGITS[(byte & 0xF) as usize]); | 13 | let _ = uart.write_byte(HEX_DIGITS[(byte & 0xF) as usize]); |
| 13 | } | 14 | } |
| 14 | 15 | ||
| 15 | #[embassy_executor::main] | 16 | #[embassy_executor::main] |
| 16 | async fn main(_spawner: Spawner) { | 17 | async fn main(_spawner: Spawner) { |
| 17 | let p = hal::init(hal::config::Config::default()); | 18 | let mut cfg = hal::config::Config::default(); |
| 19 | cfg.clock_cfg.sirc.fro_12m_enabled = true; | ||
| 20 | cfg.clock_cfg.sirc.fro_lf_div = Some(Div8::no_div()); | ||
| 21 | let p = hal::init(cfg); | ||
| 18 | 22 | ||
| 19 | defmt::info!("boot"); | 23 | defmt::info!("boot"); |
| 20 | 24 | ||
| 21 | // Create UART configuration | 25 | // Create UART configuration |
| 22 | let config = Config { | 26 | let config = Config { |
| 23 | baudrate_bps: 115_200, | 27 | baudrate_bps: 115_200, |
| 24 | enable_tx: true, | ||
| 25 | enable_rx: true, | ||
| 26 | ..Default::default() | 28 | ..Default::default() |
| 27 | }; | 29 | }; |
| 28 | 30 | ||
| @@ -97,7 +99,7 @@ async fn main(_spawner: Spawner) { | |||
| 97 | // Regular character | 99 | // Regular character |
| 98 | buffer[buf_idx] = byte; | 100 | buffer[buf_idx] = byte; |
| 99 | buf_idx += 1; | 101 | buf_idx += 1; |
| 100 | uart.write_byte(byte); | 102 | let _ = uart.write_byte(byte); |
| 101 | } | 103 | } |
| 102 | } | 104 | } |
| 103 | } | 105 | } |
diff --git a/examples/src/bin/lpuart_buffered.rs b/examples/src/bin/lpuart_buffered.rs index 7f77d557d..420589d00 100644 --- a/examples/src/bin/lpuart_buffered.rs +++ b/examples/src/bin/lpuart_buffered.rs | |||
| @@ -27,8 +27,6 @@ async fn main(_spawner: Spawner) { | |||
| 27 | // UART configuration (enable both TX and RX) | 27 | // UART configuration (enable both TX and RX) |
| 28 | let config = Config { | 28 | let config = Config { |
| 29 | baudrate_bps: 115_200, | 29 | baudrate_bps: 115_200, |
| 30 | enable_tx: true, | ||
| 31 | enable_rx: true, | ||
| 32 | rx_fifo_watermark: 0, | 30 | rx_fifo_watermark: 0, |
| 33 | tx_fifo_watermark: 0, | 31 | tx_fifo_watermark: 0, |
| 34 | ..Default::default() | 32 | ..Default::default() |
diff --git a/examples/src/bin/lpuart_polling.rs b/examples/src/bin/lpuart_polling.rs index 9cea418cc..b80668834 100644 --- a/examples/src/bin/lpuart_polling.rs +++ b/examples/src/bin/lpuart_polling.rs | |||
| @@ -19,8 +19,6 @@ async fn main(_spawner: Spawner) { | |||
| 19 | // Create UART configuration | 19 | // Create UART configuration |
| 20 | let config = Config { | 20 | let config = Config { |
| 21 | baudrate_bps: 115_200, | 21 | baudrate_bps: 115_200, |
| 22 | enable_tx: true, | ||
| 23 | enable_rx: true, | ||
| 24 | ..Default::default() | 22 | ..Default::default() |
| 25 | }; | 23 | }; |
| 26 | 24 | ||
