Minimal version of mBuino Sleep

This is a cut down version of mBuino_Sleep that doesn't control any IO lines.

This version should work fine on any NXP LPC11Uxx CPUs although you will need to check the external pin pullup/down status, any conflicts between external and internal pulls will increase power usage.

If using this version on an mBuino ensure you disable the pullup on P0_3 (on by default for any unconfigured GPIO lines) and turn all the LEDs off before entering sleep. Failing to do so will increase the sleep mode power draw.

See mBuino_Sleep for usage details.

Revision:
0:af76f9a302ea
Child:
1:219e1147c6fa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mBuinoSleep.cpp	Sun Sep 21 11:02:19 2014 +0000
@@ -0,0 +1,51 @@
+#include "mBuinoSleep.h"
+
+void mBuinoSleep(enum sleepMode_t mode)
+{
+    switch (mode) {
+        default:
+        case Sleep:
+            LPC_PMU->PCON = 0x0;
+            SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
+            __WFI();
+            break;
+
+        case DeepSleep:
+        case PowerDown: {
+            if (mode == PowerDown)
+                LPC_PMU->PCON = 0x2;
+            else
+                LPC_PMU->PCON = 0x1;
+
+            LPC_SYSCON->PDSLEEPCFG |= 0x7f;  // shut off everything we can.
+            SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
+
+            bool IRCPowered = (LPC_SYSCON->PDRUNCFG & 0x01); // only used for cleen shutdown but defined here for scope reasons.
+            if (!IRCPowered)
+                LPC_SYSCON->PDRUNCFG &= 0xfffffffe; // power up the IRC
+
+            if(sleep_CleanShutdown) {
+                LPC_SYSCON->MAINCLKSEL    = 0x00; // switch to IRC to avoid glitches
+                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       */
+            }
+
+            LPC_SYSCON->PDAWAKECFG = LPC_SYSCON->PDRUNCFG; // switch on everything that is currently on when we wake up.
+            __WFI();
+
+            if(sleep_CleanShutdown) {
+                LPC_SYSCON->MAINCLKSEL    = 0x03; // switch to PLL 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 (!IRCPowered)
+                LPC_SYSCON->PDRUNCFG |= 0x01; // power down the IRC if it was off before
+        }
+        break;
+    }
+}