Emily Wilson / Mbed 2 deprecated ECE4180Lab1

Dependencies:   mbed MCP23S17 PinDetect USBDevice

Committer:
emilywilson
Date:
Tue Jan 21 21:06:20 2020 +0000
Revision:
11:2cfdab516b21
power management extra credit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilywilson 11:2cfdab516b21 1 /* mbed PowerControl Library
emilywilson 11:2cfdab516b21 2 * Copyright (c) 2010 Michael Wei
emilywilson 11:2cfdab516b21 3 */
emilywilson 11:2cfdab516b21 4
emilywilson 11:2cfdab516b21 5 #ifndef MBED_POWERCONTROL_H
emilywilson 11:2cfdab516b21 6 #define MBED_POWERCONTROL_H
emilywilson 11:2cfdab516b21 7
emilywilson 11:2cfdab516b21 8 //shouldn't have to include, but fixes weird problems with defines
emilywilson 11:2cfdab516b21 9 //#include "LPC1768/LPC17xx.h"
emilywilson 11:2cfdab516b21 10
emilywilson 11:2cfdab516b21 11 //System Control Register
emilywilson 11:2cfdab516b21 12 // bit 0: Reserved
emilywilson 11:2cfdab516b21 13 // bit 1: Sleep on Exit
emilywilson 11:2cfdab516b21 14 #define LPC1768_SCR_SLEEPONEXIT 0x2
emilywilson 11:2cfdab516b21 15 // bit 2: Deep Sleep
emilywilson 11:2cfdab516b21 16 #define LPC1768_SCR_SLEEPDEEP 0x4
emilywilson 11:2cfdab516b21 17 // bit 3: Resereved
emilywilson 11:2cfdab516b21 18 // bit 4: Send on Pending
emilywilson 11:2cfdab516b21 19 #define LPC1768_SCR_SEVONPEND 0x10
emilywilson 11:2cfdab516b21 20 // bit 5-31: Reserved
emilywilson 11:2cfdab516b21 21
emilywilson 11:2cfdab516b21 22 //Power Control Register
emilywilson 11:2cfdab516b21 23 // bit 0: Power mode control bit 0 (power-down mode)
emilywilson 11:2cfdab516b21 24 #define LPC1768_PCON_PM0 0x1
emilywilson 11:2cfdab516b21 25 // bit 1: Power mode control bit 1 (deep power-down mode)
emilywilson 11:2cfdab516b21 26 #define LPC1768_PCON_PM1 0x2
emilywilson 11:2cfdab516b21 27 // bit 2: Brown-out reduced power mode
emilywilson 11:2cfdab516b21 28 #define LPC1768_PCON_BODRPM 0x4
emilywilson 11:2cfdab516b21 29 // bit 3: Brown-out global disable
emilywilson 11:2cfdab516b21 30 #define LPC1768_PCON_BOGD 0x8
emilywilson 11:2cfdab516b21 31 // bit 4: Brown-out reset disable
emilywilson 11:2cfdab516b21 32 #define LPC1768_PCON_BORD 0x10
emilywilson 11:2cfdab516b21 33 // bit 5-7 : Reserved
emilywilson 11:2cfdab516b21 34 // bit 8: Sleep Mode Entry Flag
emilywilson 11:2cfdab516b21 35 #define LPC1768_PCON_SMFLAG 0x100
emilywilson 11:2cfdab516b21 36 // bit 9: Deep Sleep Entry Flag
emilywilson 11:2cfdab516b21 37 #define LPC1768_PCON_DSFLAG 0x200
emilywilson 11:2cfdab516b21 38 // bit 10: Power Down Entry Flag
emilywilson 11:2cfdab516b21 39 #define LPC1768_PCON_PDFLAG 0x400
emilywilson 11:2cfdab516b21 40 // bit 11: Deep Power Down Entry Flag
emilywilson 11:2cfdab516b21 41 #define LPC1768_PCON_DPDFLAG 0x800
emilywilson 11:2cfdab516b21 42 // bit 12-31: Reserved
emilywilson 11:2cfdab516b21 43
emilywilson 11:2cfdab516b21 44 //"Sleep Mode" (WFI).
emilywilson 11:2cfdab516b21 45 inline void Sleep(void)
emilywilson 11:2cfdab516b21 46 {
emilywilson 11:2cfdab516b21 47 __WFI();
emilywilson 11:2cfdab516b21 48 }
emilywilson 11:2cfdab516b21 49
emilywilson 11:2cfdab516b21 50 //"Deep Sleep" Mode
emilywilson 11:2cfdab516b21 51 inline void DeepSleep(void)
emilywilson 11:2cfdab516b21 52 {
emilywilson 11:2cfdab516b21 53 SCB->SCR |= LPC1768_SCR_SLEEPDEEP;
emilywilson 11:2cfdab516b21 54 __WFI();
emilywilson 11:2cfdab516b21 55 }
emilywilson 11:2cfdab516b21 56
emilywilson 11:2cfdab516b21 57 //"Power-Down" Mode
emilywilson 11:2cfdab516b21 58 inline void PowerDown(void)
emilywilson 11:2cfdab516b21 59 {
emilywilson 11:2cfdab516b21 60 SCB->SCR |= LPC1768_SCR_SLEEPDEEP;
emilywilson 11:2cfdab516b21 61 LPC_SC->PCON &= ~LPC1768_PCON_PM1;
emilywilson 11:2cfdab516b21 62 LPC_SC->PCON |= LPC1768_PCON_PM0;
emilywilson 11:2cfdab516b21 63 __WFI();
emilywilson 11:2cfdab516b21 64 //reset back to normal
emilywilson 11:2cfdab516b21 65 LPC_SC->PCON &= ~(LPC1768_PCON_PM1 | LPC1768_PCON_PM0);
emilywilson 11:2cfdab516b21 66 }
emilywilson 11:2cfdab516b21 67
emilywilson 11:2cfdab516b21 68 //"Deep Power-Down" Mode
emilywilson 11:2cfdab516b21 69 inline void DeepPowerDown(void)
emilywilson 11:2cfdab516b21 70 {
emilywilson 11:2cfdab516b21 71 SCB->SCR |= LPC1768_SCR_SLEEPDEEP;
emilywilson 11:2cfdab516b21 72 LPC_SC->PCON |= LPC1768_PCON_PM1 | LPC1768_PCON_PM0;
emilywilson 11:2cfdab516b21 73 __WFI();
emilywilson 11:2cfdab516b21 74 //reset back to normal
emilywilson 11:2cfdab516b21 75 LPC_SC->PCON &= ~(LPC1768_PCON_PM1 | LPC1768_PCON_PM0);
emilywilson 11:2cfdab516b21 76 }
emilywilson 11:2cfdab516b21 77
emilywilson 11:2cfdab516b21 78 //shut down BOD during power-down/deep sleep
emilywilson 11:2cfdab516b21 79 inline void BrownOut_ReducedPowerMode_Enable(void)
emilywilson 11:2cfdab516b21 80 {
emilywilson 11:2cfdab516b21 81 LPC_SC->PCON |= LPC1768_PCON_BODRPM;
emilywilson 11:2cfdab516b21 82 }
emilywilson 11:2cfdab516b21 83
emilywilson 11:2cfdab516b21 84 //turn on BOD during power-down/deep sleep
emilywilson 11:2cfdab516b21 85 inline void BrownOut_ReducedPowerMode_Disable(void)
emilywilson 11:2cfdab516b21 86 {
emilywilson 11:2cfdab516b21 87 LPC_SC->PCON &= ~LPC1768_PCON_BODRPM;
emilywilson 11:2cfdab516b21 88 }
emilywilson 11:2cfdab516b21 89
emilywilson 11:2cfdab516b21 90 //turn off brown out circutry
emilywilson 11:2cfdab516b21 91 inline void BrownOut_Global_Disable(void)
emilywilson 11:2cfdab516b21 92 {
emilywilson 11:2cfdab516b21 93 LPC_SC->PCON |= LPC1768_PCON_BOGD;
emilywilson 11:2cfdab516b21 94 }
emilywilson 11:2cfdab516b21 95
emilywilson 11:2cfdab516b21 96 //turn on brown out circutry
emilywilson 11:2cfdab516b21 97 inline void BrownOut_Global_Enable(void)
emilywilson 11:2cfdab516b21 98 {
emilywilson 11:2cfdab516b21 99 LPC_SC->PCON &= !LPC1768_PCON_BOGD;
emilywilson 11:2cfdab516b21 100 }
emilywilson 11:2cfdab516b21 101
emilywilson 11:2cfdab516b21 102 //turn off brown out reset circutry
emilywilson 11:2cfdab516b21 103 inline void BrownOut_Reset_Disable(void)
emilywilson 11:2cfdab516b21 104 {
emilywilson 11:2cfdab516b21 105 LPC_SC->PCON |= LPC1768_PCON_BORD;
emilywilson 11:2cfdab516b21 106 }
emilywilson 11:2cfdab516b21 107
emilywilson 11:2cfdab516b21 108 //turn on brown outreset circutry
emilywilson 11:2cfdab516b21 109 inline void BrownOut_Reset_Enable(void)
emilywilson 11:2cfdab516b21 110 {
emilywilson 11:2cfdab516b21 111 LPC_SC->PCON &= ~LPC1768_PCON_BORD;
emilywilson 11:2cfdab516b21 112 }
emilywilson 11:2cfdab516b21 113 //Peripheral Control Register
emilywilson 11:2cfdab516b21 114 // bit 0: Reserved
emilywilson 11:2cfdab516b21 115 // bit 1: PCTIM0: Timer/Counter 0 power/clock enable
emilywilson 11:2cfdab516b21 116 #define LPC1768_PCONP_PCTIM0 0x2
emilywilson 11:2cfdab516b21 117 // bit 2: PCTIM1: Timer/Counter 1 power/clock enable
emilywilson 11:2cfdab516b21 118 #define LPC1768_PCONP_PCTIM1 0x4
emilywilson 11:2cfdab516b21 119 // bit 3: PCUART0: UART 0 power/clock enable
emilywilson 11:2cfdab516b21 120 #define LPC1768_PCONP_PCUART0 0x8
emilywilson 11:2cfdab516b21 121 // bit 4: PCUART1: UART 1 power/clock enable
emilywilson 11:2cfdab516b21 122 #define LPC1768_PCONP_PCUART1 0x10
emilywilson 11:2cfdab516b21 123 // bit 5: Reserved
emilywilson 11:2cfdab516b21 124 // bit 6: PCPWM1: PWM 1 power/clock enable
emilywilson 11:2cfdab516b21 125 #define LPC1768_PCONP_PCPWM1 0x40
emilywilson 11:2cfdab516b21 126 // bit 7: PCI2C0: I2C interface 0 power/clock enable
emilywilson 11:2cfdab516b21 127 #define LPC1768_PCONP_PCI2C0 0x80
emilywilson 11:2cfdab516b21 128 // bit 8: PCSPI: SPI interface power/clock enable
emilywilson 11:2cfdab516b21 129 #define LPC1768_PCONP_PCSPI 0x100
emilywilson 11:2cfdab516b21 130 // bit 9: PCRTC: RTC power/clock enable
emilywilson 11:2cfdab516b21 131 #define LPC1768_PCONP_PCRTC 0x200
emilywilson 11:2cfdab516b21 132 // bit 10: PCSSP1: SSP interface 1 power/clock enable
emilywilson 11:2cfdab516b21 133 #define LPC1768_PCONP_PCSSP1 0x400
emilywilson 11:2cfdab516b21 134 // bit 11: Reserved
emilywilson 11:2cfdab516b21 135 // bit 12: PCADC: A/D converter power/clock enable
emilywilson 11:2cfdab516b21 136 #define LPC1768_PCONP_PCADC 0x1000
emilywilson 11:2cfdab516b21 137 // bit 13: PCCAN1: CAN controller 1 power/clock enable
emilywilson 11:2cfdab516b21 138 #define LPC1768_PCONP_PCCAN1 0x2000
emilywilson 11:2cfdab516b21 139 // bit 14: PCCAN2: CAN controller 2 power/clock enable
emilywilson 11:2cfdab516b21 140 #define LPC1768_PCONP_PCCAN2 0x4000
emilywilson 11:2cfdab516b21 141 // bit 15: PCGPIO: GPIOs power/clock enable
emilywilson 11:2cfdab516b21 142 #define LPC1768_PCONP_PCGPIO 0x8000
emilywilson 11:2cfdab516b21 143 // bit 16: PCRIT: Repetitive interrupt timer power/clock enable
emilywilson 11:2cfdab516b21 144 #define LPC1768_PCONP_PCRIT 0x10000
emilywilson 11:2cfdab516b21 145 // bit 17: PCMCPWM: Motor control PWM power/clock enable
emilywilson 11:2cfdab516b21 146 #define LPC1768_PCONP_PCMCPWM 0x20000
emilywilson 11:2cfdab516b21 147 // bit 18: PCQEI: Quadrature encoder interface power/clock enable
emilywilson 11:2cfdab516b21 148 #define LPC1768_PCONP_PCQEI 0x40000
emilywilson 11:2cfdab516b21 149 // bit 19: PCI2C1: I2C interface 1 power/clock enable
emilywilson 11:2cfdab516b21 150 #define LPC1768_PCONP_PCI2C1 0x80000
emilywilson 11:2cfdab516b21 151 // bit 20: Reserved
emilywilson 11:2cfdab516b21 152 // bit 21: PCSSP0: SSP interface 0 power/clock enable
emilywilson 11:2cfdab516b21 153 #define LPC1768_PCONP_PCSSP0 0x200000
emilywilson 11:2cfdab516b21 154 // bit 22: PCTIM2: Timer 2 power/clock enable
emilywilson 11:2cfdab516b21 155 #define LPC1768_PCONP_PCTIM2 0x400000
emilywilson 11:2cfdab516b21 156 // bit 23: PCTIM3: Timer 3 power/clock enable
emilywilson 11:2cfdab516b21 157 #define LPC1768_PCONP_PCQTIM3 0x800000
emilywilson 11:2cfdab516b21 158 // bit 24: PCUART2: UART 2 power/clock enable
emilywilson 11:2cfdab516b21 159 #define LPC1768_PCONP_PCUART2 0x1000000
emilywilson 11:2cfdab516b21 160 // bit 25: PCUART3: UART 3 power/clock enable
emilywilson 11:2cfdab516b21 161 #define LPC1768_PCONP_PCUART3 0x2000000
emilywilson 11:2cfdab516b21 162 // bit 26: PCI2C2: I2C interface 2 power/clock enable
emilywilson 11:2cfdab516b21 163 #define LPC1768_PCONP_PCI2C2 0x4000000
emilywilson 11:2cfdab516b21 164 // bit 27: PCI2S: I2S interface power/clock enable
emilywilson 11:2cfdab516b21 165 #define LPC1768_PCONP_PCI2S 0x8000000
emilywilson 11:2cfdab516b21 166 // bit 28: Reserved
emilywilson 11:2cfdab516b21 167 // bit 29: PCGPDMA: GP DMA function power/clock enable
emilywilson 11:2cfdab516b21 168 #define LPC1768_PCONP_PCGPDMA 0x20000000
emilywilson 11:2cfdab516b21 169 // bit 30: PCENET: Ethernet block power/clock enable
emilywilson 11:2cfdab516b21 170 #define LPC1768_PCONP_PCENET 0x40000000
emilywilson 11:2cfdab516b21 171 // bit 31: PCUSB: USB interface power/clock enable
emilywilson 11:2cfdab516b21 172 #define LPC1768_PCONP_PCUSB 0x80000000
emilywilson 11:2cfdab516b21 173
emilywilson 11:2cfdab516b21 174 //Powers Up specified Peripheral(s)
emilywilson 11:2cfdab516b21 175 inline unsigned int Peripheral_PowerUp(unsigned int bitMask)
emilywilson 11:2cfdab516b21 176 {
emilywilson 11:2cfdab516b21 177 return LPC_SC->PCONP |= bitMask;
emilywilson 11:2cfdab516b21 178 }
emilywilson 11:2cfdab516b21 179
emilywilson 11:2cfdab516b21 180 //Powers Down specified Peripheral(s)
emilywilson 11:2cfdab516b21 181 inline unsigned int Peripheral_PowerDown(unsigned int bitMask)
emilywilson 11:2cfdab516b21 182 {
emilywilson 11:2cfdab516b21 183 return LPC_SC->PCONP &= ~bitMask;
emilywilson 11:2cfdab516b21 184 }
emilywilson 11:2cfdab516b21 185
emilywilson 11:2cfdab516b21 186 //returns if the peripheral is on or off
emilywilson 11:2cfdab516b21 187 inline bool Peripheral_GetStatus(unsigned int peripheral)
emilywilson 11:2cfdab516b21 188 {
emilywilson 11:2cfdab516b21 189 return (LPC_SC->PCONP & peripheral) ? true : false;
emilywilson 11:2cfdab516b21 190 }
emilywilson 11:2cfdab516b21 191
emilywilson 11:2cfdab516b21 192 #endif