lib
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32620/sleep.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /******************************************************************************* |
<> | 149:156823d33999 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 149:156823d33999 | 5 | * copy of this software and associated documentation files (the "Software"), |
<> | 149:156823d33999 | 6 | * to deal in the Software without restriction, including without limitation |
<> | 149:156823d33999 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 149:156823d33999 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 149:156823d33999 | 9 | * Software is furnished to do so, subject to the following conditions: |
<> | 149:156823d33999 | 10 | * |
<> | 149:156823d33999 | 11 | * The above copyright notice and this permission notice shall be included |
<> | 149:156823d33999 | 12 | * in all copies or substantial portions of the Software. |
<> | 149:156823d33999 | 13 | * |
<> | 149:156823d33999 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 149:156823d33999 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 149:156823d33999 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 149:156823d33999 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 149:156823d33999 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 149:156823d33999 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 149:156823d33999 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 149:156823d33999 | 21 | * |
<> | 149:156823d33999 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 149:156823d33999 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 149:156823d33999 | 24 | * Products, Inc. Branding Policy. |
<> | 149:156823d33999 | 25 | * |
<> | 149:156823d33999 | 26 | * The mere transfer of this software does not imply any licenses |
<> | 149:156823d33999 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 149:156823d33999 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 149:156823d33999 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 149:156823d33999 | 30 | * ownership rights. |
<> | 149:156823d33999 | 31 | ******************************************************************************* |
<> | 149:156823d33999 | 32 | */ |
<> | 149:156823d33999 | 33 | |
<> | 149:156823d33999 | 34 | #include "sleep_api.h" |
<> | 149:156823d33999 | 35 | #include "pwrman_regs.h" |
<> | 149:156823d33999 | 36 | #include "pwrseq_regs.h" |
<> | 149:156823d33999 | 37 | #include "clkman_regs.h" |
<> | 149:156823d33999 | 38 | #include "ioman_regs.h" |
<> | 149:156823d33999 | 39 | #include "rtc_regs.h" |
<> | 149:156823d33999 | 40 | #include "usb_regs.h" |
<> | 149:156823d33999 | 41 | |
<> | 149:156823d33999 | 42 | #define REVISION_A3 2 |
<> | 149:156823d33999 | 43 | #define REVISION_A4 3 |
<> | 149:156823d33999 | 44 | |
<> | 149:156823d33999 | 45 | // USB state to be restored upon wakeup |
<> | 149:156823d33999 | 46 | typedef struct { |
<> | 149:156823d33999 | 47 | uint32_t dev_cn; |
<> | 149:156823d33999 | 48 | uint32_t dev_inten; |
<> | 149:156823d33999 | 49 | uint32_t ep_base; |
<> | 149:156823d33999 | 50 | uint32_t ep[MXC_USB_NUM_EP]; |
<> | 149:156823d33999 | 51 | } usb_state_t; |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | static mxc_uart_regs_t *stdio_uart = (mxc_uart_regs_t*)STDIO_UART; |
<> | 149:156823d33999 | 54 | static int restore_usb; |
<> | 149:156823d33999 | 55 | static usb_state_t usb_state; |
<> | 149:156823d33999 | 56 | |
<> | 149:156823d33999 | 57 | void sleep(void) |
<> | 149:156823d33999 | 58 | { |
<> | 149:156823d33999 | 59 | // Normal sleep mode for ARM core |
<> | 149:156823d33999 | 60 | SCB->SCR = 0; |
<> | 149:156823d33999 | 61 | |
<> | 149:156823d33999 | 62 | __DSB(); |
<> | 149:156823d33999 | 63 | __WFI(); |
<> | 149:156823d33999 | 64 | } |
<> | 149:156823d33999 | 65 | |
<> | 149:156823d33999 | 66 | static void usb_sleep(void) |
<> | 149:156823d33999 | 67 | { |
<> | 149:156823d33999 | 68 | int i; |
<> | 149:156823d33999 | 69 | |
<> | 149:156823d33999 | 70 | if (MXC_USB->cn & MXC_F_USB_CN_USB_EN) { |
<> | 149:156823d33999 | 71 | // The USB module will not survive Deep Sleep. |
<> | 149:156823d33999 | 72 | |
<> | 149:156823d33999 | 73 | // Save the USB state to restore it upon wakeup |
<> | 149:156823d33999 | 74 | usb_state.dev_cn = MXC_USB->dev_cn; |
<> | 149:156823d33999 | 75 | usb_state.dev_inten = MXC_USB->dev_inten; |
<> | 149:156823d33999 | 76 | usb_state.ep_base = MXC_USB->ep_base; |
<> | 149:156823d33999 | 77 | for (i = 0; i < MXC_USB_NUM_EP; i++) { |
<> | 149:156823d33999 | 78 | usb_state.ep[i] = MXC_USB->ep[i] & (MXC_F_USB_EP_DIR | MXC_F_USB_EP_BUF2 | MXC_F_USB_EP_INT_EN | MXC_F_USB_EP_NAK_EN); |
<> | 149:156823d33999 | 79 | } |
<> | 149:156823d33999 | 80 | restore_usb = 1; |
<> | 149:156823d33999 | 81 | |
<> | 149:156823d33999 | 82 | // Shut down the USB module. |
<> | 149:156823d33999 | 83 | MXC_USB->dev_inten = 0; |
<> | 149:156823d33999 | 84 | MXC_USB->dev_cn = 0; |
<> | 149:156823d33999 | 85 | MXC_USB->cn = 0; |
<> | 149:156823d33999 | 86 | restore_usb = 1; // USB should be restored upon wakeup |
<> | 149:156823d33999 | 87 | } else { |
<> | 149:156823d33999 | 88 | restore_usb = 0; |
<> | 149:156823d33999 | 89 | } |
<> | 149:156823d33999 | 90 | } |
<> | 149:156823d33999 | 91 | |
<> | 149:156823d33999 | 92 | // Restore the USB module state. |
<> | 149:156823d33999 | 93 | static void usb_wakeup(void) |
<> | 149:156823d33999 | 94 | { |
<> | 149:156823d33999 | 95 | int i; |
<> | 149:156823d33999 | 96 | |
<> | 149:156823d33999 | 97 | if (restore_usb) { |
<> | 149:156823d33999 | 98 | MXC_USB->cn = MXC_F_USB_CN_USB_EN; |
<> | 149:156823d33999 | 99 | MXC_USB->dev_cn = MXC_F_USB_DEV_CN_URST; |
<> | 149:156823d33999 | 100 | MXC_USB->dev_cn = 0; |
<> | 149:156823d33999 | 101 | for (i = 0; i < MXC_USB_NUM_EP; i++) { |
<> | 149:156823d33999 | 102 | MXC_USB->ep[i] = usb_state.ep[i]; |
<> | 149:156823d33999 | 103 | } |
<> | 149:156823d33999 | 104 | MXC_USB->ep_base = usb_state.ep_base; |
<> | 149:156823d33999 | 105 | MXC_USB->dev_cn = usb_state.dev_cn; |
<> | 149:156823d33999 | 106 | MXC_USB->dev_inten = usb_state.dev_inten; |
<> | 149:156823d33999 | 107 | restore_usb = 0; |
<> | 149:156823d33999 | 108 | } |
<> | 149:156823d33999 | 109 | } |
<> | 149:156823d33999 | 110 | |
<> | 149:156823d33999 | 111 | // Low-power stop mode |
<> | 149:156823d33999 | 112 | void deepsleep(void) |
<> | 149:156823d33999 | 113 | { |
<> | 149:156823d33999 | 114 | unsigned int part_rev = MXC_PWRMAN->mask_id0 & MXC_F_PWRMAN_MASK_ID0_REVISION_ID; |
<> | 149:156823d33999 | 115 | |
<> | 149:156823d33999 | 116 | // Deep Sleep is not working properly on Revisions A3 and earlier |
<> | 149:156823d33999 | 117 | if (part_rev <= REVISION_A3) { |
<> | 149:156823d33999 | 118 | sleep(); |
<> | 149:156823d33999 | 119 | return; |
<> | 149:156823d33999 | 120 | } |
<> | 149:156823d33999 | 121 | |
<> | 149:156823d33999 | 122 | // Wait for all STDIO characters to be sent. The UART clock will stop. |
<> | 149:156823d33999 | 123 | while ((stdio_uart->tx_fifo_ctrl & MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY) || |
<> | 149:156823d33999 | 124 | !(stdio_uart->intfl & MXC_F_UART_INTFL_TX_DONE)); |
<> | 149:156823d33999 | 125 | |
<> | 149:156823d33999 | 126 | __disable_irq(); |
<> | 149:156823d33999 | 127 | |
<> | 149:156823d33999 | 128 | // Do not enter Deep Sleep if connected to VBUS |
<> | 149:156823d33999 | 129 | if (MXC_USB->dev_intfl & MXC_F_USB_DEV_INTFL_VBUS_ST) { |
<> | 149:156823d33999 | 130 | __enable_irq(); |
<> | 149:156823d33999 | 131 | sleep(); |
<> | 149:156823d33999 | 132 | return; |
<> | 149:156823d33999 | 133 | } |
<> | 149:156823d33999 | 134 | |
<> | 149:156823d33999 | 135 | // The USB module will not survive Deep Sleep. Shut it down. |
<> | 149:156823d33999 | 136 | usb_sleep(); |
<> | 149:156823d33999 | 137 | |
<> | 149:156823d33999 | 138 | // Make sure RTC is not pending before sleeping, if its still synchronizing |
<> | 149:156823d33999 | 139 | // we might not wake up. |
<> | 149:156823d33999 | 140 | while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING); |
<> | 149:156823d33999 | 141 | |
<> | 149:156823d33999 | 142 | // Clear any active GPIO Wake Up Events |
<> | 149:156823d33999 | 143 | if (MXC_PWRSEQ->flags & MXC_F_PWRSEQ_FLAGS_PWR_IOWAKEUP) { |
<> | 149:156823d33999 | 144 | // NOTE: These must be cleared before clearing IOWAKEUP |
<> | 149:156823d33999 | 145 | MXC_PWRSEQ->reg1 |= MXC_F_PWRSEQ_REG1_PWR_CLR_IO_EVENT_LATCH; |
<> | 149:156823d33999 | 146 | MXC_PWRSEQ->reg1 &= ~MXC_F_PWRSEQ_REG1_PWR_CLR_IO_EVENT_LATCH; |
<> | 149:156823d33999 | 147 | |
<> | 149:156823d33999 | 148 | MXC_PWRSEQ->flags |= MXC_F_PWRSEQ_FLAGS_PWR_IOWAKEUP; |
<> | 149:156823d33999 | 149 | } |
<> | 149:156823d33999 | 150 | |
<> | 149:156823d33999 | 151 | // Set the LP1 select bit so CPU goes to LP1 during SLEEPDEEP |
<> | 149:156823d33999 | 152 | if (part_rev == REVISION_A4) { |
<> | 149:156823d33999 | 153 | MXC_PWRSEQ->reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_LP1; // A4 requires part to go to pseudo LP0 |
<> | 149:156823d33999 | 154 | } else { |
<> | 149:156823d33999 | 155 | MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_LP1; |
<> | 149:156823d33999 | 156 | } |
<> | 149:156823d33999 | 157 | |
<> | 149:156823d33999 | 158 | // The SLEEPDEEP bit will cause a WFE() to trigger LP0/LP1 (depending on ..._REG0_PWR_LP1 state) |
<> | 149:156823d33999 | 159 | SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; |
<> | 149:156823d33999 | 160 | |
<> | 149:156823d33999 | 161 | if (part_rev == REVISION_A4) { |
<> | 149:156823d33999 | 162 | // WORKAROUND: Toggle SVM bits, which send extra clocks to the power sequencer to fix retention controller |
<> | 149:156823d33999 | 163 | MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_MSK_FLAGS_PWR_TVDD12_RST_BAD; |
<> | 149:156823d33999 | 164 | MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_SVMTVDD12EN_RUN); |
<> | 149:156823d33999 | 165 | MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_SVMTVDD12EN_RUN; |
<> | 149:156823d33999 | 166 | MXC_PWRSEQ->msk_flags &= ~MXC_F_PWRSEQ_MSK_FLAGS_PWR_TVDD12_RST_BAD; |
<> | 149:156823d33999 | 167 | } |
<> | 149:156823d33999 | 168 | |
<> | 149:156823d33999 | 169 | // Enable Retention controller |
<> | 149:156823d33999 | 170 | MXC_PWRSEQ->retn_ctrl0 |= MXC_F_PWRSEQ_RETN_CTRL0_RETN_CTRL_EN; |
<> | 149:156823d33999 | 171 | |
<> | 149:156823d33999 | 172 | // Clear the firstboot bit, which is generated by a POR event and locks out LPx modes |
<> | 149:156823d33999 | 173 | MXC_PWRSEQ->reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_FIRST_BOOT; |
<> | 149:156823d33999 | 174 | |
<> | 149:156823d33999 | 175 | // Freeze GPIO using MBUS so that it doesn't flail while digital core is asleep |
<> | 149:156823d33999 | 176 | MXC_PWRSEQ->reg1 |= MXC_F_PWRSEQ_REG1_PWR_MBUS_GATE; |
<> | 149:156823d33999 | 177 | |
<> | 149:156823d33999 | 178 | // Dummy read to make sure SSB writes are complete |
<> | 149:156823d33999 | 179 | MXC_PWRSEQ->reg0 = MXC_PWRSEQ->reg0; |
<> | 149:156823d33999 | 180 | |
<> | 149:156823d33999 | 181 | if (part_rev == REVISION_A4) { |
<> | 149:156823d33999 | 182 | // Note: ARM deep-sleep requires a specific sequence to clear event latches, |
<> | 149:156823d33999 | 183 | // otherwise the CPU will not enter sleep. |
<> | 149:156823d33999 | 184 | __SEV(); |
<> | 149:156823d33999 | 185 | __WFE(); |
<> | 149:156823d33999 | 186 | __WFI(); |
<> | 149:156823d33999 | 187 | } else { |
<> | 149:156823d33999 | 188 | // Note: ARM deep-sleep requires a specific sequence to clear event latches, |
<> | 149:156823d33999 | 189 | // otherwise the CPU will not enter sleep. |
<> | 149:156823d33999 | 190 | __SEV(); |
<> | 149:156823d33999 | 191 | __WFE(); |
<> | 149:156823d33999 | 192 | __WFE(); |
<> | 149:156823d33999 | 193 | } |
<> | 149:156823d33999 | 194 | |
<> | 149:156823d33999 | 195 | // We'll wakeup here ... |
<> | 149:156823d33999 | 196 | |
<> | 149:156823d33999 | 197 | // Unfreeze the GPIO by clearing MBUS_GATE |
<> | 149:156823d33999 | 198 | MXC_PWRSEQ->reg1 &= ~MXC_F_PWRSEQ_REG1_PWR_MBUS_GATE; |
<> | 149:156823d33999 | 199 | |
<> | 149:156823d33999 | 200 | usb_wakeup(); |
<> | 149:156823d33999 | 201 | |
<> | 149:156823d33999 | 202 | // Clear power sequencer event flags (write-1-to-clear) |
<> | 149:156823d33999 | 203 | // RTC and GPIO flags are cleared in their interrupts handlers |
<> | 149:156823d33999 | 204 | // NOTE: We are ignoring all of these potential wake up types |
<> | 149:156823d33999 | 205 | MXC_PWRSEQ->flags = (MXC_F_PWRSEQ_FLAGS_PWR_POWER_FAIL | |
<> | 149:156823d33999 | 206 | MXC_F_PWRSEQ_FLAGS_PWR_BOOT_FAIL | |
<> | 149:156823d33999 | 207 | MXC_F_PWRSEQ_FLAGS_PWR_FLASH_DISCHARGE | |
<> | 149:156823d33999 | 208 | MXC_F_PWRSEQ_FLAGS_PWR_VDD12_RST_BAD | |
<> | 149:156823d33999 | 209 | MXC_F_PWRSEQ_FLAGS_PWR_VDD18_RST_BAD | |
<> | 149:156823d33999 | 210 | MXC_F_PWRSEQ_FLAGS_PWR_VRTC_RST_BAD | |
<> | 149:156823d33999 | 211 | MXC_F_PWRSEQ_FLAGS_PWR_VDDB_RST_BAD | |
<> | 149:156823d33999 | 212 | MXC_F_PWRSEQ_FLAGS_PWR_TVDD12_RST_BAD | |
<> | 149:156823d33999 | 213 | MXC_F_PWRSEQ_FLAGS_PWR_POR18Z_FAIL_LATCH | |
<> | 149:156823d33999 | 214 | MXC_F_PWRSEQ_FLAGS_PWR_USB_PLUG_WAKEUP | |
<> | 149:156823d33999 | 215 | MXC_F_PWRSEQ_FLAGS_PWR_USB_REMOVE_WAKEUP | |
<> | 149:156823d33999 | 216 | MXC_F_PWRSEQ_FLAGS_PWR_TVDD12_BAD); |
<> | 149:156823d33999 | 217 | |
<> | 149:156823d33999 | 218 | __enable_irq(); |
<> | 149:156823d33999 | 219 | } |