Skip to content

Commit 3ee9a6d

Browse files
committed
Cleanup examples
1 parent 4982a32 commit 3ee9a6d

File tree

7 files changed

+22
-21
lines changed

7 files changed

+22
-21
lines changed

examples/adc-continious.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ use crate::hal::{
66
config::{Continuous, Resolution, SampleTime, Sequence},
77
AdcClaim, ClockSource, Temperature, Vref,
88
},
9+
delay::SYSTDelayExt,
910
gpio::GpioExt,
1011
pwr::PwrExt,
1112
rcc::{Config, RccExt},
1213
signature::{VrefCal, VDDA_CALIB},
1314
stm32::Peripherals,
1415
};
15-
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};
16+
use stm32g4xx_hal as hal;
1617

1718
use cortex_m_rt::entry;
1819

examples/adc-one-shot-dma.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ use crate::hal::{
88
config::{Continuous, Dma as AdcDma, Resolution, SampleTime, Sequence},
99
AdcClaim, ClockSource, Temperature,
1010
},
11+
delay::SYSTDelayExt,
1112
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
1213
gpio::GpioExt,
1314
pwr::PwrExt,
1415
rcc::{Config, RccExt},
1516
stm32::Peripherals,
1617
};
17-
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};
18+
use stm32g4xx_hal as hal;
1819

1920
#[macro_use]
2021
mod utils;

examples/comp_w_dac.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ fn main() -> ! {
1313
use embedded_hal_old::Direction;
1414
use hal::comparator::{self, ComparatorExt, ComparatorSplit};
1515
use hal::dac::{Dac1IntSig1, DacExt, DacOut};
16+
use hal::delay::SYSTDelayExt;
1617
use hal::gpio::GpioExt;
1718
use hal::rcc::RccExt;
1819
use hal::stm32;
1920
use stm32g4xx_hal as hal;
20-
use stm32g4xx_hal::delay::SYSTDelayExt;
2121

2222
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
2323
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

examples/dac.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
#![no_main]
99
#![no_std]
1010

11-
use embedded_hal_old::Direction;
1211
use hal::dac::{DacExt, DacOut, GeneratorConfig};
12+
use hal::delay::SYSTDelayExt;
1313
use hal::gpio::GpioExt;
1414
use hal::rcc::RccExt;
1515
use stm32g4xx_hal as hal;
16-
use stm32g4xx_hal::delay::SYSTDelayExt;
1716
mod utils;
1817
extern crate cortex_m_rt as rt;
1918

@@ -26,7 +25,6 @@ fn main() -> ! {
2625
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
2726

2827
let mut rcc = dp.RCC.constrain();
29-
// cortex-m doesn't yet support hal-1 DelayNs on systick (PR #504)
3028
let mut delay = cp.SYST.delay(&rcc.clocks);
3129

3230
let gpioa = dp.GPIOA.split(&mut rcc);

examples/uart-fifo.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ extern crate cortex_m_rt as rt;
77

88
use core::fmt::Write;
99

10+
use embedded_io::{Read, ReadReady};
1011
use hal::prelude::*;
1112
use hal::pwr::PwrExt;
1213
use hal::serial::*;
1314
use hal::{rcc, stm32};
1415
use stm32g4xx_hal as hal;
15-
// TODO: switch to embedded-hal-nb
16-
use hal::hal_02::serial::Read;
1716

1817
use cortex_m_rt::entry;
1918

@@ -52,23 +51,24 @@ fn main() -> ! {
5251

5352
let (mut tx1, mut rx1) = usart.split();
5453

54+
let mut buffer = [0; 4];
5555
let mut cnt = 0;
5656
loop {
5757
if rx1.fifo_threshold_reached() {
5858
loop {
59-
match rx1.read() {
60-
Err(nb::Error::WouldBlock) => {
61-
// no more data available in fifo
62-
break;
63-
}
64-
Err(nb::Error::Other(_err)) => {
59+
match rx1.read_ready() {
60+
Ok(true) => (),
61+
Ok(false) => break, // no more data available in fifo
62+
Err(e) => {
6563
// Handle other error Overrun, Framing, Noise or Parity
66-
}
67-
Ok(byte) => {
68-
writeln!(tx1, "{}: {}\r", cnt, byte).unwrap();
69-
cnt += 1;
64+
utils::logger::error!("Error: {:?}", e);
7065
}
7166
}
67+
68+
let count = rx1.read(&mut buffer).unwrap();
69+
let bytes = &buffer[count];
70+
writeln!(tx1, "{}: {}\r", cnt, bytes).unwrap();
71+
cnt += count;
7272
}
7373
}
7474
}

examples/uart.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use hal::{rcc, stm32};
1111
use stm32g4xx_hal as hal;
1212

1313
use cortex_m_rt::entry;
14-
use nb::block;
1514
use utils::logger::info;
1615

1716
#[macro_use]
@@ -57,10 +56,11 @@ fn main() -> ! {
5756
usart.read_exact(&mut read_buf).unwrap();
5857
usart.write_all(&read_buf).unwrap();
5958

59+
let mut single_byte_buffer = [0; 1];
6060
let mut cnt = 0;
6161
loop {
62-
match block!(embedded_hal_old::serial::Read::read(&mut usart)) {
63-
Ok(byte) => writeln!(usart, "{}: {}\r", cnt, byte).unwrap(),
62+
match usart.read_exact(&mut single_byte_buffer) {
63+
Ok(()) => writeln!(usart, "{}: {}\r", cnt, single_byte_buffer[0]).unwrap(),
6464
Err(e) => writeln!(usart, "E: {:?}\r", e).unwrap(),
6565
};
6666
cnt += 1;

src/serial/usart.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use embedded_io::{ReadReady, WriteReady};
1818
use crate::serial::config::*;
1919
/// Serial error
2020
#[derive(Debug)]
21+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2122
pub enum Error {
2223
/// Framing error
2324
Framing,

0 commit comments

Comments
 (0)