2015-08-06 12:04:59 +00:00
|
|
|
/***************************************************************************//**
|
|
|
|
* @file lp_ticker.c
|
|
|
|
*******************************************************************************
|
|
|
|
* @section License
|
|
|
|
* <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
|
|
|
|
*******************************************************************************
|
2015-04-27 18:11:02 +00:00
|
|
|
*
|
2016-06-13 22:23:58 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-27 18:11:02 +00:00
|
|
|
*
|
2016-06-13 22:23:58 +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
|
2015-04-27 18:11:02 +00:00
|
|
|
*
|
2016-06-13 22:23:58 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-08-06 12:04:59 +00:00
|
|
|
*
|
2016-06-13 22:23:58 +00:00
|
|
|
* 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.
|
2015-08-06 12:04:59 +00:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
2015-04-27 18:11:02 +00:00
|
|
|
|
|
|
|
#include "device.h"
|
2016-10-22 20:26:29 +00:00
|
|
|
#include "clocking.h"
|
2015-04-27 20:26:11 +00:00
|
|
|
#if DEVICE_LOWPOWERTIMER
|
2015-04-27 18:11:02 +00:00
|
|
|
|
|
|
|
#include "rtc_api.h"
|
|
|
|
#include "rtc_api_HAL.h"
|
2015-04-27 20:26:11 +00:00
|
|
|
#include "lp_ticker_api.h"
|
2015-04-27 18:11:02 +00:00
|
|
|
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "mbed_critical.h"
|
2015-10-14 11:55:38 +00:00
|
|
|
#if (defined RTCC_COUNT) && (RTCC_COUNT > 0)
|
|
|
|
#include "em_rtcc.h"
|
|
|
|
#endif
|
|
|
|
|
2015-10-12 13:25:13 +00:00
|
|
|
static int rtc_reserved = 0;
|
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
void lp_ticker_init()
|
|
|
|
{
|
2016-01-05 15:23:45 +00:00
|
|
|
if(!rtc_reserved) {
|
2016-06-14 23:20:22 +00:00
|
|
|
core_util_critical_section_enter();
|
2016-01-05 15:23:45 +00:00
|
|
|
rtc_init_real(RTC_INIT_LPTIMER);
|
|
|
|
rtc_set_comp0_handler((uint32_t)lp_ticker_irq_handler);
|
|
|
|
rtc_reserved = 1;
|
2016-06-14 23:20:22 +00:00
|
|
|
core_util_critical_section_exit();
|
2016-01-05 15:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lp_ticker_free()
|
|
|
|
{
|
|
|
|
if(rtc_reserved) {
|
2016-06-14 23:20:22 +00:00
|
|
|
core_util_critical_section_enter();
|
2016-01-05 15:23:45 +00:00
|
|
|
rtc_free_real(RTC_INIT_LPTIMER);
|
|
|
|
rtc_reserved = 0;
|
2016-06-14 23:20:22 +00:00
|
|
|
core_util_critical_section_exit();
|
2016-01-05 15:23:45 +00:00
|
|
|
}
|
2015-04-27 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 13:25:13 +00:00
|
|
|
#ifndef RTCC_COUNT
|
|
|
|
|
|
|
|
/* RTC API */
|
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
void lp_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
uint64_t timestamp_ticks;
|
2015-04-27 18:11:02 +00:00
|
|
|
uint64_t current_ticks = RTC_CounterGet();
|
|
|
|
timestamp_t current_time = ((uint64_t)(current_ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT));
|
2015-05-22 08:47:37 +00:00
|
|
|
|
2015-10-12 13:25:13 +00:00
|
|
|
/* Initialize RTC */
|
2016-01-05 15:23:45 +00:00
|
|
|
lp_ticker_init();
|
2015-05-22 08:47:37 +00:00
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
/* calculate offset value */
|
|
|
|
timestamp_t offset = timestamp - current_time;
|
|
|
|
if(offset > 0xEFFFFFFF) offset = 100;
|
2015-05-22 08:47:37 +00:00
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
/* map offset to RTC value */
|
2015-05-22 08:47:37 +00:00
|
|
|
// ticks = offset * RTC frequency div 1000000
|
|
|
|
timestamp_ticks = ((uint64_t)offset * (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) / 1000000;
|
2015-04-27 18:11:02 +00:00
|
|
|
timestamp_ticks += current_ticks;
|
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
/* RTC has 24 bit resolution */
|
|
|
|
timestamp_ticks &= 0xFFFFFF;
|
2015-04-27 18:11:02 +00:00
|
|
|
|
|
|
|
/* check for RTC limitation */
|
|
|
|
if((timestamp_ticks - RTC_CounterGet()) >= 0x800000) timestamp_ticks = RTC_CounterGet() + 2;
|
2015-05-22 08:47:37 +00:00
|
|
|
|
|
|
|
/* Set callback */
|
|
|
|
RTC_FreezeEnable(true);
|
|
|
|
RTC_CompareSet(0, (uint32_t)timestamp_ticks);
|
|
|
|
RTC_IntEnable(RTC_IF_COMP0);
|
|
|
|
RTC_FreezeEnable(false);
|
2015-04-27 18:11:02 +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)
|
|
|
|
{
|
|
|
|
RTC_IntSet(RTC_IFS_COMP0);
|
|
|
|
}
|
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
inline void lp_ticker_disable_interrupt()
|
|
|
|
{
|
|
|
|
RTC_IntDisable(RTC_IF_COMP0);
|
2015-04-27 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
inline void lp_ticker_clear_interrupt()
|
|
|
|
{
|
|
|
|
RTC_IntClear(RTC_IF_COMP0);
|
2015-04-27 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
timestamp_t lp_ticker_read()
|
|
|
|
{
|
2016-01-05 15:23:45 +00:00
|
|
|
lp_ticker_init();
|
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
uint64_t ticks_temp;
|
|
|
|
uint64_t ticks = RTC_CounterGet();
|
2015-04-27 18:11:02 +00:00
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
/* ticks = counter tick value
|
|
|
|
* timestamp = value in microseconds
|
|
|
|
* timestamp = ticks * 1.000.000 / RTC frequency
|
|
|
|
*/
|
2015-04-27 18:11:02 +00:00
|
|
|
|
2015-05-22 08:47:37 +00:00
|
|
|
ticks_temp = (ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT);
|
|
|
|
return (timestamp_t) (ticks_temp & 0xFFFFFFFF);
|
2015-04-27 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 13:25:13 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* RTCC API */
|
|
|
|
|
|
|
|
void lp_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
uint64_t timestamp_ticks;
|
|
|
|
uint64_t current_ticks = RTCC_CounterGet();
|
|
|
|
timestamp_t current_time = ((uint64_t)(current_ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT));
|
|
|
|
|
|
|
|
/* Initialize RTC */
|
2016-01-05 15:23:45 +00:00
|
|
|
lp_ticker_init();
|
2015-10-12 13:25:13 +00:00
|
|
|
|
|
|
|
/* calculate offset value */
|
|
|
|
timestamp_t offset = timestamp - current_time;
|
|
|
|
if(offset > 0xEFFFFFFF) offset = 100;
|
|
|
|
|
|
|
|
/* map offset to RTC value */
|
|
|
|
// ticks = offset * RTC frequency div 1000000
|
|
|
|
timestamp_ticks = ((uint64_t)offset * (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) / 1000000;
|
2015-11-09 11:07:02 +00:00
|
|
|
// checking the rounding. If timeout is wanted between RTCC ticks, irq should be configured to
|
|
|
|
// trigger in the latter RTCC-tick. Otherwise ticker-api fails to send timer event to its client
|
|
|
|
if(((timestamp_ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) < offset){
|
|
|
|
timestamp_ticks++;
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:25:13 +00:00
|
|
|
timestamp_ticks += current_ticks;
|
|
|
|
|
|
|
|
/* RTCC has 32 bit resolution */
|
|
|
|
timestamp_ticks &= 0xFFFFFFFF;
|
|
|
|
|
|
|
|
/* check for RTCC limitation */
|
|
|
|
if((timestamp_ticks - RTCC_CounterGet()) >= 0x80000000) timestamp_ticks = RTCC_CounterGet() + 2;
|
|
|
|
|
2015-11-04 14:54:18 +00:00
|
|
|
/* init channel */
|
|
|
|
RTCC_CCChConf_TypeDef ccchConf = RTCC_CH_INIT_COMPARE_DEFAULT;
|
|
|
|
RTCC_ChannelInit(0,&ccchConf);
|
2015-10-12 13:25:13 +00:00
|
|
|
/* Set callback */
|
|
|
|
RTCC_ChannelCCVSet(0, (uint32_t)timestamp_ticks);
|
2015-10-14 11:55:38 +00:00
|
|
|
RTCC_IntEnable(RTCC_IF_CC0);
|
2015-10-12 13:25:13 +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)
|
|
|
|
{
|
|
|
|
RTCC_IntSet(RTCC_IFS_CC0);
|
|
|
|
}
|
|
|
|
|
2015-10-12 13:25:13 +00:00
|
|
|
inline void lp_ticker_disable_interrupt()
|
|
|
|
{
|
2015-10-14 11:55:38 +00:00
|
|
|
RTCC_IntDisable(RTCC_IF_CC0);
|
2015-10-12 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void lp_ticker_clear_interrupt()
|
|
|
|
{
|
2015-10-14 11:55:38 +00:00
|
|
|
RTCC_IntClear(RTCC_IF_CC0);
|
2015-10-12 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timestamp_t lp_ticker_read()
|
|
|
|
{
|
2016-01-05 15:23:45 +00:00
|
|
|
lp_ticker_init();
|
|
|
|
|
2015-10-12 13:25:13 +00:00
|
|
|
uint64_t ticks_temp;
|
|
|
|
uint64_t ticks = RTCC_CounterGet();
|
|
|
|
|
|
|
|
/* ticks = counter tick value
|
|
|
|
* timestamp = value in microseconds
|
|
|
|
* timestamp = ticks * 1.000.000 / RTC frequency
|
|
|
|
*/
|
|
|
|
|
|
|
|
ticks_temp = (ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT);
|
|
|
|
return (timestamp_t) (ticks_temp & 0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* RTCC */
|
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
#endif
|