2016-08-09 01:58:33 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2015-2016 Nuvoton
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-01-24 02:07:55 +00:00
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
#include "lp_ticker_api.h"
|
|
|
|
|
2018-03-13 17:11:18 +00:00
|
|
|
#if DEVICE_LPTICKER
|
2016-08-09 01:58:33 +00:00
|
|
|
|
|
|
|
#include "sleep_api.h"
|
2018-01-24 02:07:55 +00:00
|
|
|
#include "mbed_assert.h"
|
2016-08-09 01:58:33 +00:00
|
|
|
#include "nu_modutil.h"
|
2018-08-13 03:39:02 +00:00
|
|
|
#include "nu_timer.h"
|
2016-08-09 01:58:33 +00:00
|
|
|
#include "nu_miscutil.h"
|
|
|
|
|
2018-01-24 02:07:55 +00:00
|
|
|
/* Micro seconds per second */
|
|
|
|
#define NU_US_PER_SEC 1000000
|
|
|
|
/* Timer clock per lp_ticker tick */
|
|
|
|
#define NU_TMRCLK_PER_TICK 1
|
|
|
|
/* Timer clock per second */
|
|
|
|
#define NU_TMRCLK_PER_SEC (__LXT)
|
|
|
|
/* Timer max counter bit size */
|
|
|
|
#define NU_TMR_MAXCNT_BITSIZE 24
|
|
|
|
/* Timer max counter */
|
|
|
|
#define NU_TMR_MAXCNT ((1 << NU_TMR_MAXCNT_BITSIZE) - 1)
|
2016-08-09 01:58:33 +00:00
|
|
|
|
2018-02-22 06:26:10 +00:00
|
|
|
static void tmr1_vec(void);
|
2016-08-09 01:58:33 +00:00
|
|
|
|
2018-01-24 02:07:55 +00:00
|
|
|
/* NOTE: To wake the system from power down mode, timer clock source must be ether LXT or LIRC. */
|
2018-02-22 06:26:10 +00:00
|
|
|
static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_LXT, 0, TMR1_RST, TMR1_IRQn, (void *) tmr1_vec};
|
|
|
|
|
|
|
|
#define TIMER_MODINIT timer1_modinit
|
|
|
|
|
2018-06-11 02:29:41 +00:00
|
|
|
/* Timer interrupt enable/disable
|
2018-06-01 07:37:53 +00:00
|
|
|
*
|
2018-06-11 02:29:41 +00:00
|
|
|
* Because Timer interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs wait for lp_ticker,
|
|
|
|
* we call NVIC_DisableIRQ/NVIC_EnableIRQ instead.
|
2018-06-01 07:37:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Track ticker status */
|
2018-06-11 02:29:41 +00:00
|
|
|
static volatile uint16_t ticker_inited = 0;
|
2016-08-09 01:58:33 +00:00
|
|
|
|
|
|
|
#define TMR_CMP_MIN 2
|
|
|
|
#define TMR_CMP_MAX 0xFFFFFFu
|
|
|
|
|
2018-06-28 08:27:37 +00:00
|
|
|
/* Synchronization issue with LXT/LIRC-clocked Timer
|
|
|
|
*
|
|
|
|
* PCLK : typical HCLK/2
|
|
|
|
* ECLK (engine clock) : LXT/LIRC for Timer used to implement lp_ticker
|
|
|
|
*
|
|
|
|
* When system clock is higher than Timer clock (LXT/LIRC), we need to add delay for ECLK
|
|
|
|
* domain to take effect:
|
|
|
|
* 1. Write : typical 1PCLK + 2ECLK
|
|
|
|
* Read-check doesn't work because it just checks PCLK domain and doesn't check into
|
|
|
|
* ECLK domain.
|
|
|
|
* 2. Clear interrupt flag : typical 2PCLK
|
|
|
|
* It is very rare that we would meet dummy interrupt and get stuck in ISR until
|
|
|
|
* 'clear interrupt flag' takes effect. The issue is ignorable because the pending
|
|
|
|
* time is very short (at most 1 dummy interrupt). We won't take special handling for it.
|
|
|
|
*/
|
2018-03-26 01:45:59 +00:00
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
void lp_ticker_init(void)
|
|
|
|
{
|
2018-06-11 02:29:41 +00:00
|
|
|
if (ticker_inited) {
|
2018-05-23 09:17:59 +00:00
|
|
|
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
|
|
|
|
* ticker interrupt. */
|
|
|
|
lp_ticker_disable_interrupt();
|
2016-08-09 01:58:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-11 02:29:41 +00:00
|
|
|
ticker_inited = 1;
|
2018-01-24 02:07:55 +00:00
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
// Select IP clock source
|
2018-02-22 06:26:10 +00:00
|
|
|
CLK_SetModuleClock(TIMER_MODINIT.clkidx, TIMER_MODINIT.clksrc, TIMER_MODINIT.clkdiv);
|
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
// Enable IP clock
|
2018-02-22 06:26:10 +00:00
|
|
|
CLK_EnableModuleClock(TIMER_MODINIT.clkidx);
|
2016-08-09 01:58:33 +00:00
|
|
|
|
2019-07-25 07:28:51 +00:00
|
|
|
// Reset module
|
|
|
|
SYS_ResetModule(TIMER_MODINIT.rsetidx);
|
|
|
|
|
2018-03-26 01:42:53 +00:00
|
|
|
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
|
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
// Configure clock
|
2018-03-26 01:42:53 +00:00
|
|
|
uint32_t clk_timer = TIMER_GetModuleClock(timer_base);
|
2018-02-22 06:26:10 +00:00
|
|
|
uint32_t prescale_timer = clk_timer / NU_TMRCLK_PER_SEC - 1;
|
|
|
|
MBED_ASSERT((prescale_timer != (uint32_t) -1) && prescale_timer <= 127);
|
|
|
|
MBED_ASSERT((clk_timer % NU_TMRCLK_PER_SEC) == 0);
|
|
|
|
uint32_t cmp_timer = TMR_CMP_MAX;
|
|
|
|
MBED_ASSERT(cmp_timer >= TMR_CMP_MIN && cmp_timer <= TMR_CMP_MAX);
|
2016-08-09 01:58:33 +00:00
|
|
|
// Continuous mode
|
|
|
|
// NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451. In M451, TIMER_CNT is updated continuously by default.
|
2018-03-26 01:42:53 +00:00
|
|
|
timer_base->CTL = TIMER_CONTINUOUS_MODE | prescale_timer/* | TIMER_CTL_CNTDATEN_Msk*/;
|
2018-08-13 03:39:02 +00:00
|
|
|
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
|
2018-03-26 01:42:53 +00:00
|
|
|
|
|
|
|
timer_base->CMP = cmp_timer;
|
2018-08-13 03:39:02 +00:00
|
|
|
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
|
2018-01-24 02:07:55 +00:00
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
// Set vector
|
2018-02-22 06:26:10 +00:00
|
|
|
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);
|
2018-01-24 02:07:55 +00:00
|
|
|
|
2018-06-11 02:29:41 +00:00
|
|
|
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
|
2018-01-24 02:07:55 +00:00
|
|
|
|
2018-06-01 07:37:53 +00:00
|
|
|
TIMER_EnableInt(timer_base);
|
2018-08-13 03:39:02 +00:00
|
|
|
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
|
2018-03-26 01:42:53 +00:00
|
|
|
|
|
|
|
TIMER_EnableWakeup(timer_base);
|
2018-08-13 03:39:02 +00:00
|
|
|
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
|
2018-03-26 01:42:53 +00:00
|
|
|
|
|
|
|
TIMER_Start(timer_base);
|
2018-08-13 03:39:02 +00:00
|
|
|
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
|
2018-03-26 01:42:53 +00:00
|
|
|
|
|
|
|
/* Wait for timer to start counting and raise active flag */
|
|
|
|
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
|
2016-08-09 01:58:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 09:17:59 +00:00
|
|
|
void lp_ticker_free(void)
|
|
|
|
{
|
|
|
|
/* Disable interrupt */
|
|
|
|
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
|
|
|
|
|
|
|
|
/* Disable IP clock */
|
|
|
|
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
|
|
|
|
|
2018-06-11 02:29:41 +00:00
|
|
|
ticker_inited = 0;
|
2018-05-23 09:17:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
timestamp_t lp_ticker_read()
|
2018-01-24 02:07:55 +00:00
|
|
|
{
|
2018-06-11 02:29:41 +00:00
|
|
|
if (! ticker_inited) {
|
2016-08-09 01:58:33 +00:00
|
|
|
lp_ticker_init();
|
|
|
|
}
|
2018-01-24 02:07:55 +00:00
|
|
|
|
2018-03-26 01:42:53 +00:00
|
|
|
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
|
2018-01-24 02:07:55 +00:00
|
|
|
|
2018-02-22 06:26:10 +00:00
|
|
|
return (TIMER_GetCounter(timer_base) / NU_TMRCLK_PER_TICK);
|
2016-08-09 01:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lp_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
2018-08-08 01:38:48 +00:00
|
|
|
/* Clear any previously pending interrupts */
|
|
|
|
lp_ticker_clear_interrupt();
|
|
|
|
NVIC_ClearPendingIRQ(TIMER_MODINIT.irq_n);
|
|
|
|
|
2018-02-22 06:26:10 +00:00
|
|
|
/* In continuous mode, counter will be reset to zero with the following sequence:
|
|
|
|
* 1. Stop counting
|
|
|
|
* 2. Configure new CMP value
|
|
|
|
* 3. Restart counting
|
2018-01-24 02:07:55 +00:00
|
|
|
*
|
2018-02-22 06:26:10 +00:00
|
|
|
* This behavior is not what we want. To fix it, we could configure new CMP value
|
|
|
|
* without stopping counting first.
|
2018-01-24 02:07:55 +00:00
|
|
|
*/
|
2018-03-26 01:42:53 +00:00
|
|
|
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
|
Ticker: add fire interrupt now function
fire_interrupt function should be used for events in the past. As we have now
64bit timestamp, we can figure out what is in the past, and ask a target to invoke
an interrupt immediately. The previous attemps in the target HAL tickers were not ideal, as it can wrap around easily (16 or 32 bit counters). This new
functionality should solve this problem.
set_interrupt for tickers in HAL code should not handle anything but the next match interrupt. If it was in the past is handled by the upper layer.
It is possible that we are setting next event to the close future, so once it is set it is already in the past. Therefore we add a check after set interrupt to verify it is in future.
If it is not, we fire interrupt immediately. This results in
two events - first one immediate, correct one. The second one might be scheduled in far future (almost entire ticker range),
that should be discarded.
The specification for the fire_interrupts are:
- should set pending bit for the ticker interrupt (as soon as possible),
the event we are scheduling is already in the past, and we do not want to skip
any events
- no arguments are provided, neither return value, not needed
- ticker should be initialized prior calling this function (no need to check if it is already initialized)
All our targets provide this new functionality, removing old misleading if (timestamp is in the past) checks.
2017-06-27 11:18:59 +00:00
|
|
|
|
2018-02-22 06:26:10 +00:00
|
|
|
/* NOTE: Because H/W timer requests min compare value, our implementation would have alarm delay of
|
|
|
|
* (TMR_CMP_MIN - interval_clk) clocks when interval_clk is between [1, TMR_CMP_MIN). */
|
|
|
|
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
|
|
|
|
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
|
2018-03-26 01:42:53 +00:00
|
|
|
|
2018-06-01 07:37:53 +00:00
|
|
|
/* NOTE: Rely on LPTICKER_DELAY_TICKS to be non-blocking. */
|
2018-02-22 06:26:10 +00:00
|
|
|
timer_base->CMP = cmp_timer;
|
2018-06-11 02:29:41 +00:00
|
|
|
|
|
|
|
/* We can call ticker_irq_handler now. */
|
|
|
|
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
|
2016-08-09 01:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lp_ticker_disable_interrupt(void)
|
|
|
|
{
|
2018-06-01 07:37:53 +00:00
|
|
|
/* We cannot call ticker_irq_handler now. */
|
2018-06-11 02:29:41 +00:00
|
|
|
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
|
2016-08-09 01:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lp_ticker_clear_interrupt(void)
|
|
|
|
{
|
2018-06-01 07:37:53 +00:00
|
|
|
/* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate
|
|
|
|
* driver API:
|
|
|
|
*
|
|
|
|
* TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
|
|
|
|
* TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
|
|
|
|
*/
|
|
|
|
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
|
|
|
|
timer_base->INTSTS = TIMER_INTSTS_TIF_Msk | TIMER_INTSTS_TWKF_Msk;
|
2016-08-09 01:58:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 02:07:55 +00:00
|
|
|
void lp_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
|
|
|
|
// This prevents a recursive loop under heavy load which can lead to a stack overflow.
|
2018-02-22 06:26:10 +00:00
|
|
|
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
|
2018-06-11 02:29:41 +00:00
|
|
|
|
|
|
|
/* We can call ticker_irq_handler now. */
|
|
|
|
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
|
2016-08-09 01:58:33 +00:00
|
|
|
}
|
2018-01-24 02:07:55 +00:00
|
|
|
|
|
|
|
const ticker_info_t* lp_ticker_get_info()
|
|
|
|
{
|
|
|
|
static const ticker_info_t info = {
|
|
|
|
NU_TMRCLK_PER_SEC / NU_TMRCLK_PER_TICK,
|
|
|
|
NU_TMR_MAXCNT_BITSIZE
|
|
|
|
};
|
|
|
|
return &info;
|
|
|
|
}
|
|
|
|
|
2018-02-22 06:26:10 +00:00
|
|
|
static void tmr1_vec(void)
|
|
|
|
{
|
2018-06-01 07:37:53 +00:00
|
|
|
lp_ticker_clear_interrupt();
|
2018-03-26 01:42:53 +00:00
|
|
|
|
2018-02-22 06:26:10 +00:00
|
|
|
// NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler();
|
2018-06-11 02:29:41 +00:00
|
|
|
lp_ticker_irq_handler();
|
2018-02-22 06:26:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 01:58:33 +00:00
|
|
|
#endif
|