Skip to content

Commit

Permalink
Merge branch 'stm32-rs:main' into spi_improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
amcelroy authored Nov 30, 2023
2 parents 3b47e7a + d96a466 commit 03b01cd
Show file tree
Hide file tree
Showing 24 changed files with 1,276 additions and 60 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ optional = true
[dev-dependencies]
cortex-m-rt = "0.7.2"
defmt-rtt = "0.4.0"
cortex-m-rtic = "0.5.8"
cortex-m-rtic = "1.1.4"
cortex-m-semihosting = "0.3.5"
panic-probe = { version = "0.3.0", features = ["print-defmt"] }
panic-semihosting = "0.5.3"
Expand Down Expand Up @@ -101,3 +101,7 @@ debug = false
codegen-units = 1
incremental = false
lto = true

[[example]]
name = "flash_with_rtic"
required-features = ["stm32g474"]
2 changes: 1 addition & 1 deletion examples/can-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn main() -> ! {

loop {
if let Ok(rxheader) = block!(can.receive0(&mut buffer)) {
block!(can.transmit(rxheader.unwrap().to_tx_header(None), &mut buffer)).unwrap();
block!(can.transmit(rxheader.unwrap().to_tx_header(None), &buffer)).unwrap();
}
}
}
69 changes: 69 additions & 0 deletions examples/comp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! ## Origin
//!
//! This code has been taken from the stm32g0xx-hal project and modified slightly to support
//! STM32G4xx MCUs.
//#![deny(warnings)]
#![deny(unsafe_code)]
#![no_main]
#![no_std]

mod utils;
extern crate cortex_m_rt as rt;

use rt::entry;

#[cfg(not(feature = "stm32g474"))]
#[entry]
fn main() -> ! {
loop {} // TODO: add support for more devices
}

#[cfg(feature = "stm32g474")]
#[entry]
fn main() -> ! {
use hal::comparator::{ComparatorExt, ComparatorSplit, Config, Hysteresis, RefintInput};
use hal::gpio::GpioExt;
use hal::prelude::OutputPin;
use hal::rcc::RccExt;
use hal::stm32;
use stm32g4xx_hal as hal;

let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let mut rcc = dp.RCC.constrain();

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

let (comp1, comp2, ..) = dp.COMP.split(&mut rcc);

let pa1 = gpioa.pa1.into_analog();
let pa0 = gpioa.pa0.into_analog();
let comp1 = comp1.comparator(&pa1, pa0, Config::default(), &rcc.clocks);
let comp1 = comp1.enable();

// led1 pa1 will be updated manually when to match comp1 value
let mut led1 = gpioa.pa5.into_push_pull_output();

let pa7 = gpioa.pa7.into_analog();
let comp2 = comp2.comparator(
&pa7,
RefintInput::VRefintM12,
Config::default()
.hysteresis(Hysteresis::None)
.output_inverted(),
&rcc.clocks,
);
let led2 = gpioa.pa12.into_push_pull_output();
// Configure PA12 to the comparator's alternate function so it gets
// changed directly by the comparator.
comp2.output_pin(led2);
let _comp2 = comp2.enable().lock();

loop {
// Read comp1 output and update led1 accordingly
match comp1.output() {
true => led1.set_high().unwrap(),
false => led1.set_low().unwrap(),
}
}
}
81 changes: 81 additions & 0 deletions examples/comp_w_dac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// #![deny(warnings)]
#![deny(unsafe_code)]
#![no_main]
#![no_std]

mod utils;
extern crate cortex_m_rt as rt;

use rt::entry;

#[cfg(not(feature = "stm32g474"))]
#[entry]
fn main() -> ! {
loop {} // TODO: add support for more devices
}

#[cfg(feature = "stm32g474")]
#[entry]
fn main() -> ! {
use embedded_hal::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;

let dp = stm32::Peripherals::take().expect("cannot take 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 gpioa = dp.GPIOA.split(&mut rcc);

// Set up DAC to output to pa4 and to internal signal Dac1IntSig1
// which just so happens is compatible with comp1
let dac1ch1 = dp.DAC1.constrain((gpioa.pa4, Dac1IntSig1), &mut rcc);
let mut dac = dac1ch1.calibrate_buffer(&mut delay).enable();

let (comp1, _comp2, ..) = dp.COMP.split(&mut rcc);
let pa1 = gpioa.pa1.into_analog();

// Set up comparator with pa1 as positive, and the DAC as negative input
let comp = comp1.comparator(
&pa1,
&dac,
comparator::Config::default().hysteresis(comparator::Hysteresis::None),
&rcc.clocks,
);

let led2 = gpioa.pa0.into_push_pull_output();
// Configure PA12 to the comparator's alternate function so it gets
// changed directly by the comparator.
comp.output_pin(led2);
let _comp1 = comp.enable().lock();

let mut dir = Direction::Upcounting;
let mut val = 0;

// Manually step the DAC's value to produce a triangle wave
//
// This will produce a pwm-like signal at pa0 with the duty controlled by pa1
// where
// * 0V at p1 => 0% duty
// * VDDA at p1 => 100% duty
loop {
dac.set_value(val);
match val {
0 => dir = Direction::Upcounting,
4095 => dir = Direction::Downcounting,
_ => (),
};

match dir {
Direction::Upcounting => val += 1,
Direction::Downcounting => val -= 1,
}
}
}
61 changes: 61 additions & 0 deletions examples/dac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! ## Origin
//!
//! This code has been taken from the stm32g0xx-hal project and modified to support
//! STM32G4xx MCUs.
// #![deny(warnings)]
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use embedded_hal::Direction;
use hal::dac::{DacExt, DacOut, GeneratorConfig};
use hal::delay::SYSTDelayExt;
use hal::gpio::GpioExt;
use hal::rcc::RccExt;
use stm32g4xx_hal as hal;
mod utils;
extern crate cortex_m_rt as rt;

use hal::stm32;
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 mut rcc = dp.RCC.constrain();
let mut delay = cp.SYST.delay(&rcc.clocks);

let gpioa = dp.GPIOA.split(&mut rcc);
let (dac1ch1, dac1ch2) = dp.DAC1.constrain((gpioa.pa4, gpioa.pa5), &mut rcc);

// dac_manual will have its value set manually
let mut dac_manual = dac1ch1.calibrate_buffer(&mut delay).enable();

// dac_generator will have its value set automatically from its internal noise generator
let mut dac_generator = dac1ch2.enable_generator(GeneratorConfig::noise(11));

let mut dir = Direction::Upcounting;
let mut val = 0;

loop {
// This will pull out a new value from the noise generator and apply it to the DAC
dac_generator.trigger();

// This will manually set the DAC's value
dac_manual.set_value(val);
match val {
0 => dir = Direction::Upcounting,
4095 => dir = Direction::Downcounting,
_ => (),
};

// Step manually set value as a triangle wave
match dir {
Direction::Upcounting => val += 1,
Direction::Downcounting => val -= 1,
}
}
}
2 changes: 0 additions & 2 deletions examples/flash_with_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ mod app {

const LOG_LEVEL: log::LevelFilter = log::LevelFilter::Info;

use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics

// Resources shared between tasks
#[shared]
struct Shared {}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use utils::logger::println;

#[entry]
fn main() -> ! {
let _ = println!("Hello, STM32G4!");
println!("Hello, STM32G4!");

#[allow(clippy::empty_loop)]
loop {}
Expand Down
6 changes: 3 additions & 3 deletions examples/i2c-bme680.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ fn main() -> ! {
.with_run_gas(true)
.build();

let profile_dur = dev.get_profile_dur(&settings.0).unwrap();
let _profile_dur = dev.get_profile_dur(&settings.0).unwrap();
dev.set_sensor_settings(&mut delayer, settings).unwrap();
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
.unwrap();
let sensor_settings = dev.get_sensor_settings(settings.1);
let _sensor_settings = dev.get_sensor_settings(settings.1);

loop {
delay.delay_ms(500u32);
let power_mode = dev.get_sensor_mode();
let _power_mode = dev.get_sensor_mode();
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
.unwrap();
let (data, _state) = dev.get_sensor_data(&mut delayer).unwrap();
Expand Down
8 changes: 5 additions & 3 deletions examples/opamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() -> ! {
);

// Configure op with pa7 as non-inverting input and set gain to x4
let mut opamp2 = opamp2.pga(
let opamp2 = opamp2.pga(
pa7,
PgaModeInternal::gain(NonInvertingGain::Gain4),
Option::<PA6<Analog>>::None, // Do not route output to any external pin, use internal AD instead
Expand All @@ -72,7 +72,7 @@ fn main() -> ! {
loop {
// Here we can sample the output of opamp2 as if it was a regular AD pin
let sample = adc.convert(
&mut opamp2,
&opamp2,
stm32g4xx_hal::adc::config::SampleTime::Cycles_640_5,
);

Expand All @@ -87,6 +87,8 @@ fn main() -> ! {
let (_opamp1, _pa1, _mode, _some_pa2) = _opamp1.disable();
let (_opamp2, _pa7, _mode, _none) = opamp2.disable();

loop {}
loop {
delay.delay_ms(100);
}
}
}
6 changes: 5 additions & 1 deletion examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ use hal::prelude::*;
use hal::stm32;
use hal::time::RateExtU32;
use stm32g4xx_hal as hal;
mod utils;
extern crate cortex_m_rt as rt;

#[macro_use]
mod utils;

#[entry]
fn main() -> ! {
utils::logger::init();

let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let mut rcc = dp.RCC.constrain();
let gpioa = dp.GPIOA.split(&mut rcc);
Expand Down
20 changes: 9 additions & 11 deletions examples/spi-sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,30 @@

extern crate embedded_sdmmc;

use fugit::RateExtU32;
use hal::gpio::gpiob::PB14;
use hal::gpio::gpiob::PB15;
use hal::gpio::gpiof::PF8;
use hal::gpio::gpiof::PF9;
use hal::gpio::Alternate;
use hal::gpio::AF5;
use hal::prelude::*;
use hal::rcc::Config;
use hal::spi;
use hal::stm32;

use hal::stm32::Peripherals;
use hal::time::RateExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;

use embedded_sdmmc::{
Block, BlockCount, BlockDevice, BlockIdx, Controller, Error, Mode, TimeSource, Timestamp,
VolumeIdx,
};
use embedded_sdmmc::{TimeSource, Timestamp};

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

#[macro_use]
mod utils;

#[entry]
fn main() -> ! {
utils::logger::init();

let dp = Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let mut rcc = rcc.freeze(Config::hsi());
Expand All @@ -49,7 +45,7 @@ fn main() -> ! {
let miso: PB14<Alternate<AF5>> = gpiob.pb14.into_alternate();
let mosi: PB15<Alternate<AF5>> = gpiob.pb15.into_alternate();

let mut spi = dp
let spi = dp
.SPI2
.spi((sck, miso, mosi), spi::MODE_0, 400.kHz(), &mut rcc);

Expand All @@ -70,6 +66,8 @@ fn main() -> ! {

let mut cont = embedded_sdmmc::Controller::new(embedded_sdmmc::SdMmcSpi::new(spi, cs), Clock);

cont.device().init();
cont.device().init().unwrap();

#[allow(clippy::empty_loop)]
loop {}
}
8 changes: 7 additions & 1 deletion examples/utils/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ cfg_if::cfg_if! {
pub use defmt::println as println;
} else {
pub use log::{info, trace, warn, debug, error};
pub use cortex_m_semihosting::hprintln as println;

#[allow(unused_macros)]
macro_rules! println {
($($tt:tt)*) => {
$crate::cortex_m_semihosting::hprintln!($($tt,)*).unwrap();
};
}
}
}

Expand Down
Loading

0 comments on commit 03b01cd

Please sign in to comment.