Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed_blinky-bmd-200 bmd-200_accel_demo firstRig
Fork of mbed-src by
targets/hal/TARGET_Maxim/TARGET_MAX32610/rtc_api.c@510:d4fc7603a669, 2015-04-08 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Apr 08 07:45:08 2015 +0100
- Revision:
- 510:d4fc7603a669
- Child:
- 540:c48d7048ab6e
Synchronized with git revision 158cbeb2927b64c560005dbec6f60463a468c9da
Full URL: https://github.com/mbedmicro/mbed/commit/158cbeb2927b64c560005dbec6f60463a468c9da/
USB - Add macros to alias the endpoint callback functions to support configurability
Who changed what in which revision?
User | Revision | Line number | New 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 | 510:d4fc7603a669 | 35 | #include "cmsis.h" |
mbed_official | 510:d4fc7603a669 | 36 | #include "rtc_regs.h" |
mbed_official | 510:d4fc7603a669 | 37 | #include "pwrseq_regs.h" |
mbed_official | 510:d4fc7603a669 | 38 | #include "clkman_regs.h" |
mbed_official | 510:d4fc7603a669 | 39 | |
mbed_official | 510:d4fc7603a669 | 40 | static int rtc_inited = 0; |
mbed_official | 510:d4fc7603a669 | 41 | static volatile uint32_t overflow_cnt = 0; |
mbed_official | 510:d4fc7603a669 | 42 | static uint32_t overflow_alarm = 0; |
mbed_official | 510:d4fc7603a669 | 43 | |
mbed_official | 510:d4fc7603a669 | 44 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 45 | static void overflow_handler(void) |
mbed_official | 510:d4fc7603a669 | 46 | { |
mbed_official | 510:d4fc7603a669 | 47 | MXC_RTCTMR->flags = MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS; |
mbed_official | 510:d4fc7603a669 | 48 | overflow_cnt++; |
mbed_official | 510:d4fc7603a669 | 49 | |
mbed_official | 510:d4fc7603a669 | 50 | if (overflow_cnt == overflow_alarm) { |
mbed_official | 510:d4fc7603a669 | 51 | // Enable the comparator interrupt for the alarm |
mbed_official | 510:d4fc7603a669 | 52 | MXC_RTCTMR->inten |= MXC_F_RTC_INTEN_COMP0; |
mbed_official | 510:d4fc7603a669 | 53 | } |
mbed_official | 510:d4fc7603a669 | 54 | } |
mbed_official | 510:d4fc7603a669 | 55 | |
mbed_official | 510:d4fc7603a669 | 56 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 57 | static void alarm_handler(void) |
mbed_official | 510:d4fc7603a669 | 58 | { |
mbed_official | 510:d4fc7603a669 | 59 | MXC_RTCTMR->inten &= ~MXC_F_RTC_INTEN_COMP0; |
mbed_official | 510:d4fc7603a669 | 60 | MXC_RTCTMR->flags = MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS; |
mbed_official | 510:d4fc7603a669 | 61 | } |
mbed_official | 510:d4fc7603a669 | 62 | |
mbed_official | 510:d4fc7603a669 | 63 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 64 | void rtc_init(void) |
mbed_official | 510:d4fc7603a669 | 65 | { |
mbed_official | 510:d4fc7603a669 | 66 | if(rtc_inited) { |
mbed_official | 510:d4fc7603a669 | 67 | return; |
mbed_official | 510:d4fc7603a669 | 68 | } |
mbed_official | 510:d4fc7603a669 | 69 | rtc_inited = 1; |
mbed_official | 510:d4fc7603a669 | 70 | |
mbed_official | 510:d4fc7603a669 | 71 | // Enable the clock to the synchronizer |
mbed_official | 510:d4fc7603a669 | 72 | MXC_CLKMAN->clk_ctrl_13_rtc_int_sync = MXC_E_CLKMAN_CLK_SCALE_ENABLED; |
mbed_official | 510:d4fc7603a669 | 73 | |
mbed_official | 510:d4fc7603a669 | 74 | // Enable the clock to the RTC |
mbed_official | 510:d4fc7603a669 | 75 | MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN; |
mbed_official | 510:d4fc7603a669 | 76 | |
mbed_official | 510:d4fc7603a669 | 77 | // Set the divider from the 4kHz clock |
mbed_official | 510:d4fc7603a669 | 78 | MXC_RTCTMR->prescale = MXC_E_RTC_PRESCALE_DIV_2_0; |
mbed_official | 510:d4fc7603a669 | 79 | |
mbed_official | 510:d4fc7603a669 | 80 | // Enable the overflow interrupt |
mbed_official | 510:d4fc7603a669 | 81 | MXC_RTCTMR->inten |= MXC_F_RTC_FLAGS_OVERFLOW; |
mbed_official | 510:d4fc7603a669 | 82 | |
mbed_official | 510:d4fc7603a669 | 83 | // Prepare interrupt handlers |
mbed_official | 510:d4fc7603a669 | 84 | NVIC_SetVector(RTC0_IRQn, (uint32_t)alarm_handler); |
mbed_official | 510:d4fc7603a669 | 85 | NVIC_EnableIRQ(RTC0_IRQn); |
mbed_official | 510:d4fc7603a669 | 86 | NVIC_SetVector(RTC3_IRQn, (uint32_t)overflow_handler); |
mbed_official | 510:d4fc7603a669 | 87 | NVIC_EnableIRQ(RTC3_IRQn); |
mbed_official | 510:d4fc7603a669 | 88 | |
mbed_official | 510:d4fc7603a669 | 89 | // Enable the RTC |
mbed_official | 510:d4fc7603a669 | 90 | MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_ENABLE; |
mbed_official | 510:d4fc7603a669 | 91 | } |
mbed_official | 510:d4fc7603a669 | 92 | |
mbed_official | 510:d4fc7603a669 | 93 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 94 | void rtc_free(void) |
mbed_official | 510:d4fc7603a669 | 95 | { |
mbed_official | 510:d4fc7603a669 | 96 | if (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_ENABLE) { |
mbed_official | 510:d4fc7603a669 | 97 | // Clear and disable RTC |
mbed_official | 510:d4fc7603a669 | 98 | MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_CLEAR; |
mbed_official | 510:d4fc7603a669 | 99 | MXC_RTCTMR->ctrl &= ~MXC_F_RTC_CTRL_ENABLE; |
mbed_official | 510:d4fc7603a669 | 100 | |
mbed_official | 510:d4fc7603a669 | 101 | // Wait for pending transactions |
mbed_official | 510:d4fc7603a669 | 102 | while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING); |
mbed_official | 510:d4fc7603a669 | 103 | } |
mbed_official | 510:d4fc7603a669 | 104 | |
mbed_official | 510:d4fc7603a669 | 105 | // Disable the clock to the RTC |
mbed_official | 510:d4fc7603a669 | 106 | MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP); |
mbed_official | 510:d4fc7603a669 | 107 | |
mbed_official | 510:d4fc7603a669 | 108 | // Disable the clock to the synchronizer |
mbed_official | 510:d4fc7603a669 | 109 | MXC_CLKMAN->clk_ctrl_13_rtc_int_sync = MXC_E_CLKMAN_CLK_SCALE_DISABLED; |
mbed_official | 510:d4fc7603a669 | 110 | } |
mbed_official | 510:d4fc7603a669 | 111 | |
mbed_official | 510:d4fc7603a669 | 112 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 113 | int rtc_isenabled(void) |
mbed_official | 510:d4fc7603a669 | 114 | { |
mbed_official | 510:d4fc7603a669 | 115 | return (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_ENABLE); |
mbed_official | 510:d4fc7603a669 | 116 | } |
mbed_official | 510:d4fc7603a669 | 117 | |
mbed_official | 510:d4fc7603a669 | 118 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 119 | time_t rtc_read(void) |
mbed_official | 510:d4fc7603a669 | 120 | { |
mbed_official | 510:d4fc7603a669 | 121 | unsigned int shift_amt; |
mbed_official | 510:d4fc7603a669 | 122 | uint32_t ovf_cnt_1, ovf_cnt_2, timer_cnt; |
mbed_official | 510:d4fc7603a669 | 123 | |
mbed_official | 510:d4fc7603a669 | 124 | // Account for a change in the default prescaler |
mbed_official | 510:d4fc7603a669 | 125 | shift_amt = MXC_E_RTC_PRESCALE_DIV_2_12 - MXC_RTCTMR->prescale; |
mbed_official | 510:d4fc7603a669 | 126 | |
mbed_official | 510:d4fc7603a669 | 127 | // Ensure coherency between overflow_cnt and timer |
mbed_official | 510:d4fc7603a669 | 128 | do { |
mbed_official | 510:d4fc7603a669 | 129 | ovf_cnt_1 = overflow_cnt; |
mbed_official | 510:d4fc7603a669 | 130 | timer_cnt = MXC_RTCTMR->timer; |
mbed_official | 510:d4fc7603a669 | 131 | ovf_cnt_2 = overflow_cnt; |
mbed_official | 510:d4fc7603a669 | 132 | } while (ovf_cnt_1 != ovf_cnt_2); |
mbed_official | 510:d4fc7603a669 | 133 | |
mbed_official | 510:d4fc7603a669 | 134 | return (timer_cnt >> shift_amt) + (ovf_cnt_1 << (32 - shift_amt)); |
mbed_official | 510:d4fc7603a669 | 135 | } |
mbed_official | 510:d4fc7603a669 | 136 | |
mbed_official | 510:d4fc7603a669 | 137 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 138 | uint64_t rtc_read_us(void) |
mbed_official | 510:d4fc7603a669 | 139 | { |
mbed_official | 510:d4fc7603a669 | 140 | unsigned int shift_amt; |
mbed_official | 510:d4fc7603a669 | 141 | uint32_t ovf_cnt_1, ovf_cnt_2, timer_cnt; |
mbed_official | 510:d4fc7603a669 | 142 | uint64_t currentUs; |
mbed_official | 510:d4fc7603a669 | 143 | |
mbed_official | 510:d4fc7603a669 | 144 | // Account for a change in the default prescaler |
mbed_official | 510:d4fc7603a669 | 145 | shift_amt = MXC_E_RTC_PRESCALE_DIV_2_12 - MXC_RTCTMR->prescale; |
mbed_official | 510:d4fc7603a669 | 146 | |
mbed_official | 510:d4fc7603a669 | 147 | // Ensure coherency between overflow_cnt and timer |
mbed_official | 510:d4fc7603a669 | 148 | do { |
mbed_official | 510:d4fc7603a669 | 149 | ovf_cnt_1 = overflow_cnt; |
mbed_official | 510:d4fc7603a669 | 150 | timer_cnt = MXC_RTCTMR->timer; |
mbed_official | 510:d4fc7603a669 | 151 | ovf_cnt_2 = overflow_cnt; |
mbed_official | 510:d4fc7603a669 | 152 | } while (ovf_cnt_1 != ovf_cnt_2); |
mbed_official | 510:d4fc7603a669 | 153 | |
mbed_official | 510:d4fc7603a669 | 154 | currentUs = (((uint64_t)timer_cnt * 1000000) >> shift_amt) + (((uint64_t)ovf_cnt_1 * 1000000) << (32 - shift_amt)); |
mbed_official | 510:d4fc7603a669 | 155 | |
mbed_official | 510:d4fc7603a669 | 156 | return currentUs; |
mbed_official | 510:d4fc7603a669 | 157 | } |
mbed_official | 510:d4fc7603a669 | 158 | |
mbed_official | 510:d4fc7603a669 | 159 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 160 | void rtc_write(time_t t) |
mbed_official | 510:d4fc7603a669 | 161 | { |
mbed_official | 510:d4fc7603a669 | 162 | // Account for a change in the default prescaler |
mbed_official | 510:d4fc7603a669 | 163 | unsigned int shift_amt = MXC_E_RTC_PRESCALE_DIV_2_12 - MXC_RTCTMR->prescale; |
mbed_official | 510:d4fc7603a669 | 164 | |
mbed_official | 510:d4fc7603a669 | 165 | MXC_RTCTMR->ctrl &= ~MXC_F_RTC_CTRL_ENABLE; // disable the timer while updating |
mbed_official | 510:d4fc7603a669 | 166 | MXC_RTCTMR->timer = t << shift_amt; |
mbed_official | 510:d4fc7603a669 | 167 | overflow_cnt = t >> (32 - shift_amt); |
mbed_official | 510:d4fc7603a669 | 168 | MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_ENABLE; // enable the timer while updating |
mbed_official | 510:d4fc7603a669 | 169 | } |
mbed_official | 510:d4fc7603a669 | 170 | |
mbed_official | 510:d4fc7603a669 | 171 | //****************************************************************************** |
mbed_official | 510:d4fc7603a669 | 172 | void rtc_set_wakeup(uint64_t wakeupUs) |
mbed_official | 510:d4fc7603a669 | 173 | { |
mbed_official | 510:d4fc7603a669 | 174 | // Account for a change in the default prescaler |
mbed_official | 510:d4fc7603a669 | 175 | unsigned int shift_amt = MXC_E_RTC_PRESCALE_DIV_2_12 - MXC_RTCTMR->prescale; |
mbed_official | 510:d4fc7603a669 | 176 | |
mbed_official | 510:d4fc7603a669 | 177 | // Disable the alarm while it is prepared |
mbed_official | 510:d4fc7603a669 | 178 | MXC_RTCTMR->inten &= ~MXC_F_RTC_INTEN_COMP0; |
mbed_official | 510:d4fc7603a669 | 179 | MXC_RTCTMR->flags = MXC_F_RTC_FLAGS_COMP0; // clear interrupt |
mbed_official | 510:d4fc7603a669 | 180 | |
mbed_official | 510:d4fc7603a669 | 181 | overflow_alarm = (wakeupUs >> (32 - shift_amt)) / 1000000; |
mbed_official | 510:d4fc7603a669 | 182 | |
mbed_official | 510:d4fc7603a669 | 183 | if (overflow_alarm == overflow_cnt) { |
mbed_official | 510:d4fc7603a669 | 184 | MXC_RTCTMR->comp[0] = (wakeupUs << shift_amt) / 1000000; |
mbed_official | 510:d4fc7603a669 | 185 | MXC_RTCTMR->inten |= MXC_F_RTC_INTEN_COMP0; |
mbed_official | 510:d4fc7603a669 | 186 | } |
mbed_official | 510:d4fc7603a669 | 187 | |
mbed_official | 510:d4fc7603a669 | 188 | // Enable wakeup from RTC |
mbed_official | 510:d4fc7603a669 | 189 | MXC_PWRSEQ->msk_flags &= ~(MXC_F_PWRSEQ_MSK_FLAGS_RTC_ROLLOVER | MXC_F_PWRSEQ_MSK_FLAGS_RTC_CMPR0); |
mbed_official | 510:d4fc7603a669 | 190 | } |