Skip to content

Commit 5e9733d

Browse files
committed
Fix compile warnings.
1 parent 402ad19 commit 5e9733d

File tree

3 files changed

+58
-51
lines changed

3 files changed

+58
-51
lines changed

examples/nvmc-demo/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#[cfg(feature = "52840")]
77
use nrf52840_hal as hal;
88

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

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

3838
erase_if_needed(&mut nvmc, LAST_PAGE);
3939

nrf-hal-common/src/pwm.rs

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use crate::{
1414
time::*,
1515
};
1616
use core::{
17-
cell::Cell,
1817
convert::Infallible,
1918
ops::Deref,
19+
ptr::addr_of_mut,
2020
sync::atomic::{compiler_fence, Ordering},
2121
};
2222
use embedded_dma::*;
@@ -299,7 +299,7 @@ where
299299
// Internal helper function that returns 15 bit duty cycle value.
300300
#[inline(always)]
301301
fn duty_on_value(&self, index: usize) -> u16 {
302-
let val = T::buffer().get()[index];
302+
let val = unsafe { (*T::buffer())[index] };
303303
let is_inverted = (val >> 15) & 1 == 0;
304304
match is_inverted {
305305
false => val,
@@ -310,7 +310,7 @@ where
310310
// Internal helper function that returns 15 bit inverted duty cycle value.
311311
#[inline(always)]
312312
fn duty_off_value(&self, index: usize) -> u16 {
313-
let val = T::buffer().get()[index];
313+
let val = unsafe { (*T::buffer())[index] };
314314
let is_inverted = (val >> 15) & 1 == 0;
315315
match is_inverted {
316316
false => self.max_duty() - val,
@@ -321,31 +321,33 @@ where
321321
/// Sets duty cycle (15 bit) for all PWM channels.
322322
/// Will replace any ongoing sequence playback.
323323
pub fn set_duty_on_common(&self, duty: u16) {
324-
let mut buffer = T::buffer().get();
325-
buffer.copy_from_slice(&[duty.min(self.max_duty()) & 0x7FFF; 4][..]);
326-
T::buffer().set(buffer);
324+
let buffer = T::buffer();
325+
unsafe {
326+
*buffer = [duty.min(self.max_duty()) & 0x7FFF; 4];
327+
}
327328
self.one_shot();
328329
self.set_load_mode(LoadMode::Common);
329330
self.pwm
330331
.seq0
331332
.ptr
332-
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
333+
.write(|w| unsafe { w.bits(buffer as u32) });
333334
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(1) });
334335
self.start_seq(Seq::Seq0);
335336
}
336337

337338
/// Sets inverted duty cycle (15 bit) for all PWM channels.
338339
/// Will replace any ongoing sequence playback.
339340
pub fn set_duty_off_common(&self, duty: u16) {
340-
let mut buffer = T::buffer().get();
341-
buffer.copy_from_slice(&[duty.min(self.max_duty()) | 0x8000; 4][..]);
342-
T::buffer().set(buffer);
341+
let buffer = T::buffer();
342+
unsafe {
343+
*buffer = [duty.min(self.max_duty()) | 0x8000; 4];
344+
}
343345
self.one_shot();
344346
self.set_load_mode(LoadMode::Common);
345347
self.pwm
346348
.seq0
347349
.ptr
348-
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
350+
.write(|w| unsafe { w.bits(buffer as u32) });
349351
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(1) });
350352
self.start_seq(Seq::Seq0);
351353
}
@@ -365,31 +367,33 @@ where
365367
/// Sets duty cycle (15 bit) for a PWM group.
366368
/// Will replace any ongoing sequence playback.
367369
pub fn set_duty_on_group(&self, group: Group, duty: u16) {
368-
let mut buffer = T::buffer().get();
369-
buffer[usize::from(group)] = duty.min(self.max_duty()) & 0x7FFF;
370-
T::buffer().set(buffer);
370+
let buffer = T::buffer();
371+
unsafe {
372+
(*buffer)[usize::from(group)] = duty.min(self.max_duty()) & 0x7FFF;
373+
}
371374
self.one_shot();
372375
self.set_load_mode(LoadMode::Grouped);
373376
self.pwm
374377
.seq0
375378
.ptr
376-
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
379+
.write(|w| unsafe { w.bits(buffer as u32) });
377380
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(2) });
378381
self.start_seq(Seq::Seq0);
379382
}
380383

381384
/// Sets inverted duty cycle (15 bit) for a PWM group.
382385
/// Will replace any ongoing sequence playback.
383386
pub fn set_duty_off_group(&self, group: Group, duty: u16) {
384-
let mut buffer = T::buffer().get();
385-
buffer[usize::from(group)] = duty.min(self.max_duty()) | 0x8000;
386-
T::buffer().set(buffer);
387+
let buffer = T::buffer();
388+
unsafe {
389+
(*buffer)[usize::from(group)] = duty.min(self.max_duty()) | 0x8000;
390+
}
387391
self.one_shot();
388392
self.set_load_mode(LoadMode::Grouped);
389393
self.pwm
390394
.seq0
391395
.ptr
392-
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
396+
.write(|w| unsafe { w.bits(buffer as u32) });
393397
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(2) });
394398
self.start_seq(Seq::Seq0);
395399
}
@@ -409,31 +413,33 @@ where
409413
/// Sets duty cycle (15 bit) for a PWM channel.
410414
/// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
411415
pub fn set_duty_on(&self, channel: Channel, duty: u16) {
412-
let mut buffer = T::buffer().get();
413-
buffer[usize::from(channel)] = duty.min(self.max_duty()) & 0x7FFF;
414-
T::buffer().set(buffer);
416+
let buffer = T::buffer();
417+
unsafe {
418+
(*buffer)[usize::from(channel)] = duty.min(self.max_duty()) & 0x7FFF;
419+
}
415420
self.one_shot();
416421
self.set_load_mode(LoadMode::Individual);
417422
self.pwm
418423
.seq0
419424
.ptr
420-
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
425+
.write(|w| unsafe { w.bits(buffer as u32) });
421426
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(4) });
422427
self.start_seq(Seq::Seq0);
423428
}
424429

425430
/// Sets inverted duty cycle (15 bit) for a PWM channel.
426431
/// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
427432
pub fn set_duty_off(&self, channel: Channel, duty: u16) {
428-
let mut buffer = T::buffer().get();
429-
buffer[usize::from(channel)] = duty.min(self.max_duty()) | 0x8000;
430-
T::buffer().set(buffer);
433+
let buffer = T::buffer();
434+
unsafe {
435+
(*buffer)[usize::from(channel)] = duty.min(self.max_duty()) | 0x8000;
436+
}
431437
self.one_shot();
432438
self.set_load_mode(LoadMode::Individual);
433439
self.pwm
434440
.seq0
435441
.ptr
436-
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
442+
.write(|w| unsafe { w.bits(buffer as u32) });
437443
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(4) });
438444
self.start_seq(Seq::Seq0);
439445
}
@@ -1185,24 +1191,24 @@ pub trait Instance: sealed::Sealed + Deref<Target = RegisterBlock> {
11851191
const INTERRUPT: Interrupt;
11861192

11871193
/// Provides access to the associated internal duty buffer for the instance.
1188-
fn buffer() -> &'static Cell<[u16; 4]>;
1194+
fn buffer() -> *mut [u16; 4];
11891195
}
11901196

11911197
// Internal static duty buffers. One per instance.
1192-
static mut BUF0: Cell<[u16; 4]> = Cell::new([0; 4]);
1198+
static mut BUF0: [u16; 4] = [0; 4];
11931199
#[cfg(not(any(feature = "52810", feature = "52811")))]
1194-
static mut BUF1: Cell<[u16; 4]> = Cell::new([0; 4]);
1200+
static mut BUF1: [u16; 4] = [0; 4];
11951201
#[cfg(not(any(feature = "52810", feature = "52811")))]
1196-
static mut BUF2: Cell<[u16; 4]> = Cell::new([0; 4]);
1202+
static mut BUF2: [u16; 4] = [0; 4];
11971203
#[cfg(not(any(feature = "52810", feature = "52811", feature = "52832")))]
1198-
static mut BUF3: Cell<[u16; 4]> = Cell::new([0; 4]);
1204+
static mut BUF3: [u16; 4] = [0; 4];
11991205

12001206
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
12011207
impl Instance for crate::pac::PWM0 {
12021208
const INTERRUPT: Interrupt = Interrupt::PWM0;
12031209
#[inline(always)]
1204-
fn buffer() -> &'static Cell<[u16; 4]> {
1205-
unsafe { &BUF0 }
1210+
fn buffer() -> *mut [u16; 4] {
1211+
unsafe { addr_of_mut!(BUF0) }
12061212
}
12071213
}
12081214

@@ -1214,8 +1220,8 @@ impl Instance for crate::pac::PWM0 {
12141220
)))]
12151221
impl Instance for crate::pac::PWM1 {
12161222
const INTERRUPT: Interrupt = Interrupt::PWM1;
1217-
fn buffer() -> &'static Cell<[u16; 4]> {
1218-
unsafe { &BUF1 }
1223+
fn buffer() -> *mut [u16; 4] {
1224+
unsafe { addr_of_mut!(BUF1) }
12191225
}
12201226
}
12211227

@@ -1227,8 +1233,8 @@ impl Instance for crate::pac::PWM1 {
12271233
)))]
12281234
impl Instance for crate::pac::PWM2 {
12291235
const INTERRUPT: Interrupt = Interrupt::PWM2;
1230-
fn buffer() -> &'static Cell<[u16; 4]> {
1231-
unsafe { &BUF2 }
1236+
fn buffer() -> *mut [u16; 4] {
1237+
unsafe { addr_of_mut!(BUF2) }
12321238
}
12331239
}
12341240

@@ -1241,41 +1247,41 @@ impl Instance for crate::pac::PWM2 {
12411247
)))]
12421248
impl Instance for crate::pac::PWM3 {
12431249
const INTERRUPT: Interrupt = Interrupt::PWM3;
1244-
fn buffer() -> &'static Cell<[u16; 4]> {
1245-
unsafe { &BUF3 }
1250+
fn buffer() -> *mut [u16; 4] {
1251+
unsafe { addr_of_mut!(BUF3) }
12461252
}
12471253
}
12481254

12491255
#[cfg(any(feature = "9160", feature = "5340-app"))]
12501256
impl Instance for crate::pac::PWM0_NS {
12511257
const INTERRUPT: Interrupt = Interrupt::PWM0;
12521258
#[inline(always)]
1253-
fn buffer() -> &'static Cell<[u16; 4]> {
1254-
unsafe { &BUF0 }
1259+
fn buffer() -> *mut [u16; 4] {
1260+
unsafe { addr_of_mut!(BUF0) }
12551261
}
12561262
}
12571263

12581264
#[cfg(any(feature = "9160", feature = "5340-app"))]
12591265
impl Instance for crate::pac::PWM1_NS {
12601266
const INTERRUPT: Interrupt = Interrupt::PWM1;
1261-
fn buffer() -> &'static Cell<[u16; 4]> {
1262-
unsafe { &BUF1 }
1267+
fn buffer() -> *mut [u16; 4] {
1268+
unsafe { addr_of_mut!(BUF1) }
12631269
}
12641270
}
12651271

12661272
#[cfg(any(feature = "9160", feature = "5340-app"))]
12671273
impl Instance for crate::pac::PWM2_NS {
12681274
const INTERRUPT: Interrupt = Interrupt::PWM2;
1269-
fn buffer() -> &'static Cell<[u16; 4]> {
1270-
unsafe { &BUF2 }
1275+
fn buffer() -> *mut [u16; 4] {
1276+
unsafe { addr_of_mut!(BUF2) }
12711277
}
12721278
}
12731279

12741280
#[cfg(any(feature = "9160", feature = "5340-app"))]
12751281
impl Instance for crate::pac::PWM3_NS {
12761282
const INTERRUPT: Interrupt = Interrupt::PWM3;
1277-
fn buffer() -> &'static Cell<[u16; 4]> {
1278-
unsafe { &BUF3 }
1283+
fn buffer() -> *mut [u16; 4] {
1284+
unsafe { addr_of_mut!(BUF3) }
12791285
}
12801286
}
12811287
mod sealed {

nrf52840-hal-tests/tests/nvmc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use defmt_rtt as _;
55
use nrf52840_hal as _;
66
use panic_probe as _;
77

8+
use core::ptr::addr_of_mut;
89
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
910
use nrf52840_hal::{nvmc::Nvmc, pac};
1011

@@ -32,7 +33,7 @@ mod tests {
3233
let p = unwrap!(pac::Peripherals::take());
3334

3435
State {
35-
nvmc: Nvmc::new(p.NVMC, unsafe { &mut CONFIG }),
36+
nvmc: Nvmc::new(p.NVMC, unsafe { addr_of_mut!(CONFIG).as_mut().unwrap() }),
3637
}
3738
}
3839

0 commit comments

Comments
 (0)