ok
Dependencies: LPC1114_WakeInterruptIn
Fork of WakeUp by
Diff: Device/WakeUp_Freescale.cpp
- Revision:
- 18:13aed323e040
- Parent:
- 16:f3adba7cf7c4
- Child:
- 19:9d355da2770e
--- a/Device/WakeUp_Freescale.cpp Wed Apr 22 20:36:13 2015 +0000 +++ b/Device/WakeUp_Freescale.cpp Wed Jul 01 19:28:55 2015 +0000 @@ -10,6 +10,12 @@ static uint32_t oldvector; static uint8_t oldPSR; +//See if we have a 32kHz crystal on the clock input +//Check if the DMX32 bit is set, not perfect, but most cases will work +static inline bool is32kXtal(void) { + return (MCG->C4 & MCG_C4_DMX32_MASK); +} + void restore(void); void WakeUp::set_ms(uint32_t ms) @@ -35,15 +41,23 @@ LPTMR0->CSR = 0; if (ms != 0) { - //Clock from the 1kHz LPO - LPTMR0->PSR = LPTMR_PSR_PCS(1); - /* Set interrupt handler */ NVIC_SetVector(LPTimer_IRQn, (uint32_t)WakeUp::irq_handler); NVIC_EnableIRQ(LPTimer_IRQn); - uint32_t counts = (uint32_t)((float)ms * cycles_per_ms); - + uint32_t counts; + //Set clock + if (is32kXtal()) { + SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK; //Put RTC/LPTMR on 32kHz external. + OSC0->CR |= OSC_CR_EREFSTEN_MASK; + LPTMR0->PSR = LPTMR_PSR_PCS(2); + counts = (uint32_t)((float)ms * 32.768f); + } else { + //Clock from the 1kHz LPO + LPTMR0->PSR = LPTMR_PSR_PCS(1); + counts = (uint32_t)((float)ms * cycles_per_ms); + } + //If no prescaler is needed if (counts <= 0xFFFF) LPTMR0->PSR |= LPTMR_PSR_PBYP_MASK; @@ -77,16 +91,18 @@ void WakeUp::calibrate(void) { - wait_us(1); //Otherwise next wait might overwrite our settings - cycles_per_ms = 1.0; - set_ms(1100); - wait_ms(100); - - //Write first to sync value - LPTMR0->CNR = 0; - uint32_t ticks = LPTMR0->CNR; - cycles_per_ms = ticks / 100.0; - set_ms(0); + if (!is32kXtal()) { + wait_us(1); //Otherwise next wait might overwrite our settings + cycles_per_ms = 1.0; + set_ms(1100); + wait_ms(100); + + //Write first to sync value + LPTMR0->CNR = 0; + uint32_t ticks = LPTMR0->CNR; + cycles_per_ms = ticks / 100.0; + set_ms(0); + } } void restore(void){