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"
|
2018-03-13 17:11:18 +00:00
|
|
|
#if DEVICE_LPTICKER
|
2015-04-27 18:11:02 +00:00
|
|
|
|
2018-01-15 14:56:55 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* The Silicon Labs lp_ticker implementation is mapped on top of an extended RTC
|
|
|
|
* API, since the RTC is available in the lowest energy modes. By default, the
|
|
|
|
* RTC counter is configured to run at 4kHz, giving us a quarter-ms resolution
|
|
|
|
* for the low power timer, which should be good enough for a low power use
|
|
|
|
* case.
|
|
|
|
*
|
2018-03-26 20:52:11 +00:00
|
|
|
* Mapping of mbed APIs to Silicon Labs peripherals:
|
|
|
|
* ---: Does not meet mbed API requirements
|
|
|
|
* X : Implemented to provide mbed API functionality
|
|
|
|
*
|
|
|
|
* --------------------------------------------
|
|
|
|
* | ------------- | RTCC | BURTC | RTC | TIMER |
|
|
|
|
* | rtc_api | X | X | --- | ----- |
|
|
|
|
* | lp_ticker_api | X | | X | ----- |
|
|
|
|
* | us_ticker_api | --- | ----- | --- | X |
|
|
|
|
* --------------------------------------------
|
|
|
|
*
|
2018-01-15 14:56:55 +00:00
|
|
|
* On Silicon Labs devices, the lowest width RTC implementation has a 24-bit
|
|
|
|
* counter, which gets extended with a further 32-bit software counter. This
|
|
|
|
* gives 56 bits of actual width, which with the default speed maps to
|
|
|
|
* 557462 years before the extended RTC counter wraps around. We are pretty
|
|
|
|
* certain no device is going to have that amount of uptime.
|
|
|
|
* (At max speed the wraparound is at 69730 years, which is unlikely as well)
|
|
|
|
******************************************************************************/
|
|
|
|
|
2018-03-26 20:52:11 +00:00
|
|
|
#if defined(RTC_PRESENT)
|
|
|
|
#include "em_rtc.h"
|
|
|
|
#include "em_cmu.h"
|
2015-04-27 20:26:11 +00:00
|
|
|
#include "lp_ticker_api.h"
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "mbed_critical.h"
|
2015-10-14 11:55:38 +00:00
|
|
|
|
2018-03-26 20:52:11 +00:00
|
|
|
#if RTC_CLOCKDIV_INT > 16
|
|
|
|
#error invalid prescaler value RTC_CLOCKDIV_INT, since LP ticker resolution will exceed 1ms.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define RTC_BITS (24U)
|
|
|
|
#define RTC_MAX_VALUE (0xFFFFFFUL)
|
|
|
|
|
|
|
|
static bool rtc_inited = false;
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
|
|
|
|
const ticker_info_t* lp_ticker_get_info(void)
|
|
|
|
{
|
|
|
|
static const ticker_info_t rtc_info = {
|
|
|
|
LOW_ENERGY_CLOCK_FREQUENCY,
|
|
|
|
RTC_BITS
|
|
|
|
};
|
|
|
|
return &rtc_info;
|
|
|
|
}
|
2018-03-26 20:52:11 +00:00
|
|
|
|
|
|
|
void RTC_IRQHandler(void)
|
|
|
|
{
|
|
|
|
uint32_t flags;
|
|
|
|
flags = RTC_IntGet();
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
if ((flags & RTC_IF_COMP0) && rtc_inited) {
|
2018-03-26 20:52:11 +00:00
|
|
|
RTC_IntClear(RTC_IF_COMP0);
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
lp_ticker_irq_handler();
|
2018-03-26 20:52:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-12 13:25:13 +00:00
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
void lp_ticker_init()
|
|
|
|
{
|
2018-03-26 20:52:11 +00:00
|
|
|
core_util_critical_section_enter();
|
|
|
|
if (!rtc_inited) {
|
|
|
|
CMU_ClockEnable(cmuClock_RTC, true);
|
|
|
|
|
|
|
|
/* Initialize RTC */
|
|
|
|
RTC_Init_TypeDef init = RTC_INIT_DEFAULT;
|
|
|
|
init.enable = 1;
|
|
|
|
/* Don't use compare register 0 as top value */
|
|
|
|
init.comp0Top = 0;
|
|
|
|
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
/* Initialize */
|
|
|
|
RTC_Init(&init);
|
|
|
|
RTC_CounterSet(20);
|
|
|
|
|
2018-03-26 20:52:11 +00:00
|
|
|
/* Enable Interrupt from RTC */
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
RTC_IntDisable(RTC_IF_COMP0);
|
|
|
|
RTC_IntClear(RTC_IF_COMP0);
|
2018-03-26 20:52:11 +00:00
|
|
|
NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler);
|
|
|
|
NVIC_EnableIRQ(RTC_IRQn);
|
|
|
|
|
|
|
|
rtc_inited = true;
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
} else {
|
|
|
|
/* Cancel current interrupt by virtue of calling init again */
|
|
|
|
RTC_IntDisable(RTC_IF_COMP0);
|
|
|
|
RTC_IntClear(RTC_IF_COMP0);
|
2016-01-05 15:23:45 +00:00
|
|
|
}
|
2018-03-26 20:52:11 +00:00
|
|
|
core_util_critical_section_exit();
|
2016-01-05 15:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lp_ticker_free()
|
|
|
|
{
|
2018-03-26 20:52:11 +00:00
|
|
|
/* Disable the RTC if it was inited and is no longer in use by anyone. */
|
|
|
|
if (rtc_inited) {
|
|
|
|
NVIC_DisableIRQ(RTC_IRQn);
|
|
|
|
RTC_Reset();
|
|
|
|
CMU_ClockEnable(cmuClock_RTC, false);
|
|
|
|
rtc_inited = false;
|
|
|
|
}
|
2018-03-26 20:32:55 +00:00
|
|
|
RTC_FreezeEnable(false);
|
|
|
|
}
|
|
|
|
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
void lp_ticker_set_interrupt(timestamp_t timestamp)
|
2018-03-26 20:52:11 +00:00
|
|
|
{
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
RTC_IntDisable(RTC_IF_COMP0);
|
|
|
|
RTC_IntClear(RTC_IF_COMP0);
|
2018-03-26 20:52:11 +00:00
|
|
|
RTC_FreezeEnable(true);
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
RTC_CompareSet(0, (uint32_t) (timestamp & RTC_MAX_VALUE));
|
2018-03-26 20:52:11 +00:00
|
|
|
RTC_FreezeEnable(false);
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
RTC_IntEnable(RTC_IF_COMP0);
|
2015-10-12 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 20:52:11 +00:00
|
|
|
void lp_ticker_fire_interrupt(void)
|
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
|
|
|
{
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
RTC_IntEnable(RTC_IF_COMP0);
|
|
|
|
RTC_IntSet(RTC_IF_COMP0);
|
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-03-26 20:52:11 +00:00
|
|
|
void lp_ticker_disable_interrupt()
|
2015-10-12 13:25:13 +00:00
|
|
|
{
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
RTC_IntDisable(RTC_IF_COMP0);
|
2015-10-12 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 20:52:11 +00:00
|
|
|
void lp_ticker_clear_interrupt()
|
2015-10-12 13:25:13 +00:00
|
|
|
{
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
RTC_IntClear(RTC_IF_COMP0);
|
2015-10-12 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timestamp_t lp_ticker_read()
|
|
|
|
{
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
return (timestamp_t) RTC_CounterGet();
|
2015-10-12 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 20:52:11 +00:00
|
|
|
#elif defined(RTCC_PRESENT)
|
|
|
|
/* lp_ticker api is implemented in rtc_rtcc.c */
|
|
|
|
#endif /* RTC_PRESENT */
|
Re-implement us_ticker and lp_ticker for Silicon Labs targets
Re-implemented both us_ticker and lp_ticker to match the new API and specifications.
Details:
* On EFM32GG, EFM32WG, EFM32LG, EFM32HG, EFM32ZG: Use the RTC peripheral to back lp_ticker, and a TIMER to back us_ticker.
* On EFM32PG, EFR32MG, EFM32PG12, EFR32MG12: Use the RTCC peripheral to back lp_ticker (dual-purpose, also used to back RTC), and a TIMER to back us_ticker.
2018-03-27 08:55:46 +00:00
|
|
|
#endif /* DEVICE_LPTICKER */
|