Team DIANA / mbed-src

Dependents:   MX106-finaltest

Fork of mbed-src by mbed official

Committer:
dconsoli
Date:
Mon May 23 02:17:28 2016 +0000
Revision:
636:2405a6d45c2c
Parent:
538:3c6246291cdb
I don't know;

Who changed what in which revision?

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