mbed library sources. Supersedes mbed-src. Add PORTG support for STM32L476JG (SensorTile kit)
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Child:
- 153:fa9ff456f731
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2015-2016 Nuvoton |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | |
<> | 149:156823d33999 | 17 | #include "lp_ticker_api.h" |
<> | 149:156823d33999 | 18 | |
<> | 149:156823d33999 | 19 | #if DEVICE_LOWPOWERTIMER |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | #include "sleep_api.h" |
<> | 149:156823d33999 | 22 | #include "nu_modutil.h" |
<> | 149:156823d33999 | 23 | #include "nu_miscutil.h" |
<> | 149:156823d33999 | 24 | #include "critical.h" |
<> | 149:156823d33999 | 25 | |
<> | 149:156823d33999 | 26 | // lp_ticker tick = us = timestamp |
<> | 149:156823d33999 | 27 | #define US_PER_TICK (1) |
<> | 149:156823d33999 | 28 | #define US_PER_SEC (1000 * 1000) |
<> | 149:156823d33999 | 29 | |
<> | 149:156823d33999 | 30 | #define US_PER_TMR2_INT (US_PER_SEC * 10) |
<> | 149:156823d33999 | 31 | #define TMR2_CLK_PER_SEC (__LXT) |
<> | 149:156823d33999 | 32 | #define TMR2_CLK_PER_TMR2_INT ((uint32_t) ((uint64_t) US_PER_TMR2_INT * TMR2_CLK_PER_SEC / US_PER_SEC)) |
<> | 149:156823d33999 | 33 | #define TMR3_CLK_PER_SEC (__LXT) |
<> | 149:156823d33999 | 34 | |
<> | 149:156823d33999 | 35 | static void tmr2_vec(void); |
<> | 149:156823d33999 | 36 | static void tmr3_vec(void); |
<> | 149:156823d33999 | 37 | static void lp_ticker_arm_cd(void); |
<> | 149:156823d33999 | 38 | |
<> | 149:156823d33999 | 39 | static int lp_ticker_inited = 0; |
<> | 149:156823d33999 | 40 | static volatile uint32_t counter_major = 0; |
<> | 149:156823d33999 | 41 | static volatile uint32_t cd_major_minor_clks = 0; |
<> | 149:156823d33999 | 42 | static volatile uint32_t cd_minor_clks = 0; |
<> | 149:156823d33999 | 43 | static volatile uint32_t wakeup_tick = (uint32_t) -1; |
<> | 149:156823d33999 | 44 | |
<> | 149:156823d33999 | 45 | // NOTE: To wake the system from power down mode, timer clock source must be ether LXT or LIRC. |
<> | 149:156823d33999 | 46 | // NOTE: TIMER_2 for normal counting and TIMER_3 for scheduled wakeup |
<> | 149:156823d33999 | 47 | static const struct nu_modinit_s timer2_modinit = {TIMER_2, TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_LXT, 0, TMR2_RST, TMR2_IRQn, (void *) tmr2_vec}; |
<> | 149:156823d33999 | 48 | static const struct nu_modinit_s timer3_modinit = {TIMER_3, TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_LXT, 0, TMR3_RST, TMR3_IRQn, (void *) tmr3_vec}; |
<> | 149:156823d33999 | 49 | |
<> | 149:156823d33999 | 50 | #define TMR_CMP_MIN 2 |
<> | 149:156823d33999 | 51 | #define TMR_CMP_MAX 0xFFFFFFu |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | void lp_ticker_init(void) |
<> | 149:156823d33999 | 54 | { |
<> | 149:156823d33999 | 55 | if (lp_ticker_inited) { |
<> | 149:156823d33999 | 56 | return; |
<> | 149:156823d33999 | 57 | } |
<> | 149:156823d33999 | 58 | lp_ticker_inited = 1; |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | counter_major = 0; |
<> | 149:156823d33999 | 61 | cd_major_minor_clks = 0; |
<> | 149:156823d33999 | 62 | cd_minor_clks = 0; |
<> | 149:156823d33999 | 63 | wakeup_tick = (uint32_t) -1; |
<> | 149:156823d33999 | 64 | |
<> | 149:156823d33999 | 65 | // Reset module |
<> | 149:156823d33999 | 66 | SYS_ResetModule(timer2_modinit.rsetidx); |
<> | 149:156823d33999 | 67 | SYS_ResetModule(timer3_modinit.rsetidx); |
<> | 149:156823d33999 | 68 | |
<> | 149:156823d33999 | 69 | // Select IP clock source |
<> | 149:156823d33999 | 70 | CLK_SetModuleClock(timer2_modinit.clkidx, timer2_modinit.clksrc, timer2_modinit.clkdiv); |
<> | 149:156823d33999 | 71 | CLK_SetModuleClock(timer3_modinit.clkidx, timer3_modinit.clksrc, timer3_modinit.clkdiv); |
<> | 149:156823d33999 | 72 | // Enable IP clock |
<> | 149:156823d33999 | 73 | CLK_EnableModuleClock(timer2_modinit.clkidx); |
<> | 149:156823d33999 | 74 | CLK_EnableModuleClock(timer3_modinit.clkidx); |
<> | 149:156823d33999 | 75 | |
<> | 149:156823d33999 | 76 | // Configure clock |
<> | 149:156823d33999 | 77 | uint32_t clk_timer2 = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(timer2_modinit.modname)); |
<> | 149:156823d33999 | 78 | uint32_t prescale_timer2 = clk_timer2 / TMR2_CLK_PER_SEC - 1; |
<> | 149:156823d33999 | 79 | MBED_ASSERT((prescale_timer2 != (uint32_t) -1) && prescale_timer2 <= 127); |
<> | 149:156823d33999 | 80 | MBED_ASSERT((clk_timer2 % TMR2_CLK_PER_SEC) == 0); |
<> | 149:156823d33999 | 81 | uint32_t cmp_timer2 = TMR2_CLK_PER_TMR2_INT; |
<> | 149:156823d33999 | 82 | MBED_ASSERT(cmp_timer2 >= TMR_CMP_MIN && cmp_timer2 <= TMR_CMP_MAX); |
<> | 149:156823d33999 | 83 | // Continuous mode |
<> | 149:156823d33999 | 84 | // NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451. In M451, TIMER_CNT is updated continuously by default. |
<> | 149:156823d33999 | 85 | ((TIMER_T *) NU_MODBASE(timer2_modinit.modname))->CTL = TIMER_PERIODIC_MODE | prescale_timer2/* | TIMER_CTL_CNTDATEN_Msk*/; |
<> | 149:156823d33999 | 86 | ((TIMER_T *) NU_MODBASE(timer2_modinit.modname))->CMP = cmp_timer2; |
<> | 149:156823d33999 | 87 | |
<> | 149:156823d33999 | 88 | // Set vector |
<> | 149:156823d33999 | 89 | NVIC_SetVector(timer2_modinit.irq_n, (uint32_t) timer2_modinit.var); |
<> | 149:156823d33999 | 90 | NVIC_SetVector(timer3_modinit.irq_n, (uint32_t) timer3_modinit.var); |
<> | 149:156823d33999 | 91 | |
<> | 149:156823d33999 | 92 | NVIC_EnableIRQ(timer2_modinit.irq_n); |
<> | 149:156823d33999 | 93 | NVIC_EnableIRQ(timer3_modinit.irq_n); |
<> | 149:156823d33999 | 94 | |
<> | 149:156823d33999 | 95 | TIMER_EnableInt((TIMER_T *) NU_MODBASE(timer2_modinit.modname)); |
<> | 149:156823d33999 | 96 | TIMER_EnableWakeup((TIMER_T *) NU_MODBASE(timer2_modinit.modname)); |
<> | 149:156823d33999 | 97 | |
<> | 149:156823d33999 | 98 | // Schedule wakeup to match semantics of lp_ticker_get_compare_match() |
<> | 149:156823d33999 | 99 | lp_ticker_set_interrupt(wakeup_tick); |
<> | 149:156823d33999 | 100 | |
<> | 149:156823d33999 | 101 | // Start timer |
<> | 149:156823d33999 | 102 | TIMER_Start((TIMER_T *) NU_MODBASE(timer2_modinit.modname)); |
<> | 149:156823d33999 | 103 | } |
<> | 149:156823d33999 | 104 | |
<> | 149:156823d33999 | 105 | timestamp_t lp_ticker_read() |
<> | 149:156823d33999 | 106 | { |
<> | 149:156823d33999 | 107 | if (! lp_ticker_inited) { |
<> | 149:156823d33999 | 108 | lp_ticker_init(); |
<> | 149:156823d33999 | 109 | } |
<> | 149:156823d33999 | 110 | |
<> | 149:156823d33999 | 111 | TIMER_T * timer2_base = (TIMER_T *) NU_MODBASE(timer2_modinit.modname); |
<> | 149:156823d33999 | 112 | |
<> | 149:156823d33999 | 113 | do { |
<> | 149:156823d33999 | 114 | uint64_t major_minor_clks; |
<> | 149:156823d33999 | 115 | uint32_t minor_clks; |
<> | 149:156823d33999 | 116 | |
<> | 149:156823d33999 | 117 | // NOTE: As TIMER_CNT = TIMER_CMP and counter_major has increased by one, TIMER_CNT doesn't change to 0 for one tick time. |
<> | 149:156823d33999 | 118 | // NOTE: As TIMER_CNT = TIMER_CMP or TIMER_CNT = 0, counter_major (ISR) may not sync with TIMER_CNT. So skip and fetch stable one at the cost of 1 clock delay on this read. |
<> | 149:156823d33999 | 119 | do { |
<> | 149:156823d33999 | 120 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 121 | |
<> | 149:156823d33999 | 122 | // NOTE: Order of reading minor_us/carry here is significant. |
<> | 149:156823d33999 | 123 | minor_clks = TIMER_GetCounter(timer2_base); |
<> | 149:156823d33999 | 124 | uint32_t carry = (timer2_base->INTSTS & TIMER_INTSTS_TIF_Msk) ? 1 : 0; |
<> | 149:156823d33999 | 125 | // When TIMER_CNT approaches TIMER_CMP and will wrap soon, we may get carry but TIMER_CNT not wrapped. Hanlde carefully carry == 1 && TIMER_CNT is near TIMER_CMP. |
<> | 149:156823d33999 | 126 | if (carry && minor_clks > (TMR2_CLK_PER_TMR2_INT / 2)) { |
<> | 149:156823d33999 | 127 | major_minor_clks = (counter_major + 1) * TMR2_CLK_PER_TMR2_INT; |
<> | 149:156823d33999 | 128 | } |
<> | 149:156823d33999 | 129 | else { |
<> | 149:156823d33999 | 130 | major_minor_clks = (counter_major + carry) * TMR2_CLK_PER_TMR2_INT + minor_clks; |
<> | 149:156823d33999 | 131 | } |
<> | 149:156823d33999 | 132 | |
<> | 149:156823d33999 | 133 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 134 | } |
<> | 149:156823d33999 | 135 | while (minor_clks == 0 || minor_clks == TMR2_CLK_PER_TMR2_INT); |
<> | 149:156823d33999 | 136 | |
<> | 149:156823d33999 | 137 | // Add power-down compensation |
<> | 149:156823d33999 | 138 | return ((uint64_t) major_minor_clks * US_PER_SEC / TMR3_CLK_PER_SEC / US_PER_TICK); |
<> | 149:156823d33999 | 139 | } |
<> | 149:156823d33999 | 140 | while (0); |
<> | 149:156823d33999 | 141 | } |
<> | 149:156823d33999 | 142 | |
<> | 149:156823d33999 | 143 | void lp_ticker_set_interrupt(timestamp_t timestamp) |
<> | 149:156823d33999 | 144 | { |
<> | 149:156823d33999 | 145 | uint32_t now = lp_ticker_read(); |
<> | 149:156823d33999 | 146 | wakeup_tick = timestamp; |
<> | 149:156823d33999 | 147 | |
<> | 149:156823d33999 | 148 | TIMER_Stop((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); |
<> | 149:156823d33999 | 149 | |
<> | 149:156823d33999 | 150 | /** |
<> | 149:156823d33999 | 151 | * FIXME: Scheduled alarm may go off incorrectly due to wrap around. |
<> | 149:156823d33999 | 152 | * Conditions in which delta is negative: |
<> | 149:156823d33999 | 153 | * 1. Wrap around |
<> | 149:156823d33999 | 154 | * 2. Newly scheduled alarm is behind now |
<> | 149:156823d33999 | 155 | */ |
<> | 149:156823d33999 | 156 | //int delta = (timestamp > now) ? (timestamp - now) : (uint32_t) ((uint64_t) timestamp + 0xFFFFFFFFu - now); |
<> | 149:156823d33999 | 157 | int delta = (int) (timestamp - now); |
<> | 149:156823d33999 | 158 | if (delta > 0) { |
<> | 149:156823d33999 | 159 | cd_major_minor_clks = (uint64_t) delta * US_PER_TICK * TMR3_CLK_PER_SEC / US_PER_SEC; |
<> | 149:156823d33999 | 160 | lp_ticker_arm_cd(); |
<> | 149:156823d33999 | 161 | } |
<> | 149:156823d33999 | 162 | else { |
<> | 149:156823d33999 | 163 | cd_major_minor_clks = cd_minor_clks = 0; |
<> | 149:156823d33999 | 164 | /** |
<> | 149:156823d33999 | 165 | * This event was in the past. Set the interrupt as pending, but don't process it here. |
<> | 149:156823d33999 | 166 | * This prevents a recurive loop under heavy load which can lead to a stack overflow. |
<> | 149:156823d33999 | 167 | */ |
<> | 149:156823d33999 | 168 | NVIC_SetPendingIRQ(timer3_modinit.irq_n); |
<> | 149:156823d33999 | 169 | } |
<> | 149:156823d33999 | 170 | } |
<> | 149:156823d33999 | 171 | |
<> | 149:156823d33999 | 172 | void lp_ticker_disable_interrupt(void) |
<> | 149:156823d33999 | 173 | { |
<> | 149:156823d33999 | 174 | TIMER_DisableInt((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); |
<> | 149:156823d33999 | 175 | } |
<> | 149:156823d33999 | 176 | |
<> | 149:156823d33999 | 177 | void lp_ticker_clear_interrupt(void) |
<> | 149:156823d33999 | 178 | { |
<> | 149:156823d33999 | 179 | TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); |
<> | 149:156823d33999 | 180 | } |
<> | 149:156823d33999 | 181 | |
<> | 149:156823d33999 | 182 | static void tmr2_vec(void) |
<> | 149:156823d33999 | 183 | { |
<> | 149:156823d33999 | 184 | TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer2_modinit.modname)); |
<> | 149:156823d33999 | 185 | TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(timer2_modinit.modname)); |
<> | 149:156823d33999 | 186 | counter_major ++; |
<> | 149:156823d33999 | 187 | } |
<> | 149:156823d33999 | 188 | |
<> | 149:156823d33999 | 189 | static void tmr3_vec(void) |
<> | 149:156823d33999 | 190 | { |
<> | 149:156823d33999 | 191 | TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); |
<> | 149:156823d33999 | 192 | TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); |
<> | 149:156823d33999 | 193 | cd_major_minor_clks = (cd_major_minor_clks > cd_minor_clks) ? (cd_major_minor_clks - cd_minor_clks) : 0; |
<> | 149:156823d33999 | 194 | if (cd_major_minor_clks == 0) { |
<> | 149:156823d33999 | 195 | // NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler(); |
<> | 149:156823d33999 | 196 | lp_ticker_irq_handler(); |
<> | 149:156823d33999 | 197 | } |
<> | 149:156823d33999 | 198 | else { |
<> | 149:156823d33999 | 199 | lp_ticker_arm_cd(); |
<> | 149:156823d33999 | 200 | } |
<> | 149:156823d33999 | 201 | } |
<> | 149:156823d33999 | 202 | |
<> | 149:156823d33999 | 203 | static void lp_ticker_arm_cd(void) |
<> | 149:156823d33999 | 204 | { |
<> | 149:156823d33999 | 205 | TIMER_T * timer3_base = (TIMER_T *) NU_MODBASE(timer3_modinit.modname); |
<> | 149:156823d33999 | 206 | |
<> | 149:156823d33999 | 207 | // Reset 8-bit PSC counter, 24-bit up counter value and CNTEN bit |
<> | 149:156823d33999 | 208 | timer3_base->CTL |= TIMER_CTL_RSTCNT_Msk; |
<> | 149:156823d33999 | 209 | // One-shot mode, Clock = 1 KHz |
<> | 149:156823d33999 | 210 | uint32_t clk_timer3 = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); |
<> | 149:156823d33999 | 211 | uint32_t prescale_timer3 = clk_timer3 / TMR3_CLK_PER_SEC - 1; |
<> | 149:156823d33999 | 212 | MBED_ASSERT((prescale_timer3 != (uint32_t) -1) && prescale_timer3 <= 127); |
<> | 149:156823d33999 | 213 | MBED_ASSERT((clk_timer3 % TMR3_CLK_PER_SEC) == 0); |
<> | 149:156823d33999 | 214 | // NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451. In M451, TIMER_CNT is updated continuously by default. |
<> | 149:156823d33999 | 215 | timer3_base->CTL &= ~(TIMER_CTL_OPMODE_Msk | TIMER_CTL_PSC_Msk/* | TIMER_CTL_CNTDATEN_Msk*/); |
<> | 149:156823d33999 | 216 | timer3_base->CTL |= TIMER_ONESHOT_MODE | prescale_timer3/* | TIMER_CTL_CNTDATEN_Msk*/; |
<> | 149:156823d33999 | 217 | |
<> | 149:156823d33999 | 218 | cd_minor_clks = cd_major_minor_clks; |
<> | 149:156823d33999 | 219 | cd_minor_clks = NU_CLAMP(cd_minor_clks, TMR_CMP_MIN, TMR_CMP_MAX); |
<> | 149:156823d33999 | 220 | timer3_base->CMP = cd_minor_clks; |
<> | 149:156823d33999 | 221 | |
<> | 149:156823d33999 | 222 | TIMER_EnableInt(timer3_base); |
<> | 149:156823d33999 | 223 | TIMER_EnableWakeup((TIMER_T *) NU_MODBASE(timer3_modinit.modname)); |
<> | 149:156823d33999 | 224 | TIMER_Start(timer3_base); |
<> | 149:156823d33999 | 225 | } |
<> | 149:156823d33999 | 226 | #endif |