From a4cd117e0a8985638cc12bbe907ad5a8569dc558 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 19 Jan 2025 19:30:39 +0100 Subject: [PATCH] Cleanup examples --- examples/adc-continious.rs | 3 ++- examples/adc-one-shot-dma.rs | 3 ++- examples/comp_w_dac.rs | 8 ++++++-- examples/dac.rs | 9 ++++++--- examples/uart-fifo.rs | 24 ++++++++++++------------ examples/uart.rs | 6 +++--- src/serial/usart.rs | 1 + 7 files changed, 32 insertions(+), 22 deletions(-) diff --git a/examples/adc-continious.rs b/examples/adc-continious.rs index 27445bb1..53f12e1a 100644 --- a/examples/adc-continious.rs +++ b/examples/adc-continious.rs @@ -6,13 +6,14 @@ use crate::hal::{ config::{Continuous, Resolution, SampleTime, Sequence}, AdcClaim, ClockSource, Temperature, Vref, }, + delay::SYSTDelayExt, gpio::GpioExt, pwr::PwrExt, rcc::{Config, RccExt}, signature::{VrefCal, VDDA_CALIB}, stm32::Peripherals, }; -use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt}; +use stm32g4xx_hal as hal; use cortex_m_rt::entry; diff --git a/examples/adc-one-shot-dma.rs b/examples/adc-one-shot-dma.rs index 62c5429a..7226f6a2 100644 --- a/examples/adc-one-shot-dma.rs +++ b/examples/adc-one-shot-dma.rs @@ -8,13 +8,14 @@ use crate::hal::{ config::{Continuous, Dma as AdcDma, Resolution, SampleTime, Sequence}, AdcClaim, ClockSource, Temperature, }, + delay::SYSTDelayExt, dma::{config::DmaConfig, stream::DMAExt, TransferExt}, gpio::GpioExt, pwr::PwrExt, rcc::{Config, RccExt}, stm32::Peripherals, }; -use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt}; +use stm32g4xx_hal as hal; #[macro_use] mod utils; diff --git a/examples/comp_w_dac.rs b/examples/comp_w_dac.rs index f7be226c..a2a3d703 100644 --- a/examples/comp_w_dac.rs +++ b/examples/comp_w_dac.rs @@ -10,14 +10,13 @@ use rt::entry; #[entry] fn main() -> ! { - use embedded_hal_old::Direction; use hal::comparator::{self, ComparatorExt, ComparatorSplit}; use hal::dac::{Dac1IntSig1, DacExt, DacOut}; + use hal::delay::SYSTDelayExt; use hal::gpio::GpioExt; use hal::rcc::RccExt; use hal::stm32; use stm32g4xx_hal as hal; - use stm32g4xx_hal::delay::SYSTDelayExt; let dp = stm32::Peripherals::take().expect("cannot take peripherals"); let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals"); @@ -49,6 +48,11 @@ fn main() -> ! { comp.output_pin(led2); let _comp1 = comp.enable().lock(); + enum Direction { + Upcounting, + Downcounting, + } + let mut dir = Direction::Upcounting; let mut val = 0; diff --git a/examples/dac.rs b/examples/dac.rs index 0e6bf21d..45552572 100644 --- a/examples/dac.rs +++ b/examples/dac.rs @@ -8,12 +8,11 @@ #![no_main] #![no_std] -use embedded_hal_old::Direction; use hal::dac::{DacExt, DacOut, GeneratorConfig}; +use hal::delay::SYSTDelayExt; use hal::gpio::GpioExt; use hal::rcc::RccExt; use stm32g4xx_hal as hal; -use stm32g4xx_hal::delay::SYSTDelayExt; mod utils; extern crate cortex_m_rt as rt; @@ -26,7 +25,6 @@ fn main() -> ! { let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals"); let mut rcc = dp.RCC.constrain(); - // cortex-m doesn't yet support hal-1 DelayNs on systick (PR #504) let mut delay = cp.SYST.delay(&rcc.clocks); let gpioa = dp.GPIOA.split(&mut rcc); @@ -38,6 +36,11 @@ fn main() -> ! { // dac_generator will have its value set automatically from its internal noise generator let mut dac_generator = dac1ch2.enable_generator(GeneratorConfig::noise(11)); + enum Direction { + Upcounting, + Downcounting, + } + let mut dir = Direction::Upcounting; let mut val = 0; diff --git a/examples/uart-fifo.rs b/examples/uart-fifo.rs index 74084b97..801ae471 100644 --- a/examples/uart-fifo.rs +++ b/examples/uart-fifo.rs @@ -7,13 +7,12 @@ extern crate cortex_m_rt as rt; use core::fmt::Write; +use embedded_io::{Read, ReadReady}; use hal::prelude::*; use hal::pwr::PwrExt; use hal::serial::*; use hal::{rcc, stm32}; use stm32g4xx_hal as hal; -// TODO: switch to embedded-hal-nb -use hal::hal_02::serial::Read; use cortex_m_rt::entry; @@ -52,23 +51,24 @@ fn main() -> ! { let (mut tx1, mut rx1) = usart.split(); + let mut buffer = [0; 4]; let mut cnt = 0; loop { if rx1.fifo_threshold_reached() { loop { - match rx1.read() { - Err(nb::Error::WouldBlock) => { - // no more data available in fifo - break; - } - Err(nb::Error::Other(_err)) => { + match rx1.read_ready() { + Ok(true) => (), + Ok(false) => break, // no more data available in fifo + Err(e) => { // Handle other error Overrun, Framing, Noise or Parity - } - Ok(byte) => { - writeln!(tx1, "{}: {}\r", cnt, byte).unwrap(); - cnt += 1; + utils::logger::error!("Error: {:?}", e); } } + + let count = rx1.read(&mut buffer).unwrap(); + let bytes = &buffer[count]; + writeln!(tx1, "{}: {}\r", cnt, bytes).unwrap(); + cnt += count; } } } diff --git a/examples/uart.rs b/examples/uart.rs index a7154f39..e4ec6519 100644 --- a/examples/uart.rs +++ b/examples/uart.rs @@ -11,7 +11,6 @@ use hal::{rcc, stm32}; use stm32g4xx_hal as hal; use cortex_m_rt::entry; -use nb::block; use utils::logger::info; #[macro_use] @@ -57,10 +56,11 @@ fn main() -> ! { usart.read_exact(&mut read_buf).unwrap(); usart.write_all(&read_buf).unwrap(); + let mut single_byte_buffer = [0; 1]; let mut cnt = 0; loop { - match block!(embedded_hal_old::serial::Read::read(&mut usart)) { - Ok(byte) => writeln!(usart, "{}: {}\r", cnt, byte).unwrap(), + match usart.read_exact(&mut single_byte_buffer) { + Ok(()) => writeln!(usart, "{}: {}\r", cnt, single_byte_buffer[0]).unwrap(), Err(e) => writeln!(usart, "E: {:?}\r", e).unwrap(), }; cnt += 1; diff --git a/src/serial/usart.rs b/src/serial/usart.rs index 4aa5ae41..f4a5dc12 100644 --- a/src/serial/usart.rs +++ b/src/serial/usart.rs @@ -18,6 +18,7 @@ use embedded_io::{ReadReady, WriteReady}; use crate::serial::config::*; /// Serial error #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { /// Framing error Framing,