Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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