2015-03-12 09:21:58 +00:00
|
|
|
/* mbed Microcontroller Library
|
2016-11-14 09:01:07 +00:00
|
|
|
* Copyright (c) 2006-2016 ARM Limited
|
2015-03-12 09:21:58 +00:00
|
|
|
*
|
2016-11-14 09:01:07 +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-03-12 09:21:58 +00:00
|
|
|
*
|
2016-11-14 09:01:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-03-12 09:21:58 +00:00
|
|
|
*
|
2016-11-14 09:01:07 +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-03-12 09:21:58 +00:00
|
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "us_ticker_api.h"
|
|
|
|
#include "PeripheralNames.h"
|
2016-10-25 13:33:40 +00:00
|
|
|
#include "hal_tick.h"
|
2015-03-12 09:21:58 +00:00
|
|
|
|
2016-11-02 16:06:31 +00:00
|
|
|
TIM_HandleTypeDef TimMasterHandle;
|
2015-03-12 09:21:58 +00:00
|
|
|
|
2018-06-27 12:21:07 +00:00
|
|
|
const ticker_info_t *us_ticker_get_info()
|
2018-03-26 10:20:21 +00:00
|
|
|
{
|
|
|
|
static const ticker_info_t info = {
|
|
|
|
1000000,
|
2018-06-20 12:50:20 +00:00
|
|
|
TIM_MST_BIT_WIDTH
|
2018-03-26 10:20:21 +00:00
|
|
|
};
|
|
|
|
return &info;
|
|
|
|
}
|
2015-03-12 09:21:58 +00:00
|
|
|
|
2018-06-20 11:40:28 +00:00
|
|
|
void us_ticker_irq_handler(void);
|
|
|
|
|
|
|
|
// ************************************ 16-bit timer ************************************
|
2018-06-20 12:50:20 +00:00
|
|
|
#if TIM_MST_BIT_WIDTH == 16
|
2018-06-20 11:40:28 +00:00
|
|
|
|
|
|
|
#if defined(TARGET_STM32F0)
|
2018-06-20 12:50:20 +00:00
|
|
|
void timer_update_irq_handler(void)
|
|
|
|
{
|
2018-06-20 11:40:28 +00:00
|
|
|
#else
|
|
|
|
void timer_irq_handler(void)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
|
|
|
|
|
|
|
#if defined(TARGET_STM32F0)
|
|
|
|
} // end timer_update_irq_handler function
|
|
|
|
|
|
|
|
void timer_oc_irq_handler(void)
|
|
|
|
{
|
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
|
|
|
#endif
|
|
|
|
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
|
|
|
|
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
|
|
|
|
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
|
|
|
|
us_ticker_irq_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ************************************ 32-bit timer ************************************
|
|
|
|
#else
|
|
|
|
|
|
|
|
void timer_irq_handler(void)
|
|
|
|
{
|
2018-06-20 12:50:20 +00:00
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
2018-06-20 11:40:28 +00:00
|
|
|
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
|
|
|
|
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
|
|
|
|
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
|
|
|
|
us_ticker_irq_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // 16-bit/32-bit timer
|
|
|
|
|
2016-11-14 08:56:54 +00:00
|
|
|
void us_ticker_init(void)
|
|
|
|
{
|
2018-06-20 11:40:28 +00:00
|
|
|
|
|
|
|
// ************************************ 16-bit timer ************************************
|
2018-06-20 12:50:20 +00:00
|
|
|
#if TIM_MST_BIT_WIDTH == 16
|
2018-06-20 11:40:28 +00:00
|
|
|
// Enable timer clock
|
|
|
|
TIM_MST_RCC;
|
|
|
|
|
|
|
|
// Reset timer
|
|
|
|
TIM_MST_RESET_ON;
|
|
|
|
TIM_MST_RESET_OFF;
|
|
|
|
|
|
|
|
// Update the SystemCoreClock variable
|
|
|
|
SystemCoreClockUpdate();
|
|
|
|
|
|
|
|
// Configure time base
|
2018-06-20 12:50:20 +00:00
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
2018-06-20 11:40:28 +00:00
|
|
|
TimMasterHandle.Init.Period = 0xFFFF;
|
|
|
|
TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
|
|
|
|
TimMasterHandle.Init.ClockDivision = 0;
|
|
|
|
TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
|
|
#if !defined(TARGET_STM32L0) && !defined(TARGET_STM32L1)
|
|
|
|
TimMasterHandle.Init.RepetitionCounter = 0;
|
|
|
|
#endif
|
|
|
|
#ifdef TIM_AUTORELOAD_PRELOAD_DISABLE
|
|
|
|
TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
|
|
#endif
|
|
|
|
HAL_TIM_Base_Init(&TimMasterHandle);
|
|
|
|
|
|
|
|
// Configure output compare channel 1 for mbed timeout (enabled later when used)
|
|
|
|
HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
|
|
|
|
|
|
|
|
// Output compare channel 1 interrupt for mbed timeout
|
|
|
|
#if defined(TARGET_STM32F0)
|
|
|
|
NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler);
|
|
|
|
NVIC_EnableIRQ(TIM_MST_UP_IRQ);
|
|
|
|
NVIC_SetPriority(TIM_MST_UP_IRQ, 0);
|
|
|
|
NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler);
|
|
|
|
NVIC_EnableIRQ(TIM_MST_OC_IRQ);
|
|
|
|
NVIC_SetPriority(TIM_MST_OC_IRQ, 1);
|
|
|
|
#else
|
|
|
|
NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
|
|
|
|
NVIC_EnableIRQ(TIM_MST_IRQ);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Enable timer
|
|
|
|
HAL_TIM_Base_Start(&TimMasterHandle);
|
|
|
|
|
|
|
|
// Freeze timer on stop/breakpoint
|
|
|
|
// Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example
|
|
|
|
#if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE)
|
|
|
|
TIM_MST_DBGMCU_FREEZE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
|
|
|
|
|
|
|
// ************************************ 32-bit timer ************************************
|
|
|
|
#else
|
|
|
|
|
|
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
|
|
|
uint32_t PclkFreq;
|
|
|
|
|
|
|
|
// Get clock configuration
|
|
|
|
// Note: PclkFreq contains here the Latency (not used after)
|
|
|
|
HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
|
|
|
|
|
|
|
|
// Get timer clock value
|
|
|
|
#if TIM_MST_PCLK == 1
|
|
|
|
PclkFreq = HAL_RCC_GetPCLK1Freq();
|
|
|
|
#else
|
|
|
|
PclkFreq = HAL_RCC_GetPCLK2Freq();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Enable timer clock
|
|
|
|
TIM_MST_RCC;
|
|
|
|
|
|
|
|
// Reset timer
|
|
|
|
TIM_MST_RESET_ON;
|
|
|
|
TIM_MST_RESET_OFF;
|
|
|
|
|
|
|
|
// Configure time base
|
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
|
|
|
TimMasterHandle.Init.Period = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
// TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
|
|
|
|
#if TIM_MST_PCLK == 1
|
|
|
|
if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) {
|
|
|
|
#else
|
|
|
|
if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) {
|
|
|
|
#endif
|
|
|
|
TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
|
|
|
|
} else {
|
|
|
|
TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
|
|
|
|
}
|
|
|
|
|
|
|
|
TimMasterHandle.Init.ClockDivision = 0;
|
|
|
|
TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
|
|
#if !TARGET_STM32L1
|
|
|
|
TimMasterHandle.Init.RepetitionCounter = 0;
|
|
|
|
#endif
|
|
|
|
#ifdef TIM_AUTORELOAD_PRELOAD_DISABLE
|
|
|
|
TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
|
|
#endif
|
|
|
|
HAL_TIM_OC_Init(&TimMasterHandle);
|
|
|
|
|
|
|
|
NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
|
|
|
|
NVIC_EnableIRQ(TIM_MST_IRQ);
|
|
|
|
|
|
|
|
// Channel 1 for mbed timeout
|
|
|
|
HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
|
|
|
|
|
|
|
|
// Freeze timer on stop/breakpoint
|
|
|
|
// Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example
|
|
|
|
#if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE)
|
|
|
|
TIM_MST_DBGMCU_FREEZE;
|
|
|
|
#endif
|
|
|
|
|
2018-03-26 13:49:34 +00:00
|
|
|
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
2018-06-20 11:40:28 +00:00
|
|
|
|
|
|
|
#endif // 16-bit/32-bit timer
|
|
|
|
|
2015-03-12 09:21:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 08:56:54 +00:00
|
|
|
uint32_t us_ticker_read()
|
|
|
|
{
|
2018-03-26 10:20:21 +00:00
|
|
|
return TIM_MST->CNT;
|
2016-10-25 13:33:40 +00:00
|
|
|
}
|
2015-03-12 09:21:58 +00:00
|
|
|
|
2016-11-14 08:56:54 +00:00
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
2017-06-02 11:10:41 +00:00
|
|
|
// NOTE: This function must be called with interrupts disabled to keep our
|
|
|
|
// timer interrupt setup atomic
|
|
|
|
// Set new output compare value
|
2018-03-26 13:49:34 +00:00
|
|
|
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
|
2017-06-02 11:10:41 +00:00
|
|
|
// Ensure the compare event starts clear
|
|
|
|
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
|
|
|
|
// Enable IT
|
|
|
|
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
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)
|
|
|
|
{
|
2018-03-26 10:20:21 +00:00
|
|
|
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
|
|
|
|
LL_TIM_GenerateEvent_CC1(TimMasterHandle.Instance);
|
|
|
|
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
2015-03-12 09:21:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 08:56:54 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
2015-03-12 09:21:58 +00:00
|
|
|
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
|
|
|
}
|
|
|
|
|
2017-07-25 09:50:50 +00:00
|
|
|
/* NOTE: must be called with interrupts disabled! */
|
2016-11-14 08:56:54 +00:00
|
|
|
void us_ticker_clear_interrupt(void)
|
|
|
|
{
|
2017-06-02 07:30:31 +00:00
|
|
|
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
|
2015-03-12 09:21:58 +00:00
|
|
|
}
|
2016-10-25 13:33:40 +00:00
|
|
|
|
2018-07-04 14:44:00 +00:00
|
|
|
uint32_t timer_cnt_reg;
|
|
|
|
uint32_t timer_ccr1_reg;
|
|
|
|
uint32_t timer_dier_reg;
|
|
|
|
|
|
|
|
void save_timer_ctx(void)
|
|
|
|
{
|
|
|
|
timer_cnt_reg = __HAL_TIM_GET_COUNTER(&TimMasterHandle);
|
|
|
|
timer_ccr1_reg = __HAL_TIM_GET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1);
|
|
|
|
timer_dier_reg = TIM_MST->DIER;
|
|
|
|
}
|
|
|
|
|
|
|
|
void restore_timer_ctx(void)
|
|
|
|
{
|
|
|
|
__HAL_TIM_SET_COUNTER(&TimMasterHandle, timer_cnt_reg);
|
|
|
|
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, timer_ccr1_reg);
|
|
|
|
TIM_MST->DIER = timer_dier_reg;
|
2018-06-20 11:40:28 +00:00
|
|
|
}
|