2014-01-10 16:46:18 +00:00
|
|
|
/* mbed Microcontroller Library
|
2016-11-14 09:01:07 +00:00
|
|
|
* Copyright (c) 2006-2016 ARM Limited
|
2014-01-10 16:46:18 +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
|
2014-01-10 16:46:18 +00:00
|
|
|
*
|
2016-11-14 09:01:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-01-10 16:46:18 +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.
|
2014-01-10 16:46:18 +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"
|
2014-01-10 16:46:18 +00:00
|
|
|
|
2016-10-25 16:23:09 +00:00
|
|
|
// A 32-bit timer is used
|
2016-10-25 13:33:40 +00:00
|
|
|
#if !TIM_MST_16BIT
|
2014-01-13 13:20:27 +00:00
|
|
|
|
2016-11-02 16:06:31 +00:00
|
|
|
TIM_HandleTypeDef TimMasterHandle;
|
|
|
|
|
2016-11-14 08:56:54 +00:00
|
|
|
void us_ticker_init(void)
|
|
|
|
{
|
2017-07-25 09:50:50 +00:00
|
|
|
/* NOTE: assuming that HAL tick has already been initialized! */
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 08:56:54 +00:00
|
|
|
uint32_t us_ticker_read()
|
|
|
|
{
|
2014-02-19 15:29:09 +00:00
|
|
|
return TIM_MST->CNT;
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 08:56:54 +00:00
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
2017-07-25 09:50:50 +00:00
|
|
|
// NOTE: This function must be called with interrupts disabled to keep our
|
|
|
|
// timer interrupt setup atomic
|
|
|
|
|
2017-06-26 14:20:54 +00:00
|
|
|
// disable IT while we are handling the correct timestamp
|
|
|
|
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
2014-02-19 15:29:09 +00:00
|
|
|
// Set new output compare value
|
2016-10-25 13:33:40 +00:00
|
|
|
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
|
2017-06-26 14:20:54 +00:00
|
|
|
// Enable IT
|
|
|
|
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
2014-01-10 16:46:18 +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 us_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
LL_TIM_GenerateEvent_CC1(TimMasterHandle.Instance);
|
|
|
|
}
|
|
|
|
|
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_disable_interrupt(void)
|
|
|
|
{
|
2014-10-16 06:48:34 +00:00
|
|
|
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
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-07-25 09:50:50 +00:00
|
|
|
__HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
2016-10-25 13:33:40 +00:00
|
|
|
|
|
|
|
#endif // !TIM_MST_16BIT
|