This is the code used on my video series "Hybrid Supercapacitor Car Battery" for my own hardware monitoring system. THe videos can be found on madelectronengineering.com
Dependencies: BurstSPI Fonts INA219 mbed LPC1114_WakeInterruptIn
Fork of SharpMemoryLCD by
Device/WakeUp_LPC11u24.cpp@2:0c49a8f32f6e, 2017-12-26 (annotated)
- Committer:
- madelectroneng
- Date:
- Tue Dec 26 21:30:09 2017 +0000
- Revision:
- 2:0c49a8f32f6e
Inital setup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madelectroneng | 2:0c49a8f32f6e | 1 | /** |
madelectroneng | 2:0c49a8f32f6e | 2 | Due to lack of another option for the LPC11u24 the watchdog timer is used as wakeup source. |
madelectroneng | 2:0c49a8f32f6e | 3 | Since if the reset on watchdog event bit is set, I cannot remove it again, this means if you also got watchdog code running |
madelectroneng | 2:0c49a8f32f6e | 4 | the most likely result is that it just resets your board. |
madelectroneng | 2:0c49a8f32f6e | 5 | **/ |
madelectroneng | 2:0c49a8f32f6e | 6 | |
madelectroneng | 2:0c49a8f32f6e | 7 | |
madelectroneng | 2:0c49a8f32f6e | 8 | #ifdef TARGET_LPC11U24 |
madelectroneng | 2:0c49a8f32f6e | 9 | |
madelectroneng | 2:0c49a8f32f6e | 10 | #include "WakeUp.h" |
madelectroneng | 2:0c49a8f32f6e | 11 | |
madelectroneng | 2:0c49a8f32f6e | 12 | Callback<void()> WakeUp::callback; |
madelectroneng | 2:0c49a8f32f6e | 13 | float WakeUp::cycles_per_ms = 5.0; |
madelectroneng | 2:0c49a8f32f6e | 14 | |
madelectroneng | 2:0c49a8f32f6e | 15 | void WakeUp::set_ms(uint32_t ms) |
madelectroneng | 2:0c49a8f32f6e | 16 | { |
madelectroneng | 2:0c49a8f32f6e | 17 | if (ms != 0) { |
madelectroneng | 2:0c49a8f32f6e | 18 | LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000; |
madelectroneng | 2:0c49a8f32f6e | 19 | LPC_SYSCON->PDRUNCFG &= ~(1<<6); |
madelectroneng | 2:0c49a8f32f6e | 20 | LPC_SYSCON->PDSLEEPCFG &= ~(1<<6); |
madelectroneng | 2:0c49a8f32f6e | 21 | LPC_SYSCON->STARTERP1 |= 1<<12; |
madelectroneng | 2:0c49a8f32f6e | 22 | |
madelectroneng | 2:0c49a8f32f6e | 23 | //Set oscillator for 20kHz = 5kHz after divide by 4 in WDT |
madelectroneng | 2:0c49a8f32f6e | 24 | LPC_SYSCON->WDTOSCCTRL = 14 | (1<<5); |
madelectroneng | 2:0c49a8f32f6e | 25 | |
madelectroneng | 2:0c49a8f32f6e | 26 | LPC_WWDT->MOD = 1; //Enable WDT |
madelectroneng | 2:0c49a8f32f6e | 27 | LPC_WWDT->TC = (uint32_t)((float)ms * cycles_per_ms); |
madelectroneng | 2:0c49a8f32f6e | 28 | LPC_WWDT->CLKSEL = 1; //WDTOSC |
madelectroneng | 2:0c49a8f32f6e | 29 | LPC_WWDT->WARNINT = 0; |
madelectroneng | 2:0c49a8f32f6e | 30 | |
madelectroneng | 2:0c49a8f32f6e | 31 | NVIC_SetVector(WDT_IRQn, (uint32_t)WakeUp::irq_handler); |
madelectroneng | 2:0c49a8f32f6e | 32 | |
madelectroneng | 2:0c49a8f32f6e | 33 | //Feeeeeed me |
madelectroneng | 2:0c49a8f32f6e | 34 | LPC_WWDT->FEED = 0xAA; |
madelectroneng | 2:0c49a8f32f6e | 35 | LPC_WWDT->FEED = 0x55; |
madelectroneng | 2:0c49a8f32f6e | 36 | |
madelectroneng | 2:0c49a8f32f6e | 37 | NVIC_EnableIRQ(WDT_IRQn); |
madelectroneng | 2:0c49a8f32f6e | 38 | } else |
madelectroneng | 2:0c49a8f32f6e | 39 | NVIC_DisableIRQ(WDT_IRQn); |
madelectroneng | 2:0c49a8f32f6e | 40 | |
madelectroneng | 2:0c49a8f32f6e | 41 | } |
madelectroneng | 2:0c49a8f32f6e | 42 | |
madelectroneng | 2:0c49a8f32f6e | 43 | void WakeUp::irq_handler(void) |
madelectroneng | 2:0c49a8f32f6e | 44 | { |
madelectroneng | 2:0c49a8f32f6e | 45 | LPC_WWDT->MOD = 1<<3; |
madelectroneng | 2:0c49a8f32f6e | 46 | callback.call(); |
madelectroneng | 2:0c49a8f32f6e | 47 | } |
madelectroneng | 2:0c49a8f32f6e | 48 | |
madelectroneng | 2:0c49a8f32f6e | 49 | void WakeUp::calibrate(void) |
madelectroneng | 2:0c49a8f32f6e | 50 | { |
madelectroneng | 2:0c49a8f32f6e | 51 | cycles_per_ms = 5.0; |
madelectroneng | 2:0c49a8f32f6e | 52 | set_ms(1100); |
madelectroneng | 2:0c49a8f32f6e | 53 | wait_ms(10); //Give time to sync |
madelectroneng | 2:0c49a8f32f6e | 54 | uint32_t count1 = LPC_WWDT->TV; |
madelectroneng | 2:0c49a8f32f6e | 55 | wait_ms(100); |
madelectroneng | 2:0c49a8f32f6e | 56 | uint32_t count2 = LPC_WWDT->TV; |
madelectroneng | 2:0c49a8f32f6e | 57 | set_ms(0); |
madelectroneng | 2:0c49a8f32f6e | 58 | count1 = count1 - count2; |
madelectroneng | 2:0c49a8f32f6e | 59 | |
madelectroneng | 2:0c49a8f32f6e | 60 | cycles_per_ms = count1 / 100.0; |
madelectroneng | 2:0c49a8f32f6e | 61 | } |
madelectroneng | 2:0c49a8f32f6e | 62 | |
madelectroneng | 2:0c49a8f32f6e | 63 | #endif |