-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
649 lines (564 loc) · 14.3 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#![deny(rust_2018_compatibility)]
#![deny(rust_2018_idioms)]
#![deny(warnings)]
#![no_std]
use core::{
cmp,
convert::{Infallible, TryInto},
fmt, ops,
sync::atomic::{self, Ordering},
};
use bare_metal::Nr;
use rtfm::Monotonic;
#[cfg(master)]
pub const NVIC_PRIO_BITS: u8 = 3;
#[cfg(not(master))]
pub const NVIC_PRIO_BITS: u8 = 2;
const MAILBOX_BASE: usize = 0x4008_B000;
pub struct CTIMER0;
const CTIMER0_BASE: usize = 0x40008000;
const CTIMER0_TCR: *mut u32 = (CTIMER0_BASE + 0x4) as *mut u32;
const CTIMER0_TC: *const u32 = (CTIMER0_BASE + 0x8) as *const u32;
unsafe impl Monotonic for CTIMER0 {
type Instant = Instant;
fn ratio() -> u32 {
1
}
fn now() -> Instant {
Instant::now()
}
/// Resets the counter to *zero*
unsafe fn reset() {
CTIMER0_TCR.write_volatile(0b01); // release from reset
}
fn zero() -> Instant {
Instant { inner: 0 }
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Instant {
inner: i32,
}
impl Instant {
pub fn now() -> Self {
Self {
inner: unsafe { CTIMER0_TC.read_volatile() } as i32,
}
}
pub fn duration_since(&self, earlier: Instant) -> Duration {
let diff = self.inner.wrapping_sub(earlier.inner);
assert!(diff >= 0, "second instant is later than `self`");
Duration { inner: diff as u32 }
}
}
impl fmt::Debug for Instant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Instant")
.field(&(self.inner as u32))
.finish()
}
}
impl ops::Add<Duration> for Instant {
type Output = Self;
fn add(self, dur: Duration) -> Instant {
Instant {
inner: self.inner.wrapping_add(dur.inner as i32),
}
}
}
impl ops::Sub for Instant {
type Output = Duration;
fn sub(self, rhs: Self) -> Duration {
self.duration_since(rhs)
}
}
impl Ord for Instant {
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
self.inner.wrapping_sub(rhs.inner).cmp(&0)
}
}
impl PartialOrd for Instant {
fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(rhs))
}
}
#[derive(Clone, Copy)]
pub struct Duration {
inner: u32,
}
impl Duration {
pub fn from_cycles(cycles: u32) -> Self {
Self { inner: cycles }
}
pub fn as_cycles(&self) -> u32 {
self.inner
}
}
impl TryInto<u32> for Duration {
type Error = Infallible;
fn try_into(self) -> Result<u32, Infallible> {
Ok(self.inner)
}
}
pub fn xpend(core: u8, int: impl Nr) {
// Cortex-M0+
const MAILBOX_IRQ0SET: *mut u32 = (MAILBOX_BASE + 0x04) as *mut u32;
// Cortex-M4
const MAILBOX_IRQ1SET: *mut u32 = (MAILBOX_BASE + 0x14) as *mut u32;
let nr = int.nr();
assert!(core < 2);
assert!(nr < 32);
unsafe {
if core == 0 {
MAILBOX_IRQ1SET.write_volatile(1 << nr);
} else {
MAILBOX_IRQ0SET.write_volatile(1 << nr);
}
}
}
// forward interrupts
#[no_mangle]
extern "C" fn MAILBOX() {
unsafe {
const NVIC_ISPR: *mut u32 = 0xE000_E200 as *mut u32;
match () {
#[cfg(master)]
() => {
// Cortex-M4
const MAILBOX_IRQ1: *const u32 = (MAILBOX_BASE + 0x10) as *const u32;
const MAILBOX_IRQ1CLR: *mut u32 = (MAILBOX_BASE + 0x18) as *mut u32;
let mask = MAILBOX_IRQ1.read_volatile();
NVIC_ISPR.write_volatile(mask);
MAILBOX_IRQ1CLR.write_volatile(mask);
}
#[cfg(not(master))]
() => {
// Cortex-M0+
const MAILBOX_IRQ0: *const u32 = MAILBOX_BASE as *const u32;
const MAILBOX_IRQ0CLR: *mut u32 = (MAILBOX_BASE + 0x8) as *mut u32;
let mask = MAILBOX_IRQ0.read_volatile();
NVIC_ISPR.write_volatile(mask);
MAILBOX_IRQ0CLR.write_volatile(mask);
}
}
}
}
// This is the pseudo-Rust version of the common entry point, executed by both cores.
//
// Because this requires a custom calling convention this is actually written in assembly and can
// be found in the `asm.s` file
#[cfg(unused)]
#[no_mangle]
pub unsafe extern "C" fn _start() -> ! {
const CPUID: *mut u32 = 0x0e000_ed00 as *mut u32;
const SYSCON_CPBOOT: *mut u32 = 0x4000_0804 as *mut u32;
const SYSCON_CPSTACK: *mut u32 = 0x4000_0808 as *mut u32;
if (CPUID.read_volatile() >> 4) & 0xfff == 0xc24 {
// this is the Cortex-M4F core
start()
} else {
// this is the Cortex-M0+ core
let boot = SYSCON_CPBOOT.read_volatile();
if boot == 0 {
// not yet set by the master; sleep
loop {
// WFI
}
} else {
// written to the SP (Stack Pointer) register
let _sp = SYSCON_CPSTACK.read_volatile();
let boot = core::mem::transmute::<u32, extern "C" fn() -> !>(boot);
// NOTE "branch" not "branch link"
boot()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn start() -> ! {
// Start of core #1 (slave) vector table
// XXX It'd be best to get this from the linker script
const SLAVE_VECTORS: *const u32 = 0x0002_0000 as *const u32;
match () {
#[cfg(master)]
() => {
extern "C" {
static mut _sshared: u32;
static mut _eshared: u32;
static _sishared: u32;
}
const SYSCON_AHBCLKCTRLSET0: *mut u32 = 0x4000_0220 as *mut u32;
// enable SRAM1, SRAM2 and MAILBOX
// (SRAM1 should be enabled on boot / reset according to the data sheet but it isn't)
SYSCON_AHBCLKCTRLSET0.write_volatile((1 << 26) | (1 << 4) | (1 << 3));
const SYSCON_AHBCLKCTRLSET1: *mut u32 = 0x4000_0224 as *mut u32;
// enable CTIMER0
SYSCON_AHBCLKCTRLSET1.write_volatile(1 << 26);
// held the CTIMER0 counter in reset
CTIMER0_TCR.write_volatile(0b10);
// SRAM2 must be enabled before the `.shared` section is initialized
atomic::compiler_fence(Ordering::SeqCst);
// initialize `.shared` variables
// this is *not* a volatile operation so compiler fences are required
r0::init_data(&mut _sshared, &mut _eshared, &_sishared);
// ensure that the slave is only active *after* shared variables have been initialized
atomic::compiler_fence(Ordering::SeqCst);
const SYSCON_CPBOOT: *mut u32 = 0x4000_0804 as *mut u32;
const SYSCON_CPSTACK: *mut u32 = 0x4000_0808 as *mut u32;
const SYSCON_CPUCTRL: *mut u32 = 0x4000_0800 as *mut u32;
SYSCON_CPSTACK.write_volatile(SLAVE_VECTORS.read());
SYSCON_CPBOOT.write_volatile(SLAVE_VECTORS.add(1).read());
// enable the M0+ clock but hold the core in reset
SYSCON_CPUCTRL.write_volatile(0xc0c4_806d);
// release the M0+ from reset
SYSCON_CPUCTRL.write_volatile(0xc0c4_804d);
}
#[cfg(not(master))]
() => {
const SCB_VTOR: *mut u32 = 0xe000_ed08 as *mut u32;
// after reset the slave uses 0x0 as the start of the vector table
// this needs to be updated to use the right address
SCB_VTOR.write_volatile(SLAVE_VECTORS as u32);
}
}
const NVIC_ISER: *mut u32 = 0xE000_E100 as *mut u32;
// unmask the MAILBOX interrupt
NVIC_ISER.write_volatile(1 << 31);
extern "C" {
static mut _sbss: u32;
static mut _ebss: u32;
static mut _sdata: u32;
static mut _edata: u32;
static _sidata: u32;
}
// initialize .bss and .data
r0::zero_bss(&mut _sbss, &mut _ebss);
r0::init_data(&mut _sdata, &mut _edata, &_sidata);
// do not run `main` before the `static` variables have been initialized
atomic::compiler_fence(Ordering::SeqCst);
extern "Rust" {
// user program entry point
fn main() -> !;
}
match () {
#[cfg(master)]
() => {
const SCB_CPACR: *mut u32 = 0xe000_ed88 as *mut u32;
// enable the FPU
SCB_CPACR.write_volatile(SCB_CPACR.read_volatile() | (0b01_01 << 20) | 0b10_10 << 20);
// trampoline to prevent the user `main` from being inlined into this (`start`) function
#[inline(never)]
#[no_mangle]
unsafe fn _main() -> ! {
main()
}
_main()
}
#[cfg(not(master))]
() => main(),
}
}
#[no_mangle]
extern "C" fn DefaultHandler(_msp: &ExceptionFrame) -> ! {
loop {}
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct ExceptionFrame {
/// (General purpose) Register 0
pub r0: u32,
/// (General purpose) Register 1
pub r1: u32,
/// (General purpose) Register 2
pub r2: u32,
/// (General purpose) Register 3
pub r3: u32,
/// (General purpose) Register 12
pub r12: u32,
/// Linker Register
pub lr: u32,
/// Program Counter
pub pc: u32,
/// Program Status Register
pub xpsr: u32,
}
#[allow(non_camel_case_types)]
pub enum Interrupt {
WDT,
DMA,
GINT0,
GINT1,
PIN_INT0,
PIN_INT1,
PIN_INT2,
PIN_INT3,
UTICK,
MRT,
CTIMER0,
CTIMER1,
SCT0,
CTIMER3,
Flexcomm0,
Flexcomm1,
Flexcomm2,
Flexcomm3,
Flexcomm4,
Flexcomm5,
Flexcomm6,
Flexcomm7,
ADC0_SEQA,
ADC0_SEQB,
ADC0_THCMP,
DMIC,
HWVAD,
USB_WAKEUP,
USB,
RTC,
MAILBOX,
#[cfg(master)]
PIN_INT4,
#[cfg(master)]
PIN_INT5,
#[cfg(master)]
PIN_INT6,
#[cfg(master)]
PIN_INT7,
#[cfg(master)]
CTIMER2,
#[cfg(master)]
CTIMER4,
}
unsafe impl Nr for Interrupt {
fn nr(&self) -> u8 {
use Interrupt::*;
match self {
WDT => 0,
DMA => 1,
GINT0 => 2,
GINT1 => 3,
PIN_INT0 => 4,
PIN_INT1 => 5,
PIN_INT2 => 6,
PIN_INT3 => 7,
UTICK => 8,
MRT => 9,
CTIMER0 => 10,
CTIMER1 => 11,
SCT0 => 12,
CTIMER3 => 13,
Flexcomm0 => 14,
Flexcomm1 => 15,
Flexcomm2 => 16,
Flexcomm3 => 17,
Flexcomm4 => 18,
Flexcomm5 => 19,
Flexcomm6 => 20,
Flexcomm7 => 21,
ADC0_SEQA => 22,
ADC0_SEQB => 23,
ADC0_THCMP => 24,
DMIC => 25,
HWVAD => 26,
USB_WAKEUP => 27,
USB => 28,
RTC => 29,
MAILBOX => 31,
#[cfg(master)]
PIN_INT4 => 32,
#[cfg(master)]
PIN_INT5 => 33,
#[cfg(master)]
PIN_INT6 => 34,
#[cfg(master)]
PIN_INT7 => 35,
#[cfg(master)]
CTIMER2 => 36,
#[cfg(master)]
CTIMER4 => 37,
}
}
}
extern "C" {
fn NMI();
fn HardFault();
#[cfg(master)]
fn MemoryManagement();
#[cfg(master)]
fn BusFault();
#[cfg(master)]
fn UsageFault();
fn SVCall();
fn DebugMonitor();
fn PendSV();
fn SysTick();
fn WDT();
fn DMA();
fn GINT0();
fn GINT1();
fn PIN_INT0();
fn PIN_INT1();
fn PIN_INT2();
fn PIN_INT3();
fn UTICK();
fn MRT();
fn CTIMER1();
fn SCT0();
fn CTIMER3();
fn Flexcomm0();
fn Flexcomm1();
fn Flexcomm2();
fn Flexcomm3();
fn Flexcomm4();
fn Flexcomm5();
fn Flexcomm6();
fn Flexcomm7();
fn ADC0_SEQA();
fn ADC0_SEQB();
fn ADC0_THCMP();
fn DMIC();
fn HWVAD();
fn USB_WAKEUP();
fn USB();
fn RTC();
// defined above
// fn MAILBOX();
#[cfg(master)]
fn PIN_INT4();
#[cfg(master)]
fn PIN_INT5();
#[cfg(master)]
fn PIN_INT6();
#[cfg(master)]
fn PIN_INT7();
#[cfg(master)]
fn CTIMER2();
#[cfg(master)]
fn CTIMER4();
}
#[cfg(master)]
#[link_section = ".vectors"]
#[no_mangle]
static VECTORS: [Option<unsafe extern "C" fn()>; 52] = [
// Cortex-M exceptions
Some(NMI),
Some(HardFault),
Some(MemoryManagement),
Some(BusFault),
Some(UsageFault),
None, // NOTE checksum goes here
None,
None,
None,
Some(SVCall),
Some(DebugMonitor),
None,
Some(PendSV),
Some(SysTick),
// 0 - Device-specific interrupts
Some(WDT),
Some(DMA),
Some(GINT0),
Some(GINT1),
Some(PIN_INT0),
Some(PIN_INT1),
Some(PIN_INT2),
Some(PIN_INT3),
Some(UTICK),
Some(MRT),
// 10
Some({
extern "C" {
fn CTIMER0();
}
CTIMER0
}),
Some(CTIMER1),
Some(SCT0),
Some(CTIMER3),
Some(Flexcomm0),
Some(Flexcomm1),
Some(Flexcomm2),
Some(Flexcomm3),
Some(Flexcomm4),
Some(Flexcomm5),
// 20
Some(Flexcomm6),
Some(Flexcomm7),
Some(ADC0_SEQA),
Some(ADC0_SEQB),
Some(ADC0_THCMP),
Some(DMIC),
Some(HWVAD),
Some(USB_WAKEUP),
Some(USB),
Some(RTC),
// 30
None,
Some(MAILBOX),
Some(PIN_INT4),
Some(PIN_INT5),
Some(PIN_INT6),
Some(PIN_INT7),
Some(CTIMER2),
Some(CTIMER4),
];
#[cfg(not(master))]
#[link_section = ".vectors"]
#[no_mangle]
static VECTORS: [Option<unsafe extern "C" fn()>; 46] = [
// Cortex-M exceptions
Some(NMI),
Some(HardFault),
None,
None,
None,
None,
None,
None,
None,
Some(SVCall),
Some(DebugMonitor),
None,
Some(PendSV),
Some(SysTick),
// 0 - Device-specific interrupts
Some(WDT),
Some(DMA),
Some(GINT0),
Some(GINT1),
Some(PIN_INT0),
Some(PIN_INT1),
Some(PIN_INT2),
Some(PIN_INT3),
Some(UTICK),
Some(MRT),
// 10
Some({
extern "C" {
fn CTIMER0();
}
CTIMER0
}),
Some(CTIMER1),
Some(SCT0),
Some(CTIMER3),
Some(Flexcomm0),
Some(Flexcomm1),
Some(Flexcomm2),
Some(Flexcomm3),
Some(Flexcomm4),
Some(Flexcomm5),
// 20
Some(Flexcomm6),
Some(Flexcomm7),
Some(ADC0_SEQA),
Some(ADC0_SEQB),
Some(ADC0_THCMP),
Some(DMIC),
Some(HWVAD),
Some(USB_WAKEUP),
Some(USB),
Some(RTC),
// 30
None,
Some(MAILBOX),
];