Skip to content

Commit f0d1ed4

Browse files
bors[bot]JOE1994
andauthored
Merge #235
235: make 'fn ptr()' APIs to be 'const fn ptr()' r=therealprof a=JOE1994 This PR changes functions like `ITM::ptr()`, `DWT::ptr()` to become `const fn`s. * functions like `ITM::ptr()`, `DWT::ptr()` return pointers that are cast from constants, but currently these functions can't be used to define a constant. This PR will allow below code to compile. ```rust use cortex_m::peripheral::ITM; use cortex_m::peripheral::itm::RegisterBlock; // Below line currently won't compile, since `ITM::ptr()` is not a `const fn` const ITM_PTR: *mut RegisterBlock = ITM::ptr(); ``` I couldn't think of disadvantages that might accompany this change, but please correct me if I'm wrong. Thank you for reviewing this PR :+1: Co-authored-by: JOE1994 <[email protected]> Co-authored-by: Youngsuk Kim <[email protected]>
2 parents 9dee813 + 68ec039 commit f0d1ed4

File tree

2 files changed

+103
-59
lines changed

2 files changed

+103
-59
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
- New `InterruptNumber` trait is now required on interrupt arguments to the
1313
various NVIC functions, replacing the previous use of `Nr` from bare-metal.
14+
- Associated const `PTR` is introduced to Core Peripherals to
15+
eventually replace the existing `ptr()` API.
1416

1517
## [v0.6.2] - 2020-01-12
1618

src/peripheral/mod.rs

Lines changed: 101 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,19 @@ unsafe impl Send for CBP {}
231231
#[cfg(not(armv6m))]
232232
impl CBP {
233233
#[inline(always)]
234-
pub(crate) unsafe fn new() -> Self {
234+
pub(crate) const unsafe fn new() -> Self {
235235
CBP {
236236
_marker: PhantomData,
237237
}
238238
}
239239

240-
/// Returns a pointer to the register block
240+
/// Pointer to the register block
241+
pub const PTR: *const self::cbp::RegisterBlock = 0xE000_EF50 as *const _;
242+
243+
/// Returns a pointer to the register block (to be deprecated in 0.7)
241244
#[inline(always)]
242-
pub fn ptr() -> *const self::cbp::RegisterBlock {
243-
0xE000_EF50 as *const _
245+
pub const fn ptr() -> *const self::cbp::RegisterBlock {
246+
Self::PTR
244247
}
245248
}
246249

@@ -250,7 +253,7 @@ impl ops::Deref for CBP {
250253

251254
#[inline(always)]
252255
fn deref(&self) -> &Self::Target {
253-
unsafe { &*Self::ptr() }
256+
unsafe { &*Self::PTR }
254257
}
255258
}
256259

@@ -262,10 +265,13 @@ pub struct CPUID {
262265
unsafe impl Send for CPUID {}
263266

264267
impl CPUID {
265-
/// Returns a pointer to the register block
268+
/// Pointer to the register block
269+
pub const PTR: *const self::cpuid::RegisterBlock = 0xE000_ED00 as *const _;
270+
271+
/// Returns a pointer to the register block (to be deprecated in 0.7)
266272
#[inline(always)]
267-
pub fn ptr() -> *const self::cpuid::RegisterBlock {
268-
0xE000_ED00 as *const _
273+
pub const fn ptr() -> *const self::cpuid::RegisterBlock {
274+
Self::PTR
269275
}
270276
}
271277

@@ -274,7 +280,7 @@ impl ops::Deref for CPUID {
274280

275281
#[inline(always)]
276282
fn deref(&self) -> &Self::Target {
277-
unsafe { &*Self::ptr() }
283+
unsafe { &*Self::PTR }
278284
}
279285
}
280286

@@ -286,10 +292,13 @@ pub struct DCB {
286292
unsafe impl Send for DCB {}
287293

288294
impl DCB {
289-
/// Returns a pointer to the register block
295+
/// Pointer to the register block
296+
pub const PTR: *const dcb::RegisterBlock = 0xE000_EDF0 as *const _;
297+
298+
/// Returns a pointer to the register block (to be deprecated in 0.7)
290299
#[inline(always)]
291-
pub fn ptr() -> *const dcb::RegisterBlock {
292-
0xE000_EDF0 as *const _
300+
pub const fn ptr() -> *const dcb::RegisterBlock {
301+
Self::PTR
293302
}
294303
}
295304

@@ -298,7 +307,7 @@ impl ops::Deref for DCB {
298307

299308
#[inline(always)]
300309
fn deref(&self) -> &Self::Target {
301-
unsafe { &*DCB::ptr() }
310+
unsafe { &*DCB::PTR }
302311
}
303312
}
304313

@@ -310,10 +319,13 @@ pub struct DWT {
310319
unsafe impl Send for DWT {}
311320

312321
impl DWT {
313-
/// Returns a pointer to the register block
322+
/// Pointer to the register block
323+
pub const PTR: *const dwt::RegisterBlock = 0xE000_1000 as *const _;
324+
325+
/// Returns a pointer to the register block (to be deprecated in 0.7)
314326
#[inline(always)]
315-
pub fn ptr() -> *const dwt::RegisterBlock {
316-
0xE000_1000 as *const _
327+
pub const fn ptr() -> *const dwt::RegisterBlock {
328+
Self::PTR
317329
}
318330
}
319331

@@ -322,7 +334,7 @@ impl ops::Deref for DWT {
322334

323335
#[inline(always)]
324336
fn deref(&self) -> &Self::Target {
325-
unsafe { &*Self::ptr() }
337+
unsafe { &*Self::PTR }
326338
}
327339
}
328340

@@ -335,10 +347,13 @@ unsafe impl Send for FPB {}
335347

336348
#[cfg(not(armv6m))]
337349
impl FPB {
338-
/// Returns a pointer to the register block
350+
/// Pointer to the register block
351+
pub const PTR: *const fpb::RegisterBlock = 0xE000_2000 as *const _;
352+
353+
/// Returns a pointer to the register block (to be deprecated in 0.7)
339354
#[inline(always)]
340-
pub fn ptr() -> *const fpb::RegisterBlock {
341-
0xE000_2000 as *const _
355+
pub const fn ptr() -> *const fpb::RegisterBlock {
356+
Self::PTR
342357
}
343358
}
344359

@@ -348,7 +363,7 @@ impl ops::Deref for FPB {
348363

349364
#[inline(always)]
350365
fn deref(&self) -> &Self::Target {
351-
unsafe { &*Self::ptr() }
366+
unsafe { &*Self::PTR }
352367
}
353368
}
354369

@@ -361,10 +376,13 @@ unsafe impl Send for FPU {}
361376

362377
#[cfg(any(has_fpu, target_arch = "x86_64"))]
363378
impl FPU {
364-
/// Returns a pointer to the register block
379+
/// Pointer to the register block
380+
pub const PTR: *const fpu::RegisterBlock = 0xE000_EF30 as *const _;
381+
382+
/// Returns a pointer to the register block (to be deprecated in 0.7)
365383
#[inline(always)]
366-
pub fn ptr() -> *const fpu::RegisterBlock {
367-
0xE000_EF30 as *const _
384+
pub const fn ptr() -> *const fpu::RegisterBlock {
385+
Self::PTR
368386
}
369387
}
370388

@@ -374,7 +392,7 @@ impl ops::Deref for FPU {
374392

375393
#[inline(always)]
376394
fn deref(&self) -> &Self::Target {
377-
unsafe { &*Self::ptr() }
395+
unsafe { &*Self::PTR }
378396
}
379397
}
380398

@@ -391,10 +409,13 @@ pub struct ICB {
391409
unsafe impl Send for ICB {}
392410

393411
impl ICB {
394-
/// Returns a pointer to the register block
412+
/// Pointer to the register block
413+
pub const PTR: *mut icb::RegisterBlock = 0xE000_E004 as *mut _;
414+
415+
/// Returns a pointer to the register block (to be deprecated in 0.7)
395416
#[inline(always)]
396-
pub fn ptr() -> *mut icb::RegisterBlock {
397-
0xE000_E004 as *mut _
417+
pub const fn ptr() -> *mut icb::RegisterBlock {
418+
Self::PTR
398419
}
399420
}
400421

@@ -403,14 +424,14 @@ impl ops::Deref for ICB {
403424

404425
#[inline(always)]
405426
fn deref(&self) -> &Self::Target {
406-
unsafe { &*Self::ptr() }
427+
unsafe { &*Self::PTR }
407428
}
408429
}
409430

410431
impl ops::DerefMut for ICB {
411432
#[inline(always)]
412433
fn deref_mut(&mut self) -> &mut Self::Target {
413-
unsafe { &mut *Self::ptr() }
434+
unsafe { &mut *Self::PTR }
414435
}
415436
}
416437

@@ -423,10 +444,13 @@ unsafe impl Send for ITM {}
423444

424445
#[cfg(all(not(armv6m), not(armv8m_base)))]
425446
impl ITM {
426-
/// Returns a pointer to the register block
447+
/// Pointer to the register block
448+
pub const PTR: *mut itm::RegisterBlock = 0xE000_0000 as *mut _;
449+
450+
/// Returns a pointer to the register block (to be deprecated in 0.7)
427451
#[inline(always)]
428-
pub fn ptr() -> *mut itm::RegisterBlock {
429-
0xE000_0000 as *mut _
452+
pub const fn ptr() -> *mut itm::RegisterBlock {
453+
Self::PTR
430454
}
431455
}
432456

@@ -436,15 +460,15 @@ impl ops::Deref for ITM {
436460

437461
#[inline(always)]
438462
fn deref(&self) -> &Self::Target {
439-
unsafe { &*Self::ptr() }
463+
unsafe { &*Self::PTR }
440464
}
441465
}
442466

443467
#[cfg(all(not(armv6m), not(armv8m_base)))]
444468
impl ops::DerefMut for ITM {
445469
#[inline(always)]
446470
fn deref_mut(&mut self) -> &mut Self::Target {
447-
unsafe { &mut *Self::ptr() }
471+
unsafe { &mut *Self::PTR }
448472
}
449473
}
450474

@@ -456,10 +480,13 @@ pub struct MPU {
456480
unsafe impl Send for MPU {}
457481

458482
impl MPU {
459-
/// Returns a pointer to the register block
483+
/// Pointer to the register block
484+
pub const PTR: *const mpu::RegisterBlock = 0xE000_ED90 as *const _;
485+
486+
/// Returns a pointer to the register block (to be deprecated in 0.7)
460487
#[inline(always)]
461-
pub fn ptr() -> *const mpu::RegisterBlock {
462-
0xE000_ED90 as *const _
488+
pub const fn ptr() -> *const mpu::RegisterBlock {
489+
Self::PTR
463490
}
464491
}
465492

@@ -468,7 +495,7 @@ impl ops::Deref for MPU {
468495

469496
#[inline(always)]
470497
fn deref(&self) -> &Self::Target {
471-
unsafe { &*Self::ptr() }
498+
unsafe { &*Self::PTR }
472499
}
473500
}
474501

@@ -480,10 +507,13 @@ pub struct NVIC {
480507
unsafe impl Send for NVIC {}
481508

482509
impl NVIC {
483-
/// Returns a pointer to the register block
510+
/// Pointer to the register block
511+
pub const PTR: *const nvic::RegisterBlock = 0xE000_E100 as *const _;
512+
513+
/// Returns a pointer to the register block (to be deprecated in 0.7)
484514
#[inline(always)]
485-
pub fn ptr() -> *const nvic::RegisterBlock {
486-
0xE000_E100 as *const _
515+
pub const fn ptr() -> *const nvic::RegisterBlock {
516+
Self::PTR
487517
}
488518
}
489519

@@ -492,7 +522,7 @@ impl ops::Deref for NVIC {
492522

493523
#[inline(always)]
494524
fn deref(&self) -> &Self::Target {
495-
unsafe { &*Self::ptr() }
525+
unsafe { &*Self::PTR }
496526
}
497527
}
498528

@@ -505,10 +535,13 @@ unsafe impl Send for SAU {}
505535

506536
#[cfg(armv8m)]
507537
impl SAU {
508-
/// Returns a pointer to the register block
538+
/// Pointer to the register block
539+
pub const PTR: *const sau::RegisterBlock = 0xE000_EDD0 as *const _;
540+
541+
/// Returns a pointer to the register block (to be deprecated in 0.7)
509542
#[inline(always)]
510-
pub fn ptr() -> *const sau::RegisterBlock {
511-
0xE000_EDD0 as *const _
543+
pub const fn ptr() -> *const sau::RegisterBlock {
544+
Self::PTR
512545
}
513546
}
514547

@@ -518,7 +551,7 @@ impl ops::Deref for SAU {
518551

519552
#[inline(always)]
520553
fn deref(&self) -> &Self::Target {
521-
unsafe { &*Self::ptr() }
554+
unsafe { &*Self::PTR }
522555
}
523556
}
524557

@@ -530,10 +563,13 @@ pub struct SCB {
530563
unsafe impl Send for SCB {}
531564

532565
impl SCB {
533-
/// Returns a pointer to the register block
566+
/// Pointer to the register block
567+
pub const PTR: *const scb::RegisterBlock = 0xE000_ED04 as *const _;
568+
569+
/// Returns a pointer to the register block (to be deprecated in 0.7)
534570
#[inline(always)]
535-
pub fn ptr() -> *const scb::RegisterBlock {
536-
0xE000_ED04 as *const _
571+
pub const fn ptr() -> *const scb::RegisterBlock {
572+
Self::PTR
537573
}
538574
}
539575

@@ -542,7 +578,7 @@ impl ops::Deref for SCB {
542578

543579
#[inline(always)]
544580
fn deref(&self) -> &Self::Target {
545-
unsafe { &*Self::ptr() }
581+
unsafe { &*Self::PTR }
546582
}
547583
}
548584

@@ -554,10 +590,13 @@ pub struct SYST {
554590
unsafe impl Send for SYST {}
555591

556592
impl SYST {
557-
/// Returns a pointer to the register block
593+
/// Pointer to the register block
594+
pub const PTR: *const syst::RegisterBlock = 0xE000_E010 as *const _;
595+
596+
/// Returns a pointer to the register block (to be deprecated in 0.7)
558597
#[inline(always)]
559-
pub fn ptr() -> *const syst::RegisterBlock {
560-
0xE000_E010 as *const _
598+
pub const fn ptr() -> *const syst::RegisterBlock {
599+
Self::PTR
561600
}
562601
}
563602

@@ -566,7 +605,7 @@ impl ops::Deref for SYST {
566605

567606
#[inline(always)]
568607
fn deref(&self) -> &Self::Target {
569-
unsafe { &*Self::ptr() }
608+
unsafe { &*Self::PTR }
570609
}
571610
}
572611

@@ -579,10 +618,13 @@ unsafe impl Send for TPIU {}
579618

580619
#[cfg(not(armv6m))]
581620
impl TPIU {
582-
/// Returns a pointer to the register block
621+
/// Pointer to the register block
622+
pub const PTR: *const tpiu::RegisterBlock = 0xE004_0000 as *const _;
623+
624+
/// Returns a pointer to the register block (to be deprecated in 0.7)
583625
#[inline(always)]
584-
pub fn ptr() -> *const tpiu::RegisterBlock {
585-
0xE004_0000 as *const _
626+
pub const fn ptr() -> *const tpiu::RegisterBlock {
627+
Self::PTR
586628
}
587629
}
588630

@@ -592,6 +634,6 @@ impl ops::Deref for TPIU {
592634

593635
#[inline(always)]
594636
fn deref(&self) -> &Self::Target {
595-
unsafe { &*Self::ptr() }
637+
unsafe { &*Self::PTR }
596638
}
597639
}

0 commit comments

Comments
 (0)