Rigado / mbed-src-bmd-200

Dependents:   mbed_blinky-bmd-200 bmd-200_accel_demo firstRig

Fork of mbed-src by mbed official

Committer:
dcnichols
Date:
Fri Jul 10 17:36:27 2015 +0000
Revision:
592:5e2eb8beba71
Parent:
542:3c6246291cdb
updating to latest mbed-src

Who changed what in which revision?

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