Skip to content

Commit

Permalink
Merge from main (#173)
Browse files Browse the repository at this point in the history
* Export pac as pac (#156)

* rm remaining fdcan files (#160)

* defmt format for rcc clocks (#163)

* export PwrExt in prelude (#164)

* e-hal-v1 (#172)

* Implement SpiBus from embedded-hal 1

* i2c: implement embedded-hal traits

* gpio: implement embedded-hal 1 digital traits

* pwm: implement SetDutyCycle from embedded-hal 1

* delay: temporary DelayNs impl (μs-resolution)

* serial: implement embedded-io traits for UART

* bump hal-1, remove ToggleableOutputPin impls

* remove embedded-hal 0.2 from prelude, rename hals

embedded-hal 1.0 renamed from embedded-hal-one to embedded-hal
embedded-hal 0.2 renamed from embedded-hal to embedded-hal-old, still
re-exported as hal-02

* pwm: use set_duty_cycle_percent in example

* run cargo fmt

* remove unused dependencies

I had added them by accident for an example that didn't end up in the PR

* time: fix duration variable names, add doc

* docs: fix cargo doc warnings

* Try to make syst delay work as before

* Cleanup examples

---------

Co-authored-by: techmccat <[email protected]>

---------

Co-authored-by: AdinAck <[email protected]>
Co-authored-by: techmccat <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent e8ac42d commit 79867ef
Show file tree
Hide file tree
Showing 32 changed files with 862 additions and 301 deletions.
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 = { git = "https://github.com/stm32-rs/stm32-rs-nightlies" } #"0.15.1"
stm32g4 = { version = "0.19.0", package = "stm32g4-staging" }
paste = "1.0"
Expand All @@ -22,6 +22,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 @@ -39,9 +40,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 @@ -94,7 +99,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
4 changes: 2 additions & 2 deletions examples/adc-one-shot-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ use crate::hal::{
};
use stm32g4xx_hal as hal;

use log::info;

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

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -49,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
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
6 changes: 5 additions & 1 deletion examples/comp_w_dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use rt::entry;

#[entry]
fn main() -> ! {
use embedded_hal::Direction;
use hal::comparator::{self, ComparatorExt, ComparatorSplit};
use hal::dac::{Dac1IntSig1, DacExt, DacOut};
use hal::delay::SYSTDelayExt;
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion examples/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#![no_main]
#![no_std]

use embedded_hal::Direction;
use hal::dac::{DacExt, DacOut, GeneratorConfig};
use hal::delay::SYSTDelayExt;
use hal::gpio::GpioExt;
Expand Down Expand Up @@ -37,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;

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 @@ -69,6 +70,6 @@ fn main() -> ! {
);
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
24 changes: 13 additions & 11 deletions examples/uart-fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ 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;

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

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

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -50,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;
}
}
}
Expand Down
Loading

0 comments on commit 79867ef

Please sign in to comment.