Modified WakeUp program to run on STM32L152RE
Dependencies: mbed LPC1114_WakeInterruptIn
Fork of WakeUp by
Diff: Device/WakeUp_LPC11XX.cpp
- Revision:
- 11:72db657fc572
- Parent:
- 10:c41bc9154a7c
- Child:
- 12:779d866b8a2d
--- a/Device/WakeUp_LPC11XX.cpp Mon Jul 28 19:46:03 2014 +0000 +++ b/Device/WakeUp_LPC11XX.cpp Wed Jul 30 09:53:58 2014 +0000 @@ -4,23 +4,16 @@ #ifdef TARGET_LPC11XX_11CXX -//dp1 or dp2 can be chosen -#define WAKEUP_PIN dp1 -//#define WAKEUP_PIN dp2 - -#define WAKEUP_MATCH (((WAKEUP_PIN >> PIN_SHIFT) & 0xF) - 8) -#define WAKEUP_CHANNEL ((WAKEUP_PIN >> PIN_SHIFT) & 0xF) - -#define WAKEUP_TIMER LPC_TMR16B0 -#define WAKEUP_TIMER_EN 7 - -#define SYSAHBCLKCTRL_SLEEP (0x15 | (1<<WAKEUP_TIMER_EN)) - #include "WakeUp.h" #include "WakeInterruptIn.h" -WakeInterruptIn IRQ_in(WAKEUP_PIN); -PwmOut pulse_out(WAKEUP_PIN); +//Pin used, allowed pins = P0_1 (dp24, default), P0_8 (dp1) and P0_9 (dp2) +//By defining WakeUpPin in for example your main.cpp this can be overridden +WEAK PinName WakeUpPin = dp24; +extern PinName WakeUpPin; + +WakeInterruptIn IRQ_in(WakeUpPin); +PwmOut pulse_out(WakeUpPin); FunctionPointer WakeUp::callback; float WakeUp::cycles_per_ms = 20.0; @@ -28,19 +21,42 @@ static uint32_t old_clk_sel = ~0; static uint32_t SYSAHBCLKCTRL; static uint32_t TCR, PR, MR3; +static LPC_TMR_TypeDef *WakeUpTimer; +static uint32_t SYSAHBCLKCTRL_Sleep; +static uint8_t WakeUpTimer_Match; static inline void restore(void); void WakeUp::set_ms(uint32_t ms) { - if (ms != 0) { - if (old_clk_sel == ~0) { //Only during first run - old_clk_sel = LPC_SYSCON->MAINCLKSEL; - SYSAHBCLKCTRL = LPC_SYSCON->SYSAHBCLKCTRL; - } + if (old_clk_sel == ~0) { //Only during first run + old_clk_sel = LPC_SYSCON->MAINCLKSEL; + SYSAHBCLKCTRL = LPC_SYSCON->SYSAHBCLKCTRL; - if (LPC_SYSCON->SYSAHBCLKCTRL != SYSAHBCLKCTRL_SLEEP) //Always when it is different from sleep settings + switch(WakeUpPin) { + case dp24: + WakeUpTimer = LPC_TMR32B0; + SYSAHBCLKCTRL_Sleep = 0x15 | (1<<9); + WakeUpTimer_Match = 2; + break; + case dp1: + WakeUpTimer = LPC_TMR16B0; + SYSAHBCLKCTRL_Sleep = 0x15 | (1<<7); + WakeUpTimer_Match = 0; + break; + case dp2: + WakeUpTimer = LPC_TMR16B0; + SYSAHBCLKCTRL_Sleep = 0x15 | (1<<7); + WakeUpTimer_Match = 1; + break; + default: + error("Invalid WakeUp pin, choose dp1, dp2 or dp24"); + } + } + + if (ms != 0) { + if (LPC_SYSCON->SYSAHBCLKCTRL != SYSAHBCLKCTRL_Sleep) //Always when it is different from sleep settings SYSAHBCLKCTRL = LPC_SYSCON->SYSAHBCLKCTRL; LPC_SYSCON->PDRUNCFG &= ~PDRUNCFG_WDTOSC_PD; @@ -50,23 +66,23 @@ LPC_SYSCON->WDTOSCCTRL = 14 | (1<<5); //Store old PWM - TCR = WAKEUP_TIMER->TCR; - PR = WAKEUP_TIMER->PR; - MR3 = WAKEUP_TIMER->MR3; + TCR = WakeUpTimer->TCR; + PR = WakeUpTimer->PR; + MR3 = WakeUpTimer->MR3; //Setup PWM - WAKEUP_TIMER->TCR = TMR16B0TCR_CRST; + WakeUpTimer->TCR = TMR16B0TCR_CRST; uint32_t ticks = (float)ms * cycles_per_ms; //whatever timer it is, we treat it as 16-bit (with PR that is 32-bit still, do the math, it is enough for this) - WAKEUP_TIMER->PR = ticks >> 16; - WAKEUP_TIMER->MR[WAKEUP_MATCH] = ticks / ((ticks >> 16) + 1); - WAKEUP_TIMER->MR3 = 0xFFFF; + WakeUpTimer->PR = ticks >> 16; + WakeUpTimer->MR[WakeUpTimer_Match] = ticks / ((ticks >> 16) + 1); + WakeUpTimer->MR3 = 0xFFFF; IRQ_in.rise(irq_handler); //Disable most peripherals - LPC_SYSCON->SYSAHBCLKCTRL = SYSAHBCLKCTRL_SLEEP; + LPC_SYSCON->SYSAHBCLKCTRL = SYSAHBCLKCTRL_Sleep; //Switch clock to WD OSC LPC_SYSCON->MAINCLKSEL = 0x2; @@ -74,7 +90,7 @@ LPC_SYSCON->MAINCLKUEN = MAINCLKUEN_ENA; //Enable PWM: - WAKEUP_TIMER->TCR = TMR16B0TCR_CEN; + WakeUpTimer->TCR = TMR16B0TCR_CEN; } else { //Else restore normal settings restore(); @@ -94,7 +110,7 @@ static inline void restore(void) { - WAKEUP_TIMER->MR[WAKEUP_MATCH] = 0xFFFF; + WakeUpTimer->MR[WakeUpTimer_Match] = 0xFFFFFFFF; if (old_clk_sel == 3) //Was running on PLL while(LPC_SYSCON->SYSPLLSTAT != SYSPLLSTAT_LOCK); @@ -109,9 +125,9 @@ LPC_SYSCON->SYSAHBCLKCTRL = SYSAHBCLKCTRL; - WAKEUP_TIMER->MR3 = MR3; - WAKEUP_TIMER->PR = PR; - WAKEUP_TIMER->TCR = TCR; + WakeUpTimer->MR3 = MR3; + WakeUpTimer->PR = PR; + WakeUpTimer->TCR = TCR; } #endif