Skip to content

Commit

Permalink
Fix ccm support for nRF51
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfragoso committed Jun 5, 2020
1 parent c5c3c74 commit 7f15e70
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
2 changes: 0 additions & 2 deletions examples/ccm-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ nrf52810-hal = { path = "../../nrf52810-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 }
nrf51-hal = { path = "../../nrf51-hal", features = ["rt"], optional = true}

[[bin]]
name = "ccm-demo"
doc = false
test = false

[features]
51 = ["nrf51-hal"]
52810 = ["nrf52810-hal"]
52832 = ["nrf52832-hal"]
52840 = ["nrf52840-hal"]
Expand Down
2 changes: 0 additions & 2 deletions examples/ccm-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#![no_main]

// Import the right HAL/PAC crate, depending on the target chip
#[cfg(feature = "51")]
pub use nrf51_hal as hal;
#[cfg(feature = "52810")]
pub use nrf52810_hal as hal;
#[cfg(feature = "52832")]
Expand Down
65 changes: 52 additions & 13 deletions nrf-hal-common/src/ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
//! Clear packet:
//!
//! ```notrust
//! +----------+---------------+----------+-----------------+
//! | S0 | Packet length | S1 | Payload |
//! | (1 byte) | (1 byte) | (1 byte) | (0 - 251 bytes) |
//! +----------+---------------+----------+-----------------+
//! +----------+---------------+----------+------------------+
//! | S0 | Packet length | S1 | Payload |
//! | (1 byte) | (1 byte) | (1 byte) | (0 - 251* bytes) |
//! +----------+---------------+----------+------------------+
//! ```
//!
//! The contents of `S0` and `S1` are not relevant, but the fields must be present in the slice.
Expand All @@ -26,17 +26,19 @@
//! Cipher packet:
//!
//! ```notrust
//! +----------+---------------+----------+-----------------+-----------+
//! | S0 | Packet length | S1 | Payload | MIC |
//! | (1 byte) | (1 byte) | (1 byte) | (0 - 251 bytes) | (4 bytes) |
//! +----------+---------------+----------+-----------------+-----------+
//! +----------+---------------+----------+-----------------+-------------+
//! | S0 | Packet length | S1 | Payload | MIC |
//! | (1 byte) | (1 byte) | (1 byte) | (0 - 251* bytes) | (4 bytes) |
//! +----------+---------------+----------+-----------------+-------------+
//! ```
//! The contents of `S0` and `S1` are not relevant, but the fields must be present in the slice. The
//! `Packet length` is the sum of the lengths of the `Payload` and `MIC`.
//! The decryption operation will also check the MIC field and return an error when it is invalid
//! and it will decrement the `Length` field by four. During decryption, the `clear text` slice does
//! not need to have space for the MIC field.
//!
//! * nRF51 devices only support payloads of up to 27 bytes.
//!
//! # Scratch Area
//!
//! The peripheral also needs an area in RAM to store temporary values used during
Expand All @@ -45,13 +47,13 @@
use crate::{
slice_in_ram,
target::{
ccm::mode::{DATARATE_A, LENGTH_A},
AAR, CCM,
},
target::{AAR, CCM},
};
use core::sync::atomic::{compiler_fence, Ordering};

#[cfg(not(feature = "51"))]
use crate::target::ccm::mode::{DATARATE_A, LENGTH_A};

const MINIMUM_SCRATCH_AREA_SIZE: usize = 43;
const HEADER_SIZE: usize = 3;
const LENGTH_HEADER_INDEX: usize = 1;
Expand All @@ -62,12 +64,21 @@ const MAXIMUM_LENGTH_5BITS: usize = 31;
const MAXIMUM_COUNTER: u64 = 0x7F_FFFF_FFFF;

/// Data rate that CCM peripheral shall run in sync with.
#[cfg(not(feature = "51"))]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum DataRate {
_1Mbit,
_2Mbit,
}

/// Data rate that CCM peripheral shall run in sync with.
#[cfg(feature = "51")]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum DataRate {
_1Mbit,
}

#[cfg(not(feature = "51"))]
impl From<DataRate> for DATARATE_A {
fn from(data_rate: DataRate) -> Self {
if data_rate == DataRate::_1Mbit {
Expand Down Expand Up @@ -177,8 +188,12 @@ impl Ccm {
regs.tasks_stop.write(|w| unsafe { w.bits(1) });

// This register is shared with AAR, reset it and write the chosen data rate
#[cfg(not(feature = "51"))]
regs.mode.write(|w| w.datarate().variant(data_rate.into()));

#[cfg(feature = "51")]
let _ = data_rate;

regs.enable.write(|w| w.enable().enabled());

Self { regs, _aar: arr }
Expand Down Expand Up @@ -224,7 +239,15 @@ impl Ccm {
return Err(CcmError::InsufficientScratchArea);
}

let length_variant = if payload_len <= MAXIMUM_LENGTH_5BITS {
#[cfg(feature = "51")]
{
if payload_len > MAXIMUM_LENGTH_5BITS - MIC_SIZE {
return Err(CcmError::WrongPacketLength);
}
}

#[cfg(not(feature = "51"))]
let length_variant = if payload_len <= MAXIMUM_LENGTH_5BITS - MIC_SIZE {
LENGTH_A::DEFAULT
} else {
#[cfg(any(feature = "52840", feature = "52833", feature = "52810"))]
Expand All @@ -236,6 +259,10 @@ impl Ccm {
LENGTH_A::EXTENDED
};

#[cfg(feature = "51")]
self.regs.mode.write(|w| w.mode().encryption());

#[cfg(not(feature = "51"))]
self.regs
.mode
.modify(|_, w| w.mode().encryption().length().variant(length_variant));
Expand Down Expand Up @@ -336,6 +363,14 @@ impl Ccm {
return Err(CcmError::InsufficientScratchArea);
}

#[cfg(feature = "51")]
{
if payload_len > MAXIMUM_LENGTH_5BITS {
return Err(CcmError::WrongPacketLength);
}
}

#[cfg(not(feature = "51"))]
let length_variant = if payload_len <= MAXIMUM_LENGTH_5BITS {
LENGTH_A::DEFAULT
} else {
Expand All @@ -348,6 +383,10 @@ impl Ccm {
LENGTH_A::EXTENDED
};

#[cfg(feature = "51")]
self.regs.mode.write(|w| w.mode().decryption());

#[cfg(not(feature = "51"))]
self.regs
.mode
.modify(|_, w| w.mode().decryption().length().variant(length_variant));
Expand Down
3 changes: 1 addition & 2 deletions nrf-hal-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub mod prelude {
}

/// Length of Nordic EasyDMA differs for MCUs
#[cfg(any(feature = "52810", feature = "52832"))]
#[cfg(any(feature = "52810", feature = "52832", feature = "51"))]
pub mod target_constants {
// NRF52832 8 bits1..0xFF
pub const EASY_DMA_SIZE: usize = 255;
Expand All @@ -82,7 +82,6 @@ pub mod target_constants {
}

/// Does this slice reside entirely within RAM?
#[cfg(not(feature = "51"))]
pub(crate) fn slice_in_ram(slice: &[u8]) -> bool {
let ptr = slice.as_ptr() as usize;
ptr >= target_constants::SRAM_LOWER && (ptr + slice.len()) < target_constants::SRAM_UPPER
Expand Down

0 comments on commit 7f15e70

Please sign in to comment.