2015-08-06 12:04:59 +00:00
|
|
|
/***************************************************************************//**
|
|
|
|
* @file us_ticker.c
|
|
|
|
*******************************************************************************
|
|
|
|
* @section License
|
2016-09-12 08:13:44 +00:00
|
|
|
* <b>(C) Copyright 2016 Silicon Labs, http://www.silabs.com</b>
|
2015-08-06 12:04:59 +00:00
|
|
|
*******************************************************************************
|
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 <stddef.h>
|
|
|
|
#include "us_ticker_api.h"
|
2016-10-22 17:03:18 +00:00
|
|
|
#include "device.h"
|
2015-04-27 18:11:02 +00:00
|
|
|
#include "mbed_assert.h"
|
|
|
|
#include "em_cmu.h"
|
|
|
|
#include "em_timer.h"
|
|
|
|
#include "clocking.h"
|
|
|
|
|
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
|
|
|
#define TICKER_FREQUENCY ((REFERENCE_FREQUENCY > 24000000) ? REFERENCE_FREQUENCY / 16 : REFERENCE_FREQUENCY / 8)
|
2015-04-27 18:11:02 +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
|
|
|
const ticker_info_t* us_ticker_get_info(void)
|
2015-04-27 18:11:02 +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
|
|
|
static const ticker_info_t info = {
|
|
|
|
TICKER_FREQUENCY,
|
|
|
|
16
|
|
|
|
};
|
|
|
|
return &info;
|
2015-04-27 18:11:02 +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
|
|
|
static bool us_ticker_inited = false; // Is ticker initialized yet
|
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
void us_ticker_init(void)
|
|
|
|
{
|
|
|
|
if (us_ticker_inited) {
|
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
|
|
|
/* calling init again should cancel current interrupt */
|
|
|
|
us_ticker_disable_interrupt();
|
2015-04-27 18:11:02 +00:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
us_ticker_inited = true;
|
2015-04-27 18:11:02 +00:00
|
|
|
|
|
|
|
/* Enable clock for TIMERs */
|
|
|
|
CMU_ClockEnable(US_TICKER_TIMER_CLOCK, 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
|
|
|
if (REFERENCE_FREQUENCY > 24000000) {
|
|
|
|
US_TICKER_TIMER->CTRL = (US_TICKER_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (4 << _TIMER_CTRL_PRESC_SHIFT);
|
|
|
|
} else {
|
|
|
|
US_TICKER_TIMER->CTRL = (US_TICKER_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (3 << _TIMER_CTRL_PRESC_SHIFT);
|
2015-04-27 18:11:02 +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
|
|
|
/* Clear TIMER counter value */
|
|
|
|
TIMER_CounterSet(US_TICKER_TIMER, 0);
|
2017-11-24 18:11:41 +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
|
|
|
/* Start TIMER */
|
|
|
|
TIMER_Enable(US_TICKER_TIMER, true);
|
2016-09-12 08:13:44 +00:00
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
/* Select Compare Channel parameters */
|
|
|
|
TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
|
|
|
|
timerCCInit.mode = timerCCModeCompare;
|
|
|
|
|
|
|
|
/* Configure Compare Channel 0 */
|
|
|
|
TIMER_InitCC(US_TICKER_TIMER, 0, &timerCCInit);
|
|
|
|
|
|
|
|
/* Enable interrupt vector in NVIC */
|
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
|
|
|
TIMER_IntClear(US_TICKER_TIMER, TIMER_IEN_CC0);
|
|
|
|
NVIC_SetVector(US_TICKER_TIMER_IRQ, (uint32_t) us_ticker_irq_handler);
|
2015-04-27 18:11:02 +00:00
|
|
|
NVIC_EnableIRQ(US_TICKER_TIMER_IRQ);
|
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
|
|
|
}
|
2015-04-27 18:11:02 +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
|
|
|
void us_ticker_free(void)
|
|
|
|
{
|
|
|
|
if (us_ticker_inited) {
|
|
|
|
us_ticker_disable_interrupt();
|
|
|
|
NVIC_DisableIRQ(US_TICKER_TIMER_IRQ);
|
2015-04-27 18:11:02 +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
|
|
|
TIMER_Enable(US_TICKER_TIMER, false);
|
|
|
|
|
|
|
|
CMU_ClockEnable(US_TICKER_TIMER_CLOCK, false);
|
|
|
|
|
|
|
|
us_ticker_inited = false;
|
|
|
|
}
|
2015-04-27 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t us_ticker_read()
|
|
|
|
{
|
|
|
|
if (!us_ticker_inited) {
|
|
|
|
us_ticker_init();
|
|
|
|
}
|
|
|
|
|
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 US_TICKER_TIMER->CNT;
|
2015-04-27 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
TIMER_IntDisable(US_TICKER_TIMER, TIMER_IEN_CC0);
|
|
|
|
|
2017-11-24 18:11:41 +00:00
|
|
|
TIMER_IntClear(US_TICKER_TIMER, TIMER_IEN_CC0);
|
|
|
|
|
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
|
|
|
TIMER_CompareSet(US_TICKER_TIMER, 0, timestamp);
|
2016-09-12 08:13:44 +00:00
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
TIMER_IntEnable(US_TICKER_TIMER, TIMER_IEN_CC0);
|
|
|
|
}
|
|
|
|
|
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 us_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
NVIC_SetPendingIRQ(US_TICKER_TIMER_IRQ);
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:11:02 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
|
|
|
/* Disable compare channel interrupts */
|
|
|
|
TIMER_IntDisable(US_TICKER_TIMER, TIMER_IEN_CC0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_clear_interrupt(void)
|
|
|
|
{
|
|
|
|
/* Clear compare channel interrupts */
|
|
|
|
TIMER_IntClear(US_TICKER_TIMER, TIMER_IFC_CC0);
|
|
|
|
}
|