Skip to content

Commit

Permalink
[WIP] Refactor for embedded-hal v1.0.0-alpha.6
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Scheuren <[email protected]>
  • Loading branch information
systec-ms committed Nov 19, 2021
1 parent e84d5a3 commit 69cd1b8
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 196 deletions.
18 changes: 8 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@ version = "0.6.0"
features = ["stm32f746", "rt"]

[dependencies]
as-slice = "0.1.0"
as-slice = "0.2.1"
cortex-m = "0.7"
cortex-m-rt = ">=0.6.15, <0.8"
embedded-time = "0.12.0"
nb = "0.1.2"
nb = "1.0.0"
rtcc = "0.2"
stm32f7 = "0.14.0"
micromath = "1.0.0"
micromath = "2.0.0"
synopsys-usb-otg = { version = "0.2.3", features = ["cortex-m"], optional = true }
stm32-fmc = { version = "0.2.0", features = ["sdram"], optional = true }
rand_core = "0.6"
bxcan = ">=0.4, <0.6"
bxcan = "0.6.2"

[dependencies.bare-metal]
version = "0.2.4"
features = ["const-fn"]
version = "1.0.0"

[dependencies.cast]
default-features = false
version = "0.2.2"
version = "0.3.0"

[dependencies.embedded-hal]
features = ["unproven"]
version = "0.2.3"
version = "1.0.0-alpha.6"

[dependencies.void]
default-features = false
Expand All @@ -52,7 +50,7 @@ version = "0.4.1"
cortex-m-semihosting = "0.3.3"
panic-halt = "0.2.0"
panic-semihosting = "0.5.2"
embedded-graphics = "0.6.1"
embedded-graphics = "0.6.2"
usb-device = "0.2.5"
usbd-serial = "0.1.0"

Expand Down
5 changes: 3 additions & 2 deletions examples/blinky_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
extern crate panic_halt;

use cortex_m_rt::entry;
use embedded_hal::delay::blocking::DelayUs;
use stm32f7xx_hal::{delay::Delay, pac, prelude::*};

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

loop {
led.set_high();
delay.delay_ms(500_u16);
delay.delay_ms(500).unwrap();

led.set_low();
delay.delay_ms(500_u16);
delay.delay_ms(500).unwrap();
}
}
22 changes: 9 additions & 13 deletions examples/can-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ fn main() -> ! {
let tx = gpioa.pa12.into_alternate();

let can = Can::new(dp.CAN1, &mut rcc.apb1, (tx, rx));
bxcan::Can::new(can)

bxcan::Can::builder(can)
// APB1 (PCLK1): 130MHz, Bit rate: 512kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
.set_bit_timing(0x001e_000b)
.enable()
};
can1.configure(|config| {
// APB1 (PCLK1): 130MHz, Bit rate: 512kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
config.set_bit_timing(0x001e_000b);
});

// Configure filters so that can frames can be received.
let mut filters = can1.modify_filters();
Expand All @@ -58,12 +58,11 @@ fn main() -> ! {

let can = Can::new(dp.CAN2, &mut rcc.apb1, (tx, rx));

let mut can2 = bxcan::Can::new(can);
can2.configure(|config| {
let can2 = bxcan::Can::builder(can)
// APB1 (PCLK1): 130MHz, Bit rate: 512kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
config.set_bit_timing(0x001e_000b);
});
.set_bit_timing(0x001e_000b)
.enable();

// A total of 28 filters are shared between the two CAN instances.
// Split them equally between CAN1 and CAN2.
Expand All @@ -80,9 +79,6 @@ fn main() -> ! {
let mut can = can1;
//let mut can = can2;

// Split the peripheral into transmitter and receiver parts.
block!(can.enable()).unwrap();

// Echo back received packages in sequence.
// See the `can-rtfm` example for an echo implementation that adheres to
// correct frame ordering based on the transfer id.
Expand Down
17 changes: 6 additions & 11 deletions examples/can-loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ fn main() -> ! {

let can = Can::new(dp.CAN1, &mut rcc.apb1, (tx, rx));

let mut can = bxcan::Can::new(can);

// Use loopback mode: No pins need to be assigned to peripheral.
can.configure(|config| {
let mut can = bxcan::Can::builder(can)
// APB1 (PCLK1): 130MHz, Bit rate: 512kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
config.set_bit_timing(0x001e_000b);
config.set_loopback(true);
config.set_silent(true);
});
.set_bit_timing(0x001e_000b)
.set_loopback(true)
.set_silent(true)
.enable();

let mut filters = can.modify_filters();
assert!(filters.num_banks() > 3);
Expand Down Expand Up @@ -90,12 +88,9 @@ fn main() -> ! {
],
);

// Enable filters.
// Drop filters to leave filter configuraiton mode.
drop(filters);

// Sync to the bus and start normal operation.
block!(can.enable()).ok();

// Some messages shall pass the filters.
for &id in &[0, 1, 2, 8, 9, 10, 11] {
let frame_tx = Frame::new_data(StandardId::new(id).unwrap(), [id as u8]);
Expand Down
1 change: 1 addition & 0 deletions examples/i2c_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use panic_semihosting as _;
use cortex_m_rt::entry;
use cortex_m_semihosting::{hprint, hprintln};

use embedded_hal::i2c::blocking::Write;
use stm32f7xx_hal::{self as hal, gpio::GpioExt, pac, prelude::*};

const VALID_ADDR_RANGE: Range<u8> = 0x08..0x78;
Expand Down
3 changes: 2 additions & 1 deletion examples/serial_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate panic_halt;
use core::fmt::Write;

use cortex_m_rt::entry;
use embedded_hal::delay::blocking::DelayUs;
use stm32f7xx_hal::{
delay::Delay,
pac,
Expand Down Expand Up @@ -50,6 +51,6 @@ fn main() -> ! {
let hello: &str = "Hello, I'm a STM32F7xx!\r\n";
loop {
tx.write_str(hello).unwrap();
delay.delay_ms(500_u16);
delay.delay_ms(500).unwrap();
}
}
1 change: 1 addition & 0 deletions examples/serial_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate panic_halt;
use nb::block;

use cortex_m_rt::entry;
use embedded_hal::serial::nb::{Read, Write};
use stm32f7xx_hal::{
pac,
prelude::*,
Expand Down
3 changes: 2 additions & 1 deletion examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern crate panic_semihosting;

use cortex_m_rt::entry;
use embedded_hal::spi::blocking::TransferInplace;
use stm32f7xx_hal::{
pac,
prelude::*,
Expand Down Expand Up @@ -48,7 +49,7 @@ fn main() -> ! {
let mut buffer = [0; 2];
buffer[0] = 0x75 | 0x80;
ncs.set_low();
spi.transfer(&mut buffer).unwrap();
spi.transfer_inplace(&mut buffer).unwrap();
ncs.set_high();

// The WHO_AM_I register should always return 0x71.
Expand Down
1 change: 1 addition & 0 deletions examples/spi_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern crate panic_semihosting;

use cortex_m_rt::entry;
use embedded_hal::spi::blocking::Write;
use stm32f7xx_hal::{
pac,
prelude::*,
Expand Down
2 changes: 1 addition & 1 deletion examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() -> ! {
unsafe {
NVIC::unmask(pac::Interrupt::TIM2);
}
let mut timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1);
let mut timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1).unwrap();
timer.listen(Event::TimeOut);

loop {}
Expand Down
8 changes: 4 additions & 4 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::pac::{ADC1, ADC2, ADC3, ADC_COMMON};

use cortex_m::asm::delay;

use embedded_hal::adc::{Channel, OneShot};
use embedded_hal::adc::nb::{Channel, OneShot};

#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -95,7 +95,7 @@ macro_rules! adc_pins {
impl Channel<$ADC> for $pin {
type ID = u8;

fn channel() -> u8 { $chan }
fn channel(&self) -> u8 { $chan }
}
)+
};
Expand Down Expand Up @@ -442,8 +442,8 @@ macro_rules! adc_hal {
{
type Error = ();

fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
let res = self.convert(PIN::channel());
fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
let res = self.convert(PIN::channel(pin));
Ok(res.into())
}
}
Expand Down
38 changes: 5 additions & 33 deletions src/delay.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Delays
use cast::u32;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;

use crate::hal::blocking::delay::{DelayMs, DelayUs};
use crate::hal::delay::blocking::DelayUs;
use crate::rcc::Clocks;

/// System timer (SysTick) as a delay provider
Expand All @@ -27,26 +26,10 @@ impl Delay {
}
}

impl DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
self.delay_us(ms * 1_000);
}
}

impl DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
self.delay_ms(u32(ms));
}
}

impl DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(u32(ms));
}
}
impl DelayUs for Delay {
type Error = ();

impl DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
const MAX_RVR: u32 = 0x00FF_FFFF;

Expand All @@ -70,17 +53,6 @@ impl DelayUs<u32> for Delay {

self.syst.disable_counter();
}
}
}

impl DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
self.delay_us(u32(us))
}
}

impl DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
self.delay_us(u32(us))
Ok(())
}
}
6 changes: 3 additions & 3 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
use core::convert::Infallible;
use core::marker::PhantomData;

pub use embedded_hal::digital::v2::PinState;
use embedded_hal::digital::v2::{
use embedded_hal::digital::blocking::{
InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin,
};
pub use embedded_hal::digital::PinState;

use crate::pac::{EXTI, SYSCFG};
use crate::rcc::{Enable, APB2};
Expand Down Expand Up @@ -450,7 +450,7 @@ impl<MODE, const P: char, const N: u8> Pin<Output<MODE>, P, N> {

#[inline(always)]
pub fn toggle(&mut self) {
if self.is_set_low() {
if self.is_set_low().unwrap() {
self.set_high()
} else {
self.set_low()
Expand Down
2 changes: 1 addition & 1 deletion src/gpio/erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<MODE> ErasedPin<Output<MODE>> {

#[inline(always)]
pub fn toggle(&mut self) {
if self.is_set_low() {
if self.is_set_low().unwrap() {
self.set_high()
} else {
self.set_low()
Expand Down
2 changes: 1 addition & 1 deletion src/gpio/partially_erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<MODE, const P: char> PartiallyErasedPin<Output<MODE>, P> {

#[inline(always)]
pub fn toggle(&mut self) {
if self.is_set_low() {
if self.is_set_low().unwrap() {
self.set_high()
} else {
self.set_low()
Expand Down
Loading

0 comments on commit 69cd1b8

Please sign in to comment.