Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nb = "0.1"
paste = "1"
rtcc = "0.2"
stm32f3 = "0.12"
embedded-time = "0.10"

[dependencies.embedded-hal-can]
version = "0.1.0"
Expand Down
13 changes: 8 additions & 5 deletions examples/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use panic_semihosting as _;

use stm32f3xx_hal as hal;

use core::convert::TryFrom;

use cortex_m::asm;
use cortex_m_rt::entry;

use hal::prelude::*;
use hal::stm32;
use hal::time::{duration::*, rate::*};
use hal::watchdog::IndependentWatchDog;

use hal::can::{Can, CanFilter, CanFrame, CanId, Filter, Frame, Receiver, Transmitter};
Expand All @@ -31,10 +34,10 @@ fn main() -> ! {

let _clocks = rcc
.cfgr
.use_hse(32.mhz())
.sysclk(32.mhz())
.pclk1(16.mhz())
.pclk2(16.mhz())
.use_hse(Hertz::try_from(32u32.MHz()).unwrap())
.sysclk(Hertz::try_from(32u32.MHz()).unwrap())
.pclk1(Hertz::try_from(16u32.MHz()).unwrap())
.pclk2(Hertz::try_from(16u32.MHz()).unwrap())
.freeze(&mut flash.acr);

// Configure CAN RX and TX pins (AF9)
Expand All @@ -60,7 +63,7 @@ fn main() -> ! {
// Watchdog makes sure this gets restarted periodically if nothing happens
let mut iwdg = IndependentWatchDog::new(dp.IWDG);
iwdg.stop_on_debug(&dp.DBGMCU, true);
iwdg.start(100.ms());
iwdg.start(100u32.milliseconds());

// Send an initial message!
asm::delay(100_000);
Expand Down
12 changes: 11 additions & 1 deletion examples/i2c_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ use core::ops::Range;

use panic_semihosting as _;

use core::convert::TryFrom;

use cortex_m::asm;
use cortex_m_rt::entry;
use cortex_m_semihosting::{hprint, hprintln};

use hal::time::rate::*;
use stm32f3xx_hal::{self as hal, pac, prelude::*};

const VALID_ADDR_RANGE: Range<u8> = 0x08..0x78;
Expand All @@ -33,7 +36,14 @@ fn main() -> ! {
gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SCL
gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SDA
);
let mut i2c = hal::i2c::I2c::new(dp.I2C1, pins, 100.khz(), clocks, &mut rcc.apb1);

let mut i2c = hal::i2c::I2c::new(
dp.I2C1,
pins,
Hertz::try_from(100u32.kHz()).unwrap(),
clocks,
&mut rcc.apb1,
);

hprintln!("Start i2c scanning...").expect("Error using hprintln.");
hprintln!().unwrap();
Expand Down
33 changes: 19 additions & 14 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use panic_semihosting as _;

use stm32f3xx_hal as hal;

use core::convert::TryFrom;

use cortex_m::asm;
use cortex_m_rt::entry;

Expand All @@ -18,7 +20,7 @@ use hal::gpio::GpioExt;
use hal::pac;
use hal::pwm::{tim16, tim2, tim3, tim8};
use hal::rcc::RccExt;
use hal::time::U32Ext;
use hal::time::rate::*;

#[entry]
fn main() -> ! {
Expand All @@ -28,7 +30,10 @@ fn main() -> ! {
// Configure our clocks
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(16.mhz()).freeze(&mut flash.acr);
let clocks = rcc
.cfgr
.sysclk(Hertz::try_from(16u32.MHz()).unwrap())
.freeze(&mut flash.acr);

// Prep the pins we need in their correct alternate function
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
Expand All @@ -52,9 +57,9 @@ fn main() -> ! {
// A four channel general purpose timer that's broadly available
let tim3_channels = tim3(
dp.TIM3,
1280, // resolution of duty cycle
50.hz(), // frequency of period
&clocks, // To get the timer's clock speed
1280, // resolution of duty cycle
50u32.Hz(), // frequency of period
&clocks, // To get the timer's clock speed
);

// Channels without pins cannot be enabled, so we can't forget to
Expand Down Expand Up @@ -101,9 +106,9 @@ fn main() -> ! {
// A 32-bit timer, so we can set a larger resolution
let tim2_channels = tim2(
dp.TIM2,
160000, // resolution of duty cycle
50.hz(), // frequency of period
&clocks, // To get the timer's clock speed
160000, // resolution of duty cycle
50u32.Hz(), // frequency of period
&clocks, // To get the timer's clock speed
);

let mut tim2_ch3 = tim2_channels.2.output_to_pb10(pb10);
Expand All @@ -116,9 +121,9 @@ fn main() -> ! {
// just use it directly
let mut tim16_ch1 = tim16(
dp.TIM16,
1280, // resolution of duty cycle
50.hz(), // frequency of period
&clocks, // To get the timer's clock speed
1280, // resolution of duty cycle
50u32.Hz(), // frequency of period
&clocks, // To get the timer's clock speed
)
.output_to_pb8(pb8);
tim16_ch1.set_duty(tim16_ch1.get_max_duty() / 20); // 5% duty cyle
Expand All @@ -130,9 +135,9 @@ fn main() -> ! {
// to complementary pins (works just like standard pins)
let tim8_channels = tim8(
dp.TIM8,
1280, // resolution of duty cycle
50.hz(), // frequency of period
&clocks, // To get the timer's clock speed
1280, // resolution of duty cycle
50u32.Hz(), // frequency of period
&clocks, // To get the timer's clock speed
);

let mut tim8_ch1 = tim8_channels.0.output_to_pc10(pc10);
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use panic_semihosting as _;

use cortex_m::{asm, singleton};
use cortex_m_rt::entry;
use stm32f3xx_hal::{pac, prelude::*, serial::Serial};
use stm32f3xx_hal::{pac, prelude::*, serial::Serial, time::rate::*};

#[entry]
fn main() -> ! {
Expand All @@ -25,7 +25,7 @@ fn main() -> ! {
gpioa.pa9.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
gpioa.pa10.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
);
let serial = Serial::usart1(dp.USART1, pins, 9600.bps(), clocks, &mut rcc.apb2);
let serial = Serial::usart1(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
let (tx, rx) = serial.split();

let dma1 = dp.DMA1.split(&mut rcc.ahb);
Expand Down
11 changes: 7 additions & 4 deletions examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ use panic_semihosting as _;

use stm32f3xx_hal as hal;

use core::convert::TryFrom;

use cortex_m::asm;
use cortex_m_rt::entry;

use hal::pac;
use hal::prelude::*;
use hal::spi::{Mode, Phase, Polarity, Spi};
use hal::time::rate::*;

#[entry]
fn main() -> ! {
Expand All @@ -24,9 +27,9 @@ fn main() -> ! {

let clocks = rcc
.cfgr
.use_hse(8.mhz())
.sysclk(48.mhz())
.pclk1(24.mhz())
.use_hse(Hertz::try_from(8u32.MHz()).unwrap())
.sysclk(Hertz::try_from(48u32.MHz()).unwrap())
.pclk1(Hertz::try_from(24u32.MHz()).unwrap())
.freeze(&mut flash.acr);

// Configure pins for SPI
Expand All @@ -43,7 +46,7 @@ fn main() -> ! {
dp.SPI1,
(sck, miso, mosi),
spi_mode,
3.mhz(),
Hertz::try_from(3u32.MHz()).unwrap(),
clocks,
&mut rcc.apb2,
);
Expand Down
11 changes: 7 additions & 4 deletions examples/usb_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ use stm32f3xx_hal as hal;
use cortex_m::asm::delay;
use cortex_m_rt::entry;

use core::convert::TryFrom;

use hal::pac;
use hal::prelude::*;
use hal::time::rate::*;
use hal::usb::{Peripheral, UsbBus};

use usb_device::prelude::*;
Expand All @@ -26,10 +29,10 @@ fn main() -> ! {

let clocks = rcc
.cfgr
.use_hse(8.mhz())
.sysclk(48.mhz())
.pclk1(24.mhz())
.pclk2(24.mhz())
.use_hse(Hertz::try_from(8u32.MHz()).unwrap())
.sysclk(Hertz::try_from(48u32.MHz()).unwrap())
.pclk1(Hertz::try_from(24u32.MHz()).unwrap())
.pclk2(Hertz::try_from(24u32.MHz()).unwrap())
.freeze(&mut flash.acr);

assert!(clocks.usbclk_valid());
Expand Down
4 changes: 2 additions & 2 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
hal::blocking::i2c::{Read, Write, WriteRead},
pac::{i2c1::RegisterBlock, rcc::cfgr3::I2C1SW_A, I2C1, RCC},
rcc::{Clocks, APB1},
time::{Hertz, U32Ext},
time::rate::Hertz,
};

#[cfg(not(feature = "gpio-f333"))]
Expand Down Expand Up @@ -451,7 +451,7 @@ macro_rules! i2c {
fn clock(clocks: &Clocks) -> Hertz {
// NOTE(unsafe) atomic read with no side effects
match unsafe { (*RCC::ptr()).cfgr3.read().$i2cXsw().variant() } {
I2C1SW_A::HSI => 8.mhz().into(),
I2C1SW_A::HSI => Hertz(8_000_000),
I2C1SW_A::SYSCLK => clocks.sysclk(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub use embedded_hal as hal;
pub use nb;
pub use nb::block;

pub use embedded_time as time;

#[cfg(feature = "defmt")]
pub(crate) use defmt::{assert, panic, unreachable, unwrap};
#[cfg(feature = "defmt")]
Expand Down Expand Up @@ -187,7 +189,6 @@ cfg_if::cfg_if! {
pub mod rtc;
pub mod serial;
pub mod spi;
pub mod time;
pub mod timer;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub use crate::flash::FlashExt as _stm32f3xx_hal_flash_FlashExt;
pub use crate::gpio::GpioExt as _stm32f3xx_hal_gpio_GpioExt;
pub use crate::hal::prelude::*;
pub use crate::rcc::RccExt as _stm32f3xx_hal_rcc_RccExt;
pub use crate::time::U32Ext as _stm32f3xx_hal_time_U32Ext;
#[cfg(feature = "unproven")]
pub use crate::{
hal::digital::v2::InputPin as _embedded_hal_digital_InputPin,
Expand Down
2 changes: 1 addition & 1 deletion src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ use crate::{
hal::PwmPin,
pac::{RCC, TIM15, TIM16, TIM17, TIM2},
rcc::Clocks,
time::Hertz,
time::rate::Hertz,
};
use core::marker::PhantomData;

Expand Down
2 changes: 1 addition & 1 deletion src/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use crate::pac::{
};

use crate::flash::ACR;
use crate::time::Hertz;
use crate::time::rate::Hertz;

/// Extension trait that constrains the `RCC` peripheral
pub trait RccExt {
Expand Down
5 changes: 3 additions & 2 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::{
hal::{blocking, serial},
pac::{USART1, USART2, USART3},
rcc::{Clocks, APB1, APB2},
time::Bps,
time::rate::Baud,
};

use cfg_if::cfg_if;
use core::{convert::Infallible, marker::PhantomData, ptr};

Expand Down Expand Up @@ -111,7 +112,7 @@ macro_rules! hal {
pub fn $usartX(
usart: $USARTX,
pins: (TX, RX),
baud_rate: Bps,
baud_rate: Baud,
clocks: Clocks,
apb: &mut $APB,
) -> Self
Expand Down
2 changes: 1 addition & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ use crate::rcc::APB1;
feature = "stm32f398"
))]
use crate::rcc::APB2;
use crate::time::Hertz;
use crate::time::rate::Hertz;
use core::marker::PhantomData;

/// SPI error
Expand Down
Loading