mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32620C/rtc_api.c@186:707f6e361f3e, 2018-06-22 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Jun 22 16:45:37 2018 +0100
- Revision:
- 186:707f6e361f3e
mbed-dev library. Release version 162
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anna Bridge |
186:707f6e361f3e | 1 | /******************************************************************************* |
Anna Bridge |
186:707f6e361f3e | 2 | * Copyright (C) 2016,2018 Maxim Integrated Products, Inc., All Rights Reserved. |
Anna Bridge |
186:707f6e361f3e | 3 | * |
Anna Bridge |
186:707f6e361f3e | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
Anna Bridge |
186:707f6e361f3e | 5 | * copy of this software and associated documentation files (the "Software"), |
Anna Bridge |
186:707f6e361f3e | 6 | * to deal in the Software without restriction, including without limitation |
Anna Bridge |
186:707f6e361f3e | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Anna Bridge |
186:707f6e361f3e | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
Anna Bridge |
186:707f6e361f3e | 9 | * Software is furnished to do so, subject to the following conditions: |
Anna Bridge |
186:707f6e361f3e | 10 | * |
Anna Bridge |
186:707f6e361f3e | 11 | * The above copyright notice and this permission notice shall be included |
Anna Bridge |
186:707f6e361f3e | 12 | * in all copies or substantial portions of the Software. |
Anna Bridge |
186:707f6e361f3e | 13 | * |
Anna Bridge |
186:707f6e361f3e | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
Anna Bridge |
186:707f6e361f3e | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Anna Bridge |
186:707f6e361f3e | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Anna Bridge |
186:707f6e361f3e | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
Anna Bridge |
186:707f6e361f3e | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
Anna Bridge |
186:707f6e361f3e | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
Anna Bridge |
186:707f6e361f3e | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
Anna Bridge |
186:707f6e361f3e | 21 | * |
Anna Bridge |
186:707f6e361f3e | 22 | * Except as contained in this notice, the name of Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 24 | * Products, Inc. Branding Policy. |
Anna Bridge |
186:707f6e361f3e | 25 | * |
Anna Bridge |
186:707f6e361f3e | 26 | * The mere transfer of this software does not imply any licenses |
Anna Bridge |
186:707f6e361f3e | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
Anna Bridge |
186:707f6e361f3e | 28 | * trademarks, maskwork rights, or any other form of intellectual |
Anna Bridge |
186:707f6e361f3e | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
Anna Bridge |
186:707f6e361f3e | 30 | * ownership rights. |
Anna Bridge |
186:707f6e361f3e | 31 | ******************************************************************************* |
Anna Bridge |
186:707f6e361f3e | 32 | */ |
Anna Bridge |
186:707f6e361f3e | 33 | |
Anna Bridge |
186:707f6e361f3e | 34 | #include <string.h> |
Anna Bridge |
186:707f6e361f3e | 35 | #include "rtc_api.h" |
Anna Bridge |
186:707f6e361f3e | 36 | #include "lp_ticker_api.h" |
Anna Bridge |
186:707f6e361f3e | 37 | #include "rtc.h" |
Anna Bridge |
186:707f6e361f3e | 38 | #include "lp.h" |
Anna Bridge |
186:707f6e361f3e | 39 | #include <string.h> |
Anna Bridge |
186:707f6e361f3e | 40 | |
Anna Bridge |
186:707f6e361f3e | 41 | // LOG2 for 32-bit powers of 2 |
Anna Bridge |
186:707f6e361f3e | 42 | #define LOG2_1(n) (((n) >= (1 << 1)) ? 1 : 0) |
Anna Bridge |
186:707f6e361f3e | 43 | #define LOG2_2(n) (((n) >= (1 << 2)) ? ( 2 + (LOG2_1((n) >> 2))) : LOG2_1(n)) |
Anna Bridge |
186:707f6e361f3e | 44 | #define LOG2_4(n) (((n) >= (1 << 4)) ? ( 4 + (LOG2_2((n) >> 4))) : LOG2_2(n)) |
Anna Bridge |
186:707f6e361f3e | 45 | #define LOG2_8(n) (((n) >= (1 << 8)) ? ( 8 + (LOG2_4((n) >> 8))) : LOG2_4(n)) |
Anna Bridge |
186:707f6e361f3e | 46 | #define LOG2(n) (((n) >= (1 << 16)) ? (16 + (LOG2_8((n) >> 16))) : LOG2_8(n)) |
Anna Bridge |
186:707f6e361f3e | 47 | |
Anna Bridge |
186:707f6e361f3e | 48 | #define LP_TIMER_FREQ_HZ 4096 |
Anna Bridge |
186:707f6e361f3e | 49 | #define LP_TIMER_PRESCALE RTC_PRESCALE_DIV_2_0 |
Anna Bridge |
186:707f6e361f3e | 50 | #define LP_TIMER_RATE_HZ (LP_TIMER_FREQ_HZ >> LP_TIMER_PRESCALE) |
Anna Bridge |
186:707f6e361f3e | 51 | #define LP_TIMER_WIDTH 32 |
Anna Bridge |
186:707f6e361f3e | 52 | |
Anna Bridge |
186:707f6e361f3e | 53 | static volatile int rtc_inited = 0; |
Anna Bridge |
186:707f6e361f3e | 54 | static volatile int lp_ticker_inited = 0; |
Anna Bridge |
186:707f6e361f3e | 55 | |
Anna Bridge |
186:707f6e361f3e | 56 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 57 | static void init_rtc(void) |
Anna Bridge |
186:707f6e361f3e | 58 | { |
Anna Bridge |
186:707f6e361f3e | 59 | /* Enable power for RTC for all LPx states */ |
Anna Bridge |
186:707f6e361f3e | 60 | MXC_PWRSEQ->reg0 |= (MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | |
Anna Bridge |
186:707f6e361f3e | 61 | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP); |
Anna Bridge |
186:707f6e361f3e | 62 | |
Anna Bridge |
186:707f6e361f3e | 63 | /* Enable clock to synchronizers */ |
Anna Bridge |
186:707f6e361f3e | 64 | CLKMAN_SetClkScale(CLKMAN_CLK_SYNC, CLKMAN_SCALE_DIV_1); |
Anna Bridge |
186:707f6e361f3e | 65 | |
Anna Bridge |
186:707f6e361f3e | 66 | /* RTC registers are only reset on a power cycle. Do not reconfigure the RTC |
Anna Bridge |
186:707f6e361f3e | 67 | * if it is already running. |
Anna Bridge |
186:707f6e361f3e | 68 | */ |
Anna Bridge |
186:707f6e361f3e | 69 | if (!RTC_IsActive()) { |
Anna Bridge |
186:707f6e361f3e | 70 | rtc_cfg_t cfg; |
Anna Bridge |
186:707f6e361f3e | 71 | memset(&cfg, 0, sizeof(rtc_cfg_t)); |
Anna Bridge |
186:707f6e361f3e | 72 | cfg.prescaler = LP_TIMER_PRESCALE; |
Anna Bridge |
186:707f6e361f3e | 73 | cfg.snoozeMode = RTC_SNOOZE_DISABLE; |
Anna Bridge |
186:707f6e361f3e | 74 | |
Anna Bridge |
186:707f6e361f3e | 75 | int retval = RTC_Init(&cfg); |
Anna Bridge |
186:707f6e361f3e | 76 | MBED_ASSERT(retval == E_NO_ERROR); |
Anna Bridge |
186:707f6e361f3e | 77 | |
Anna Bridge |
186:707f6e361f3e | 78 | RTC_EnableINT(MXC_F_RTC_FLAGS_OVERFLOW); |
Anna Bridge |
186:707f6e361f3e | 79 | RTC_Start(); |
Anna Bridge |
186:707f6e361f3e | 80 | } |
Anna Bridge |
186:707f6e361f3e | 81 | } |
Anna Bridge |
186:707f6e361f3e | 82 | |
Anna Bridge |
186:707f6e361f3e | 83 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 84 | static void overflow_handler(void) |
Anna Bridge |
186:707f6e361f3e | 85 | { |
Anna Bridge |
186:707f6e361f3e | 86 | MXC_RTCTMR->comp[1] += ((UINT32_MAX >> LOG2(LP_TIMER_RATE_HZ)) + 1); |
Anna Bridge |
186:707f6e361f3e | 87 | RTC_ClearFlags(MXC_F_RTC_FLAGS_OVERFLOW); |
Anna Bridge |
186:707f6e361f3e | 88 | } |
Anna Bridge |
186:707f6e361f3e | 89 | |
Anna Bridge |
186:707f6e361f3e | 90 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 91 | void rtc_init(void) |
Anna Bridge |
186:707f6e361f3e | 92 | { |
Anna Bridge |
186:707f6e361f3e | 93 | NVIC_SetVector(RTC3_IRQn, (uint32_t)overflow_handler); |
Anna Bridge |
186:707f6e361f3e | 94 | NVIC_EnableIRQ(RTC3_IRQn); |
Anna Bridge |
186:707f6e361f3e | 95 | |
Anna Bridge |
186:707f6e361f3e | 96 | // Enable as LP wakeup source |
Anna Bridge |
186:707f6e361f3e | 97 | MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_FLAGS_RTC_ROLLOVER; |
Anna Bridge |
186:707f6e361f3e | 98 | |
Anna Bridge |
186:707f6e361f3e | 99 | init_rtc(); |
Anna Bridge |
186:707f6e361f3e | 100 | } |
Anna Bridge |
186:707f6e361f3e | 101 | |
Anna Bridge |
186:707f6e361f3e | 102 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 103 | void rtc_free(void) |
Anna Bridge |
186:707f6e361f3e | 104 | { |
Anna Bridge |
186:707f6e361f3e | 105 | while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING); |
Anna Bridge |
186:707f6e361f3e | 106 | } |
Anna Bridge |
186:707f6e361f3e | 107 | |
Anna Bridge |
186:707f6e361f3e | 108 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 109 | int rtc_isenabled(void) |
Anna Bridge |
186:707f6e361f3e | 110 | { |
Anna Bridge |
186:707f6e361f3e | 111 | return !!RTC_IsActive(); |
Anna Bridge |
186:707f6e361f3e | 112 | } |
Anna Bridge |
186:707f6e361f3e | 113 | |
Anna Bridge |
186:707f6e361f3e | 114 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 115 | void rtc_write(time_t t) |
Anna Bridge |
186:707f6e361f3e | 116 | { |
Anna Bridge |
186:707f6e361f3e | 117 | MXC_RTCTMR->comp[1] = t - (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ)); |
Anna Bridge |
186:707f6e361f3e | 118 | while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING); |
Anna Bridge |
186:707f6e361f3e | 119 | } |
Anna Bridge |
186:707f6e361f3e | 120 | |
Anna Bridge |
186:707f6e361f3e | 121 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 122 | time_t rtc_read(void) |
Anna Bridge |
186:707f6e361f3e | 123 | { |
Anna Bridge |
186:707f6e361f3e | 124 | return (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ)) + MXC_RTCTMR->comp[1]; |
Anna Bridge |
186:707f6e361f3e | 125 | } |
Anna Bridge |
186:707f6e361f3e | 126 | |
Anna Bridge |
186:707f6e361f3e | 127 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 128 | void lp_ticker_init(void) |
Anna Bridge |
186:707f6e361f3e | 129 | { |
Anna Bridge |
186:707f6e361f3e | 130 | RTC_DisableINT(MXC_F_RTC_INTEN_COMP0); |
Anna Bridge |
186:707f6e361f3e | 131 | NVIC_SetVector(RTC0_IRQn, (uint32_t)lp_ticker_irq_handler); |
Anna Bridge |
186:707f6e361f3e | 132 | NVIC_EnableIRQ(RTC0_IRQn); |
Anna Bridge |
186:707f6e361f3e | 133 | init_rtc(); |
Anna Bridge |
186:707f6e361f3e | 134 | } |
Anna Bridge |
186:707f6e361f3e | 135 | |
Anna Bridge |
186:707f6e361f3e | 136 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 137 | void lp_ticker_free(void) |
Anna Bridge |
186:707f6e361f3e | 138 | { |
Anna Bridge |
186:707f6e361f3e | 139 | // Disable interrupt associated with LPTICKER API |
Anna Bridge |
186:707f6e361f3e | 140 | RTC_DisableINT(MXC_F_RTC_INTEN_COMP0); |
Anna Bridge |
186:707f6e361f3e | 141 | |
Anna Bridge |
186:707f6e361f3e | 142 | // RTC hardware is shared by LPTICKER and RTC APIs. |
Anna Bridge |
186:707f6e361f3e | 143 | // Prior initialization of the RTC API gates disabling the RTC hardware. |
Anna Bridge |
186:707f6e361f3e | 144 | if (!(MXC_RTCTMR->inten & MXC_F_RTC_INTEN_OVERFLOW)) { |
Anna Bridge |
186:707f6e361f3e | 145 | RTC_Stop(); |
Anna Bridge |
186:707f6e361f3e | 146 | } |
Anna Bridge |
186:707f6e361f3e | 147 | } |
Anna Bridge |
186:707f6e361f3e | 148 | |
Anna Bridge |
186:707f6e361f3e | 149 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 150 | uint32_t lp_ticker_read(void) |
Anna Bridge |
186:707f6e361f3e | 151 | { |
Anna Bridge |
186:707f6e361f3e | 152 | return MXC_RTCTMR->timer; |
Anna Bridge |
186:707f6e361f3e | 153 | } |
Anna Bridge |
186:707f6e361f3e | 154 | |
Anna Bridge |
186:707f6e361f3e | 155 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 156 | void lp_ticker_set_interrupt(timestamp_t timestamp) |
Anna Bridge |
186:707f6e361f3e | 157 | { |
Anna Bridge |
186:707f6e361f3e | 158 | MXC_RTCTMR->comp[0] = timestamp; |
Anna Bridge |
186:707f6e361f3e | 159 | MXC_RTCTMR->flags = MXC_F_RTC_FLAGS_COMP0; |
Anna Bridge |
186:707f6e361f3e | 160 | RTC_EnableINT(MXC_F_RTC_INTEN_COMP0); |
Anna Bridge |
186:707f6e361f3e | 161 | |
Anna Bridge |
186:707f6e361f3e | 162 | // Enable as LP wakeup source |
Anna Bridge |
186:707f6e361f3e | 163 | MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_FLAGS_RTC_CMPR0; |
Anna Bridge |
186:707f6e361f3e | 164 | |
Anna Bridge |
186:707f6e361f3e | 165 | // Postponed write pending wait for comp0 and flags |
Anna Bridge |
186:707f6e361f3e | 166 | while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING); |
Anna Bridge |
186:707f6e361f3e | 167 | } |
Anna Bridge |
186:707f6e361f3e | 168 | |
Anna Bridge |
186:707f6e361f3e | 169 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 170 | void lp_ticker_disable_interrupt(void) |
Anna Bridge |
186:707f6e361f3e | 171 | { |
Anna Bridge |
186:707f6e361f3e | 172 | RTC_DisableINT(MXC_F_RTC_INTEN_COMP0); |
Anna Bridge |
186:707f6e361f3e | 173 | } |
Anna Bridge |
186:707f6e361f3e | 174 | |
Anna Bridge |
186:707f6e361f3e | 175 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 176 | void lp_ticker_clear_interrupt(void) |
Anna Bridge |
186:707f6e361f3e | 177 | { |
Anna Bridge |
186:707f6e361f3e | 178 | RTC_ClearFlags(MXC_F_RTC_FLAGS_COMP0); |
Anna Bridge |
186:707f6e361f3e | 179 | } |
Anna Bridge |
186:707f6e361f3e | 180 | |
Anna Bridge |
186:707f6e361f3e | 181 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 182 | void lp_ticker_fire_interrupt(void) |
Anna Bridge |
186:707f6e361f3e | 183 | { |
Anna Bridge |
186:707f6e361f3e | 184 | NVIC_SetPendingIRQ(RTC0_IRQn); |
Anna Bridge |
186:707f6e361f3e | 185 | } |
Anna Bridge |
186:707f6e361f3e | 186 | |
Anna Bridge |
186:707f6e361f3e | 187 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 188 | const ticker_info_t *lp_ticker_get_info(void) |
Anna Bridge |
186:707f6e361f3e | 189 | { |
Anna Bridge |
186:707f6e361f3e | 190 | static const ticker_info_t info = { |
Anna Bridge |
186:707f6e361f3e | 191 | LP_TIMER_RATE_HZ, |
Anna Bridge |
186:707f6e361f3e | 192 | LP_TIMER_WIDTH |
Anna Bridge |
186:707f6e361f3e | 193 | }; |
Anna Bridge |
186:707f6e361f3e | 194 | |
Anna Bridge |
186:707f6e361f3e | 195 | return &info; |
Anna Bridge |
186:707f6e361f3e | 196 | } |