]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Maxim/TARGET_MAX32610/sleep.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Maxim / TARGET_MAX32610 / sleep.c
1 /*******************************************************************************
2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Except as contained in this notice, the name of Maxim Integrated
23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
24 * Products, Inc. Branding Policy.
25 *
26 * The mere transfer of this software does not imply any licenses
27 * of trade secrets, proprietary technology, copyrights, patents,
28 * trademarks, maskwork rights, or any other form of intellectual
29 * property whatsoever. Maxim Integrated Products, Inc. retains all
30 * ownership rights.
31 *******************************************************************************
32 */
33
34 #include "sleep_api.h"
35 #include "us_ticker_api.h"
36 #include "cmsis.h"
37 #include "pwrman_regs.h"
38 #include "pwrseq_regs.h"
39 #include "ioman_regs.h"
40 #include "rtc_regs.h"
41
42 #define MIN_DEEP_SLEEP_US 500
43
44 uint64_t rtc_read_us(void);
45 void rtc_set_wakeup(uint64_t wakeupUs);
46 void us_ticker_deinit(void);
47 void us_ticker_set(timestamp_t timestamp);
48
49 static mxc_uart_regs_t *stdio_uart = (mxc_uart_regs_t*)STDIO_UART;
50
51 // Normal wait mode
52 void sleep(void)
53 {
54 // Normal sleep mode for ARM core
55 SCB->SCR = 0;
56
57 __DSB();
58 __WFI();
59 }
60
61 // Work-around for issue of clearing power sequencer I/O flag
62 static void clearAllGPIOWUD(void)
63 {
64 uint32_t wud_req0 = MXC_IOMAN->wud_req0;
65 uint32_t wud_req1 = MXC_IOMAN->wud_req1;
66
67 // I/O must be a wakeup detect to clear
68 MXC_IOMAN->wud_req0 = 0xffffffff;
69 MXC_IOMAN->wud_req1 = 0xffffffff;
70
71 // Clear all WUDs
72 MXC_PWRMAN->wud_ctrl = (MXC_E_PWRMAN_PAD_MODE_CLEAR_SET << MXC_F_PWRMAN_WUD_CTRL_PAD_MODE_POS) | MXC_F_PWRMAN_WUD_CTRL_CLEAR_ALL;
73 MXC_PWRMAN->wud_pulse0 = 1;
74
75 // Restore WUD requests
76 MXC_IOMAN->wud_req0 = wud_req0;
77 MXC_IOMAN->wud_req1 = wud_req1;
78 }
79
80 // Low-power stop mode
81 void deepsleep(void)
82 {
83 uint64_t sleepStartRtcUs;
84 uint32_t sleepStartTickerUs;
85 int32_t sleepDurationUs;
86 uint64_t sleepEndRtcUs;
87 uint64_t elapsedUs;
88
89 __disable_irq();
90
91 // Wait for all STDIO characters to be sent. The UART clock will stop.
92 while (stdio_uart->status & MXC_F_UART_STATUS_TX_BUSY);
93
94 // Record the current times
95 sleepStartRtcUs = rtc_read_us();
96 sleepStartTickerUs = us_ticker_read();
97
98 // Get the next mbed timer expiration
99 timestamp_t next_event = 0;
100 us_ticker_get_next_timestamp(&next_event);
101 sleepDurationUs = next_event - sleepStartTickerUs;
102
103 if (sleepDurationUs < MIN_DEEP_SLEEP_US) {
104 /* The next wakeup is too soon. */
105 __enable_irq();
106 return;
107 }
108
109 // Disable the us_ticker. It won't be clocked in DeepSleep
110 us_ticker_deinit();
111
112 // Prepare to wakeup from the RTC
113 rtc_set_wakeup(sleepStartRtcUs + sleepDurationUs);
114
115 // Prepare for LP1
116 uint32_t reg0 = MXC_PWRSEQ->reg0;
117 reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_SVM3EN_SLP; // disable VDD3 SVM during sleep mode
118 reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_SVM1EN_SLP; // disable VREG18 SVM during sleep mode
119 if (reg0 & MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN) { // if real-time clock enabled during run
120 reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // enable real-time clock during sleep mode
121 } else {
122 reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // disable real-time clock during sleep mode
123 }
124 reg0 |= MXC_F_PWRSEQ_REG0_PWR_CHZYEN_SLP; // enable CHZY regulator during sleep mode
125 reg0 |= MXC_F_PWRSEQ_REG0_PWR_LP1; // go into LP1
126 reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_FIRST_BOOT; // clear first boot flag
127 MXC_PWRSEQ->reg0 = reg0;
128
129 MXC_PWRSEQ->reg3 = (MXC_PWRSEQ->reg3 & ~MXC_F_PWRSEQ_REG3_PWR_ROSEL_QUICK) | (3 << MXC_F_PWRSEQ_REG3_PWR_ROSEL_QUICK_POS);
130
131 // Deep sleep for ARM core
132 SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;
133
134 // clear latches for wakeup detect
135 MXC_PWRSEQ->flags = MXC_PWRSEQ->flags;
136 if (MXC_PWRSEQ->flags & MXC_F_PWRSEQ_FLAGS_PWR_IO_WAKEUP) {
137 // attempt work-around for I/O flag clearing issue
138 clearAllGPIOWUD();
139 MXC_PWRSEQ->flags = MXC_F_PWRSEQ_FLAGS_PWR_IO_WAKEUP;
140 }
141
142 // Wait for pending RTC transaction
143 while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
144
145 // Ensure that the event register is clear
146 __SEV(); // set event
147 __WFE(); // clear event
148
149 // Enter LP1
150 __WFE();
151 // Woke up from LP1
152
153 // The RTC timer does not update until the next tick
154 uint64_t tempUs = rtc_read_us();
155 do {
156 sleepEndRtcUs = rtc_read_us();
157 } while(sleepEndRtcUs == tempUs);
158
159 // Get the elapsed time from the RTC. Wakeup could have been from some other event.
160 elapsedUs = sleepEndRtcUs - sleepStartRtcUs;
161
162 // Update the us_ticker. It was not clocked during DeepSleep
163 us_ticker_init();
164 us_ticker_set(sleepStartTickerUs + elapsedUs);
165 us_ticker_get_next_timestamp(&next_event);
166 us_ticker_set_interrupt(next_event);
167
168 __enable_irq();
169 }
Imprint / Impressum