Skip to content

Commit

Permalink
Merge pull request #38 from gd32-rust/embedded-hal
Browse files Browse the repository at this point in the history
Add support for embedded-hal 1.0
  • Loading branch information
qwandor authored Jan 16, 2024
2 parents 166ab11 + 69ea4b5 commit ce7f164
Show file tree
Hide file tree
Showing 33 changed files with 531 additions and 152 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Breaking changes

- Removed `embedded-hal` 0.2 traits from prelude.
- Changed error type of `embedded-hal` 0.2 trait implementations for `BlockingI2c`.

### New features

- Implemented `embedded-hal` 1.0 traits where relevant.

## [0.7.1]

### Other changes
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ cast = { version = "0.3.0", default-features = false }
cortex-m = "0.7.7"
cortex-m-rt = "0.7.3"
embedded-dma = "0.2.0"
embedded-hal = { version = "0.2.7", features = ["unproven"] }
embedded-hal = "1.0.0"
embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = [
"unproven",
] }
embedded-io = "0.6.1"
gd32f1 = { version = "0.8.0", features = ["critical-section"] }
nb = "1.1.0"
void = { version = "1.0.2", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use panic_semihosting as _;

use cortex_m_rt::entry;
use gd32f1x0_hal::{adc::Adc, pac, prelude::*};

use cortex_m_semihosting::hprintln;
use embedded_hal_02::adc::OneShot;
use gd32f1x0_hal::{adc::Adc, pac, prelude::*};

#[entry]
fn main() -> ! {
Expand Down
3 changes: 2 additions & 1 deletion examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use panic_halt as _;
use nb::block;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use embedded_hal_02::timer::CountDown;
use gd32f1x0_hal::{pac, prelude::*, timer::Timer};

#[entry]
Expand Down
3 changes: 2 additions & 1 deletion examples/blinky_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use panic_halt as _;
use nb::block;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use embedded_hal_02::timer::CountDown;
use gd32f1x0_hal::{pac, prelude::*, timer::Timer};

#[entry]
Expand Down
2 changes: 1 addition & 1 deletion examples/blinky_rtc.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use panic_halt as _;
use stm32f1xx_hal::{pac, prelude::*, rtc::Rtc};

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use nb::block;

#[entry]
Expand Down
2 changes: 1 addition & 1 deletion examples/blinky_rtcalarm_irq.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::hal::{
use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;

// A type definition for the GPIO pin to be used for our LED
type LEDPIN = gpioc::PC13<Output<PushPull>>;
Expand Down
12 changes: 7 additions & 5 deletions examples/blinky_timer_irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@

use panic_halt as _;

use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal_02::{
digital::v2::{OutputPin, ToggleableOutputPin},
timer::CountDown,
};
use gd32f1x0_hal::{
gpio::{gpioc, Output, PushPull},
pac::{interrupt, Interrupt, Peripherals, TIMER1},
prelude::*,
timer::{CountDownTimer, Event, Timer},
};

use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;

// A type definition for the GPIO pin to be used for our LED
type LedPin = gpioc::PC13<Output<PushPull>>;

Expand Down
2 changes: 1 addition & 1 deletion examples/can-loopback.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bxcan::{
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use nb::block;
use stm32f1xx_hal::{can::Can, pac, prelude::*};

Expand Down
6 changes: 3 additions & 3 deletions examples/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::{delay::DelayNs, digital::OutputPin};
use gd32f1x0_hal::{delay::Delay, pac, prelude::*};

#[entry]
Expand All @@ -28,8 +28,8 @@ fn main() -> ! {

loop {
led.set_high().unwrap();
delay.delay_ms(1_000_u16);
delay.delay_ms(1_000);
led.set_low().unwrap();
delay.delay_ms(1_000_u16);
delay.delay_ms(1_000);
}
}
2 changes: 1 addition & 1 deletion examples/dynamic_gpio.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nb::block;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::digital::{InputPin, OutputPin};
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};

#[entry]
Expand Down
2 changes: 1 addition & 1 deletion examples/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use gd32f1x0_hal::{pac, prelude::*};

#[entry]
Expand Down
2 changes: 1 addition & 1 deletion examples/mfrc522.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use panic_itm as _;
use cortex_m::iprintln;

use cortex_m_rt::entry;
use embedded_hal::digital::{v1_compat::OldOutputPin, v2::OutputPin};
use embedded_hal_02::digital::{v1_compat::OldOutputPin, v2::OutputPin};
use mfrc522::Mfrc522;
use stm32f1xx_hal::{pac, prelude::*, spi::Spi};

Expand Down
2 changes: 1 addition & 1 deletion examples/multi_mode_gpio.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nb::block;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::digital::{InputPin, OutputPin};
use stm32f1xx_hal::{gpio::State, pac, prelude::*, timer::Timer};

#[entry]
Expand Down
1 change: 1 addition & 0 deletions examples/nojtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::StatefulOutputPin;
use gd32f1x0_hal::{pac, prelude::*};

#[entry]
Expand Down
1 change: 1 addition & 0 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use panic_halt as _;

use cortex_m::asm;
use cortex_m_rt::entry;
use embedded_hal_02::{Pwm, PwmPin};
use gd32f1x0_hal::{
gpio::{gpioa::PA11, OutputMode, PullMode},
pac,
Expand Down
1 change: 1 addition & 0 deletions examples/pwm_complementary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use panic_halt as _;

use cortex_m::asm;
use cortex_m_rt::entry;
use embedded_hal_02::{Pwm, PwmPin};
use gd32f1x0_hal::{
gpio::{OutputMode, PullMode},
pac,
Expand Down
22 changes: 11 additions & 11 deletions examples/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
use panic_halt as _;

use cortex_m::asm;

use nb::block;

use cortex_m_rt::entry;
use embedded_io::{Read, Write};
use gd32f1x0_hal::{
gpio::{OutputMode, PullMode},
pac,
Expand Down Expand Up @@ -65,24 +63,26 @@ fn main() -> ! {
);

// Loopback test. Write `X` and wait until the write is successful.
let sent = b'X';
block!(serial.write(sent)).ok();
let sent = b"X";
serial.write_all(sent).unwrap();

// Read the byte that was just sent. Blocks until the read is complete
let received = block!(serial.read()).unwrap();
let mut receive_buffer = [0];
assert_eq!(serial.read(&mut receive_buffer).unwrap(), 1);

// Since we have connected tx and rx, the byte we sent should be the one we received
assert_eq!(received, sent);
assert_eq!(&receive_buffer, sent);

// Trigger a breakpoint to allow us to inspect the values
asm::bkpt();

// You can also split the serial struct into a receiving and a transmitting part
let (mut tx, mut rx) = serial.split();
let sent = b'Y';
block!(tx.write(sent)).ok();
let received = block!(rx.read()).unwrap();
assert_eq!(received, sent);
let sent = b"Y";
tx.write_all(sent).unwrap();
let mut receive_buffer = [0];
assert_eq!(rx.read(&mut receive_buffer).unwrap(), 1);
assert_eq!(&receive_buffer, sent);
asm::bkpt();

#[allow(clippy::empty_loop)]
Expand Down
7 changes: 2 additions & 5 deletions examples/serial_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

use panic_halt as _;

use nb::block;

use cortex_m_rt::entry;
use embedded_io::Write;
use gd32f1x0_hal::{
gpio::{OutputMode, PullMode},
pac,
Expand Down Expand Up @@ -68,9 +67,7 @@ fn main() -> ! {
// Split the serial struct into a receiving and a transmitting part
let (mut tx, _rx) = serial.split();

let sent = b'U';
block!(tx.write(sent)).ok();
block!(tx.write(sent)).ok();
tx.write_all(b"UU").unwrap();

#[allow(clippy::empty_loop)]
loop {}
Expand Down
3 changes: 2 additions & 1 deletion examples/timer-interrupt-rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use panic_halt as _;

use rtic::app;

use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use embedded_hal_02::timer::CountDown;
use gd32f1x0_hal::{
gpio::{gpioc::PC13, Output, PushPull},
pac,
Expand Down
2 changes: 1 addition & 1 deletion examples/usb_serial.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern crate panic_semihosting;

use cortex_m::asm::delay;
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
use stm32f1xx_hal::{prelude::*, stm32};
use usb_device::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion examples/usb_serial_interrupt.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern crate panic_semihosting;

use cortex_m::asm::{delay, wfi};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use stm32f1xx_hal::pac::{interrupt, Interrupt};
use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType};
use stm32f1xx_hal::{prelude::*, stm32};
Expand Down
2 changes: 1 addition & 1 deletion examples/usb_serial_rtic.rs.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
extern crate panic_semihosting;

use cortex_m::asm::delay;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use rtic::app;
use stm32f1xx_hal::prelude::*;
use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType};
Expand Down
2 changes: 1 addition & 1 deletion src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use core::{
};
use cortex_m::asm::delay;
use embedded_dma::WriteBuffer;
use embedded_hal::adc::{Channel, OneShot};
use embedded_hal_02::adc::{Channel, OneShot};

/// The number of ADC clock cycles to wait between powering on and starting calibration.
const ADC_CALIBRATION_CYCLES: u32 = 14;
Expand Down
Loading

0 comments on commit ce7f164

Please sign in to comment.