Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e-hal-v1 #172

Merged
merged 15 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/stm32-rs/stm32g4xx-hal"
version = "0.0.2"

[dependencies]
nb = "0.1.1"
nb = "1"
stm32g4 = "0.15.1"
paste = "1.0"
bitflags = "1.2"
Expand All @@ -21,6 +21,7 @@ static_assertions = "1.1"
fugit = "0.3.7"
stm32-usbd = { version = "0.7.0", optional = true }
fixed = { version = "1.28.0", optional = true }
embedded-io = "0.6"

[dependencies.cortex-m]
version = "0.7.7"
Expand All @@ -38,9 +39,13 @@ default-features = false
features = ["const-fn"]
version = "0.2.5"

[dependencies.embedded-hal]
[dependencies.embedded-hal-old]
package = "embedded-hal"
features = ["unproven"]
version = "0.2.4"
version = "0.2.7"

[dependencies.embedded-hal]
version = "1.0.0"

[dependencies.embedded-dma]
version = "0.1.2"
Expand Down Expand Up @@ -93,7 +98,7 @@ stm32g4a1 = ["stm32g4/stm32g4a1"]
log-itm = ["cortex-m-log/itm"]
log-rtt = []
log-semihost = ["cortex-m-log/semihosting"]
defmt = ["dep:defmt", "fugit/defmt"]
defmt = ["dep:defmt", "fugit/defmt", "nb/defmt-0-3", "embedded-hal/defmt-03", "embedded-io/defmt-03"]
cordic = ["dep:fixed"]

[profile.dev]
Expand Down
3 changes: 2 additions & 1 deletion examples/adc-continious-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![no_main]

mod utils;
use utils::logger::info;

use crate::hal::{
adc::{
Expand All @@ -20,7 +21,6 @@ use crate::hal::{
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -48,6 +48,7 @@ fn main() -> ! {

info!("Setup Adc1");
let mut delay = cp.SYST.delay(&rcc.clocks);

let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
3 changes: 1 addition & 2 deletions examples/adc-continious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ 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 as hal;
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};

use cortex_m_rt::entry;

Expand Down
7 changes: 3 additions & 4 deletions examples/adc-one-shot-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ 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 as hal;

use log::info;
use stm32g4xx_hal::{self as hal, delay::SYSTDelayExt};

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -49,6 +47,7 @@ fn main() -> ! {

info!("Setup Adc1");
let mut delay = cp.SYST.delay(&rcc.clocks);

let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
4 changes: 1 addition & 3 deletions examples/adc-one-shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use crate::hal::{
adc::{config::SampleTime, AdcClaim},
delay::SYSTDelayExt,
pwr::PwrExt,
rcc::Config,
stm32::Peripherals,
Expand All @@ -13,10 +12,9 @@ use stm32g4xx_hal as hal;

use cortex_m_rt::entry;

use log::info;

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down
5 changes: 2 additions & 3 deletions examples/blinky_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![no_main]
#![no_std]

use hal::delay::DelayFromCountDownTimer;
use hal::prelude::*;
use hal::pwr::PwrExt;
use hal::rcc::Config;
Expand Down Expand Up @@ -37,7 +36,7 @@ fn main() -> ! {

info!("Init Timer2 delay");
let timer2 = Timer::new(dp.TIM2, &rcc.clocks);
let mut delay_tim2 = DelayFromCountDownTimer::new(timer2.start_count_down(100.millis()));
let mut delay_tim2 = timer2.start_count_down(100.millis()).delay();

loop {
info!("Toggle");
Expand All @@ -47,6 +46,6 @@ fn main() -> ! {
info!("Toggle");
led.toggle().unwrap();
info!("TIM2 delay");
delay_tim2.delay_ms(1000_u16);
delay_tim2.delay_ms(1000);
}
}
2 changes: 1 addition & 1 deletion examples/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use core::cell::RefCell;
use core::sync::atomic::{AtomicBool, Ordering};
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;

type ButtonPin = gpioc::PC13<Input<PullDown>>;

Expand Down
5 changes: 2 additions & 3 deletions examples/can-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ use core::num::{NonZeroU16, NonZeroU8};

use cortex_m_rt::entry;

use log::info;

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -116,7 +115,7 @@ fn main() -> ! {
bit_rate_switching: false,
marker: None,
};
info!("Initial Header: {:#X?}", &header);
info!("Initial Header: {:#?}", &header);

info!("Transmit initial message");
block!(can.transmit(header, &buffer)).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/comp_w_dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use rt::entry;

#[entry]
fn main() -> ! {
use embedded_hal::Direction;
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");
Expand Down
5 changes: 3 additions & 2 deletions examples/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#![no_main]
#![no_std]

use embedded_hal::Direction;
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;

Expand All @@ -26,6 +26,7 @@ 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);
Expand Down
4 changes: 2 additions & 2 deletions examples/i2c-bme680.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use bme680::*;
use core::time::Duration;
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::delay::DelayNs;
use hal::delay::DelayFromCountDownTimer;
use hal::i2c::Config;
use hal::prelude::*;
Expand All @@ -15,10 +15,10 @@ use hal::timer::Timer;
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
use log::info;

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down
2 changes: 1 addition & 1 deletion examples/i2c-mpu6050.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use hal::time::{ExtU32, RateExtU32};
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
use log::info;
use mpu6050::*;

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down
4 changes: 2 additions & 2 deletions examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use hal::time::RateExtU32;
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
use log::info;

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand All @@ -35,7 +35,7 @@ fn main() -> ! {

let buf: [u8; 1] = [0];
loop {
match i2c.write(0x3c, &buf) {
match i2c.write(0x3Cu8, &buf) {
Ok(_) => info!("ok"),
Err(err) => info!("error: {:?}", err),
}
Expand Down
3 changes: 2 additions & 1 deletion examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use hal::stm32;
use hal::time::RateExtU32;
use stm32g4xx_hal as hal;
extern crate cortex_m_rt as rt;
use hal::prelude::SetDutyCycle;

#[macro_use]
mod utils;
Expand All @@ -26,7 +27,7 @@ fn main() -> ! {

let mut pwm = dp.TIM1.pwm(pin, 100.Hz(), &mut rcc);

pwm.set_duty(pwm.get_max_duty() / 2);
let _ = pwm.set_duty_cycle_percent(50);
pwm.enable();

loop {
Expand Down
3 changes: 2 additions & 1 deletion examples/spi-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use stm32g4xx_hal::dma::TransferExt;

#[macro_use]
mod utils;
// use utils::logger::info;

const BUFFER_SIZE: usize = 254;

Expand Down Expand Up @@ -68,6 +69,6 @@ fn main() -> ! {
.into_memory_to_peripheral_transfer(spi.enable_tx_dma(), &mut dma_buf[..], config);
transfer_dma.start(|_spi| {});
loop {
delay_tim2.delay_ms(1000_u16);
delay_tim2.delay_ms(1000);
}
}
8 changes: 4 additions & 4 deletions examples/spi-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#![no_main]
#![no_std]

use crate::hal::{
block,
use hal::{
delay::DelayFromCountDownTimer,
gpio::gpioa::PA5,
gpio::gpioa::PA6,
gpio::gpioa::PA7,
gpio::Alternate,
gpio::AF5,
hal_02::spi::FullDuplex,
prelude::*,
pwr::PwrExt,
rcc::Config,
Expand Down Expand Up @@ -59,11 +59,11 @@ fn main() -> ! {
for byte in message.iter() {
cs.set_low().unwrap();
spi.send(*byte as u8).unwrap();
received_byte = block!(spi.read()).unwrap();
received_byte = nb::block!(FullDuplex::read(&mut spi)).unwrap();
cs.set_high().unwrap();

info!("{}", received_byte as char);
}
delay_tim2.delay_ms(1000_u16);
delay_tim2.delay_ms(1000);
}
}
2 changes: 1 addition & 1 deletion examples/uart-dma-tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use hal::{rcc, stm32};
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
use log::info;

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down
4 changes: 3 additions & 1 deletion examples/uart-fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ 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;
use log::info;

#[macro_use]
mod utils;
use utils::logger::info;

#[entry]
fn main() -> ! {
Expand Down
8 changes: 5 additions & 3 deletions examples/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#![no_main]
#![no_std]

use core::fmt::Write;

use embedded_io::{Read, Write};
use hal::prelude::*;
use hal::pwr::PwrExt;
use hal::serial::FullConfig;
Expand Down Expand Up @@ -54,10 +53,13 @@ fn main() -> ! {
.unwrap();

writeln!(usart, "Hello USART3, yay!!\r\n").unwrap();
let mut read_buf = [0u8; 8];
usart.read_exact(&mut read_buf).unwrap();
usart.write_all(&read_buf).unwrap();

let mut cnt = 0;
loop {
match block!(usart.read()) {
match block!(embedded_hal_old::serial::Read::read(&mut usart)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because the name conflicts with the new trait's
We might want to switch to embedded-io altogether for the examples?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this? 1b768d7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks fine

Ok(byte) => writeln!(usart, "{}: {}\r", cnt, byte).unwrap(),
Err(e) => writeln!(usart, "E: {:?}\r", e).unwrap(),
};
Expand Down
Loading
Loading