mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Nov 08 17:45:16 2016 +0000
Revision:
151:02e0a0aed4ec
This updates the lib to the mbed lib v129

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 151:02e0a0aed4ec 1 /*******************************************************************************
<> 151:02e0a0aed4ec 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 151:02e0a0aed4ec 3 *
<> 151:02e0a0aed4ec 4 * Permission is hereby granted, free of charge, to any person obtaining a
<> 151:02e0a0aed4ec 5 * copy of this software and associated documentation files (the "Software"),
<> 151:02e0a0aed4ec 6 * to deal in the Software without restriction, including without limitation
<> 151:02e0a0aed4ec 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 151:02e0a0aed4ec 8 * and/or sell copies of the Software, and to permit persons to whom the
<> 151:02e0a0aed4ec 9 * Software is furnished to do so, subject to the following conditions:
<> 151:02e0a0aed4ec 10 *
<> 151:02e0a0aed4ec 11 * The above copyright notice and this permission notice shall be included
<> 151:02e0a0aed4ec 12 * in all copies or substantial portions of the Software.
<> 151:02e0a0aed4ec 13 *
<> 151:02e0a0aed4ec 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 151:02e0a0aed4ec 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 151:02e0a0aed4ec 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 151:02e0a0aed4ec 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 151:02e0a0aed4ec 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 151:02e0a0aed4ec 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 151:02e0a0aed4ec 20 * OTHER DEALINGS IN THE SOFTWARE.
<> 151:02e0a0aed4ec 21 *
<> 151:02e0a0aed4ec 22 * Except as contained in this notice, the name of Maxim Integrated
<> 151:02e0a0aed4ec 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 151:02e0a0aed4ec 24 * Products, Inc. Branding Policy.
<> 151:02e0a0aed4ec 25 *
<> 151:02e0a0aed4ec 26 * The mere transfer of this software does not imply any licenses
<> 151:02e0a0aed4ec 27 * of trade secrets, proprietary technology, copyrights, patents,
<> 151:02e0a0aed4ec 28 * trademarks, maskwork rights, or any other form of intellectual
<> 151:02e0a0aed4ec 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 151:02e0a0aed4ec 30 * ownership rights.
<> 151:02e0a0aed4ec 31 *******************************************************************************
<> 151:02e0a0aed4ec 32 */
<> 151:02e0a0aed4ec 33
<> 151:02e0a0aed4ec 34 #include "rtc_api.h"
<> 151:02e0a0aed4ec 35 #include "lp_ticker_api.h"
<> 151:02e0a0aed4ec 36 #include "rtc.h"
<> 151:02e0a0aed4ec 37 #include "lp.h"
<> 151:02e0a0aed4ec 38
<> 151:02e0a0aed4ec 39 #define PRESCALE_VAL RTC_PRESCALE_DIV_2_0 // Set the divider for the 4kHz clock
<> 151:02e0a0aed4ec 40 #define SHIFT_AMT (RTC_PRESCALE_DIV_2_12 - PRESCALE_VAL)
<> 151:02e0a0aed4ec 41
<> 151:02e0a0aed4ec 42 #define WINDOW 1000
<> 151:02e0a0aed4ec 43
<> 151:02e0a0aed4ec 44 static int rtc_inited = 0;
<> 151:02e0a0aed4ec 45 static volatile uint32_t overflow_cnt = 0;
<> 151:02e0a0aed4ec 46
<> 151:02e0a0aed4ec 47 static uint64_t rtc_read64(void);
<> 151:02e0a0aed4ec 48
<> 151:02e0a0aed4ec 49 //******************************************************************************
<> 151:02e0a0aed4ec 50 static void overflow_handler(void)
<> 151:02e0a0aed4ec 51 {
<> 151:02e0a0aed4ec 52 overflow_cnt++;
<> 151:02e0a0aed4ec 53 RTC_ClearFlags(MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS);
<> 151:02e0a0aed4ec 54 }
<> 151:02e0a0aed4ec 55
<> 151:02e0a0aed4ec 56 //******************************************************************************
<> 151:02e0a0aed4ec 57 void rtc_init(void)
<> 151:02e0a0aed4ec 58 {
<> 151:02e0a0aed4ec 59 if (rtc_inited) {
<> 151:02e0a0aed4ec 60 return;
<> 151:02e0a0aed4ec 61 }
<> 151:02e0a0aed4ec 62 rtc_inited = 1;
<> 151:02e0a0aed4ec 63
<> 151:02e0a0aed4ec 64 overflow_cnt = 0;
<> 151:02e0a0aed4ec 65
<> 151:02e0a0aed4ec 66 /* Enable power for RTC for all LPx states */
<> 151:02e0a0aed4ec 67 MXC_PWRSEQ->reg0 |= (MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN |
<> 151:02e0a0aed4ec 68 MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP);
<> 151:02e0a0aed4ec 69
<> 151:02e0a0aed4ec 70 /* Enable clock to synchronizers */
<> 151:02e0a0aed4ec 71 CLKMAN_SetClkScale(CLKMAN_CLK_SYNC, CLKMAN_SCALE_DIV_1);
<> 151:02e0a0aed4ec 72
<> 151:02e0a0aed4ec 73 // Prepare interrupt handlers
<> 151:02e0a0aed4ec 74 NVIC_SetVector(RTC0_IRQn, (uint32_t)lp_ticker_irq_handler);
<> 151:02e0a0aed4ec 75 NVIC_EnableIRQ(RTC0_IRQn);
<> 151:02e0a0aed4ec 76 NVIC_SetVector(RTC3_IRQn, (uint32_t)overflow_handler);
<> 151:02e0a0aed4ec 77 NVIC_EnableIRQ(RTC3_IRQn);
<> 151:02e0a0aed4ec 78
<> 151:02e0a0aed4ec 79 // Enable wakeup on RTC rollover
<> 151:02e0a0aed4ec 80 LP_ConfigRTCWakeUp(0, 0, 0, 1);
<> 151:02e0a0aed4ec 81
<> 151:02e0a0aed4ec 82 /* RTC registers are only reset on a power cycle. Do not reconfigure the RTC
<> 151:02e0a0aed4ec 83 * if it is already running.
<> 151:02e0a0aed4ec 84 */
<> 151:02e0a0aed4ec 85 if (!RTC_IsActive()) {
<> 151:02e0a0aed4ec 86 rtc_cfg_t cfg = {0};
<> 151:02e0a0aed4ec 87 cfg.prescaler = PRESCALE_VAL;
<> 151:02e0a0aed4ec 88 cfg.snoozeMode = RTC_SNOOZE_DISABLE;
<> 151:02e0a0aed4ec 89
<> 151:02e0a0aed4ec 90 int retval = RTC_Init(&cfg);
<> 151:02e0a0aed4ec 91 MBED_ASSERT(retval == E_NO_ERROR);
<> 151:02e0a0aed4ec 92
<> 151:02e0a0aed4ec 93 RTC_EnableINT(MXC_F_RTC_FLAGS_OVERFLOW);
<> 151:02e0a0aed4ec 94 RTC_Start();
<> 151:02e0a0aed4ec 95 }
<> 151:02e0a0aed4ec 96 }
<> 151:02e0a0aed4ec 97
<> 151:02e0a0aed4ec 98 //******************************************************************************
<> 151:02e0a0aed4ec 99 void lp_ticker_init(void)
<> 151:02e0a0aed4ec 100 {
<> 151:02e0a0aed4ec 101 rtc_init();
<> 151:02e0a0aed4ec 102 }
<> 151:02e0a0aed4ec 103
<> 151:02e0a0aed4ec 104 //******************************************************************************
<> 151:02e0a0aed4ec 105 void rtc_free(void)
<> 151:02e0a0aed4ec 106 {
<> 151:02e0a0aed4ec 107 if (RTC_IsActive()) {
<> 151:02e0a0aed4ec 108 // Clear and disable RTC
<> 151:02e0a0aed4ec 109 MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_CLEAR;
<> 151:02e0a0aed4ec 110 RTC_Stop();
<> 151:02e0a0aed4ec 111 }
<> 151:02e0a0aed4ec 112 }
<> 151:02e0a0aed4ec 113
<> 151:02e0a0aed4ec 114 //******************************************************************************
<> 151:02e0a0aed4ec 115 int rtc_isenabled(void)
<> 151:02e0a0aed4ec 116 {
<> 151:02e0a0aed4ec 117 return RTC_IsActive();
<> 151:02e0a0aed4ec 118 }
<> 151:02e0a0aed4ec 119
<> 151:02e0a0aed4ec 120 //******************************************************************************
<> 151:02e0a0aed4ec 121 time_t rtc_read(void)
<> 151:02e0a0aed4ec 122 {
<> 151:02e0a0aed4ec 123 uint32_t ovf_cnt_1, ovf_cnt_2, timer_cnt;
<> 151:02e0a0aed4ec 124 uint32_t ovf1, ovf2;
<> 151:02e0a0aed4ec 125
<> 151:02e0a0aed4ec 126 // Make sure RTC is setup before trying to read
<> 151:02e0a0aed4ec 127 if (!rtc_inited) {
<> 151:02e0a0aed4ec 128 rtc_init();
<> 151:02e0a0aed4ec 129 }
<> 151:02e0a0aed4ec 130
<> 151:02e0a0aed4ec 131 // Ensure coherency between overflow_cnt and timer
<> 151:02e0a0aed4ec 132 do {
<> 151:02e0a0aed4ec 133 ovf_cnt_1 = overflow_cnt;
<> 151:02e0a0aed4ec 134 ovf1 = RTC_GetFlags() & MXC_F_RTC_FLAGS_OVERFLOW;
<> 151:02e0a0aed4ec 135 timer_cnt = RTC_GetCount();
<> 151:02e0a0aed4ec 136 ovf2 = RTC_GetFlags() & MXC_F_RTC_FLAGS_OVERFLOW;
<> 151:02e0a0aed4ec 137 ovf_cnt_2 = overflow_cnt;
<> 151:02e0a0aed4ec 138 } while ((ovf_cnt_1 != ovf_cnt_2) || (ovf1 != ovf2));
<> 151:02e0a0aed4ec 139
<> 151:02e0a0aed4ec 140 // Account for an unserviced interrupt
<> 151:02e0a0aed4ec 141 if (ovf1) {
<> 151:02e0a0aed4ec 142 ovf_cnt_1++;
<> 151:02e0a0aed4ec 143 }
<> 151:02e0a0aed4ec 144
<> 151:02e0a0aed4ec 145 return (timer_cnt >> SHIFT_AMT) + (ovf_cnt_1 << (32 - SHIFT_AMT));
<> 151:02e0a0aed4ec 146 }
<> 151:02e0a0aed4ec 147
<> 151:02e0a0aed4ec 148 //******************************************************************************
<> 151:02e0a0aed4ec 149 static uint64_t rtc_read64(void)
<> 151:02e0a0aed4ec 150 {
<> 151:02e0a0aed4ec 151 uint32_t ovf_cnt_1, ovf_cnt_2, timer_cnt;
<> 151:02e0a0aed4ec 152 uint32_t ovf1, ovf2;
<> 151:02e0a0aed4ec 153 uint64_t current_us;
<> 151:02e0a0aed4ec 154
<> 151:02e0a0aed4ec 155 // Make sure RTC is setup before trying to read
<> 151:02e0a0aed4ec 156 if (!rtc_inited) {
<> 151:02e0a0aed4ec 157 rtc_init();
<> 151:02e0a0aed4ec 158 }
<> 151:02e0a0aed4ec 159
<> 151:02e0a0aed4ec 160 // Ensure coherency between overflow_cnt and timer
<> 151:02e0a0aed4ec 161 do {
<> 151:02e0a0aed4ec 162 ovf_cnt_1 = overflow_cnt;
<> 151:02e0a0aed4ec 163 ovf1 = RTC_GetFlags() & MXC_F_RTC_FLAGS_OVERFLOW;
<> 151:02e0a0aed4ec 164 timer_cnt = RTC_GetCount();
<> 151:02e0a0aed4ec 165 ovf2 = RTC_GetFlags() & MXC_F_RTC_FLAGS_OVERFLOW;
<> 151:02e0a0aed4ec 166 ovf_cnt_2 = overflow_cnt;
<> 151:02e0a0aed4ec 167 } while ((ovf_cnt_1 != ovf_cnt_2) || (ovf1 != ovf2));
<> 151:02e0a0aed4ec 168
<> 151:02e0a0aed4ec 169 // Account for an unserviced interrupt
<> 151:02e0a0aed4ec 170 if (ovf1) {
<> 151:02e0a0aed4ec 171 ovf_cnt_1++;
<> 151:02e0a0aed4ec 172 }
<> 151:02e0a0aed4ec 173
<> 151:02e0a0aed4ec 174 current_us = (((uint64_t)timer_cnt * 1000000) >> SHIFT_AMT) + (((uint64_t)ovf_cnt_1 * 1000000) << (32 - SHIFT_AMT));
<> 151:02e0a0aed4ec 175
<> 151:02e0a0aed4ec 176 return current_us;
<> 151:02e0a0aed4ec 177 }
<> 151:02e0a0aed4ec 178
<> 151:02e0a0aed4ec 179 //******************************************************************************
<> 151:02e0a0aed4ec 180 void rtc_write(time_t t)
<> 151:02e0a0aed4ec 181 {
<> 151:02e0a0aed4ec 182 // Make sure RTC is setup before accessing
<> 151:02e0a0aed4ec 183 if (!rtc_inited) {
<> 151:02e0a0aed4ec 184 rtc_init();
<> 151:02e0a0aed4ec 185 }
<> 151:02e0a0aed4ec 186
<> 151:02e0a0aed4ec 187 RTC_Stop();
<> 151:02e0a0aed4ec 188 RTC_SetCount(t << SHIFT_AMT);
<> 151:02e0a0aed4ec 189 overflow_cnt = t >> (32 - SHIFT_AMT);
<> 151:02e0a0aed4ec 190 RTC_Start();
<> 151:02e0a0aed4ec 191 }
<> 151:02e0a0aed4ec 192
<> 151:02e0a0aed4ec 193 //******************************************************************************
<> 151:02e0a0aed4ec 194 void lp_ticker_set_interrupt(timestamp_t timestamp)
<> 151:02e0a0aed4ec 195 {
<> 151:02e0a0aed4ec 196 uint32_t comp_value;
<> 151:02e0a0aed4ec 197 uint64_t curr_ts64;
<> 151:02e0a0aed4ec 198 uint64_t ts64;
<> 151:02e0a0aed4ec 199
<> 151:02e0a0aed4ec 200 // Note: interrupts are disabled before this function is called.
<> 151:02e0a0aed4ec 201
<> 151:02e0a0aed4ec 202 // Disable the alarm while it is prepared
<> 151:02e0a0aed4ec 203 RTC_DisableINT(MXC_F_RTC_INTEN_COMP0);
<> 151:02e0a0aed4ec 204
<> 151:02e0a0aed4ec 205 curr_ts64 = rtc_read64();
<> 151:02e0a0aed4ec 206 ts64 = (uint64_t)timestamp | (curr_ts64 & 0xFFFFFFFF00000000ULL);
<> 151:02e0a0aed4ec 207
<> 151:02e0a0aed4ec 208 // If this event is older than a recent window, it must be in the future
<> 151:02e0a0aed4ec 209 if ((ts64 < (curr_ts64 - WINDOW)) && ((curr_ts64 - WINDOW) < curr_ts64)) {
<> 151:02e0a0aed4ec 210 ts64 += 0x100000000ULL;
<> 151:02e0a0aed4ec 211 }
<> 151:02e0a0aed4ec 212
<> 151:02e0a0aed4ec 213 uint32_t timer = RTC_GetCount();
<> 151:02e0a0aed4ec 214 if (ts64 <= curr_ts64) {
<> 151:02e0a0aed4ec 215 // This event has already occurred. Set the alarm to expire immediately.
<> 151:02e0a0aed4ec 216 comp_value = timer + 1;
<> 151:02e0a0aed4ec 217 } else {
<> 151:02e0a0aed4ec 218 comp_value = (ts64 << SHIFT_AMT) / 1000000;
<> 151:02e0a0aed4ec 219 }
<> 151:02e0a0aed4ec 220
<> 151:02e0a0aed4ec 221 // Ensure that the compare value is far enough in the future to guarantee the interrupt occurs.
<> 151:02e0a0aed4ec 222 if ((comp_value < (timer + 2)) && (comp_value > (timer - 10))) {
<> 151:02e0a0aed4ec 223 comp_value = timer + 2;
<> 151:02e0a0aed4ec 224 }
<> 151:02e0a0aed4ec 225
<> 151:02e0a0aed4ec 226 MXC_RTCTMR->comp[0] = comp_value;
<> 151:02e0a0aed4ec 227 MXC_RTCTMR->flags = MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS;
<> 151:02e0a0aed4ec 228 RTC_EnableINT(MXC_F_RTC_INTEN_COMP0);
<> 151:02e0a0aed4ec 229
<> 151:02e0a0aed4ec 230 // Enable wakeup from RTC
<> 151:02e0a0aed4ec 231 LP_ConfigRTCWakeUp(1, 0, 0, 1);
<> 151:02e0a0aed4ec 232
<> 151:02e0a0aed4ec 233 // Wait for pending transactions
<> 151:02e0a0aed4ec 234 while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
<> 151:02e0a0aed4ec 235 }
<> 151:02e0a0aed4ec 236
<> 151:02e0a0aed4ec 237 //******************************************************************************
<> 151:02e0a0aed4ec 238 inline void lp_ticker_disable_interrupt(void)
<> 151:02e0a0aed4ec 239 {
<> 151:02e0a0aed4ec 240 RTC_DisableINT(MXC_F_RTC_INTEN_COMP0);
<> 151:02e0a0aed4ec 241 }
<> 151:02e0a0aed4ec 242
<> 151:02e0a0aed4ec 243 //******************************************************************************
<> 151:02e0a0aed4ec 244 inline void lp_ticker_clear_interrupt(void)
<> 151:02e0a0aed4ec 245 {
<> 151:02e0a0aed4ec 246 RTC_ClearFlags(MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS);
<> 151:02e0a0aed4ec 247 }
<> 151:02e0a0aed4ec 248
<> 151:02e0a0aed4ec 249 //******************************************************************************
<> 151:02e0a0aed4ec 250 inline uint32_t lp_ticker_read(void)
<> 151:02e0a0aed4ec 251 {
<> 151:02e0a0aed4ec 252 return rtc_read64();
<> 151:02e0a0aed4ec 253 }