test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elijahsj 1:8a094db1347f 1 /* mbed Microcontroller Library
elijahsj 1:8a094db1347f 2 * Copyright (c) 2015-2016 Nuvoton
elijahsj 1:8a094db1347f 3 *
elijahsj 1:8a094db1347f 4 * Licensed under the Apache License, Version 2.0 (the "License");
elijahsj 1:8a094db1347f 5 * you may not use this file except in compliance with the License.
elijahsj 1:8a094db1347f 6 * You may obtain a copy of the License at
elijahsj 1:8a094db1347f 7 *
elijahsj 1:8a094db1347f 8 * http://www.apache.org/licenses/LICENSE-2.0
elijahsj 1:8a094db1347f 9 *
elijahsj 1:8a094db1347f 10 * Unless required by applicable law or agreed to in writing, software
elijahsj 1:8a094db1347f 11 * distributed under the License is distributed on an "AS IS" BASIS,
elijahsj 1:8a094db1347f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elijahsj 1:8a094db1347f 13 * See the License for the specific language governing permissions and
elijahsj 1:8a094db1347f 14 * limitations under the License.
elijahsj 1:8a094db1347f 15 */
elijahsj 1:8a094db1347f 16
elijahsj 1:8a094db1347f 17 #include "us_ticker_api.h"
elijahsj 1:8a094db1347f 18 #include "sleep_api.h"
elijahsj 1:8a094db1347f 19 #include "mbed_assert.h"
elijahsj 1:8a094db1347f 20 #include "nu_modutil.h"
elijahsj 1:8a094db1347f 21 #include "nu_miscutil.h"
elijahsj 1:8a094db1347f 22 #include "mbed_critical.h"
elijahsj 1:8a094db1347f 23
elijahsj 1:8a094db1347f 24 // us_ticker tick = us = timestamp
elijahsj 1:8a094db1347f 25 #define US_PER_TICK 1
elijahsj 1:8a094db1347f 26 #define US_PER_SEC (1000 * 1000)
elijahsj 1:8a094db1347f 27
elijahsj 1:8a094db1347f 28 #define TMR0HIRES_CLK_PER_SEC (1000 * 1000)
elijahsj 1:8a094db1347f 29 #define TMR1HIRES_CLK_PER_SEC (1000 * 1000)
elijahsj 1:8a094db1347f 30
elijahsj 1:8a094db1347f 31 #define US_PER_TMR0HIRES_CLK (US_PER_SEC / TMR0HIRES_CLK_PER_SEC)
elijahsj 1:8a094db1347f 32 #define US_PER_TMR1HIRES_CLK (US_PER_SEC / TMR1HIRES_CLK_PER_SEC)
elijahsj 1:8a094db1347f 33
elijahsj 1:8a094db1347f 34 #define US_PER_TMR0HIRES_INT (1000 * 1000 * 10)
elijahsj 1:8a094db1347f 35 #define TMR0HIRES_CLK_PER_TMR0HIRES_INT ((uint32_t) ((uint64_t) US_PER_TMR0HIRES_INT * TMR0HIRES_CLK_PER_SEC / US_PER_SEC))
elijahsj 1:8a094db1347f 36
elijahsj 1:8a094db1347f 37
elijahsj 1:8a094db1347f 38 static void tmr0_vec(void);
elijahsj 1:8a094db1347f 39 static void tmr1_vec(void);
elijahsj 1:8a094db1347f 40 static void us_ticker_arm_cd(void);
elijahsj 1:8a094db1347f 41
elijahsj 1:8a094db1347f 42 static int us_ticker_inited = 0;
elijahsj 1:8a094db1347f 43 static volatile uint32_t counter_major = 0;
elijahsj 1:8a094db1347f 44 static volatile uint32_t cd_major_minor_us = 0;
elijahsj 1:8a094db1347f 45 static volatile uint32_t cd_minor_us = 0;
elijahsj 1:8a094db1347f 46
elijahsj 1:8a094db1347f 47 // NOTE: PCLK is set up in mbed_sdk_init(), invocation of which must be before C++ global object constructor. See init_api.c for details.
elijahsj 1:8a094db1347f 48 // NOTE: Choose clock source of timer:
elijahsj 1:8a094db1347f 49 // 1. HIRC: Be the most accurate but might cause unknown HardFault.
elijahsj 1:8a094db1347f 50 // 2. HXT: Less accurate and cannot pass mbed-drivers test.
elijahsj 1:8a094db1347f 51 // 3. PCLK(HXT): Less accurate but can pass mbed-drivers test.
elijahsj 1:8a094db1347f 52 // NOTE: TIMER_0 for normal counter, TIMER_1 for countdown.
elijahsj 1:8a094db1347f 53 static const struct nu_modinit_s timer0hires_modinit = {TIMER_0, TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0, TMR0_RST, TMR0_IRQn, (void *) tmr0_vec};
elijahsj 1:8a094db1347f 54 static const struct nu_modinit_s timer1hires_modinit = {TIMER_1, TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_PCLK0, 0, TMR1_RST, TMR1_IRQn, (void *) tmr1_vec};
elijahsj 1:8a094db1347f 55
elijahsj 1:8a094db1347f 56 #define TMR_CMP_MIN 2
elijahsj 1:8a094db1347f 57 #define TMR_CMP_MAX 0xFFFFFFu
elijahsj 1:8a094db1347f 58
elijahsj 1:8a094db1347f 59 void us_ticker_init(void)
elijahsj 1:8a094db1347f 60 {
elijahsj 1:8a094db1347f 61 if (us_ticker_inited) {
elijahsj 1:8a094db1347f 62 return;
elijahsj 1:8a094db1347f 63 }
elijahsj 1:8a094db1347f 64
elijahsj 1:8a094db1347f 65 counter_major = 0;
elijahsj 1:8a094db1347f 66 cd_major_minor_us = 0;
elijahsj 1:8a094db1347f 67 cd_minor_us = 0;
elijahsj 1:8a094db1347f 68 us_ticker_inited = 1;
elijahsj 1:8a094db1347f 69
elijahsj 1:8a094db1347f 70 // Reset IP
elijahsj 1:8a094db1347f 71 SYS_ResetModule(timer0hires_modinit.rsetidx);
elijahsj 1:8a094db1347f 72 SYS_ResetModule(timer1hires_modinit.rsetidx);
elijahsj 1:8a094db1347f 73
elijahsj 1:8a094db1347f 74 // Select IP clock source
elijahsj 1:8a094db1347f 75 CLK_SetModuleClock(timer0hires_modinit.clkidx, timer0hires_modinit.clksrc, timer0hires_modinit.clkdiv);
elijahsj 1:8a094db1347f 76 CLK_SetModuleClock(timer1hires_modinit.clkidx, timer1hires_modinit.clksrc, timer1hires_modinit.clkdiv);
elijahsj 1:8a094db1347f 77 // Enable IP clock
elijahsj 1:8a094db1347f 78 CLK_EnableModuleClock(timer0hires_modinit.clkidx);
elijahsj 1:8a094db1347f 79 CLK_EnableModuleClock(timer1hires_modinit.clkidx);
elijahsj 1:8a094db1347f 80
elijahsj 1:8a094db1347f 81 // Timer for normal counter
elijahsj 1:8a094db1347f 82 uint32_t clk_timer0 = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
elijahsj 1:8a094db1347f 83 uint32_t prescale_timer0 = clk_timer0 / TMR0HIRES_CLK_PER_SEC - 1;
elijahsj 1:8a094db1347f 84 MBED_ASSERT((prescale_timer0 != (uint32_t) -1) && prescale_timer0 <= 127);
elijahsj 1:8a094db1347f 85 MBED_ASSERT((clk_timer0 % TMR0HIRES_CLK_PER_SEC) == 0);
elijahsj 1:8a094db1347f 86 uint32_t cmp_timer0 = TMR0HIRES_CLK_PER_TMR0HIRES_INT;
elijahsj 1:8a094db1347f 87 MBED_ASSERT(cmp_timer0 >= TMR_CMP_MIN && cmp_timer0 <= TMR_CMP_MAX);
elijahsj 1:8a094db1347f 88 // NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451. In M451, TIMER_CNT is updated continuously by default.
elijahsj 1:8a094db1347f 89 ((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname))->CTL = TIMER_PERIODIC_MODE | prescale_timer0/* | TIMER_CTL_CNTDATEN_Msk*/;
elijahsj 1:8a094db1347f 90 ((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname))->CMP = cmp_timer0;
elijahsj 1:8a094db1347f 91
elijahsj 1:8a094db1347f 92 NVIC_SetVector(timer0hires_modinit.irq_n, (uint32_t) timer0hires_modinit.var);
elijahsj 1:8a094db1347f 93 NVIC_SetVector(timer1hires_modinit.irq_n, (uint32_t) timer1hires_modinit.var);
elijahsj 1:8a094db1347f 94
elijahsj 1:8a094db1347f 95 NVIC_EnableIRQ(timer0hires_modinit.irq_n);
elijahsj 1:8a094db1347f 96 NVIC_EnableIRQ(timer1hires_modinit.irq_n);
elijahsj 1:8a094db1347f 97
elijahsj 1:8a094db1347f 98 TIMER_EnableInt((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
elijahsj 1:8a094db1347f 99 TIMER_Start((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
elijahsj 1:8a094db1347f 100 }
elijahsj 1:8a094db1347f 101
elijahsj 1:8a094db1347f 102 uint32_t us_ticker_read()
elijahsj 1:8a094db1347f 103 {
elijahsj 1:8a094db1347f 104 if (! us_ticker_inited) {
elijahsj 1:8a094db1347f 105 us_ticker_init();
elijahsj 1:8a094db1347f 106 }
elijahsj 1:8a094db1347f 107
elijahsj 1:8a094db1347f 108 TIMER_T * timer0_base = (TIMER_T *) NU_MODBASE(timer0hires_modinit.modname);
elijahsj 1:8a094db1347f 109
elijahsj 1:8a094db1347f 110 do {
elijahsj 1:8a094db1347f 111 uint32_t major_minor_us;
elijahsj 1:8a094db1347f 112 uint32_t minor_us;
elijahsj 1:8a094db1347f 113
elijahsj 1:8a094db1347f 114 // NOTE: As TIMER_CNT = TIMER_CMP and counter_major has increased by one, TIMER_CNT doesn't change to 0 for one tick time.
elijahsj 1:8a094db1347f 115 // 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.
elijahsj 1:8a094db1347f 116 do {
elijahsj 1:8a094db1347f 117 core_util_critical_section_enter();
elijahsj 1:8a094db1347f 118
elijahsj 1:8a094db1347f 119 // NOTE: Order of reading minor_us/carry here is significant.
elijahsj 1:8a094db1347f 120 minor_us = TIMER_GetCounter(timer0_base) * US_PER_TMR0HIRES_CLK;
elijahsj 1:8a094db1347f 121 uint32_t carry = (timer0_base->INTSTS & TIMER_INTSTS_TIF_Msk) ? 1 : 0;
elijahsj 1:8a094db1347f 122 // 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.
elijahsj 1:8a094db1347f 123 if (carry && minor_us > (US_PER_TMR0HIRES_INT / 2)) {
elijahsj 1:8a094db1347f 124 major_minor_us = (counter_major + 1) * US_PER_TMR0HIRES_INT;
elijahsj 1:8a094db1347f 125 }
elijahsj 1:8a094db1347f 126 else {
elijahsj 1:8a094db1347f 127 major_minor_us = (counter_major + carry) * US_PER_TMR0HIRES_INT + minor_us;
elijahsj 1:8a094db1347f 128 }
elijahsj 1:8a094db1347f 129
elijahsj 1:8a094db1347f 130 core_util_critical_section_exit();
elijahsj 1:8a094db1347f 131 }
elijahsj 1:8a094db1347f 132 while (minor_us == 0 || minor_us == US_PER_TMR0HIRES_INT);
elijahsj 1:8a094db1347f 133
elijahsj 1:8a094db1347f 134 return (major_minor_us / US_PER_TICK);
elijahsj 1:8a094db1347f 135 }
elijahsj 1:8a094db1347f 136 while (0);
elijahsj 1:8a094db1347f 137 }
elijahsj 1:8a094db1347f 138
elijahsj 1:8a094db1347f 139 void us_ticker_disable_interrupt(void)
elijahsj 1:8a094db1347f 140 {
elijahsj 1:8a094db1347f 141 TIMER_DisableInt((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
elijahsj 1:8a094db1347f 142 }
elijahsj 1:8a094db1347f 143
elijahsj 1:8a094db1347f 144 void us_ticker_clear_interrupt(void)
elijahsj 1:8a094db1347f 145 {
elijahsj 1:8a094db1347f 146 TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
elijahsj 1:8a094db1347f 147 }
elijahsj 1:8a094db1347f 148
elijahsj 1:8a094db1347f 149 void us_ticker_set_interrupt(timestamp_t timestamp)
elijahsj 1:8a094db1347f 150 {
elijahsj 1:8a094db1347f 151 TIMER_Stop((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
elijahsj 1:8a094db1347f 152
elijahsj 1:8a094db1347f 153 uint32_t delta = timestamp - us_ticker_read();
elijahsj 1:8a094db1347f 154 cd_major_minor_us = delta * US_PER_TICK;
elijahsj 1:8a094db1347f 155 us_ticker_arm_cd();
elijahsj 1:8a094db1347f 156 }
elijahsj 1:8a094db1347f 157
elijahsj 1:8a094db1347f 158 void us_ticker_fire_interrupt(void)
elijahsj 1:8a094db1347f 159 {
elijahsj 1:8a094db1347f 160 cd_major_minor_us = cd_minor_us = 0;
elijahsj 1:8a094db1347f 161 /**
elijahsj 1:8a094db1347f 162 * This event was in the past. Set the interrupt as pending, but don't process it here.
elijahsj 1:8a094db1347f 163 * This prevents a recurive loop under heavy load which can lead to a stack overflow.
elijahsj 1:8a094db1347f 164 */
elijahsj 1:8a094db1347f 165 NVIC_SetPendingIRQ(timer1hires_modinit.irq_n);
elijahsj 1:8a094db1347f 166 }
elijahsj 1:8a094db1347f 167
elijahsj 1:8a094db1347f 168 static void tmr0_vec(void)
elijahsj 1:8a094db1347f 169 {
elijahsj 1:8a094db1347f 170 TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
elijahsj 1:8a094db1347f 171 counter_major ++;
elijahsj 1:8a094db1347f 172 }
elijahsj 1:8a094db1347f 173
elijahsj 1:8a094db1347f 174 static void tmr1_vec(void)
elijahsj 1:8a094db1347f 175 {
elijahsj 1:8a094db1347f 176 TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
elijahsj 1:8a094db1347f 177 cd_major_minor_us = (cd_major_minor_us > cd_minor_us) ? (cd_major_minor_us - cd_minor_us) : 0;
elijahsj 1:8a094db1347f 178 if (cd_major_minor_us == 0) {
elijahsj 1:8a094db1347f 179 // NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
elijahsj 1:8a094db1347f 180 us_ticker_irq_handler();
elijahsj 1:8a094db1347f 181 }
elijahsj 1:8a094db1347f 182 else {
elijahsj 1:8a094db1347f 183 us_ticker_arm_cd();
elijahsj 1:8a094db1347f 184 }
elijahsj 1:8a094db1347f 185 }
elijahsj 1:8a094db1347f 186
elijahsj 1:8a094db1347f 187 static void us_ticker_arm_cd(void)
elijahsj 1:8a094db1347f 188 {
elijahsj 1:8a094db1347f 189 TIMER_T * timer1_base = (TIMER_T *) NU_MODBASE(timer1hires_modinit.modname);
elijahsj 1:8a094db1347f 190
elijahsj 1:8a094db1347f 191 cd_minor_us = cd_major_minor_us;
elijahsj 1:8a094db1347f 192
elijahsj 1:8a094db1347f 193 // Reset 8-bit PSC counter, 24-bit up counter value and CNTEN bit
elijahsj 1:8a094db1347f 194 timer1_base->CTL |= TIMER_CTL_RSTCNT_Msk;
elijahsj 1:8a094db1347f 195 // One-shot mode, Clock = 1 MHz
elijahsj 1:8a094db1347f 196 uint32_t clk_timer1 = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
elijahsj 1:8a094db1347f 197 uint32_t prescale_timer1 = clk_timer1 / TMR1HIRES_CLK_PER_SEC - 1;
elijahsj 1:8a094db1347f 198 MBED_ASSERT((prescale_timer1 != (uint32_t) -1) && prescale_timer1 <= 127);
elijahsj 1:8a094db1347f 199 MBED_ASSERT((clk_timer1 % TMR1HIRES_CLK_PER_SEC) == 0);
elijahsj 1:8a094db1347f 200 // NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451. In M451, TIMER_CNT is updated continuously by default.
elijahsj 1:8a094db1347f 201 timer1_base->CTL &= ~(TIMER_CTL_OPMODE_Msk | TIMER_CTL_PSC_Msk/* | TIMER_CTL_CNTDATEN_Msk*/);
elijahsj 1:8a094db1347f 202 timer1_base->CTL |= TIMER_ONESHOT_MODE | prescale_timer1/* | TIMER_CTL_CNTDATEN_Msk*/;
elijahsj 1:8a094db1347f 203
elijahsj 1:8a094db1347f 204 uint32_t cmp_timer1 = cd_minor_us / US_PER_TMR1HIRES_CLK;
elijahsj 1:8a094db1347f 205 cmp_timer1 = NU_CLAMP(cmp_timer1, TMR_CMP_MIN, TMR_CMP_MAX);
elijahsj 1:8a094db1347f 206 timer1_base->CMP = cmp_timer1;
elijahsj 1:8a094db1347f 207
elijahsj 1:8a094db1347f 208 TIMER_EnableInt(timer1_base);
elijahsj 1:8a094db1347f 209 TIMER_Start(timer1_base);
elijahsj 1:8a094db1347f 210 }