teensy_lc/
lib.rs

1#![no_std]
2
3pub extern crate embedded_hal as hal;
4pub extern crate mkl26z4 as dev;
5extern crate teensy_lc_macros;
6extern crate void;
7
8pub mod fgpio;
9pub mod mcg;
10pub mod panic_sos;
11pub mod prelude;
12pub mod sim;
13
14// TODO: how to make clock selection configurable and still well-optimized?
15pub const F_CPU: u32 = 48_000_000;
16// F_BUS = 24000000;
17// F_MEM = 24000000;
18// F_PLL = 96000000;
19
20pub fn delay_usec(usec: u32) {
21    cortex_m::asm::delay(usec * (F_CPU / 1_000_000));
22}
23
24pub fn init() {
25    // SKIP:
26    // enable clocks to always used peripherals
27    // In teensyduino it turns on USB OTG, GPIO, ADC0, TPM, flash
28    // (last one seems enabled by default)
29
30    // release I/O pins hold, if we woke up from VLLS mode
31    // if (PMC_REGSC & PMC_REGSC_ACKISO) PMC_REGSC |= PMC_REGSC_ACKISO;
32    let regsc = unsafe { &(*dev::PMC::ptr()).regsc };
33    if regsc.read().ackiso().is_1() {
34        regsc.modify(|_, w| w.ackiso().set_bit());
35    }
36
37    // since this is a write once register, make it visible to all F_CPU's
38    // so we can into other sleep modes in the future at any speed
39    // SMC_PMPROT = SMC_PMPROT_AVLP | SMC_PMPROT_ALLS | SMC_PMPROT_AVLLS;
40    let smc = unsafe { &(*dev::SMC::ptr()) };
41    smc.pmprot
42        .write(|w| w.avlp().set_bit().alls().set_bit().avlls().set_bit());
43
44    // teensyduino now runs equivalent of zero_bss + init_data, which
45    // cortex-m-rt will do after this function finishes
46
47    // SKIP FOR NOW:
48    // default all interrupts to medium priority level
49    // for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = _VectorsFlash[i];
50    // for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
51    // SCB_VTOR = (uint32_t)_VectorsRam;	// use vector table in RAM
52
53    //// TODO
54    // initialize the SysTick counter
55    // SYST_RVR = (F_CPU / 1000) - 1;
56    // SYST_CVR = 0;
57    // SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
58    // SCB_SHPR3 = 0x20200000;  // Systick = priority 32
59
60    //// TODO (or already done by cortex-m-rt)
61    // __enable_irq();
62
63    //// TODO
64    // _init_Teensyduino_internal_();
65}