void InitDeepSleep(uint32_t ms) { LPC_SYSCON->PDRUNCFG = BF_PDRUNCFG_RUN; // Initialize power to chip modules LPC_SYSCON->SYSAHBCLKCTRL = BF_SYSAHBCLKCTRL_RUN; // Initialize clocks // Make sure clock is set to 12 MHz and setup System Tick Timer (1 Tick = 1ms) Target_SetClock_IRC(); LPC_SYSCON->PDAWAKECFG = LPC_SYSCON->PDRUNCFG; // Configure PDAWAKECFG to restore PDRUNCFG on wake up LPC_SYSCON->PDSLEEPCFG = BF_PDSLEEPCFG_WDT; // Configure deep sleep with WDT oscillator LPC_TMR32B0->TCR = BF_TIMER_TCR_RESET; // reset timer // The following lines initializing PR and MR0 LPC_TMR32B0->PR = 0; LPC_TMR32B0->MR2 = (ms*MeasureWDO()/WDOMEASUREDURATION_MS); LPC_TMR32B0->MCR = BF_TIMER_MCR_MATCHSTOP2 | BF_TIMER_MCR_MATCHRESET2; LPC_IOCON->PIO0_1 = (LPC_IOCON->PIO0_1 & ~0x3F) | 0x2; // Set IOCON register on P0.1 to match function /* Configure Wakeup I/O */ /* Specify the start logic to allow the chip to be waken up using PIO0_1 */ LPC_SYSCON->STARTAPRP0 |= BF_STARTLOGIC_P0_1; // Rising edge LPC_SYSCON->STARTRSRP0CLR = BF_STARTLOGIC_P0_1; // Clear pending bit LPC_SYSCON->STARTERP0 |= BF_STARTLOGIC_P0_1; // Enable Start Logic NVIC_EnableIRQ(WAKEUP1_IRQn); lastTimeSetup = ms; // Save time in local variable for EnterDeepSleep() }
void InitDeepSleep(void) { #ifdef ENABLE_CLKOUT /* Output the Clk onto the CLKOUT Pin PIO0_1 to monitor the freq on a scope */ LPC_IOCON->PIO0_1 = 0xC1; /* Select the MAIN clock as the clock out selection since it's driving the core */ LPC_SYSCON->CLKOUTCLKSEL = 3; LPC_SYSCON->CLKOUTDIV = 10; LPC_SYSCON->CLKOUTUEN = 0; LPC_SYSCON->CLKOUTUEN = 1; #endif /* ENABLE_CLKOUT */ // Set up Systick timer for 1 mS timeouts // Used for general timing when awake and to calibrate the WDT SysTick->LOAD = (MainClockFrequency / 1000)-1; SysTick->VAL = 0; // reload counter SysTick->CTRL = 7; // enable counter, interrupts, select processor clock LPC_SYSCON->PDAWAKECFG = // Configure PDAWAKECFG to restore PDRUNCFG on wake up LPC_SYSCON->PDRUNCFG; LPC_SYSCON->PDSLEEPCFG = BF_PDSLEEPCFG_WDT; // Configure deep sleep with WDT oscillator LPC_TMR16B0->TCR = BF_TIMER_TCR_RESET; // reset timer // The following lines initializing PR and MR0 are at risk for overflow // if the timing and frequency parameters are changed because they are // using a 16-bit timer. #ifndef DEBUG LPC_TMR16B0->PR = 0; LPC_TMR16B0->MR0 = (SLEEPDURATION_MS*MeasureWDO()/WDOMEASUREDURATION_MS - WUTIME_CLOCKS - 1); #else LPC_TMR16B0->PR = (MainClockFrequency / WDTClockFrequency) -1; LPC_TMR16B0->MR0 = SLEEPDURATION_MS*WDTClockFrequency/1000; #endif LPC_TMR16B0->MCR = BF_TIMER_MCR_MATCHSTOP0 | BF_TIMER_MCR_MATCHRESET0; LPC_IOCON->PIO0_8 = (LPC_IOCON->PIO0_8 & ~0x3F) | 0x2; // Set IOCON register on P0.8 to match function /* Configure Wakeup I/O */ /* Specify the start logic to allow the chip to be waken up using PIO0_8 */ LPC_SYSCON->STARTAPRP0 |= BF_STARTLOGIC_P0_8; // Rising edge LPC_SYSCON->STARTRSRP0CLR = BF_STARTLOGIC_P0_8; // Clear pending bit LPC_SYSCON->STARTERP0 |= BF_STARTLOGIC_P0_8; // Enable Start Logic NVIC_EnableIRQ(WAKEUP8_IRQn); }