a

Dependencies:   mbed

Fork of DeepSleep-m0 by Suga koubou

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Mon Jun 10 02:13:58 2013 +0000
Parent:
1:d676ec86101e
Commit message:
fix

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r d676ec86101e -r 5412d5b8242c main.cpp
--- a/main.cpp	Mon Oct 15 15:06:04 2012 +0000
+++ b/main.cpp	Mon Jun 10 02:13:58 2013 +0000
@@ -1,9 +1,8 @@
 #include "mbed.h"
 
-DigitalOut myled(LED1);
+DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
 
-Serial pc(p9, p10);
-
+Serial pc(USBTX, USBRX);
 
 extern "C"
 void GINT1_IRQHandler(void) {
@@ -27,22 +26,12 @@
 }
 
 unsigned int setSystemMainClock(unsigned short S) {
-    
-    if ((S & 0x03) == 3) {
-        LPC_SYSCON->PDRUNCFG     &= ~(1 << 5);          /* Power-up System Osc      */
-        for (int i = 0; i < 200; i++) __NOP();
-    }
-    
     LPC_SYSCON->MAINCLKSEL    = S;     /* Select PLL Clock Output  */
     LPC_SYSCON->MAINCLKUEN    = 0x01;               /* Update MCLK Clock Source */
     LPC_SYSCON->MAINCLKUEN    = 0x00;               /* Toggle Update Register   */
     LPC_SYSCON->MAINCLKUEN    = 0x01;
     while (!(LPC_SYSCON->MAINCLKUEN & 0x01));       /* Wait Until Updated       */
 
-    if ((S & 0x03) == 0) {
-        LPC_SYSCON->PDRUNCFG     |= (1 << 5);          /* Power-down System Osc      */
-    }
-
     SystemCoreClockUpdate();
     return SystemCoreClock;
 }
@@ -54,12 +43,22 @@
     return SystemCoreClock;
 }
 
+void PowerDown () {
+    LPC_SYSCON->PDAWAKECFG = LPC_SYSCON->PDRUNCFG;
+    LPC_PMU->PCON = (2<<0);
+    SCB->SCR |= (1<<2);
+    __WFI();
+}
+
 void DeepSleep () {
+    LPC_SYSCON->PDAWAKECFG = LPC_SYSCON->PDRUNCFG;
+    LPC_PMU->PCON = (1<<0);
     SCB->SCR |= (1<<2);
     __WFI();
 }
 
 void Sleep () {
+    LPC_PMU->PCON = 0;
     SCB->SCR &= ~(1<<2);
     __WFI();
 }
@@ -68,32 +67,42 @@
     int i;
 
 //    setSystemFrequency(3, 1); // M, P, 48MHz
+//    setSystemFrequency(1, 2); // M, P, 24MHz
+
+    LPC_GPIO->SET[1] = (1<<26);
+    LPC_GPIO->DIR[1] |= (1<<26);
 
     pc.baud(9600);
     pc.printf("low power\r\n");
 
     for (i = 0; i < 6; i ++) {
-        myled = !myled;
-        wait(0.5);
+        led1 = !led1;
+        wait(0.2);
     }
 
-    initStartLogic((1<<8)|(1<<1), 0);
+    initStartLogic((1<<22)|(1<<1), 0); // p20(P0.22), ISP(P0.1)
 
     for (;;) {
     
         pc.printf("enter sleep\r\n");
         wait_ms(10);
-        setSystemMainClock(0);
 
-        DeepSleep();
+        setSystemMainClock(0); // IRC clock
+        LPC_GPIO->CLR[1] = (1<<26);
+//        Sleep();
+//        DeepSleep();
+        PowerDown();
 
-        setSystemMainClock(3);
+        LPC_GPIO->SET[1] = (1<<26);
+        setSystemMainClock(3); // PLL clock
 
         pc.printf("wake up\r\n");
         for (i = 0; i < 6; i ++) {
-            myled = !myled;
-            wait(0.5);
+            led1 = !led1;
+            wait(0.2);
         }
 
+        for (;;);
+
     }
 }