mbed-os/targets/TARGET_Silicon_Labs/TARGET_EFM32/us_ticker.c

132 lines
3.7 KiB
C
Raw Normal View History

/***************************************************************************//**
* @file us_ticker.c
*******************************************************************************
* @section License
* <b>(C) Copyright 2016 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
*
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-04-27 18:11:02 +00:00
#include <stddef.h>
#include "us_ticker_api.h"
#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"
#define TICKER_FREQUENCY ((REFERENCE_FREQUENCY > 24000000) ? REFERENCE_FREQUENCY / 16 : REFERENCE_FREQUENCY / 8)
2015-04-27 18:11:02 +00:00
const ticker_info_t* us_ticker_get_info(void)
2015-04-27 18:11:02 +00:00
{
static const ticker_info_t info = {
TICKER_FREQUENCY,
16
};
return &info;
2015-04-27 18:11:02 +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) {
/* calling init again should cancel current interrupt */
us_ticker_disable_interrupt();
2015-04-27 18:11:02 +00:00
return;
}
us_ticker_inited = true;
2015-04-27 18:11:02 +00:00
/* Enable clock for TIMERs */
CMU_ClockEnable(US_TICKER_TIMER_CLOCK, true);
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
}
/* Clear TIMER counter value */
TIMER_CounterSet(US_TICKER_TIMER, 0);
/* Start TIMER */
TIMER_Enable(US_TICKER_TIMER, true);
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 */
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);
}
2015-04-27 18:11:02 +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
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();
}
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);
TIMER_IntClear(US_TICKER_TIMER, TIMER_IEN_CC0);
TIMER_CompareSet(US_TICKER_TIMER, 0, timestamp);
2015-04-27 18:11:02 +00:00
TIMER_IntEnable(US_TICKER_TIMER, TIMER_IEN_CC0);
}
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);
}