Skip to content

Commit

Permalink
Merge pull request #435 from nrf-rs/embedded-hal
Browse files Browse the repository at this point in the history
Make embedded-hal 0.2 optional
  • Loading branch information
qwandor authored Apr 8, 2024
2 parents 4730554 + 5e9733d commit 1a15fb2
Show file tree
Hide file tree
Showing 40 changed files with 468 additions and 395 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
continue-on-error: ${{ matrix.rust_version == 'nightly' }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install Rust stable
uses: actions-rs/toolchain@v1
with:
Expand Down Expand Up @@ -44,10 +44,20 @@ jobs:
- rust_version: stable
rustflags: --deny warnings

format:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Copy Cargo.toml
run: cp Cargo.ci.toml Cargo.toml
- name: Format Rust code
run: cargo fmt --all -- --check

ci:
if: ${{ success() }}
# all new jobs must be added to this list
needs: [build]
needs: [build, format]
runs-on: ubuntu-latest
steps:
- name: CI succeeded
Expand Down
4 changes: 2 additions & 2 deletions examples/nvmc-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#[cfg(feature = "52840")]
use nrf52840_hal as hal;

use core::convert::TryInto;
use core::{convert::TryInto, ptr::addr_of_mut};
use embedded_storage::nor_flash::NorFlash;
use embedded_storage::nor_flash::ReadNorFlash;
use hal::nvmc::Nvmc;
Expand All @@ -33,7 +33,7 @@ fn main() -> ! {
let p = hal::pac::Peripherals::take().unwrap();

#[cfg(feature = "52840")]
let mut nvmc = Nvmc::new(p.NVMC, unsafe { &mut CONFIG });
let mut nvmc = Nvmc::new(p.NVMC, unsafe { addr_of_mut!(CONFIG).as_mut().unwrap() });

erase_if_needed(&mut nvmc, LAST_PAGE);

Expand Down
7 changes: 1 addition & 6 deletions examples/wdt-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ authors = ["James Munns <james.munns@ferrous-systems.com>"]
edition = "2018"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m = "0.7.3"
cortex-m-rt = { version = "0.7.0", features = ["device"] }
embedded-hal = "1.0.0"
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]
15 changes: 6 additions & 9 deletions examples/wdt-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
#![no_std]
#![no_main]

use embedded_hal::{
digital::v2::{InputPin, OutputPin},
timer::CountDown,
};
use embedded_hal::digital::{InputPin, OutputPin};
use {
core::panic::PanicInfo,
hal::{
Expand All @@ -37,10 +34,10 @@ fn main() -> ! {
rtt_init_print!();
let p0 = hal::gpio::p0::Parts::new(p.P0);

let btn1 = p0.p0_11.into_pullup_input().degrade();
let btn2 = p0.p0_12.into_pullup_input().degrade();
let btn3 = p0.p0_24.into_pullup_input().degrade();
let btn4 = p0.p0_25.into_pullup_input().degrade();
let mut btn1 = p0.p0_11.into_pullup_input().degrade();
let mut btn2 = p0.p0_12.into_pullup_input().degrade();
let mut btn3 = p0.p0_24.into_pullup_input().degrade();
let mut btn4 = p0.p0_25.into_pullup_input().degrade();

let mut led1 = p0.p0_13.into_push_pull_output(Level::High).degrade();
let mut led2 = p0.p0_14.into_push_pull_output(Level::High).degrade();
Expand Down Expand Up @@ -102,7 +99,7 @@ fn main() -> ! {
rprintln!("Not restarted by the dog!");
}

let buttons = [&btn1, &btn2, &btn3, &btn4];
let buttons = [&mut btn1, &mut btn2, &mut btn3, &mut btn4];

let leds = [&mut led1, &mut led2, &mut led3, &mut led4];

Expand Down
13 changes: 7 additions & 6 deletions nrf-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]
cortex-m = "0.7.3"
nb = "1.0.0"
fixed = "1.0.0"
rand_core = "0.6.3"
cortex-m = "0.7.7"
nb = "1.1.0"
fixed = "1.25.1"
rand_core = "0.6.4"
cfg-if = "1.0.0"
embedded-dma = "0.2.0"
embedded-storage = "0.3.0"
embedded-storage = "0.3.1"

[dependencies.void]
default-features = false
Expand Down Expand Up @@ -79,7 +79,8 @@ optional = true
[dependencies.embedded-hal-02]
package = "embedded-hal"
features = ["unproven"]
version = "0.2.4"
version = "0.2.7"
optional = true

[dependencies.embedded-hal]
version = "1.0.0"
Expand Down
83 changes: 51 additions & 32 deletions nrf-hal-common/src/adc.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
//! API for the Analog to Digital converter.

use embedded_hal_02::adc::{Channel, OneShot};

use core::hint::unreachable_unchecked;

use crate::{
gpio::{Floating, Input},
pac::{
adc::config::{INPSEL_A as InputSelection, REFSEL_A as Reference, RES_A as Resolution},
ADC,
},
};
use core::hint::unreachable_unchecked;

#[cfg(feature = "embedded-hal-02")]
pub trait Channel: embedded_hal_02::adc::Channel<Adc, ID = u8> {}

#[cfg(not(feature = "embedded-hal-02"))]
pub trait Channel {
fn channel() -> u8;
}

pub struct Adc(ADC);

Expand Down Expand Up @@ -57,32 +62,9 @@ impl Adc {

Self(adc)
}
}

pub struct AdcConfig {
pub resolution: Resolution,
pub input_selection: InputSelection,
pub reference: Reference,
}

// 0 volts reads as 0, VDD volts reads as 2^10.
impl Default for AdcConfig {
fn default() -> Self {
Self {
resolution: Resolution::_10BIT,
input_selection: InputSelection::ANALOG_INPUT_ONE_THIRD_PRESCALING,
reference: Reference::SUPPLY_ONE_THIRD_PRESCALING,
}
}
}

impl<PIN> OneShot<Adc, i16, PIN> for Adc
where
PIN: Channel<Adc, ID = u8>,
{
type Error = ();

fn read(&mut self, _pin: &mut PIN) -> nb::Result<i16, Self::Error> {
/// Samples the given channel of the ADC and returns the value read.
pub fn read_channel<PIN: Channel>(&mut self, _pin: &PIN) -> i16 {
let original_inpsel = self.0.config.read().inpsel();
match PIN::channel() {
0 => self.0.config.modify(|_, w| w.psel().analog_input0()),
Expand Down Expand Up @@ -118,17 +100,54 @@ where
.modify(|_, w| w.inpsel().variant(original_inpsel.variant().unwrap()));

// Max resolution is 10 bits so casting is always safe
Ok(self.0.result.read().result().bits() as i16)
self.0.result.read().result().bits() as i16
}
}

pub struct AdcConfig {
pub resolution: Resolution,
pub input_selection: InputSelection,
pub reference: Reference,
}

// 0 volts reads as 0, VDD volts reads as 2^10.
impl Default for AdcConfig {
fn default() -> Self {
Self {
resolution: Resolution::_10BIT,
input_selection: InputSelection::ANALOG_INPUT_ONE_THIRD_PRESCALING,
reference: Reference::SUPPLY_ONE_THIRD_PRESCALING,
}
}
}

#[cfg(feature = "embedded-hal-02")]
impl<PIN> embedded_hal_02::adc::OneShot<Adc, i16, PIN> for Adc
where
PIN: Channel,
{
type Error = ();

fn read(&mut self, pin: &mut PIN) -> nb::Result<i16, Self::Error> {
Ok(self.read_channel(pin))
}
}

macro_rules! channel_mappings {
($($n:expr => $pin:path),*) => {
$(
impl Channel<Adc> for $pin {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::adc::Channel<Adc> for $pin {
type ID = u8;

fn channel() -> <Self as embedded_hal_02::adc::Channel<Adc>>::ID {
fn channel() -> u8 {
$n
}
}

impl Channel for $pin {
#[cfg(not(feature = "embedded-hal-02"))]
fn channel() -> u8 {
$n
}
}
Expand Down
19 changes: 12 additions & 7 deletions nrf-hal-common/src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use core::convert::TryInto;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use embedded_hal::delay::DelayNs;
use embedded_hal_02::blocking::delay::{DelayMs, DelayUs};

/// System timer (SysTick) as a delay provider.
pub struct Delay {
Expand All @@ -26,37 +25,43 @@ impl Delay {
}
}

impl DelayMs<u32> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
DelayNs::delay_ms(self, ms);
}
}

impl DelayMs<u16> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
DelayNs::delay_ms(self, ms.into());
}
}

impl DelayMs<u8> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
DelayNs::delay_ms(self, ms.into());
}
}

impl DelayUs<u32> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
DelayNs::delay_us(self, us);
}
}

impl DelayUs<u16> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
DelayNs::delay_us(self, us.into());
}
}

impl DelayUs<u8> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
DelayNs::delay_us(self, us.into());
}
Expand Down
Loading

0 comments on commit 1a15fb2

Please sign in to comment.