deep sleep and wake-up. GPIO group interrupt, start logic sample see: http://mbed.org/users/okini3939/notebook/low-power-m0/

Dependencies:   mbed

Committer:
okini3939
Date:
Mon Oct 15 15:06:04 2012 +0000
Revision:
1:d676ec86101e
Parent:
0:ae90ac40c518
Child:
2:5412d5b8242c
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:ae90ac40c518 1 #include "mbed.h"
okini3939 0:ae90ac40c518 2
okini3939 0:ae90ac40c518 3 DigitalOut myled(LED1);
okini3939 0:ae90ac40c518 4
okini3939 0:ae90ac40c518 5 Serial pc(p9, p10);
okini3939 0:ae90ac40c518 6
okini3939 1:d676ec86101e 7
okini3939 1:d676ec86101e 8 extern "C"
okini3939 1:d676ec86101e 9 void GINT1_IRQHandler(void) {
okini3939 1:d676ec86101e 10 LPC_GPIO_GROUP_INT1->CTRL |= (1<<0);
okini3939 1:d676ec86101e 11 NVIC_ClearPendingIRQ(GINT0_IRQn);
okini3939 1:d676ec86101e 12 }
okini3939 1:d676ec86101e 13
okini3939 0:ae90ac40c518 14 void initStartLogic (unsigned int p0, unsigned int p1) {
okini3939 0:ae90ac40c518 15 LPC_SYSCON->SYSAHBCLKCTRL |= (1<<24);
okini3939 0:ae90ac40c518 16 LPC_GPIO_GROUP_INT1->PORT_POL[0] = 0;
okini3939 0:ae90ac40c518 17 LPC_GPIO_GROUP_INT1->PORT_POL[1] = 0;
okini3939 0:ae90ac40c518 18 LPC_GPIO_GROUP_INT1->PORT_ENA[0] = p0;
okini3939 0:ae90ac40c518 19 LPC_GPIO_GROUP_INT1->PORT_ENA[1] = p1;
okini3939 0:ae90ac40c518 20 LPC_GPIO_GROUP_INT1->CTRL = 0; // Edge, OR, INT
okini3939 0:ae90ac40c518 21
okini3939 0:ae90ac40c518 22 LPC_SYSCON->PDAWAKECFG = LPC_SYSCON->PDRUNCFG;
okini3939 0:ae90ac40c518 23
okini3939 0:ae90ac40c518 24 LPC_SYSCON->STARTERP1 = (1<<21); // start logic GROUP1
okini3939 0:ae90ac40c518 25 NVIC_ClearPendingIRQ(GINT1_IRQn);
okini3939 0:ae90ac40c518 26 NVIC_EnableIRQ(GINT1_IRQn);
okini3939 0:ae90ac40c518 27 }
okini3939 0:ae90ac40c518 28
okini3939 0:ae90ac40c518 29 unsigned int setSystemMainClock(unsigned short S) {
okini3939 0:ae90ac40c518 30
okini3939 0:ae90ac40c518 31 if ((S & 0x03) == 3) {
okini3939 0:ae90ac40c518 32 LPC_SYSCON->PDRUNCFG &= ~(1 << 5); /* Power-up System Osc */
okini3939 0:ae90ac40c518 33 for (int i = 0; i < 200; i++) __NOP();
okini3939 0:ae90ac40c518 34 }
okini3939 0:ae90ac40c518 35
okini3939 0:ae90ac40c518 36 LPC_SYSCON->MAINCLKSEL = S; /* Select PLL Clock Output */
okini3939 0:ae90ac40c518 37 LPC_SYSCON->MAINCLKUEN = 0x01; /* Update MCLK Clock Source */
okini3939 0:ae90ac40c518 38 LPC_SYSCON->MAINCLKUEN = 0x00; /* Toggle Update Register */
okini3939 0:ae90ac40c518 39 LPC_SYSCON->MAINCLKUEN = 0x01;
okini3939 0:ae90ac40c518 40 while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); /* Wait Until Updated */
okini3939 0:ae90ac40c518 41
okini3939 0:ae90ac40c518 42 if ((S & 0x03) == 0) {
okini3939 0:ae90ac40c518 43 LPC_SYSCON->PDRUNCFG |= (1 << 5); /* Power-down System Osc */
okini3939 0:ae90ac40c518 44 }
okini3939 0:ae90ac40c518 45
okini3939 0:ae90ac40c518 46 SystemCoreClockUpdate();
okini3939 0:ae90ac40c518 47 return SystemCoreClock;
okini3939 0:ae90ac40c518 48 }
okini3939 0:ae90ac40c518 49
okini3939 0:ae90ac40c518 50 unsigned int setSystemFrequency(unsigned short M, unsigned short P) {
okini3939 0:ae90ac40c518 51 LPC_SYSCON->SYSPLLCTRL = ((P & 0x03)<<5) | (M & 0x1f);
okini3939 0:ae90ac40c518 52 while (!(LPC_SYSCON->SYSPLLSTAT & 0x01)); // Wait Until PLL Locked
okini3939 0:ae90ac40c518 53 SystemCoreClockUpdate();
okini3939 0:ae90ac40c518 54 return SystemCoreClock;
okini3939 0:ae90ac40c518 55 }
okini3939 0:ae90ac40c518 56
okini3939 0:ae90ac40c518 57 void DeepSleep () {
okini3939 0:ae90ac40c518 58 SCB->SCR |= (1<<2);
okini3939 0:ae90ac40c518 59 __WFI();
okini3939 0:ae90ac40c518 60 }
okini3939 0:ae90ac40c518 61
okini3939 0:ae90ac40c518 62 void Sleep () {
okini3939 1:d676ec86101e 63 SCB->SCR &= ~(1<<2);
okini3939 0:ae90ac40c518 64 __WFI();
okini3939 0:ae90ac40c518 65 }
okini3939 0:ae90ac40c518 66
okini3939 0:ae90ac40c518 67 int main() {
okini3939 0:ae90ac40c518 68 int i;
okini3939 0:ae90ac40c518 69
okini3939 0:ae90ac40c518 70 // setSystemFrequency(3, 1); // M, P, 48MHz
okini3939 0:ae90ac40c518 71
okini3939 0:ae90ac40c518 72 pc.baud(9600);
okini3939 0:ae90ac40c518 73 pc.printf("low power\r\n");
okini3939 0:ae90ac40c518 74
okini3939 0:ae90ac40c518 75 for (i = 0; i < 6; i ++) {
okini3939 0:ae90ac40c518 76 myled = !myled;
okini3939 0:ae90ac40c518 77 wait(0.5);
okini3939 0:ae90ac40c518 78 }
okini3939 0:ae90ac40c518 79
okini3939 1:d676ec86101e 80 initStartLogic((1<<8)|(1<<1), 0);
okini3939 0:ae90ac40c518 81
okini3939 1:d676ec86101e 82 for (;;) {
okini3939 1:d676ec86101e 83
okini3939 1:d676ec86101e 84 pc.printf("enter sleep\r\n");
okini3939 1:d676ec86101e 85 wait_ms(10);
okini3939 1:d676ec86101e 86 setSystemMainClock(0);
okini3939 0:ae90ac40c518 87
okini3939 1:d676ec86101e 88 DeepSleep();
okini3939 0:ae90ac40c518 89
okini3939 1:d676ec86101e 90 setSystemMainClock(3);
okini3939 0:ae90ac40c518 91
okini3939 1:d676ec86101e 92 pc.printf("wake up\r\n");
okini3939 1:d676ec86101e 93 for (i = 0; i < 6; i ++) {
okini3939 1:d676ec86101e 94 myled = !myled;
okini3939 1:d676ec86101e 95 wait(0.5);
okini3939 1:d676ec86101e 96 }
okini3939 1:d676ec86101e 97
okini3939 0:ae90ac40c518 98 }
okini3939 0:ae90ac40c518 99 }