2016-08-16 09:55:30 +00:00
|
|
|
/* mbed Microcontroller Library
|
2018-02-02 14:36:08 +00:00
|
|
|
* Copyright (c) 2016 - 2018 ARM Limited
|
2016-08-16 09:55:30 +00:00
|
|
|
*
|
|
|
|
* 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-03-13 17:11:18 +00:00
|
|
|
#if DEVICE_LPTICKER
|
2016-08-16 09:55:30 +00:00
|
|
|
|
|
|
|
#include "lp_ticker_api.h"
|
|
|
|
#include "fsl_lptmr.h"
|
|
|
|
#include "cmsis.h"
|
|
|
|
|
2018-02-02 14:36:08 +00:00
|
|
|
const ticker_info_t* lp_ticker_get_info()
|
|
|
|
{
|
|
|
|
static const ticker_info_t info = {
|
|
|
|
32768, // 32kHz
|
2018-03-27 10:37:46 +00:00
|
|
|
16 // 16 bit counter
|
2018-02-02 14:36:08 +00:00
|
|
|
};
|
|
|
|
return &info;
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:55:30 +00:00
|
|
|
#define OSC32K_CLK_HZ (32768)
|
|
|
|
|
|
|
|
static bool lp_ticker_inited = false;
|
|
|
|
|
|
|
|
static void lptmr_isr(void)
|
|
|
|
{
|
|
|
|
LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
|
|
|
|
lp_ticker_irq_handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize the low power ticker
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void lp_ticker_init(void)
|
|
|
|
{
|
|
|
|
lptmr_config_t lptmrConfig;
|
|
|
|
|
2018-02-02 14:36:08 +00:00
|
|
|
if (!lp_ticker_inited) {
|
|
|
|
/* Setup high resolution clock - LPTMR */
|
|
|
|
LPTMR_GetDefaultConfig(&lptmrConfig);
|
|
|
|
|
|
|
|
/* Use 32kHz drive */
|
|
|
|
CLOCK_SetXtal32Freq(OSC32K_CLK_HZ);
|
2018-03-27 10:37:46 +00:00
|
|
|
|
2018-02-02 14:36:08 +00:00
|
|
|
lptmrConfig.prescalerClockSource = kLPTMR_PrescalerClock_2;
|
2018-03-27 10:37:46 +00:00
|
|
|
lptmrConfig.enableFreeRunning = true;
|
2018-02-02 14:36:08 +00:00
|
|
|
LPTMR_Init(LPTMR0, &lptmrConfig);
|
|
|
|
LPTMR_DisableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
|
|
|
|
NVIC_ClearPendingIRQ(LPTMR0_IRQn);
|
2018-03-27 10:37:46 +00:00
|
|
|
NVIC_SetVector(LPTMR0_IRQn, (uint32_t)lptmr_isr);
|
2018-02-02 14:36:08 +00:00
|
|
|
EnableIRQ(LPTMR0_IRQn);
|
|
|
|
|
|
|
|
lp_ticker_inited = true;
|
|
|
|
} else {
|
2018-11-21 07:11:19 +00:00
|
|
|
lp_ticker_disable_interrupt();
|
2018-02-02 14:36:08 +00:00
|
|
|
}
|
2016-08-16 09:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Read the current counter
|
|
|
|
*
|
2018-02-02 14:36:08 +00:00
|
|
|
* @return The current timer's counter value in ticks
|
2016-08-16 09:55:30 +00:00
|
|
|
*/
|
|
|
|
uint32_t lp_ticker_read(void)
|
|
|
|
{
|
2018-03-27 10:37:46 +00:00
|
|
|
return LPTMR_GetCurrentTimerCount(LPTMR0);
|
2016-08-16 09:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set interrupt for specified timestamp
|
|
|
|
*
|
2018-02-02 14:36:08 +00:00
|
|
|
* @param timestamp The time in ticks to be set
|
2016-08-16 09:55:30 +00:00
|
|
|
*/
|
|
|
|
void lp_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
2018-03-27 10:37:46 +00:00
|
|
|
if (timestamp == 0) {
|
|
|
|
timestamp = 1;
|
2016-08-16 09:55:30 +00:00
|
|
|
}
|
2018-03-27 10:37:46 +00:00
|
|
|
LPTMR_SetTimerPeriod(LPTMR0, timestamp);
|
|
|
|
LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
|
|
|
|
LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
|
|
|
|
LPTMR_StartTimer(LPTMR0);
|
2016-08-16 09:55:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
void lp_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
NVIC_SetPendingIRQ(LPTMR0_IRQn);
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:55:30 +00:00
|
|
|
/** Disable low power ticker interrupt
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void lp_ticker_disable_interrupt(void)
|
|
|
|
{
|
|
|
|
LPTMR_DisableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear the low power ticker interrupt
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void lp_ticker_clear_interrupt(void)
|
|
|
|
{
|
|
|
|
LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
|
|
|
|
}
|
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-07-25 06:40:30 +00:00
|
|
|
void lp_ticker_free(void)
|
|
|
|
{
|
2018-07-20 06:58:11 +00:00
|
|
|
LPTMR_DisableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
|
|
|
|
NVIC_DisableIRQ(LPTMR0_IRQn);
|
2018-07-25 06:40:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 17:11:18 +00:00
|
|
|
#endif /* DEVICE_LPTICKER */
|