Skip to content

Commit

Permalink
More warning fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Dec 20, 2024
1 parent d7ce73a commit 1609ad0
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 48 deletions.
12 changes: 6 additions & 6 deletions src/drivers/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl<'a, Size: KeySize> Aes<'a, Size> {

fn one_block(&self, block: &mut Block<Self>) {
// needs to be word-aligned
let aligned_block: Aligned<A4, Block<Self>> = Aligned(block.clone());
let aligned_block: Aligned<A4, Block<Self>> = Aligned(*block);
let addr: u32 = &aligned_block as *const _ as _;

self.memaddr.write(|w| unsafe { w.bits(addr) });
Expand All @@ -182,12 +182,12 @@ impl<'a, Size: KeySize> Aes<'a, Size> {

// the `block-cipher` traits

impl<'a, Size: KeySize> BlockCipher for Aes<'a, Size> {
impl<Size: KeySize> BlockCipher for Aes<'_, Size> {
type BlockSize = U16;
type ParBlocks = U1;
}

impl<'a, Size: KeySize> BlockEncrypt for Aes<'a, Size> {
impl<Size: KeySize> BlockEncrypt for Aes<'_, Size> {
fn encrypt_block(&self, block: &mut Block<Self>) {
// unfortunate implementation detail
if self.cryptcfg.read().aesdecrypt().is_decrypt() {
Expand All @@ -197,7 +197,7 @@ impl<'a, Size: KeySize> BlockEncrypt for Aes<'a, Size> {
}
}

impl<'a, Size: KeySize> BlockDecrypt for Aes<'a, Size> {
impl<Size: KeySize> BlockDecrypt for Aes<'_, Size> {
fn decrypt_block(&self, block: &mut Block<Self>) {
// unfortunate implementation detail
if self.cryptcfg.read().aesdecrypt().is_encrypt() {
Expand All @@ -210,12 +210,12 @@ impl<'a, Size: KeySize> BlockDecrypt for Aes<'a, Size> {
impl<Size: KeySize> core::ops::Deref for Aes<'_, Size> {
type Target = Hashcrypt<Enabled>;
fn deref(&self) -> &Self::Target {
&self.inner
self.inner
}
}

impl<Size: KeySize> core::ops::DerefMut for Aes<'_, Size> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
self.inner
}
}
20 changes: 13 additions & 7 deletions src/drivers/clocks.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
///!* API to configure the clocks.
///!
///! This is very incomplete (e.g., no support for PLL clocks).
///! It is also likely buggy, and more complex than needed
///!
///! It is currently used to prepare for using the USBFSD and
///! Flexcomm peripherals.
//!* API to configure the clocks.
//!
//! This is very incomplete (e.g., no support for PLL clocks).
//! It is also likely buggy, and more complex than needed
//!
//! It is currently used to prepare for using the USBFSD and
//! Flexcomm peripherals.
use core::{cmp::min, convert::TryFrom};
use embedded_time::rate::Extensions;

Expand Down Expand Up @@ -114,6 +114,9 @@ pub struct Pll {

impl Pll {
// allow user to override if they know better...
/// # Safety
///
/// Input values must be valid for PLL
pub unsafe fn new(n: u8, m: u16, p: u8) -> Pll {
// UM 4.6.6.3.2
let selp = min((m >> 2) + 1, 31) as u8;
Expand Down Expand Up @@ -441,6 +444,9 @@ impl ClockRequirements {
}

/// Same as above, but allows clock to be changed after an initial configuration.
///
/// # Safety
///
/// This is unsafe because it's up to the developer to ensure the new configuration is okay for
/// the device peripherals being used.
pub unsafe fn reconfigure(self, _clocks: Clocks, pmc: &mut Pmc, syscon: &mut Syscon) -> Clocks {
Expand Down
4 changes: 1 addition & 3 deletions src/drivers/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod prelude {

/// I2C error
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Bus error (catch-all)
Bus,
Expand All @@ -30,9 +31,6 @@ pub enum Error {
NackData,
/// Start/Stop error
StartStop,

#[doc(hidden)]
_Extensible,
}

pub type Result<T> = core::result::Result<T, Error>;
Expand Down
6 changes: 6 additions & 0 deletions src/drivers/pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ macro_rules! pins {
Self::set_all_released();
}

/// # Safety
///
/// Steals the PIN, must not be called if the PIN is already owned
pub unsafe fn steal() -> Self {
Self {
$(
Expand Down Expand Up @@ -185,6 +188,9 @@ macro_rules! pins {
unsafe { PIN_TAKEN[$port][$number] = false; }
}

/// # Safety
///
/// Steals the PIN, must not be called if the PIN is already owned
pub unsafe fn steal() -> Pin<Self, $default_state_ty> {
PIN_TAKEN[$port][$number] = true;
Pin {
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where

fn enable(&mut self, channel: Self::Channel) {
match channel {
0 | 1 | 2 => {}
0..=2 => {}
_ => {
panic!("Cannot use channel outside 0-2 for PWM.");
}
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl rand_core::RngCore for Rng<init_state::Enabled> {
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
Ok(self.fill_bytes(dest))
self.fill_bytes(dest);
Ok(())
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/drivers/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod config;

/// Serial error
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Framing error
Framing,
Expand All @@ -28,8 +29,6 @@ pub enum Error {
Overrun,
/// Parity check error
Parity,
#[doc(hidden)]
_Extensible,
}

// /// Interrupt event
Expand Down Expand Up @@ -101,7 +100,7 @@ where
pub fn new(usart: USART, pins: PINS, config: config::Config) -> Self {
use self::config::*;

let speed: Hertz = config.speed.into();
let speed: Hertz = config.speed;
let speed: u32 = speed.0;

usart
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<Size: OutputSize> Sha<'_, Size> {
// relevant code is ~line 800 in fsl_hashcrypt.c
fn process_block(peripheral: &mut Hashcrypt<Enabled>, input: &GenericArray<u8, BlockSize>) {
// input must be word-aligned
let input: Aligned<A4, GenericArray<u8, BlockSize>> = Aligned(input.clone());
let input: Aligned<A4, GenericArray<u8, BlockSize>> = Aligned(*input);
let addr: u32 = &input[0] as *const _ as _;
assert_eq!(addr & 0x3, 0);
while peripheral.raw.status.read().waiting().is_not_waiting() {
Expand Down
18 changes: 8 additions & 10 deletions src/drivers/spi.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
///! There are 8 "normal" SPIs and on high-speed SPI.
///! The high-speed SPI is tied to Flexcomm8, which it does not
///! share with any other peripherals.
///!
///! SPI3, SPI4, and this high-speed SPI8 have 4 possible chip selects,
///! whereas the others have two.
///
///
//! There are 8 "normal" SPIs and on high-speed SPI.
//! The high-speed SPI is tied to Flexcomm8, which it does not
//! share with any other peripherals.
//!
//! SPI3, SPI4, and this high-speed SPI8 have 4 possible chip selects,
//! whereas the others have two.
use core::marker::PhantomData;

use crate::time::Hertz;
Expand All @@ -29,15 +28,14 @@ pub mod prelude {
/// SPI error
/// TODO: Use the actual ones from the chip
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Overrun occurred
Overrun,
/// Mode fault occurred
ModeFault,
/// CRC error
Crc,
#[doc(hidden)]
_Extensible,
}

pub type Result<T> = nb::Result<T, Error>;
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ where

/// For debugging
pub(crate) fn get_results<'a>(&self) -> &'a mut [u32] {
return unsafe { &mut RESULTS };
unsafe { &mut RESULTS }
}

/// Used after an edge is detected to prevent the same
Expand Down
7 changes: 6 additions & 1 deletion src/drivers/usbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ where
*endpoint = mem::MaybeUninit::new(Endpoint::<USB>::new(i as u8));
}

unsafe { mem::transmute::<_, [Endpoint<USB>; NUM_ENDPOINTS]>(endpoints) }
unsafe {
mem::transmute::<
[mem::MaybeUninit<Endpoint<USB>>; NUM_ENDPOINTS],
[Endpoint<USB>; NUM_ENDPOINTS],
>(endpoints)
}
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/drivers/usbd/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ where
let nbytes = epl.eps[i].ep_out[0].read().nbytes::<USB>().bits() as usize;

// let count = min((out_buf.capacity() - nbytes) as usize, buf.len());
let count = (out_buf.capacity() - nbytes) as usize;
let count = out_buf.capacity() - nbytes;

out_buf.read(&mut buf[..count]);

Expand Down Expand Up @@ -347,7 +347,7 @@ where
} else {
let out_buf = self.out_buf.as_ref().unwrap().borrow(cs);
let nbytes = epl.eps[0].ep_out[0].read().nbytes::<USB>().bits() as usize;
let count = min((out_buf.capacity() - nbytes) as usize, buf.len());
let count = min(out_buf.capacity() - nbytes, buf.len());

out_buf.read(&mut buf[..count]);

Expand Down
11 changes: 9 additions & 2 deletions src/drivers/usbd/endpoint_registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ pub fn attach() -> Option<Instance> {
}

/// Does not zero the memory
///
/// # Safety
///
/// Steals the registers instance, must not be called if
/// the registers are owned somewhere
pub unsafe fn steal() -> Instance {
ENDPOINT_REGISTERS_ATTACHED = true;
Instance {
Expand Down Expand Up @@ -595,15 +600,15 @@ pub mod epr {
pub fn addroff<USB: Usb<init_state::Enabled>>(&self) -> ADDROFFR {
let field = AddrOffField::from(USB::SPEED);
ADDROFFR {
bits: ((self.bits >> field.offset) & field.mask as u32) as u16,
bits: ((self.bits >> field.offset) & field.mask) as u16,
}
}
#[doc = "Bits 16:25 - Endpoint buffer NBytes while in full speed operation, or bits 11:25 for high speed operation."]
#[inline]
pub fn nbytes<USB: Usb<init_state::Enabled>>(&self) -> NBYTESR {
let field = NbytesField::from(USB::SPEED);
NBYTESR {
bits: ((self.bits >> field.offset) & field.mask as u32) as u16,
bits: ((self.bits >> field.offset) & field.mask) as u16,
}
}
#[doc = "Bit 26 - Endpoint type"]
Expand Down Expand Up @@ -671,6 +676,8 @@ pub mod epr {
W { bits: 1 << 30 }
}
#[doc = r"Writes raw bits to the register"]
#[doc = r"# Safety"]
#[doc = r"Bit value must be valid"]
#[inline]
pub unsafe fn bits(&mut self, bits: u32) -> &mut Self {
self.bits = bits;
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ impl Peripherals {
// }

#[cfg(not(feature = "rtic-peripherals"))]
/// # Safety
///
/// Steals peripherals, must not be used if one of the peripherals
/// is already owned
pub unsafe fn steal() -> Self {
Self::from((raw::Peripherals::steal(), raw::CorePeripherals::steal()))
}
Expand Down
22 changes: 16 additions & 6 deletions src/peripherals/pfr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ pub struct Pfr<State = init_state::Unknown> {
}
impl<State> Pfr<State> {
fn bootloader_api_tree() -> &'static mut BootloaderTree {
unsafe { core::mem::transmute(0x130010f0u32 as *const ()) }
#[allow(clippy::transmute_ptr_to_ref)]
unsafe {
core::mem::transmute(0x130010f0u32 as *const ())
}
}
fn check_error(err: u32) -> Result<(), u32> {
if err == 0 {
Expand All @@ -252,6 +255,12 @@ impl<State> Pfr<State> {
}
}
}

impl Default for Pfr {
fn default() -> Self {
Self::new()
}
}
impl Pfr {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -290,7 +299,7 @@ impl Pfr<init_state::Enabled> {
// heprintln!("cfpa:").ok();
// dump_hex!(cfpa_bytes, 512);

let cmpa: &Cmpa = unsafe { core::mem::transmute(cmpa_bytes.as_ptr()) };
let cmpa: &Cmpa = unsafe { &*(cmpa_bytes.as_ptr() as *const _) };

Ok(*cmpa)
}
Expand All @@ -308,6 +317,7 @@ impl Pfr<init_state::Enabled> {
/// - Immediately after CFPA is updated, this method returns the latest CFPA data.
/// - After boot/reset, this method will potentially return expected old versions of CFPA.
/// - There is a pattern of how to increment VERSION to result in this method returning old CFPA versions or not which is impractical.
///
/// It's almost like there is some other cfpa page storage not documented and this bootrom method mismanages the VERSION.
pub fn read_cfpa_with_bootrom(&mut self) -> Result<Cfpa, u32> {
let mut cfpa_bytes = [0u8; 512];
Expand All @@ -322,7 +332,7 @@ impl Pfr<init_state::Enabled> {
// heprintln!("cfpa:").ok();
// dump_hex!(cfpa_bytes, 512);

let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };
let cfpa: &Cfpa = unsafe { &*(cfpa_bytes.as_ptr() as *const _) };

Ok(*cfpa)
}
Expand Down Expand Up @@ -350,7 +360,7 @@ impl Pfr<init_state::Enabled> {
copy_nonoverlapping(cfpa_ptr, cfpa_bytes.as_mut_ptr(), 128);
}

let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };
let cfpa: &Cfpa = unsafe { &*(cfpa_bytes.as_ptr() as *const _) };

Ok(*cfpa)
}
Expand All @@ -363,7 +373,7 @@ impl Pfr<init_state::Enabled> {
copy_nonoverlapping(CFPA_PTR, cfpa_bytes.as_mut_ptr(), 128);
}

let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };
let cfpa: &Cfpa = unsafe { &*(cfpa_bytes.as_ptr() as *const _) };

Ok(*cfpa)
}
Expand All @@ -376,7 +386,7 @@ impl Pfr<init_state::Enabled> {
copy_nonoverlapping(CFPA_PTR, cfpa_bytes.as_mut_ptr(), 128);
}

let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };
let cfpa: &Cfpa = unsafe { &*(cfpa_bytes.as_ptr() as *const _) };

Ok(*cfpa)
}
Expand Down
2 changes: 1 addition & 1 deletion src/peripherals/pmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Pmc {

/// Check if peripheral is powered
pub fn is_powered<P: PowerControl>(&self, peripheral: &P) -> bool {
peripheral.is_powered(&self)
peripheral.is_powered(self)
}
}

Expand Down
Loading

0 comments on commit 1609ad0

Please sign in to comment.