test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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