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

Add embedded-hal 1.0 trait support #129

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ 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"
vcell = "0.1"
static_assertions = "1.1"
fugit = "0.3.5"
embedded-io = "0.6"

[dependencies.cortex-m]
version = "0.7.7"
Expand All @@ -36,9 +37,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 @@ -88,7 +93,7 @@ stm32g4a1 = ["stm32g4/stm32g4a1"]
log-itm = ["cortex-m-log/itm"]
log-rtt = []
log-semihost = ["cortex-m-log/semihosting"]
defmt-logging = ["defmt"]
defmt-logging = ["defmt", "nb/defmt-0-3", "embedded-hal/defmt-03", "embedded-io/defmt-03"]

[profile.dev]
codegen-units = 1
Expand Down
13 changes: 9 additions & 4 deletions examples/adc-continious-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
#![no_main]

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

use crate::hal::{
adc::{
config,
config::{Continuous, Dma as AdcDma, SampleTime, Sequence},
AdcClaim, ClockSource, Temperature, Vref,
},
delay::SYSTDelayExt,
delay::DelayFromCountDownTimer,
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
gpio::GpioExt,
pwr::PwrExt,
rcc::{Config, RccExt},
signature::{VrefCal, VDDA_CALIB},
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use stm32g4xx_hal as hal;

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

#[entry]
fn main() -> ! {
Expand All @@ -29,7 +31,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");
let rcc = dp.RCC.constrain();
Expand All @@ -47,7 +49,10 @@ fn main() -> ! {
let pa0 = gpioa.pa0.into_analog();

info!("Setup Adc1");
let mut delay = cp.SYST.delay(&rcc.clocks);
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
Comment on lines -50 to +55
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed? :)

Copy link
Author

Choose a reason for hiding this comment

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

I switched out the SYST delay provider because cortex-m doesn't implement the new delay trait yet, it's probably not necessary though

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any way to avoid this since I would imagine this would break a lot of users code?

Copy link
Author

Choose a reason for hiding this comment

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

Using the old e-hal 0.2 trait should still work, but the function names are in conflict with the new ones which are exported in the prelude

Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a good idea to see how/if this is solved in for example stm32-rs/stm32f4xx-hal

Copy link
Author

Choose a reason for hiding this comment

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

DelayNs has been implemented in rust-embedded/cortex-m@ec6b3ba but it hasn't been included in a release yet

let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
11 changes: 8 additions & 3 deletions examples/adc-continious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use crate::hal::{
config::{Continuous, Resolution, SampleTime, Sequence},
AdcClaim, ClockSource, Temperature, Vref,
},
delay::SYSTDelayExt,
delay::DelayFromCountDownTimer,
gpio::GpioExt,
pwr::PwrExt,
rcc::{Config, RccExt},
signature::{VrefCal, VDDA_CALIB},
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use stm32g4xx_hal as hal;

Expand All @@ -29,7 +31,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");

Expand All @@ -42,7 +44,10 @@ fn main() -> ! {
let pa0 = gpioa.pa0.into_analog();

info!("Setup Adc1");
let mut delay = cp.SYST.delay(&rcc.clocks);
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
14 changes: 9 additions & 5 deletions examples/adc-one-shot-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ use crate::hal::{
config::{Continuous, Dma as AdcDma, Resolution, SampleTime, Sequence},
AdcClaim, ClockSource, Temperature,
},
delay::SYSTDelayExt,
delay::DelayFromCountDownTimer,
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
gpio::GpioExt,
pwr::PwrExt,
rcc::{Config, RccExt},
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use stm32g4xx_hal as hal;

use log::info;

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

#[entry]
fn main() -> ! {
Expand All @@ -29,7 +30,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");

Expand All @@ -48,7 +49,10 @@ fn main() -> ! {
let pa0 = gpioa.pa0.into_analog();

info!("Setup Adc1");
let mut delay = cp.SYST.delay(&rcc.clocks);
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
Expand Down
14 changes: 9 additions & 5 deletions examples/adc-one-shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@

use crate::hal::{
adc::{config::SampleTime, AdcClaim},
delay::SYSTDelayExt,
delay::DelayFromCountDownTimer,
pwr::PwrExt,
rcc::Config,
stm32::Peripherals,
time::ExtU32,
timer::Timer,
};
use hal::prelude::*;
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 @@ -25,7 +26,7 @@ fn main() -> ! {
info!("start");

let dp = Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");

Expand All @@ -34,7 +35,10 @@ fn main() -> ! {
let mut rcc = rcc.freeze(Config::hsi(), pwr);

info!("Setup Adc1");
let mut delay = cp.SYST.delay(&rcc.clocks);
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);
let mut adc = dp.ADC2.claim_and_configure(
stm32g4xx_hal::adc::ClockSource::SystemClock,
&rcc,
Expand Down
3 changes: 2 additions & 1 deletion examples/blinky_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![no_main]
#![no_std]

use embedded_hal::delay::DelayNs;
use hal::delay::DelayFromCountDownTimer;
use hal::prelude::*;
use hal::pwr::PwrExt;
Expand Down Expand Up @@ -47,6 +48,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
13 changes: 9 additions & 4 deletions examples/comp_w_dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ fn main() -> ! {
#[cfg(feature = "stm32g474")]
#[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::delay::DelayFromCountDownTimer;
use hal::gpio::GpioExt;
use hal::rcc::RccExt;
use hal::stm32;
use hal::time::ExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;

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

let mut rcc = dp.RCC.constrain();
let mut delay = cp.SYST.delay(&rcc.clocks);
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);

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

Expand Down
14 changes: 10 additions & 4 deletions examples/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#![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::delay::DelayFromCountDownTimer;
use hal::gpio::GpioExt;
use hal::rcc::RccExt;
use hal::time::ExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;
mod utils;
extern crate cortex_m_rt as rt;
Expand All @@ -23,10 +25,14 @@ use rt::entry;
#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
// let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

let mut rcc = dp.RCC.constrain();
let mut delay = cp.SYST.delay(&rcc.clocks);
// cortex-m doesn't yet support hal-1 DelayNs on systick (PR #504)
// let mut delay = cp.SYST.delay(&rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(
Timer::new(dp.TIM6, &rcc.clocks).start_count_down(100u32.millis()),
);

let gpioa = dp.GPIOA.split(&mut rcc);
let (dac1ch1, dac1ch2) = dp.DAC1.constrain((gpioa.pa4, gpioa.pa5), &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
Loading