From b7bb3ac9db3a9843e8b16b787c66ebbb0b482382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Mon, 21 Sep 2020 21:39:19 +0200 Subject: [PATCH 01/15] Initial support for the nRF52811 --- CHANGELOG.md | 1 + Cargo.toml | 1 + README.md | 2 + examples/ccm-demo/Cargo.toml | 2 + examples/ccm-demo/README.md | 1 + examples/ccm-demo/src/main.rs | 2 + examples/ecb-demo/Cargo.toml | 2 + examples/ecb-demo/README.md | 1 + examples/ecb-demo/src/main.rs | 2 + examples/ppi-demo/Cargo.toml | 2 + examples/ppi-demo/README.md | 1 + examples/ppi-demo/src/main.rs | 4 + examples/rtic-demo/Cargo.toml | 5 + examples/rtic-demo/src/main.rs | 3 + nrf-hal-common/Cargo.toml | 6 ++ nrf-hal-common/src/ccm.rs | 14 ++- nrf-hal-common/src/comp.rs | 6 +- nrf-hal-common/src/lib.rs | 14 ++- nrf-hal-common/src/ppi/event_nrf52811.rs | 126 +++++++++++++++++++++++ nrf-hal-common/src/ppi/mod.rs | 3 + nrf-hal-common/src/ppi/task_nrf52811.rs | 107 +++++++++++++++++++ nrf-hal-common/src/pwm.rs | 12 +-- nrf-hal-common/src/spim.rs | 12 ++- nrf-hal-common/src/uicr.rs | 1 + nrf52811-hal/Cargo.toml | 34 ++++++ nrf52811-hal/build.rs | 22 ++++ nrf52811-hal/memory.x | 23 +++++ nrf52811-hal/src/lib.rs | 22 ++++ xtask/src/lib.rs | 16 ++- 29 files changed, 427 insertions(+), 20 deletions(-) create mode 100644 nrf-hal-common/src/ppi/event_nrf52811.rs create mode 100644 nrf-hal-common/src/ppi/task_nrf52811.rs create mode 100644 nrf52811-hal/Cargo.toml create mode 100644 nrf52811-hal/build.rs create mode 100644 nrf52811-hal/memory.x create mode 100644 nrf52811-hal/src/lib.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 06836e73..639691ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - PWM module ([#200]). - I2S module ([#201]). - SPIS module ([#226]). +- Add support for the nRF52811. ### Fixes diff --git a/Cargo.toml b/Cargo.toml index b7855903..a6b0b921 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "xtask", "nrf52810-hal", + "nrf52811-hal", "nrf52832-hal", "nrf52833-hal", "nrf52840-hal", diff --git a/README.md b/README.md index 3a2b6a74..e7d12a70 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Every nRF chip has its own crate, listed below: |-------|------|-----------| | [`nrf51-hal`](./nrf51-hal) | [![docs.rs](https://docs.rs/nrf51-hal/badge.svg)](https://docs.rs/nrf51-hal) | [![crates.io](https://img.shields.io/crates/d/nrf51-hal.svg)](https://crates.io/crates/nrf51-hal) | | [`nrf52810-hal`](./nrf52810-hal) | [![docs.rs](https://docs.rs/nrf52810-hal/badge.svg)](https://docs.rs/nrf52810-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52810-hal.svg)](https://crates.io/crates/nrf52810-hal) | +| [`nrf52810-hal`](./nrf52811-hal) | [![docs.rs](https://docs.rs/nrf52811-hal/badge.svg)](https://docs.rs/nrf52811-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52811-hal.svg)](https://crates.io/crates/nrf52811-hal) | | [`nrf52832-hal`](./nrf52832-hal) | [![docs.rs](https://docs.rs/nrf52832-hal/badge.svg)](https://docs.rs/nrf52832-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52832-hal.svg)](https://crates.io/crates/nrf52832-hal) | | [`nrf52833-hal`](./nrf52833-hal) | [![docs.rs](https://docs.rs/nrf52833-hal/badge.svg)](https://docs.rs/nrf52833-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52833-hal.svg)](https://crates.io/crates/nrf52833-hal) | | [`nrf52840-hal`](./nrf52840-hal) | [![docs.rs](https://docs.rs/nrf52840-hal/badge.svg)](https://docs.rs/nrf52840-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52840-hal.svg)](https://crates.io/crates/nrf52840-hal) | @@ -27,6 +28,7 @@ Every nRF chip has its own crate, listed below: - [nRF52840 Reference Manual](http://infocenter.nordicsemi.com/pdf/nRF52840_PS_v1.0.pdf) - [nRF52832 Reference Manual](http://infocenter.nordicsemi.com/pdf/nRF52832_PS_v1.4.pdf) +- [nRF52811 Reference Manual](http://infocenter.nordicsemi.com/pdf/nRF52811_PS_v1.0.pdf) - [nRF52810 Reference Manual](http://infocenter.nordicsemi.com/pdf/nRF52810_PS_v1.2.pdf) - [nRF9160 Reference Manual](http://infocenter.nordicsemi.com/pdf/nRF9160_PS_v1.1.pdf) diff --git a/examples/ccm-demo/Cargo.toml b/examples/ccm-demo/Cargo.toml index 2c6d6050..914db149 100644 --- a/examples/ccm-demo/Cargo.toml +++ b/examples/ccm-demo/Cargo.toml @@ -12,6 +12,7 @@ rtt-target = {version = "0.2.0", features = ["cortex-m"] } rand_core = "0.5.1" nrf52810-hal = { path = "../../nrf52810-hal", features = ["rt"], optional = true } +nrf52811-hal = { path = "../../nrf52811-hal", features = ["rt"], optional = true } nrf52832-hal = { path = "../../nrf52832-hal", features = ["rt"], optional = true } nrf52840-hal = { path = "../../nrf52840-hal", features = ["rt"], optional = true } nrf52833-hal = { path = "../../nrf52833-hal", features = ["rt"], optional = true } @@ -23,6 +24,7 @@ test = false [features] 52810 = ["nrf52810-hal"] +52811 = ["nrf52811-hal"] 52832 = ["nrf52832-hal"] 52840 = ["nrf52840-hal"] 52833 = ["nrf52833-hal"] diff --git a/examples/ccm-demo/README.md b/examples/ccm-demo/README.md index 6c7f68cd..6728902d 100644 --- a/examples/ccm-demo/README.md +++ b/examples/ccm-demo/README.md @@ -2,6 +2,7 @@ Choose the microcontroller with one of the following features: - 52810 +- 52811 - 52832 - 52840 diff --git a/examples/ccm-demo/src/main.rs b/examples/ccm-demo/src/main.rs index ebbd4aee..3086019c 100644 --- a/examples/ccm-demo/src/main.rs +++ b/examples/ccm-demo/src/main.rs @@ -4,6 +4,8 @@ // Import the right HAL/PAC crate, depending on the target chip #[cfg(feature = "52810")] pub use nrf52810_hal as hal; +#[cfg(feature = "52811")] +pub use nrf52811_hal as hal; #[cfg(feature = "52832")] pub use nrf52832_hal as hal; #[cfg(feature = "52833")] diff --git a/examples/ecb-demo/Cargo.toml b/examples/ecb-demo/Cargo.toml index 3fa0c6c5..53286825 100644 --- a/examples/ecb-demo/Cargo.toml +++ b/examples/ecb-demo/Cargo.toml @@ -11,6 +11,7 @@ cortex-m-rt = "0.6.12" rtt-target = {version = "0.2.0", features = ["cortex-m"] } nrf52810-hal = { path = "../../nrf52810-hal", features = ["rt"], optional = true } +nrf52811-hal = { path = "../../nrf52811-hal", features = ["rt"], optional = true } nrf52832-hal = { path = "../../nrf52832-hal", features = ["rt"], optional = true } nrf52840-hal = { path = "../../nrf52840-hal", features = ["rt"], optional = true } nrf52833-hal = { path = "../../nrf52833-hal", features = ["rt"], optional = true } @@ -24,6 +25,7 @@ test = false [features] 51 = ["nrf51-hal"] 52810 = ["nrf52810-hal"] +52811 = ["nrf52811-hal"] 52832 = ["nrf52832-hal"] 52840 = ["nrf52840-hal"] 52833 = ["nrf52833-hal"] diff --git a/examples/ecb-demo/README.md b/examples/ecb-demo/README.md index af4c3e76..e2460508 100644 --- a/examples/ecb-demo/README.md +++ b/examples/ecb-demo/README.md @@ -3,6 +3,7 @@ Choose the microcontroller with one of the following features: - 51 - 52810 +- 52811 - 52832 - 52840 diff --git a/examples/ecb-demo/src/main.rs b/examples/ecb-demo/src/main.rs index 9a9f7dc3..9033f2ca 100644 --- a/examples/ecb-demo/src/main.rs +++ b/examples/ecb-demo/src/main.rs @@ -6,6 +6,8 @@ pub use nrf51_hal as hal; #[cfg(feature = "52810")] pub use nrf52810_hal as hal; +#[cfg(feature = "52811")] +pub use nrf52811_hal as hal; #[cfg(feature = "52832")] pub use nrf52832_hal as hal; #[cfg(feature = "52833")] diff --git a/examples/ppi-demo/Cargo.toml b/examples/ppi-demo/Cargo.toml index cbc6684f..3ee99e2a 100644 --- a/examples/ppi-demo/Cargo.toml +++ b/examples/ppi-demo/Cargo.toml @@ -11,6 +11,7 @@ cortex-m-rt = "0.6.12" rtt-target = {version = "0.2.0", features = ["cortex-m"] } nrf52810-hal = { path = "../../nrf52810-hal", features = ["rt"], optional = true } +nrf52811-hal = { path = "../../nrf52811-hal", features = ["rt"], optional = true } nrf52832-hal = { path = "../../nrf52832-hal", features = ["rt"], optional = true } nrf52840-hal = { path = "../../nrf52840-hal", features = ["rt"], optional = true } nrf52833-hal = { path = "../../nrf52833-hal", features = ["rt"], optional = true } @@ -24,6 +25,7 @@ test = false [features] 51 = ["nrf51-hal"] 52810 = ["nrf52810-hal"] +52811 = ["nrf52811-hal"] 52832 = ["nrf52832-hal"] 52840 = ["nrf52840-hal"] 52833 = ["nrf52833-hal"] diff --git a/examples/ppi-demo/README.md b/examples/ppi-demo/README.md index 49ded381..e09ded2b 100644 --- a/examples/ppi-demo/README.md +++ b/examples/ppi-demo/README.md @@ -3,6 +3,7 @@ Choose the microcontroller with one of the following features: - 51 - 52810 +- 52811 - 52832 - 52840 diff --git a/examples/ppi-demo/src/main.rs b/examples/ppi-demo/src/main.rs index c52c9d8a..d8ef6cc7 100644 --- a/examples/ppi-demo/src/main.rs +++ b/examples/ppi-demo/src/main.rs @@ -4,6 +4,7 @@ #[cfg(not(any( feature = "51", feature = "52810", + feature = "52811", feature = "52832", feature = "52833", feature = "52840" @@ -12,6 +13,7 @@ compile_error!( "This example requires one of the following device features enabled: 51 52810 + 52811 52832 52833 52840" @@ -22,6 +24,8 @@ compile_error!( pub use nrf51_hal as hal; #[cfg(feature = "52810")] pub use nrf52810_hal as hal; +#[cfg(feature = "52811")] +pub use nrf52811_hal as hal; #[cfg(feature = "52832")] pub use nrf52832_hal as hal; #[cfg(feature = "52833")] diff --git a/examples/rtic-demo/Cargo.toml b/examples/rtic-demo/Cargo.toml index 15f22877..31c885ab 100644 --- a/examples/rtic-demo/Cargo.toml +++ b/examples/rtic-demo/Cargo.toml @@ -18,6 +18,10 @@ optional = true path = "../../nrf52810-hal" optional = true +[dependencies.nrf52811-hal] +path = "../../nrf52811-hal" +optional = true + [dependencies.nrf52832-hal] path = "../../nrf52832-hal" optional = true @@ -29,5 +33,6 @@ optional = true [features] 51 = ["nrf51-hal"] 52810 = ["nrf52810-hal"] +52811 = ["nrf52811-hal"] 52832 = ["nrf52832-hal"] 52840 = ["nrf52840-hal"] diff --git a/examples/rtic-demo/src/main.rs b/examples/rtic-demo/src/main.rs index 66c7344a..6b5e7b98 100644 --- a/examples/rtic-demo/src/main.rs +++ b/examples/rtic-demo/src/main.rs @@ -13,6 +13,9 @@ use nrf51_hal as hal; #[cfg(feature = "52810")] use nrf52810_hal as hal; +#[cfg(feature = "52811")] +use nrf52811_hal as hal; + #[cfg(feature = "52832")] use nrf52832_hal as hal; diff --git a/nrf-hal-common/Cargo.toml b/nrf-hal-common/Cargo.toml index f6fbc950..dcfc2e8a 100644 --- a/nrf-hal-common/Cargo.toml +++ b/nrf-hal-common/Cargo.toml @@ -42,6 +42,11 @@ version = "0.9.0" optional = true version = "0.9.0" +[dependencies.nrf52811-pac] +optional = true +git = "https://github.com/nrf-rs/nrf52811-pac" +version = "0.9.0" + [dependencies.nrf52832-pac] optional = true version = "0.9.0" @@ -66,6 +71,7 @@ version = "0.2.4" doc = [] 51 = ["nrf51"] 52810 = ["nrf52810-pac"] +52811 = ["nrf52811-pac"] 52832 = ["nrf52832-pac"] 52833 = ["nrf52833-pac"] 52840 = ["nrf52840-pac"] diff --git a/nrf-hal-common/src/ccm.rs b/nrf-hal-common/src/ccm.rs index 05463a04..d25221fd 100644 --- a/nrf-hal-common/src/ccm.rs +++ b/nrf-hal-common/src/ccm.rs @@ -249,7 +249,12 @@ impl Ccm { let length_variant = if payload_len <= MAXIMUM_LENGTH_5BITS - MIC_SIZE { LENGTH_A::DEFAULT } else { - #[cfg(any(feature = "52840", feature = "52833", feature = "52810"))] + #[cfg(any( + feature = "52840", + feature = "52833", + feature = "52811", + feature = "52810" + ))] // NOTE(unsafe) Any 8bits pattern is safe to write to this register self.regs .maxpacketsize @@ -373,7 +378,12 @@ impl Ccm { let length_variant = if payload_len <= MAXIMUM_LENGTH_5BITS { LENGTH_A::DEFAULT } else { - #[cfg(any(feature = "52840", feature = "52833", feature = "52810"))] + #[cfg(any( + feature = "52840", + feature = "52833", + feature = "52811", + feature = "52810" + ))] // NOTE(unsafe) Any 8bits pattern is safe to write to this register self.regs .maxpacketsize diff --git a/nrf-hal-common/src/comp.rs b/nrf-hal-common/src/comp.rs index f3a8187b..f42d7451 100644 --- a/nrf-hal-common/src/comp.rs +++ b/nrf-hal-common/src/comp.rs @@ -293,7 +293,7 @@ comp_ref_pins! { P0_31> => EXTREFSEL_A::ANALOGREFERENCE7, } -#[cfg(not(feature = "52810"))] +#[cfg(not(any(feature = "52811", feature = "52810")))] comp_input_pins! { P0_02> => PSEL_A::ANALOGINPUT0, P0_03> => PSEL_A::ANALOGINPUT1, @@ -302,10 +302,10 @@ comp_input_pins! { P0_28> => PSEL_A::ANALOGINPUT4, P0_29> => PSEL_A::ANALOGINPUT5, P0_30> => PSEL_A::ANALOGINPUT6, - P0_31> => PSEL_A::ANALOGINPUT7, + P0_31> => PSEL_A::ANALOGINPUT6, } -#[cfg(feature = "52810")] +#[cfg(any(feature = "52811", feature = "52810"))] comp_input_pins! { P0_02> => PSEL_A::ANALOGINPUT0, P0_03> => PSEL_A::ANALOGINPUT1, diff --git a/nrf-hal-common/src/lib.rs b/nrf-hal-common/src/lib.rs index e710a0d7..54924aa4 100644 --- a/nrf-hal-common/src/lib.rs +++ b/nrf-hal-common/src/lib.rs @@ -12,6 +12,9 @@ pub use nrf51 as pac; #[cfg(feature = "52810")] pub use nrf52810_pac as pac; +#[cfg(feature = "52811")] +pub use nrf52811_pac as pac; + #[cfg(feature = "52832")] pub use nrf52832_pac as pac; @@ -38,9 +41,9 @@ pub mod ecb; pub mod gpio; #[cfg(not(feature = "9160"))] pub mod gpiote; -#[cfg(not(any(feature = "51", feature = "52810")))] +#[cfg(not(any(feature = "51", feature = "52810", feature = "52811")))] pub mod i2s; -#[cfg(not(any(feature = "52810", feature = "9160")))] +#[cfg(not(any(feature = "52811", feature = "52810", feature = "9160")))] pub mod lpcomp; #[cfg(not(feature = "9160"))] pub mod ppi; @@ -88,7 +91,12 @@ pub mod prelude { } /// Length of Nordic EasyDMA differs for MCUs -#[cfg(any(feature = "52810", feature = "52832", feature = "51"))] +#[cfg(any( + feature = "52810", + feature = "52811", + feature = "52832", + feature = "51" +))] pub mod target_constants { // NRF52832 8 bits1..0xFF pub const EASY_DMA_SIZE: usize = 255; diff --git a/nrf-hal-common/src/ppi/event_nrf52811.rs b/nrf-hal-common/src/ppi/event_nrf52811.rs new file mode 100644 index 00000000..6f68478f --- /dev/null +++ b/nrf-hal-common/src/ppi/event_nrf52811.rs @@ -0,0 +1,126 @@ +use crate::ppi::Event; + +// Event impls +// +// To reproduce, in the pac crate, search +// `rg 'type EVENTS_.*crate::Reg' --type rust --no-heading --no-line-number` +// Find (regex): +// `^src/(.*)\.rs:pub type (.*) = .*$` +// Replace (regex): +// `impl Event for crate::pac::$1::$2 { }` +impl Event for crate::pac::egu0::EVENTS_TRIGGERED {} +impl Event for crate::pac::gpiote::EVENTS_IN {} +impl Event for crate::pac::gpiote::EVENTS_PORT {} +impl Event for crate::pac::uarte0::EVENTS_CTS {} +impl Event for crate::pac::uarte0::EVENTS_NCTS {} +impl Event for crate::pac::uarte0::EVENTS_RXDRDY {} +impl Event for crate::pac::uarte0::EVENTS_ENDRX {} +impl Event for crate::pac::uarte0::EVENTS_TXDRDY {} +impl Event for crate::pac::uarte0::EVENTS_ENDTX {} +impl Event for crate::pac::uarte0::EVENTS_ERROR {} +impl Event for crate::pac::uarte0::EVENTS_RXTO {} +impl Event for crate::pac::uarte0::EVENTS_RXSTARTED {} +impl Event for crate::pac::uarte0::EVENTS_TXSTARTED {} +impl Event for crate::pac::uarte0::EVENTS_TXSTOPPED {} +impl Event for crate::pac::spi1::EVENTS_READY {} +impl Event for crate::pac::uart0::EVENTS_CTS {} +impl Event for crate::pac::uart0::EVENTS_NCTS {} +impl Event for crate::pac::uart0::EVENTS_RXDRDY {} +impl Event for crate::pac::uart0::EVENTS_TXDRDY {} +impl Event for crate::pac::uart0::EVENTS_ERROR {} +impl Event for crate::pac::uart0::EVENTS_RXTO {} +impl Event for crate::pac::clock::EVENTS_HFCLKSTARTED {} +impl Event for crate::pac::clock::EVENTS_LFCLKSTARTED {} +impl Event for crate::pac::clock::EVENTS_DONE {} +impl Event for crate::pac::clock::EVENTS_CTTO {} +impl Event for crate::pac::spis1::EVENTS_END {} +impl Event for crate::pac::spis1::EVENTS_ENDRX {} +impl Event for crate::pac::spis1::EVENTS_ACQUIRED {} +impl Event for crate::pac::ecb::EVENTS_ENDECB {} +impl Event for crate::pac::ecb::EVENTS_ERRORECB {} +impl Event for crate::pac::timer0::EVENTS_COMPARE {} +impl Event for crate::pac::wdt::EVENTS_TIMEOUT {} +impl Event for crate::pac::pdm::EVENTS_STARTED {} +impl Event for crate::pac::pdm::EVENTS_STOPPED {} +impl Event for crate::pac::pdm::EVENTS_END {} +impl Event for crate::pac::qdec::EVENTS_SAMPLERDY {} +impl Event for crate::pac::qdec::EVENTS_REPORTRDY {} +impl Event for crate::pac::qdec::EVENTS_ACCOF {} +impl Event for crate::pac::qdec::EVENTS_DBLRDY {} +impl Event for crate::pac::qdec::EVENTS_STOPPED {} +impl Event for crate::pac::ccm::EVENTS_ENDKSGEN {} +impl Event for crate::pac::ccm::EVENTS_ENDCRYPT {} +impl Event for crate::pac::ccm::EVENTS_ERROR {} +impl Event for crate::pac::aar::EVENTS_END {} +impl Event for crate::pac::aar::EVENTS_RESOLVED {} +impl Event for crate::pac::aar::EVENTS_NOTRESOLVED {} +impl Event for crate::pac::rtc0::EVENTS_TICK {} +impl Event for crate::pac::rtc0::EVENTS_OVRFLW {} +impl Event for crate::pac::rtc0::EVENTS_COMPARE {} +impl Event for crate::pac::radio::EVENTS_READY {} +impl Event for crate::pac::radio::EVENTS_ADDRESS {} +impl Event for crate::pac::radio::EVENTS_PAYLOAD {} +impl Event for crate::pac::radio::EVENTS_END {} +impl Event for crate::pac::radio::EVENTS_DISABLED {} +impl Event for crate::pac::radio::EVENTS_DEVMATCH {} +impl Event for crate::pac::radio::EVENTS_DEVMISS {} +impl Event for crate::pac::radio::EVENTS_RSSIEND {} +impl Event for crate::pac::radio::EVENTS_BCMATCH {} +impl Event for crate::pac::radio::EVENTS_CRCOK {} +impl Event for crate::pac::radio::EVENTS_CRCERROR {} +impl Event for crate::pac::radio::EVENTS_FRAMESTART {} +impl Event for crate::pac::radio::EVENTS_EDEND {} +impl Event for crate::pac::radio::EVENTS_EDSTOPPED {} +impl Event for crate::pac::radio::EVENTS_CCAIDLE {} +impl Event for crate::pac::radio::EVENTS_CCABUSY {} +impl Event for crate::pac::radio::EVENTS_CCASTOPPED {} +impl Event for crate::pac::radio::EVENTS_RATEBOOST {} +impl Event for crate::pac::radio::EVENTS_TXREADY {} +impl Event for crate::pac::radio::EVENTS_RXREADY {} +impl Event for crate::pac::radio::EVENTS_MHRMATCH {} +impl Event for crate::pac::radio::EVENTS_PHYEND {} +impl Event for crate::pac::radio::EVENTS_CTEPRESENT {} +impl Event for crate::pac::comp::EVENTS_READY {} +impl Event for crate::pac::comp::EVENTS_DOWN {} +impl Event for crate::pac::comp::EVENTS_UP {} +impl Event for crate::pac::comp::EVENTS_CROSS {} +impl Event for crate::pac::pwm0::EVENTS_STOPPED {} +impl Event for crate::pac::pwm0::EVENTS_SEQSTARTED {} +impl Event for crate::pac::pwm0::EVENTS_SEQEND {} +impl Event for crate::pac::pwm0::EVENTS_PWMPERIODEND {} +impl Event for crate::pac::pwm0::EVENTS_LOOPSDONE {} +impl Event for crate::pac::twim0::EVENTS_STOPPED {} +impl Event for crate::pac::twim0::EVENTS_ERROR {} +impl Event for crate::pac::twim0::EVENTS_SUSPENDED {} +impl Event for crate::pac::twim0::EVENTS_RXSTARTED {} +impl Event for crate::pac::twim0::EVENTS_TXSTARTED {} +impl Event for crate::pac::twim0::EVENTS_LASTRX {} +impl Event for crate::pac::twim0::EVENTS_LASTTX {} +impl Event for crate::pac::twi0::EVENTS_STOPPED {} +impl Event for crate::pac::twi0::EVENTS_RXDREADY {} +impl Event for crate::pac::twi0::EVENTS_TXDSENT {} +impl Event for crate::pac::twi0::EVENTS_ERROR {} +impl Event for crate::pac::twi0::EVENTS_BB {} +impl Event for crate::pac::twi0::EVENTS_SUSPENDED {} +impl Event for crate::pac::saadc::EVENTS_STARTED {} +impl Event for crate::pac::saadc::EVENTS_END {} +impl Event for crate::pac::saadc::EVENTS_DONE {} +impl Event for crate::pac::saadc::EVENTS_RESULTDONE {} +impl Event for crate::pac::saadc::EVENTS_CALIBRATEDONE {} +impl Event for crate::pac::saadc::EVENTS_STOPPED {} +impl Event for crate::pac::spim1::EVENTS_STOPPED {} +impl Event for crate::pac::spim1::EVENTS_ENDRX {} +impl Event for crate::pac::spim1::EVENTS_END {} +impl Event for crate::pac::spim1::EVENTS_ENDTX {} +impl Event for crate::pac::spim1::EVENTS_STARTED {} +impl Event for crate::pac::power::EVENTS_POFWARN {} +impl Event for crate::pac::power::EVENTS_SLEEPENTER {} +impl Event for crate::pac::power::EVENTS_SLEEPEXIT {} +impl Event for crate::pac::twis0::EVENTS_STOPPED {} +impl Event for crate::pac::twis0::EVENTS_ERROR {} +impl Event for crate::pac::twis0::EVENTS_RXSTARTED {} +impl Event for crate::pac::twis0::EVENTS_TXSTARTED {} +impl Event for crate::pac::twis0::EVENTS_WRITE {} +impl Event for crate::pac::twis0::EVENTS_READ {} +impl Event for crate::pac::temp::EVENTS_DATARDY {} +impl Event for crate::pac::rng::EVENTS_VALRDY {} diff --git a/nrf-hal-common/src/ppi/mod.rs b/nrf-hal-common/src/ppi/mod.rs index b6a3af79..1b258c42 100644 --- a/nrf-hal-common/src/ppi/mod.rs +++ b/nrf-hal-common/src/ppi/mod.rs @@ -21,6 +21,9 @@ cfg_if! { } else if #[cfg(feature = "52810")] { mod event_nrf52810; mod task_nrf52810; + } else if #[cfg(feature = "52811")] { + mod event_nrf52811; + mod task_nrf52811; } else if #[cfg(feature = "52832")] { mod event_nrf52832; mod task_nrf52832; diff --git a/nrf-hal-common/src/ppi/task_nrf52811.rs b/nrf-hal-common/src/ppi/task_nrf52811.rs new file mode 100644 index 00000000..967d2af3 --- /dev/null +++ b/nrf-hal-common/src/ppi/task_nrf52811.rs @@ -0,0 +1,107 @@ +use crate::ppi::Task; + +// Task Impls +// +// To reproduce, in the pac crate, search +// `rg 'type TASKS_.*crate::Reg' --type rust --no-heading --no-line-number` +// Find (regex): +// `^src/(.*)\.rs:pub type (.*) = .*$` +// Replace (regex): +// `impl Task for crate::pac::$1::$2 { }` +// Find (regex): +// `^impl Task for crate::pac::spim0::(.*)$` +// Replace (regex): +// `impl Task for crate::pac::spim1::$1` +impl Task for crate::pac::spim1::TASKS_START {} +impl Task for crate::pac::spim1::TASKS_STOP {} +impl Task for crate::pac::spim1::TASKS_SUSPEND {} +impl Task for crate::pac::spim1::TASKS_RESUME {} +impl Task for crate::pac::rng::TASKS_START {} +impl Task for crate::pac::rng::TASKS_STOP {} +impl Task for crate::pac::timer0::TASKS_START {} +impl Task for crate::pac::timer0::TASKS_STOP {} +impl Task for crate::pac::timer0::TASKS_COUNT {} +impl Task for crate::pac::timer0::TASKS_CLEAR {} +impl Task for crate::pac::timer0::TASKS_SHUTDOWN {} +impl Task for crate::pac::timer0::TASKS_CAPTURE {} +impl Task for crate::pac::spis1::TASKS_ACQUIRE {} +impl Task for crate::pac::spis1::TASKS_RELEASE {} +impl Task for crate::pac::uart0::TASKS_STARTRX {} +impl Task for crate::pac::uart0::TASKS_STOPRX {} +impl Task for crate::pac::uart0::TASKS_STARTTX {} +impl Task for crate::pac::uart0::TASKS_STOPTX {} +impl Task for crate::pac::uart0::TASKS_SUSPEND {} +impl Task for crate::pac::gpiote::TASKS_OUT {} +impl Task for crate::pac::gpiote::TASKS_SET {} +impl Task for crate::pac::gpiote::TASKS_CLR {} +impl Task for crate::pac::clock::TASKS_HFCLKSTART {} +impl Task for crate::pac::clock::TASKS_HFCLKSTOP {} +impl Task for crate::pac::clock::TASKS_LFCLKSTART {} +impl Task for crate::pac::clock::TASKS_LFCLKSTOP {} +impl Task for crate::pac::clock::TASKS_CAL {} +impl Task for crate::pac::clock::TASKS_CTSTART {} +impl Task for crate::pac::clock::TASKS_CTSTOP {} +impl Task for crate::pac::power::TASKS_CONSTLAT {} +impl Task for crate::pac::power::TASKS_LOWPWR {} +impl Task for crate::pac::egu0::TASKS_TRIGGER {} +impl Task for crate::pac::twim0::TASKS_STARTRX {} +impl Task for crate::pac::twim0::TASKS_STARTTX {} +impl Task for crate::pac::twim0::TASKS_STOP {} +impl Task for crate::pac::twim0::TASKS_SUSPEND {} +impl Task for crate::pac::twim0::TASKS_RESUME {} +impl Task for crate::pac::pdm::TASKS_START {} +impl Task for crate::pac::pdm::TASKS_STOP {} +impl Task for crate::pac::ecb::TASKS_STARTECB {} +impl Task for crate::pac::ecb::TASKS_STOPECB {} +impl Task for crate::pac::twi0::TASKS_STARTRX {} +impl Task for crate::pac::twi0::TASKS_STARTTX {} +impl Task for crate::pac::twi0::TASKS_STOP {} +impl Task for crate::pac::twi0::TASKS_SUSPEND {} +impl Task for crate::pac::twi0::TASKS_RESUME {} +impl Task for crate::pac::wdt::TASKS_START {} +impl Task for crate::pac::rtc0::TASKS_START {} +impl Task for crate::pac::rtc0::TASKS_STOP {} +impl Task for crate::pac::rtc0::TASKS_CLEAR {} +impl Task for crate::pac::rtc0::TASKS_TRIGOVRFLW {} +impl Task for crate::pac::radio::TASKS_TXEN {} +impl Task for crate::pac::radio::TASKS_RXEN {} +impl Task for crate::pac::radio::TASKS_START {} +impl Task for crate::pac::radio::TASKS_STOP {} +impl Task for crate::pac::radio::TASKS_DISABLE {} +impl Task for crate::pac::radio::TASKS_RSSISTART {} +impl Task for crate::pac::radio::TASKS_RSSISTOP {} +impl Task for crate::pac::radio::TASKS_BCSTART {} +impl Task for crate::pac::radio::TASKS_BCSTOP {} +impl Task for crate::pac::temp::TASKS_START {} +impl Task for crate::pac::temp::TASKS_STOP {} +impl Task for crate::pac::ccm::TASKS_KSGEN {} +impl Task for crate::pac::ccm::TASKS_CRYPT {} +impl Task for crate::pac::ccm::TASKS_STOP {} +impl Task for crate::pac::ccm::TASKS_RATEOVERRIDE {} +impl Task for crate::pac::uarte0::TASKS_STARTRX {} +impl Task for crate::pac::uarte0::TASKS_STOPRX {} +impl Task for crate::pac::uarte0::TASKS_STARTTX {} +impl Task for crate::pac::uarte0::TASKS_STOPTX {} +impl Task for crate::pac::uarte0::TASKS_FLUSHRX {} +impl Task for crate::pac::twis0::TASKS_STOP {} +impl Task for crate::pac::twis0::TASKS_SUSPEND {} +impl Task for crate::pac::twis0::TASKS_RESUME {} +impl Task for crate::pac::twis0::TASKS_PREPARERX {} +impl Task for crate::pac::twis0::TASKS_PREPARETX {} +impl Task for crate::pac::aar::TASKS_START {} +impl Task for crate::pac::aar::TASKS_STOP {} +impl Task for crate::pac::comp::TASKS_START {} +impl Task for crate::pac::comp::TASKS_STOP {} +impl Task for crate::pac::comp::TASKS_SAMPLE {} +impl Task for crate::pac::qdec::TASKS_START {} +impl Task for crate::pac::qdec::TASKS_STOP {} +impl Task for crate::pac::qdec::TASKS_READCLRACC {} +impl Task for crate::pac::qdec::TASKS_RDCLRACC {} +impl Task for crate::pac::qdec::TASKS_RDCLRDBL {} +impl Task for crate::pac::saadc::TASKS_START {} +impl Task for crate::pac::saadc::TASKS_SAMPLE {} +impl Task for crate::pac::saadc::TASKS_STOP {} +impl Task for crate::pac::saadc::TASKS_CALIBRATEOFFSET {} +impl Task for crate::pac::pwm0::TASKS_STOP {} +impl Task for crate::pac::pwm0::TASKS_SEQSTART {} +impl Task for crate::pac::pwm0::TASKS_NEXTSTEP {} diff --git a/nrf-hal-common/src/pwm.rs b/nrf-hal-common/src/pwm.rs index 5e04d007..e49535cb 100644 --- a/nrf-hal-common/src/pwm.rs +++ b/nrf-hal-common/src/pwm.rs @@ -977,13 +977,13 @@ pub trait Instance: private::Sealed {} impl Instance for PWM0 {} -#[cfg(not(any(feature = "52810")))] +#[cfg(not(any(feature = "52810", feature = "52811")))] impl Instance for PWM1 {} -#[cfg(not(any(feature = "52810")))] +#[cfg(not(any(feature = "52810", feature = "52811")))] impl Instance for PWM2 {} -#[cfg(not(any(feature = "52810", feature = "52832")))] +#[cfg(not(any(feature = "52810", feature = "52811", feature = "52832")))] impl Instance for PWM3 {} mod private { @@ -991,12 +991,12 @@ mod private { impl Sealed for crate::pwm::PWM0 {} - #[cfg(not(any(feature = "52810")))] + #[cfg(not(any(feature = "52810", feature = "52811")))] impl Sealed for crate::pwm::PWM1 {} - #[cfg(not(any(feature = "52810")))] + #[cfg(not(any(feature = "52810", feature = "52811")))] impl Sealed for crate::pwm::PWM2 {} - #[cfg(not(any(feature = "52810", feature = "52832")))] + #[cfg(not(any(feature = "52810", feature = "52811", feature = "52832")))] impl Sealed for crate::pwm::PWM3 {} } diff --git a/nrf-hal-common/src/spim.rs b/nrf-hal-common/src/spim.rs index 8c792402..b5131ac3 100644 --- a/nrf-hal-common/src/spim.rs +++ b/nrf-hal-common/src/spim.rs @@ -8,7 +8,10 @@ use core::sync::atomic::{compiler_fence, Ordering::SeqCst}; #[cfg(feature = "9160")] use crate::pac::{spim0_ns as spim0, SPIM0_NS as SPIM0}; -#[cfg(not(feature = "9160"))] +#[cfg(feature = "52811")] +use crate::pac::{spim1 as spim0, SPIM0, SPIM1}; + +#[cfg(not(any(feature = "9160", feature = "52811")))] use crate::pac::{spim0, SPIM0}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; @@ -395,7 +398,12 @@ pub trait Instance: Deref {} impl Instance for SPIM0 {} -#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] +#[cfg(any( + feature = "52832", + feature = "52833", + feature = "52840", + feature = "52811" +))] impl Instance for SPIM1 {} #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] diff --git a/nrf-hal-common/src/uicr.rs b/nrf-hal-common/src/uicr.rs index 5cb02dc0..d83c7e01 100644 --- a/nrf-hal-common/src/uicr.rs +++ b/nrf-hal-common/src/uicr.rs @@ -3,6 +3,7 @@ //! See product specification: //! //! - nrf52810: Section 4.5 +//! - nrf52811: Section 4.5 //! - nrf52832: Section 14 //! - nrf52840: Section 4.5 use crate::pac::{NVMC, UICR}; diff --git a/nrf52811-hal/Cargo.toml b/nrf52811-hal/Cargo.toml new file mode 100644 index 00000000..7a57dfce --- /dev/null +++ b/nrf52811-hal/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "nrf52811-hal" +version = "0.11.1" +edition = "2018" +description = "HAL for nRF52811 microcontrollers" +repository = "https://github.com/nrf-rs/nrf-hal" +authors = [ + "James Munns ", + "Hanno Braun ", + "John Scarrott ", + "Wez Furlong ", + "Ferdia McKeogh ", +] +categories = ["embedded", "hardware-support", "no-std"] +keywords = ["arm", "cortex-m", "nrf52", "hal", "nrf52811"] +license = "MIT OR Apache-2.0" + +[dependencies] +nrf52811-pac = { git = "https://github.com/nrf-rs/nrf52811-pac" } + +[dependencies.nrf-hal-common] +path = "../nrf-hal-common" +default-features = false +features = ["52811"] +version = "=0.11.1" + +[dependencies.embedded-hal] +features = ["unproven"] +version = "0.2.3" + +[features] +doc = [] +rt = ["nrf52811-pac/rt"] +default = ["rt"] diff --git a/nrf52811-hal/build.rs b/nrf52811-hal/build.rs new file mode 100644 index 00000000..fc8a63ea --- /dev/null +++ b/nrf52811-hal/build.rs @@ -0,0 +1,22 @@ +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + let target = env::var("TARGET").unwrap(); + if target.ends_with("eabihf") { + panic!("attempting to build nrf52811-hal for target `{}`, but the nRF52811 does not have an FPU", target); + } + + // Put the linker script somewhere the linker can find it + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=memory.x"); +} diff --git a/nrf52811-hal/memory.x b/nrf52811-hal/memory.x new file mode 100644 index 00000000..bbb40a74 --- /dev/null +++ b/nrf52811-hal/memory.x @@ -0,0 +1,23 @@ +/* Linker script for the nRF52 - WITHOUT SOFT DEVICE */ +MEMORY +{ + /* NOTE K = KiBi = 1024 bytes */ + FLASH : ORIGIN = 0x00000000, LENGTH = 192K + RAM : ORIGIN = 0x20000000, LENGTH = 24K +} + +/* This is where the call stack will be allocated. */ +/* The stack is of the full descending type. */ +/* You may want to use this variable to locate the call stack and static + variables in different memory regions. Below is shown the default value */ +/* _stack_start = ORIGIN(RAM) + LENGTH(RAM); */ + +/* You can use this symbol to customize the location of the .text section */ +/* If omitted the .text section will be placed right after the .vector_table + section */ +/* This is required only on microcontrollers that store some configuration right + after the vector table */ +/* _stext = ORIGIN(FLASH) + 0x400; */ + +/* Size of the heap (in bytes) */ +/* _heap_size = 1024; */ \ No newline at end of file diff --git a/nrf52811-hal/src/lib.rs b/nrf52811-hal/src/lib.rs new file mode 100644 index 00000000..105f70dd --- /dev/null +++ b/nrf52811-hal/src/lib.rs @@ -0,0 +1,22 @@ +#![no_std] +#![doc(html_root_url = "https://docs.rs/nrf52811-hal/0.11.1")] + +use embedded_hal as hal; +pub use nrf_hal_common::*; + +pub mod prelude { + pub use crate::hal::prelude::*; + pub use nrf_hal_common::prelude::*; + + pub use crate::time::U32Ext; +} + +pub use crate::ccm::Ccm; +pub use crate::clocks::Clocks; +pub use crate::delay::Delay; +pub use crate::ecb::Ecb; +pub use crate::saadc::Saadc; +pub use crate::spim::Spim; +pub use crate::temp::Temp; +pub use crate::timer::Timer; +pub use crate::uarte::Uarte; diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index bb4d52f1..57c90476 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -4,6 +4,7 @@ pub static HALS: &[(&str, &str)] = &[ ("nrf51-hal", "thumbv6m-none-eabi"), ("nrf9160-hal", "thumbv8m.main-none-eabihf"), ("nrf52810-hal", "thumbv7em-none-eabi"), + ("nrf52811-hal", "thumbv7em-none-eabi"), ("nrf52832-hal", "thumbv7em-none-eabihf"), ("nrf52833-hal", "thumbv7em-none-eabihf"), ("nrf52840-hal", "thumbv7em-none-eabihf"), @@ -11,17 +12,23 @@ pub static HALS: &[(&str, &str)] = &[ pub static EXAMPLES: &[(&str, &[&str])] = &[ ("blinky-button-demo", &[]), - ("ccm-demo", &["52810", "52832", "52833", "52840"]), + ("ccm-demo", &["52810", "52811", "52832", "52833", "52840"]), ("comp-demo", &[]), - ("ecb-demo", &["51", "52810", "52832", "52833", "52840"]), + ( + "ecb-demo", + &["51", "52810", "52811", "52832", "52833", "52840"], + ), ("gpiote-demo", &[]), ("i2s-controller-demo", &[]), ("i2s-peripheral-demo", &[]), ("lpcomp-demo", &[]), - ("ppi-demo", &["51", "52810", "52832", "52833", "52840"]), + ( + "ppi-demo", + &["51", "52810", "52811", "52832", "52833", "52840"], + ), ("pwm-demo", &[]), ("qdec-demo", &[]), - ("rtic-demo", &["51", "52810", "52832", "52840"]), + ("rtic-demo", &["51", "52810", "52811", "52832", "52840"]), ("spi-demo", &[]), ("spis-demo", &[]), ("twi-ssd1306", &["52832", "52840"]), @@ -34,6 +41,7 @@ pub fn feature_to_target(feat: &str) -> &str { match feat { "51" => "thumbv6m-none-eabi", "52810" => "thumbv7em-none-eabi", + "52811" => "thumbv7em-none-eabi", _ if feat.starts_with("52") => "thumbv7em-none-eabihf", _ => panic!("unknown Cargo feature `{}`", feat), } From 135f16e88d33e89341412bc6d73ca91e89cc740d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Mon, 21 Sep 2020 21:52:03 +0200 Subject: [PATCH 02/15] Use the version from crates.io --- nrf-hal-common/Cargo.toml | 1 - nrf52811-hal/Cargo.toml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/nrf-hal-common/Cargo.toml b/nrf-hal-common/Cargo.toml index dcfc2e8a..544a11b2 100644 --- a/nrf-hal-common/Cargo.toml +++ b/nrf-hal-common/Cargo.toml @@ -44,7 +44,6 @@ version = "0.9.0" [dependencies.nrf52811-pac] optional = true -git = "https://github.com/nrf-rs/nrf52811-pac" version = "0.9.0" [dependencies.nrf52832-pac] diff --git a/nrf52811-hal/Cargo.toml b/nrf52811-hal/Cargo.toml index 7a57dfce..011d85c6 100644 --- a/nrf52811-hal/Cargo.toml +++ b/nrf52811-hal/Cargo.toml @@ -16,7 +16,7 @@ keywords = ["arm", "cortex-m", "nrf52", "hal", "nrf52811"] license = "MIT OR Apache-2.0" [dependencies] -nrf52811-pac = { git = "https://github.com/nrf-rs/nrf52811-pac" } +nrf52811-pac = "0.9.0" [dependencies.nrf-hal-common] path = "../nrf-hal-common" From 895edb26678afd65b30458113a74cac5628ddfbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Mon, 21 Sep 2020 21:56:32 +0200 Subject: [PATCH 03/15] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e7d12a70..108ad95e 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Every nRF chip has its own crate, listed below: |-------|------|-----------| | [`nrf51-hal`](./nrf51-hal) | [![docs.rs](https://docs.rs/nrf51-hal/badge.svg)](https://docs.rs/nrf51-hal) | [![crates.io](https://img.shields.io/crates/d/nrf51-hal.svg)](https://crates.io/crates/nrf51-hal) | | [`nrf52810-hal`](./nrf52810-hal) | [![docs.rs](https://docs.rs/nrf52810-hal/badge.svg)](https://docs.rs/nrf52810-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52810-hal.svg)](https://crates.io/crates/nrf52810-hal) | -| [`nrf52810-hal`](./nrf52811-hal) | [![docs.rs](https://docs.rs/nrf52811-hal/badge.svg)](https://docs.rs/nrf52811-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52811-hal.svg)](https://crates.io/crates/nrf52811-hal) | +| [`nrf52811-hal`](./nrf52811-hal) | [![docs.rs](https://docs.rs/nrf52811-hal/badge.svg)](https://docs.rs/nrf52811-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52811-hal.svg)](https://crates.io/crates/nrf52811-hal) | | [`nrf52832-hal`](./nrf52832-hal) | [![docs.rs](https://docs.rs/nrf52832-hal/badge.svg)](https://docs.rs/nrf52832-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52832-hal.svg)](https://crates.io/crates/nrf52832-hal) | | [`nrf52833-hal`](./nrf52833-hal) | [![docs.rs](https://docs.rs/nrf52833-hal/badge.svg)](https://docs.rs/nrf52833-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52833-hal.svg)](https://crates.io/crates/nrf52833-hal) | | [`nrf52840-hal`](./nrf52840-hal) | [![docs.rs](https://docs.rs/nrf52840-hal/badge.svg)](https://docs.rs/nrf52840-hal) | [![crates.io](https://img.shields.io/crates/d/nrf52840-hal.svg)](https://crates.io/crates/nrf52840-hal) | From 635fb01707b22d83e242942dfaf83359693b5527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 23 Sep 2020 19:55:29 +0200 Subject: [PATCH 04/15] Fix forgotten analog input --- nrf-hal-common/src/comp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nrf-hal-common/src/comp.rs b/nrf-hal-common/src/comp.rs index f42d7451..9627d1e3 100644 --- a/nrf-hal-common/src/comp.rs +++ b/nrf-hal-common/src/comp.rs @@ -302,7 +302,7 @@ comp_input_pins! { P0_28> => PSEL_A::ANALOGINPUT4, P0_29> => PSEL_A::ANALOGINPUT5, P0_30> => PSEL_A::ANALOGINPUT6, - P0_31> => PSEL_A::ANALOGINPUT6, + P0_31> => PSEL_A::ANALOGINPUT7, } #[cfg(any(feature = "52811", feature = "52810"))] From 8e148214f770483830eb7dc2502960dd26ae632a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 23 Sep 2020 20:21:22 +0200 Subject: [PATCH 05/15] Port the newly added SPIS driver --- nrf-hal-common/src/spis.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/nrf-hal-common/src/spis.rs b/nrf-hal-common/src/spis.rs index b45e64bc..b6e998f2 100644 --- a/nrf-hal-common/src/spis.rs +++ b/nrf-hal-common/src/spis.rs @@ -15,7 +15,15 @@ use crate::pac::{ SPIS0_NS as SPIS0, }; -#[cfg(not(feature = "9160"))] +#[cfg(feature = "52811")] +use crate::pac::{ + spis1::{ + self as spis0, _EVENTS_ACQUIRED, _EVENTS_END, _EVENTS_ENDRX, _TASKS_ACQUIRE, _TASKS_RELEASE, + }, + SPIS0, SPIS1, +}; + +#[cfg(not(any(feature = "9160", feature = "52811")))] use crate::pac::{ spis0::{self, _EVENTS_ACQUIRED, _EVENTS_END, _EVENTS_ENDRX, _TASKS_ACQUIRE, _TASKS_RELEASE}, SPIS0, @@ -598,7 +606,7 @@ mod sealed { impl Sealed for super::SPIS0 {} #[cfg(not(any(feature = "9160", feature = "52810")))] impl Sealed for super::SPIS1 {} - #[cfg(not(any(feature = "9160", feature = "52810")))] + #[cfg(not(any(feature = "9160", feature = "52811", feature = "52810")))] impl Sealed for super::SPIS2 {} } @@ -607,20 +615,23 @@ pub trait Instance: sealed::Sealed + Deref { } impl Instance for SPIS0 { - #[cfg(not(any(feature = "9160", feature = "52810")))] + #[cfg(not(any(feature = "9160", feature = "52811", feature = "52810")))] const INTERRUPT: Interrupt = Interrupt::SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0; #[cfg(feature = "9160")] const INTERRUPT: Interrupt = Interrupt::UARTE0_SPIM0_SPIS0_TWIM0_TWIS0; - #[cfg(feature = "52810")] + #[cfg(any(feature = "52811", feature = "52810"))] const INTERRUPT: Interrupt = Interrupt::SPIM0_SPIS0_SPI0; } #[cfg(not(any(feature = "9160", feature = "52810")))] impl Instance for SPIS1 { + #[cfg(not(feature = "52811"))] const INTERRUPT: Interrupt = Interrupt::SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1; + #[cfg(feature = "52811")] + const INTERRUPT: Interrupt = Interrupt::TWIM0_TWIS0_TWI0_SPIM1_SPIS1_SPI1; } -#[cfg(not(any(feature = "9160", feature = "52810")))] +#[cfg(not(any(feature = "9160", feature = "52811", feature = "52810")))] impl Instance for SPIS2 { const INTERRUPT: Interrupt = Interrupt::SPIM2_SPIS2_SPI2; } From ad492197f470b91f7dca13d574be4a20f737b3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 23 Sep 2020 22:09:55 +0200 Subject: [PATCH 06/15] Put everything about the 811 in line with the other nrfs in terms of naming --- nrf-hal-common/src/ppi/event_nrf52811.rs | 18 +++++++++--------- nrf-hal-common/src/ppi/task_nrf52811.rs | 16 ++++++---------- nrf-hal-common/src/spim.rs | 8 ++++---- nrf-hal-common/src/spis.rs | 19 ++++++++----------- nrf52811-hal/Cargo.toml | 2 +- 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/nrf-hal-common/src/ppi/event_nrf52811.rs b/nrf-hal-common/src/ppi/event_nrf52811.rs index 6f68478f..f4bdcd86 100644 --- a/nrf-hal-common/src/ppi/event_nrf52811.rs +++ b/nrf-hal-common/src/ppi/event_nrf52811.rs @@ -22,7 +22,7 @@ impl Event for crate::pac::uarte0::EVENTS_RXTO {} impl Event for crate::pac::uarte0::EVENTS_RXSTARTED {} impl Event for crate::pac::uarte0::EVENTS_TXSTARTED {} impl Event for crate::pac::uarte0::EVENTS_TXSTOPPED {} -impl Event for crate::pac::spi1::EVENTS_READY {} +impl Event for crate::pac::spi0::EVENTS_READY {} impl Event for crate::pac::uart0::EVENTS_CTS {} impl Event for crate::pac::uart0::EVENTS_NCTS {} impl Event for crate::pac::uart0::EVENTS_RXDRDY {} @@ -33,9 +33,9 @@ impl Event for crate::pac::clock::EVENTS_HFCLKSTARTED {} impl Event for crate::pac::clock::EVENTS_LFCLKSTARTED {} impl Event for crate::pac::clock::EVENTS_DONE {} impl Event for crate::pac::clock::EVENTS_CTTO {} -impl Event for crate::pac::spis1::EVENTS_END {} -impl Event for crate::pac::spis1::EVENTS_ENDRX {} -impl Event for crate::pac::spis1::EVENTS_ACQUIRED {} +impl Event for crate::pac::spis0::EVENTS_END {} +impl Event for crate::pac::spis0::EVENTS_ENDRX {} +impl Event for crate::pac::spis0::EVENTS_ACQUIRED {} impl Event for crate::pac::ecb::EVENTS_ENDECB {} impl Event for crate::pac::ecb::EVENTS_ERRORECB {} impl Event for crate::pac::timer0::EVENTS_COMPARE {} @@ -108,11 +108,11 @@ impl Event for crate::pac::saadc::EVENTS_DONE {} impl Event for crate::pac::saadc::EVENTS_RESULTDONE {} impl Event for crate::pac::saadc::EVENTS_CALIBRATEDONE {} impl Event for crate::pac::saadc::EVENTS_STOPPED {} -impl Event for crate::pac::spim1::EVENTS_STOPPED {} -impl Event for crate::pac::spim1::EVENTS_ENDRX {} -impl Event for crate::pac::spim1::EVENTS_END {} -impl Event for crate::pac::spim1::EVENTS_ENDTX {} -impl Event for crate::pac::spim1::EVENTS_STARTED {} +impl Event for crate::pac::spim0::EVENTS_STOPPED {} +impl Event for crate::pac::spim0::EVENTS_ENDRX {} +impl Event for crate::pac::spim0::EVENTS_END {} +impl Event for crate::pac::spim0::EVENTS_ENDTX {} +impl Event for crate::pac::spim0::EVENTS_STARTED {} impl Event for crate::pac::power::EVENTS_POFWARN {} impl Event for crate::pac::power::EVENTS_SLEEPENTER {} impl Event for crate::pac::power::EVENTS_SLEEPEXIT {} diff --git a/nrf-hal-common/src/ppi/task_nrf52811.rs b/nrf-hal-common/src/ppi/task_nrf52811.rs index 967d2af3..7b810294 100644 --- a/nrf-hal-common/src/ppi/task_nrf52811.rs +++ b/nrf-hal-common/src/ppi/task_nrf52811.rs @@ -8,14 +8,10 @@ use crate::ppi::Task; // `^src/(.*)\.rs:pub type (.*) = .*$` // Replace (regex): // `impl Task for crate::pac::$1::$2 { }` -// Find (regex): -// `^impl Task for crate::pac::spim0::(.*)$` -// Replace (regex): -// `impl Task for crate::pac::spim1::$1` -impl Task for crate::pac::spim1::TASKS_START {} -impl Task for crate::pac::spim1::TASKS_STOP {} -impl Task for crate::pac::spim1::TASKS_SUSPEND {} -impl Task for crate::pac::spim1::TASKS_RESUME {} +impl Task for crate::pac::spim0::TASKS_START {} +impl Task for crate::pac::spim0::TASKS_STOP {} +impl Task for crate::pac::spim0::TASKS_SUSPEND {} +impl Task for crate::pac::spim0::TASKS_RESUME {} impl Task for crate::pac::rng::TASKS_START {} impl Task for crate::pac::rng::TASKS_STOP {} impl Task for crate::pac::timer0::TASKS_START {} @@ -24,8 +20,8 @@ impl Task for crate::pac::timer0::TASKS_COUNT {} impl Task for crate::pac::timer0::TASKS_CLEAR {} impl Task for crate::pac::timer0::TASKS_SHUTDOWN {} impl Task for crate::pac::timer0::TASKS_CAPTURE {} -impl Task for crate::pac::spis1::TASKS_ACQUIRE {} -impl Task for crate::pac::spis1::TASKS_RELEASE {} +impl Task for crate::pac::spis0::TASKS_ACQUIRE {} +impl Task for crate::pac::spis0::TASKS_RELEASE {} impl Task for crate::pac::uart0::TASKS_STARTRX {} impl Task for crate::pac::uart0::TASKS_STOPRX {} impl Task for crate::pac::uart0::TASKS_STARTTX {} diff --git a/nrf-hal-common/src/spim.rs b/nrf-hal-common/src/spim.rs index b5131ac3..a066ea8d 100644 --- a/nrf-hal-common/src/spim.rs +++ b/nrf-hal-common/src/spim.rs @@ -8,10 +8,7 @@ use core::sync::atomic::{compiler_fence, Ordering::SeqCst}; #[cfg(feature = "9160")] use crate::pac::{spim0_ns as spim0, SPIM0_NS as SPIM0}; -#[cfg(feature = "52811")] -use crate::pac::{spim1 as spim0, SPIM0, SPIM1}; - -#[cfg(not(any(feature = "9160", feature = "52811")))] +#[cfg(not(feature = "9160"))] use crate::pac::{spim0, SPIM0}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; @@ -19,6 +16,9 @@ pub use spim0::frequency::FREQUENCY_A as Frequency; use core::iter::repeat_with; +#[cfg(feature = "52811")] +use crate::pac::SPIM1; + #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] use crate::pac::{SPIM1, SPIM2}; diff --git a/nrf-hal-common/src/spis.rs b/nrf-hal-common/src/spis.rs index b6e998f2..ede4fe02 100644 --- a/nrf-hal-common/src/spis.rs +++ b/nrf-hal-common/src/spis.rs @@ -15,20 +15,15 @@ use crate::pac::{ SPIS0_NS as SPIS0, }; -#[cfg(feature = "52811")] -use crate::pac::{ - spis1::{ - self as spis0, _EVENTS_ACQUIRED, _EVENTS_END, _EVENTS_ENDRX, _TASKS_ACQUIRE, _TASKS_RELEASE, - }, - SPIS0, SPIS1, -}; - -#[cfg(not(any(feature = "9160", feature = "52811")))] +#[cfg(not(feature = "9160"))] use crate::pac::{ spis0::{self, _EVENTS_ACQUIRED, _EVENTS_END, _EVENTS_ENDRX, _TASKS_ACQUIRE, _TASKS_RELEASE}, SPIS0, }; +#[cfg(feature = "52811")] +use crate::pac::SPIS1; + #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] use crate::pac::{SPIS1, SPIS2}; @@ -619,8 +614,10 @@ impl Instance for SPIS0 { const INTERRUPT: Interrupt = Interrupt::SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0; #[cfg(feature = "9160")] const INTERRUPT: Interrupt = Interrupt::UARTE0_SPIM0_SPIS0_TWIM0_TWIS0; - #[cfg(any(feature = "52811", feature = "52810"))] + #[cfg(feature = "52810")] const INTERRUPT: Interrupt = Interrupt::SPIM0_SPIS0_SPI0; + #[cfg(feature = "52811")] + const INTERRUPT: Interrupt = Interrupt::TWIM0_TWIS0_TWI0_SPIM0_SPIS0_SPI0; } #[cfg(not(any(feature = "9160", feature = "52810")))] @@ -628,7 +625,7 @@ impl Instance for SPIS1 { #[cfg(not(feature = "52811"))] const INTERRUPT: Interrupt = Interrupt::SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1; #[cfg(feature = "52811")] - const INTERRUPT: Interrupt = Interrupt::TWIM0_TWIS0_TWI0_SPIM1_SPIS1_SPI1; + const INTERRUPT: Interrupt = Interrupt::SPIM1_SPIS1_SPI1; } #[cfg(not(any(feature = "9160", feature = "52811", feature = "52810")))] diff --git a/nrf52811-hal/Cargo.toml b/nrf52811-hal/Cargo.toml index 011d85c6..83f3e285 100644 --- a/nrf52811-hal/Cargo.toml +++ b/nrf52811-hal/Cargo.toml @@ -16,7 +16,7 @@ keywords = ["arm", "cortex-m", "nrf52", "hal", "nrf52811"] license = "MIT OR Apache-2.0" [dependencies] -nrf52811-pac = "0.9.0" +nrf52811-pac = "0.9.1" [dependencies.nrf-hal-common] path = "../nrf-hal-common" From 241f5fdb5a92d9640db5f95e58b825e687da9cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 27 Sep 2020 01:50:41 +0200 Subject: [PATCH 07/15] Add a mechanic to use the differential ADC --- nrf-hal-common/src/saadc.rs | 179 +++++++++++++++++++++++++++++------- 1 file changed, 147 insertions(+), 32 deletions(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index e650b180..b7f48cfc 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -36,12 +36,17 @@ use core::{ hint::unreachable_unchecked, sync::atomic::{compiler_fence, Ordering::SeqCst}, }; -use embedded_hal::adc::{Channel, OneShot}; +use embedded_hal::adc::OneShot; pub use saadc::{ - ch::config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time}, + ch::{ + config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time}, + pseln::PSELN_A as Pseln, + pselp::PSELP_A as Pselp, + }, oversample::OVERSAMPLE_A as Oversample, resolution::VAL_A as Resolution, + RegisterBlock, }; // Only 1 channel is allowed right now, a discussion needs to be had as to how @@ -52,7 +57,9 @@ pub use saadc::{ /// /// External analog channels supported by the SAADC implement the `Channel` trait. /// Currently, use of only one channel is allowed. -pub struct Saadc(SAADC); +pub struct Saadc { + saadc: SAADC, +} impl Saadc { pub fn new(saadc: SAADC, config: SaadcConfig) -> Self { @@ -61,10 +68,6 @@ impl Saadc { let SaadcConfig { resolution, oversample, - reference, - gain, - resistor, - time, } = config; saadc.enable.write(|w| w.enable().enabled()); @@ -74,7 +77,41 @@ impl Saadc { .write(|w| w.oversample().variant(oversample)); saadc.samplerate.write(|w| w.mode().task()); - saadc.ch[0].config.write(|w| { + // Calibrate + saadc.tasks_calibrateoffset.write(|w| unsafe { w.bits(1) }); + while saadc.events_calibratedone.read().bits() == 0 {} + + Saadc { saadc } + } + + pub fn channel(&mut self, n: usize) -> Channel { + Channel { + saadc: &mut self.saadc, + channel: n, + } + } +} + +pub struct Channel<'a> { + saadc: &'a mut SAADC, + channel: usize, +} + +impl<'a> Channel<'a> { + pub fn configure( + &mut self, + pseln: impl Into, + pselp: impl Into, + config: SaadcChannelConfig, + ) { + let SaadcChannelConfig { + reference, + gain, + resistor, + time, + } = config; + + self.saadc.ch[self.channel].config.write(|w| { w.refsel().variant(reference); w.gain().variant(gain); w.tacq().variant(time); @@ -84,13 +121,49 @@ impl Saadc { w.burst().enabled(); w }); - saadc.ch[0].pseln.write(|w| w.pseln().nc()); - // Calibrate - saadc.tasks_calibrateoffset.write(|w| unsafe { w.bits(1) }); - while saadc.events_calibratedone.read().bits() == 0 {} + self.saadc.ch[self.channel] + .pselp + .write(|w| w.pselp().variant(pselp.into())); + self.saadc.ch[self.channel] + .pseln + .write(|w| w.pseln().variant(pseln.into())); + } + + /// Sample channel `PIN` for the configured ADC acquisition time in differential input mode. + /// Note that this is a blocking operation. + pub fn read(&mut self) -> nb::Result { + let mut val: i16 = 0; + self.saadc + .result + .ptr + .write(|w| unsafe { w.ptr().bits(((&mut val) as *mut _) as u32) }); + self.saadc + .result + .maxcnt + .write(|w| unsafe { w.maxcnt().bits(1) }); + + // Conservative compiler fence to prevent starting the ADC before the + // pointer and maxcount have been set. + compiler_fence(SeqCst); + + self.saadc.tasks_start.write(|w| w.tasks_start().set_bit()); + self.saadc + .tasks_sample + .write(|w| w.tasks_sample().set_bit()); + + while self.saadc.events_end.read().bits() == 0 {} + self.saadc.events_end.reset(); + + // Will only occur if more than one channel has been enabled. + if self.saadc.result.amount.read().bits() != 1 { + return Err(nb::Error::Other(())); + } + + // Second fence to prevent optimizations creating issues with the EasyDMA-modified `val`. + compiler_fence(SeqCst); - Saadc(saadc) + Ok(val) } } @@ -102,6 +175,12 @@ pub struct SaadcConfig { pub resolution: Resolution, /// Average 2^`oversample` input samples before transferring the result into memory. pub oversample: Oversample, +} + +/// Used to configure a single SAADC peripheral channel. +/// +/// See the documentation of the `Default` impl for suitable default values. +pub struct SaadcChannelConfig { /// Reference voltage of the SAADC input. pub reference: Reference, /// Gain used to control the effective input range of the SAADC. @@ -150,6 +229,13 @@ impl Default for SaadcConfig { SaadcConfig { resolution: Resolution::_14BIT, oversample: Oversample::OVER8X, + } + } +} + +impl Default for SaadcChannelConfig { + fn default() -> Self { + SaadcChannelConfig { reference: Reference::VDD1_4, gain: Gain::GAIN1_4, resistor: Resistor::BYPASS, @@ -160,7 +246,7 @@ impl Default for SaadcConfig { impl OneShot for Saadc where - PIN: Channel, + PIN: embedded_hal::adc::Channel, { type Error = (); @@ -168,27 +254,27 @@ where /// Note that this is a blocking operation. fn read(&mut self, _pin: &mut PIN) -> nb::Result { match PIN::channel() { - 0 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input0()), - 1 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input1()), - 2 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input2()), - 3 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input3()), - 4 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input4()), - 5 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input5()), - 6 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input6()), - 7 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input7()), + 0 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input0()), + 1 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input1()), + 2 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input2()), + 3 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input3()), + 4 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input4()), + 5 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input5()), + 6 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input6()), + 7 => self.saadc.ch[0].pselp.write(|w| w.pselp().analog_input7()), #[cfg(not(feature = "9160"))] - 8 => self.0.ch[0].pselp.write(|w| w.pselp().vdd()), + 8 => self.saadc.ch[0].pselp.write(|w| w.pselp().vdd()), // This can never happen the only analog pins have already been defined // PAY CLOSE ATTENTION TO ANY CHANGES TO THIS IMPL OR THE `channel_mappings!` MACRO _ => unsafe { unreachable_unchecked() }, } let mut val: i16 = 0; - self.0 + self.saadc .result .ptr .write(|w| unsafe { w.ptr().bits(((&mut val) as *mut _) as u32) }); - self.0 + self.saadc .result .maxcnt .write(|w| unsafe { w.maxcnt().bits(1) }); @@ -197,14 +283,14 @@ where // pointer and maxcount have been set. compiler_fence(SeqCst); - self.0.tasks_start.write(|w| unsafe { w.bits(1) }); - self.0.tasks_sample.write(|w| unsafe { w.bits(1) }); + self.saadc.tasks_start.write(|w| unsafe { w.bits(1) }); + self.saadc.tasks_sample.write(|w| unsafe { w.bits(1) }); - while self.0.events_end.read().bits() == 0 {} - self.0.events_end.reset(); + while self.saadc.events_end.read().bits() == 0 {} + self.saadc.events_end.reset(); // Will only occur if more than one channel has been enabled. - if self.0.result.amount.read().bits() != 1 { + if self.saadc.result.amount.read().bits() != 1 { return Err(nb::Error::Other(())); } @@ -218,7 +304,7 @@ where macro_rules! channel_mappings { ( $($n:expr => $pin:ident,)*) => { $( - impl Channel for crate::gpio::p0::$pin { + impl embedded_hal::adc::Channel for crate::gpio::p0::$pin { type ID = u8; fn channel() -> >::ID { @@ -254,7 +340,7 @@ channel_mappings! { } #[cfg(not(feature = "9160"))] -impl Channel for InternalVdd { +impl embedded_hal::adc::Channel for InternalVdd { type ID = u8; fn channel() -> >::ID { @@ -265,3 +351,32 @@ impl Channel for InternalVdd { #[cfg(not(feature = "9160"))] /// Channel that doesn't sample a pin, but the internal VDD voltage. pub struct InternalVdd; + +macro_rules! psel_mappings { + ( $($psel:ident => $pin:ident,)*) => { + $( + impl Into for crate::gpio::p0::$pin { + fn into(self) -> Pseln { + Pseln::$psel + } + } + + impl Into for crate::gpio::p0::$pin { + fn into(self) -> Pselp { + Pselp::$psel + } + } + )* + }; +} + +psel_mappings! { + ANALOGINPUT0 => P0_02, + ANALOGINPUT1 => P0_03, + ANALOGINPUT2 => P0_04, + ANALOGINPUT3 => P0_05, + ANALOGINPUT4 => P0_28, + ANALOGINPUT5 => P0_29, + ANALOGINPUT6 => P0_30, + ANALOGINPUT7 => P0_31, +} From 15e477facb26985538e8b7bd0862233b8e884737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 27 Sep 2020 02:49:32 +0200 Subject: [PATCH 08/15] Add a method to transform ADC readings into millivolts --- nrf-hal-common/src/saadc.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index b7f48cfc..98de26b0 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -30,7 +30,7 @@ use crate::pac::{saadc_ns as saadc, SAADC_NS as SAADC}; #[cfg(not(feature = "9160"))] -use crate::pac::{saadc, SAADC}; +use crate::pac::{generic::Variant, saadc, SAADC}; use core::{ hint::unreachable_unchecked, @@ -165,6 +165,34 @@ impl<'a> Channel<'a> { Ok(val) } + + pub fn as_millis(&self, value: i16) -> i32 { + let lsbs = match self.saadc.resolution.read().val().variant() { + Variant::Val(Resolution::_8BIT) => 255i32, + Variant::Val(Resolution::_10BIT) => 1023i32, + Variant::Val(Resolution::_12BIT) => 4095i32, + Variant::Val(Resolution::_14BIT) => 16383i32, + _ => unreachable!(), + }; + + let gain = match self.saadc.ch[self.channel].config.read().gain().variant() { + Gain::GAIN1_6 => (1, 6), + Gain::GAIN1_5 => (1, 5), + Gain::GAIN1_4 => (1, 4), + Gain::GAIN1_3 => (1, 3), + Gain::GAIN1_2 => (1, 2), + Gain::GAIN1 => (1, 1), + Gain::GAIN2 => (2, 1), + Gain::GAIN4 => (4, 1), + }; + + let reference = match self.saadc.ch[self.channel].config.read().refsel().variant() { + Reference::INTERNAL => (3, 5), + Reference::VDD1_4 => (33, 40), + }; + + value as i32 * gain.0 * reference.1 / gain.1 / reference.0 / lsbs + } } /// Used to configure the SAADC peripheral. From 2efa9acef31a632fed37a7897aad8a290ef41e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 27 Sep 2020 02:58:41 +0200 Subject: [PATCH 09/15] Fix a mistake in the ADC value transform --- nrf-hal-common/src/saadc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index 98de26b0..6188189d 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -191,7 +191,7 @@ impl<'a> Channel<'a> { Reference::VDD1_4 => (33, 40), }; - value as i32 * gain.0 * reference.1 / gain.1 / reference.0 / lsbs + value as i32 * gain.0 * reference.1 * 1000 / gain.1 / reference.0 / lsbs } } From e3f20008f09dad040e46b641942d99c62154802f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 27 Sep 2020 13:00:42 +0200 Subject: [PATCH 10/15] Swap numbers in millis calculation --- nrf-hal-common/src/saadc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index 6188189d..13988ee2 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -191,7 +191,7 @@ impl<'a> Channel<'a> { Reference::VDD1_4 => (33, 40), }; - value as i32 * gain.0 * reference.1 * 1000 / gain.1 / reference.0 / lsbs + value as i32 * gain.1 * reference.0 * 1000 / gain.0 / reference.1 / lsbs } } From 9a510efc8b850cb999e3b0eba3253329e4f5efd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 27 Sep 2020 13:04:02 +0200 Subject: [PATCH 11/15] Make it possible to use the ADC in differential mode --- nrf-hal-common/src/saadc.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index 13988ee2..743152dd 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -40,7 +40,10 @@ use embedded_hal::adc::OneShot; pub use saadc::{ ch::{ - config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time}, + config::{ + GAIN_A as Gain, MODE_A as Mode, REFSEL_A as Reference, RESP_A as Resistor, + TACQ_A as Time, + }, pseln::PSELN_A as Pseln, pselp::PSELP_A as Pselp, }, @@ -107,6 +110,7 @@ impl<'a> Channel<'a> { let SaadcChannelConfig { reference, gain, + mode, resistor, time, } = config; @@ -115,7 +119,7 @@ impl<'a> Channel<'a> { w.refsel().variant(reference); w.gain().variant(gain); w.tacq().variant(time); - w.mode().se(); + w.mode().variant(mode); w.resp().variant(resistor); w.resn().bypass(); w.burst().enabled(); @@ -213,6 +217,8 @@ pub struct SaadcChannelConfig { pub reference: Reference, /// Gain used to control the effective input range of the SAADC. pub gain: Gain, + /// The mode of the differential ADC + pub mode: Mode, /// Positive channel resistor control. pub resistor: Resistor, /// Acquisition time in microseconds. @@ -266,6 +272,7 @@ impl Default for SaadcChannelConfig { SaadcChannelConfig { reference: Reference::VDD1_4, gain: Gain::GAIN1_4, + mode: Mode::SE, resistor: Resistor::BYPASS, time: Time::_20US, } From e856a4601d0fb28fe4539b20d7efb2b10f7d1cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 27 Sep 2020 13:14:27 +0200 Subject: [PATCH 12/15] Fix millivolts calculation for differential ADC mode --- nrf-hal-common/src/saadc.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index 743152dd..dcf0ef5d 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -171,13 +171,19 @@ impl<'a> Channel<'a> { } pub fn as_millis(&self, value: i16) -> i32 { + let mode = match self.saadc.ch[self.channel].config.read().mode().variant() { + Mode::SE => 1, + Mode::DIFF => 2, + }; + let lsbs = match self.saadc.resolution.read().val().variant() { - Variant::Val(Resolution::_8BIT) => 255i32, - Variant::Val(Resolution::_10BIT) => 1023i32, - Variant::Val(Resolution::_12BIT) => 4095i32, - Variant::Val(Resolution::_14BIT) => 16383i32, + Variant::Val(Resolution::_8BIT) => 256, + Variant::Val(Resolution::_10BIT) => 1024, + Variant::Val(Resolution::_12BIT) => 4096, + Variant::Val(Resolution::_14BIT) => 16384, _ => unreachable!(), - }; + } / mode + - 1; let gain = match self.saadc.ch[self.channel].config.read().gain().variant() { Gain::GAIN1_6 => (1, 6), From 73fc3a0d6d6bcf07d18c0c8beefba59a9c8f06d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 27 Sep 2020 13:23:58 +0200 Subject: [PATCH 13/15] Fix accidential off by one --- nrf-hal-common/src/saadc.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index dcf0ef5d..51128637 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -182,8 +182,7 @@ impl<'a> Channel<'a> { Variant::Val(Resolution::_12BIT) => 4096, Variant::Val(Resolution::_14BIT) => 16384, _ => unreachable!(), - } / mode - - 1; + } / mode; let gain = match self.saadc.ch[self.channel].config.read().gain().variant() { Gain::GAIN1_6 => (1, 6), From 89000fd49a69c26c8406d3611c8be85d09f0f228 Mon Sep 17 00:00:00 2001 From: Lukas Schrittwieser Date: Mon, 28 Sep 2020 12:22:11 +0200 Subject: [PATCH 14/15] Add an as_micros function --- nrf-hal-common/src/saadc.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index 51128637..1c58c03d 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -170,7 +170,7 @@ impl<'a> Channel<'a> { Ok(val) } - pub fn as_millis(&self, value: i16) -> i32 { + pub fn as_micros(&self, value: i16) -> i32 { let mode = match self.saadc.ch[self.channel].config.read().mode().variant() { Mode::SE => 1, Mode::DIFF => 2, @@ -200,7 +200,11 @@ impl<'a> Channel<'a> { Reference::VDD1_4 => (33, 40), }; - value as i32 * gain.1 * reference.0 * 1000 / gain.0 / reference.1 / lsbs + value as i32 * 1000 * 1000 / lsbs * gain.1 * reference.0 / gain.0 / reference.1 + } + + pub fn as_millis(&self, value: i16) -> i32 { + self.as_micros(value) / 1000 } } From 6e5645342cf48b49aa4a76ac92e24b1d02a6e0ae Mon Sep 17 00:00:00 2001 From: Lukas Schrittwieser Date: Sat, 17 Oct 2020 19:35:01 +0200 Subject: [PATCH 15/15] Fix overflow in as_micros for some configurations --- nrf-hal-common/src/saadc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nrf-hal-common/src/saadc.rs b/nrf-hal-common/src/saadc.rs index 1c58c03d..33f73e79 100644 --- a/nrf-hal-common/src/saadc.rs +++ b/nrf-hal-common/src/saadc.rs @@ -200,7 +200,7 @@ impl<'a> Channel<'a> { Reference::VDD1_4 => (33, 40), }; - value as i32 * 1000 * 1000 / lsbs * gain.1 * reference.0 / gain.0 / reference.1 + (value as i64 * 1000 * 1000 / lsbs * gain.1 * reference.0 / gain.0 / reference.1) as i32 } pub fn as_millis(&self, value: i16) -> i32 {