2017-04-06 08:09:03 +00:00
|
|
|
/* mbed Microcontroller Library
|
2017-12-20 09:35:31 +00:00
|
|
|
* Copyright (c) 2017-2018 ARM Limited
|
2017-04-06 08:09:03 +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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
/**
|
|
|
|
* Elapsed time measure and interval timer in micro-secundum,
|
|
|
|
* servicing \ref us_ticker_api.h, using CMSDK Timer0 \ref CMSDK_TIMER0_DEV.
|
|
|
|
*/
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
#include "cmsdk_ticker.h"
|
2017-04-06 08:09:03 +00:00
|
|
|
#include "us_ticker_api.h"
|
2017-12-20 09:35:31 +00:00
|
|
|
#include "platform_devices.h"
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2018-04-19 13:46:45 +00:00
|
|
|
/*
|
|
|
|
* The CMSDK Ticker counts on 32 bits.
|
|
|
|
*/
|
|
|
|
#define CMSDK_TICKER_COUNTER_BITS 32U
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
/**
|
2018-04-19 13:46:45 +00:00
|
|
|
* \brief Pass-through function to make the US ticker HAL only work in the tick
|
|
|
|
* domain. This function is needed by the CMSDK Ticker layer.
|
2017-12-20 09:35:31 +00:00
|
|
|
*
|
2018-04-19 13:46:45 +00:00
|
|
|
* \param[in] tick Number of clock ticks
|
2017-12-20 09:35:31 +00:00
|
|
|
*
|
2018-04-19 13:46:45 +00:00
|
|
|
* \return The number of ticks given.
|
2017-12-20 09:35:31 +00:00
|
|
|
*/
|
|
|
|
static uint32_t convert_tick_to_us(uint32_t tick)
|
|
|
|
{
|
2018-04-19 13:46:45 +00:00
|
|
|
/* Work only in the tick domain. */
|
|
|
|
return tick;
|
2017-12-20 09:35:31 +00:00
|
|
|
}
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
/**
|
2018-04-19 13:46:45 +00:00
|
|
|
* \brief Pass-through function to make the US ticker HAL only work in the tick
|
|
|
|
* domain. This function is needed by the CMSDK Ticker layer.
|
2017-12-20 09:35:31 +00:00
|
|
|
*
|
2018-04-19 13:46:45 +00:00
|
|
|
* \param[in] us Number of us
|
2017-12-20 09:35:31 +00:00
|
|
|
*
|
2018-04-19 13:46:45 +00:00
|
|
|
* \return The number of us given.
|
2017-12-20 09:35:31 +00:00
|
|
|
*/
|
|
|
|
static uint32_t convert_us_to_tick(uint32_t us)
|
|
|
|
{
|
2018-04-19 13:46:45 +00:00
|
|
|
/* Work only in the tick domain. */
|
|
|
|
return us;
|
2017-12-20 09:35:31 +00:00
|
|
|
}
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
static const struct tick_cfg_t cfg =
|
|
|
|
{
|
|
|
|
.timer_driver = &CMSDK_TIMER0_DEV,
|
|
|
|
.irq_n = TIMER0_IRQn,
|
|
|
|
.interval_callback = &us_ticker_irq_handler,
|
|
|
|
.convert_tick_to_time = &convert_tick_to_us,
|
|
|
|
.convert_time_to_tick = &convert_us_to_tick
|
2017-04-06 08:09:03 +00:00
|
|
|
};
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
static struct tick_data_t data =
|
|
|
|
{
|
|
|
|
.is_initialized = false,
|
|
|
|
.cumulated_time = 0,
|
|
|
|
.max_interval_time = 0,
|
|
|
|
.reload_time = 0,
|
|
|
|
.interval_callback_enabled = false,
|
|
|
|
.previous_cumulated_time = 0,
|
|
|
|
.previous_elapsed = 0
|
|
|
|
};
|
2017-04-06 08:09:03 +00:00
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
static struct tick_drv_data_t timer_data =
|
2017-04-06 08:09:03 +00:00
|
|
|
{
|
2017-12-20 09:35:31 +00:00
|
|
|
.cfg = &cfg,
|
|
|
|
.data = &data
|
|
|
|
};
|
2017-04-06 08:09:03 +00:00
|
|
|
|
|
|
|
void us_ticker_init(void)
|
|
|
|
{
|
2017-12-20 09:35:31 +00:00
|
|
|
cmsdk_ticker_init(&timer_data);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t us_ticker_read()
|
|
|
|
{
|
2017-12-20 09:35:31 +00:00
|
|
|
return cmsdk_ticker_read(&timer_data);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
2017-12-20 09:35:31 +00:00
|
|
|
cmsdk_ticker_set_interrupt(&timer_data, timestamp);
|
|
|
|
}
|
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
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
|
|
|
cmsdk_ticker_disable_interrupt(&timer_data);
|
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
|
|
|
}
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
void us_ticker_clear_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
|
|
|
{
|
2017-12-20 09:35:31 +00:00
|
|
|
cmsdk_ticker_clear_interrupt(&timer_data);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
void us_ticker_fire_interrupt(void)
|
2017-04-06 08:09:03 +00:00
|
|
|
{
|
2017-12-20 09:35:31 +00:00
|
|
|
cmsdk_ticker_fire_interrupt(&timer_data);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 09:35:31 +00:00
|
|
|
void TIMER0_IRQHandler(void)
|
2017-04-06 08:09:03 +00:00
|
|
|
{
|
2017-12-20 09:35:31 +00:00
|
|
|
cmsdk_ticker_irq_handler(&timer_data);
|
2017-04-06 08:09:03 +00:00
|
|
|
}
|
2018-04-19 13:46:45 +00:00
|
|
|
|
|
|
|
const ticker_info_t* us_ticker_get_info(void)
|
|
|
|
{
|
|
|
|
static ticker_info_t us_ticker_info = {
|
|
|
|
.bits = CMSDK_TICKER_COUNTER_BITS
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SystemCoreClock is not a constant so it cannot be used to initialize the
|
|
|
|
* ticker_info_t structure.
|
|
|
|
*/
|
|
|
|
us_ticker_info.frequency = SystemCoreClock;
|
|
|
|
|
|
|
|
return &us_ticker_info;
|
|
|
|
}
|