mbed library sources. Supersedes mbed-src. removed serial

Fork of mbed-dev by mbed official

Committer:
mrcrsch
Date:
Mon Feb 13 14:26:36 2017 +0000
Revision:
158:aa577577efe0
Parent:
150:02e0a0aed4ec
removed stdio and serial to reduce size

Who changed what in which revision?

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