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