2016-07-22 07:41:34 +00:00
|
|
|
/*
|
2016-07-01 15:20:37 +00:00
|
|
|
* Copyright (c) 2013 Nordic Semiconductor ASA
|
|
|
|
* All rights reserved.
|
2016-07-22 07:41:34 +00:00
|
|
|
*
|
2016-07-01 15:20:37 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
2016-07-22 07:41:34 +00:00
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list
|
2016-07-01 15:20:37 +00:00
|
|
|
* of conditions and the following disclaimer.
|
2016-06-15 19:56:03 +00:00
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
|
|
|
|
* integrated circuit in a product or a software update for such product, must reproduce
|
|
|
|
* the above copyright notice, this list of conditions and the following disclaimer in
|
2016-07-01 15:20:37 +00:00
|
|
|
* the documentation and/or other materials provided with the distribution.
|
2016-06-15 19:56:03 +00:00
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software without specific prior
|
2016-07-01 15:20:37 +00:00
|
|
|
* written permission.
|
2016-06-15 19:56:03 +00:00
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 4. This software, with or without modification, must only be used with a
|
2016-07-01 15:20:37 +00:00
|
|
|
* Nordic Semiconductor ASA integrated circuit.
|
|
|
|
*
|
2016-07-22 07:41:34 +00:00
|
|
|
* 5. Any software provided in binary or object form under this license must not be reverse
|
|
|
|
* engineered, decompiled, modified and/or disassembled.
|
|
|
|
*
|
2016-07-01 15:20:37 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2016-07-22 07:41:34 +00:00
|
|
|
*
|
2016-06-15 19:56:03 +00:00
|
|
|
*/
|
2016-07-01 15:20:37 +00:00
|
|
|
|
2018-05-17 05:45:28 +00:00
|
|
|
#include "us_ticker.h"
|
|
|
|
|
2016-06-15 19:56:03 +00:00
|
|
|
#include "us_ticker_api.h"
|
2017-11-30 09:44:58 +00:00
|
|
|
#include "nrf_timer.h"
|
|
|
|
#include "app_util_platform.h"
|
2016-07-22 12:58:17 +00:00
|
|
|
#include "nrf_drv_common.h"
|
2017-04-03 12:00:11 +00:00
|
|
|
#include "mbed_critical.h"
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2018-05-17 13:10:17 +00:00
|
|
|
bool us_ticker_initialized = false;
|
2017-10-06 11:50:48 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
/* us ticker is driven by 1MHz clock and counter length is 16 bits. */
|
|
|
|
const ticker_info_t* us_ticker_get_info()
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
2017-11-30 09:44:58 +00:00
|
|
|
static const ticker_info_t info = {
|
|
|
|
US_TICKER_FREQ,
|
|
|
|
US_TICKER_COUNTER_BITS
|
|
|
|
};
|
|
|
|
return &info;
|
2017-04-03 12:00:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
void us_ticker_init(void)
|
2017-04-03 12:00:11 +00:00
|
|
|
{
|
2017-11-30 09:44:58 +00:00
|
|
|
if (us_ticker_initialized) {
|
|
|
|
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
|
|
|
|
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
|
|
|
|
return;
|
2017-10-06 11:50:48 +00:00
|
|
|
}
|
2016-07-01 13:03:35 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP);
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
|
2016-10-06 09:50:10 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
/* Configure timer as follows:
|
|
|
|
* - timer mode,
|
2018-05-25 10:49:34 +00:00
|
|
|
* - timer width 16 bits for NRF51 and 32 bits for NRF52,
|
2017-11-30 09:44:58 +00:00
|
|
|
* - timer freq 1 MHz.
|
|
|
|
*/
|
|
|
|
nrf_timer_mode_set(NRF_TIMER1, NRF_TIMER_MODE_TIMER);
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_frequency_set(NRF_TIMER1, NRF_TIMER_FREQ_1MHz);
|
2017-04-26 13:18:49 +00:00
|
|
|
|
2018-06-25 07:27:05 +00:00
|
|
|
#ifdef TARGET_NRF52
|
2018-05-25 10:49:34 +00:00
|
|
|
nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_32);
|
|
|
|
#else
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_16);
|
2018-05-25 10:49:34 +00:00
|
|
|
#endif
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, 0);
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
|
2016-06-23 10:27:18 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
NVIC_SetVector(TIMER1_IRQn, (uint32_t)us_ticker_irq_handler);
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2018-05-25 11:21:48 +00:00
|
|
|
nrf_drv_common_irq_enable(TIMER1_IRQn, APP_IRQ_PRIORITY_HIGH);
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_START);
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2018-05-25 10:55:54 +00:00
|
|
|
/* Bug fix. First value can't be trusted. */
|
|
|
|
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CAPTURE1);
|
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
us_ticker_initialized = true;
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
uint32_t us_ticker_read()
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CAPTURE1);
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
return nrf_timer_cc_read(NRF_TIMER1, NRF_TIMER_CC_CHANNEL1);
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
2016-06-15 19:56:03 +00:00
|
|
|
{
|
2017-11-30 09:44:58 +00:00
|
|
|
core_util_critical_section_enter();
|
2016-06-15 19:56:03 +00:00
|
|
|
|
2018-06-19 06:55:35 +00:00
|
|
|
const uint32_t counter_mask = ((1ULL << US_TICKER_COUNTER_BITS) - 1);
|
2018-05-25 10:49:34 +00:00
|
|
|
|
|
|
|
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, timestamp & counter_mask);
|
2017-04-03 12:00:11 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
if (!nrf_timer_int_enable_check(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0))) {
|
|
|
|
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
|
|
|
|
nrf_timer_int_enable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
2016-06-22 12:00:30 +00:00
|
|
|
|
2017-04-03 12:00:11 +00:00
|
|
|
core_util_critical_section_exit();
|
2017-03-24 11:22:28 +00:00
|
|
|
}
|
2016-06-15 19:56:03 +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)
|
|
|
|
{
|
2017-11-30 09:44:58 +00:00
|
|
|
NVIC_SetPendingIRQ(TIMER1_IRQn);
|
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
|
|
|
}
|
|
|
|
|
2016-06-15 19:56:03 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_clear_interrupt(void)
|
|
|
|
{
|
2017-11-30 09:44:58 +00:00
|
|
|
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
|
2016-06-15 19:56:03 +00:00
|
|
|
}
|
2016-07-22 10:39:09 +00:00
|
|
|
|
2017-11-30 09:44:58 +00:00
|
|
|
void us_ticker_free(void)
|
2017-05-15 14:55:45 +00:00
|
|
|
{
|
2018-05-24 11:19:50 +00:00
|
|
|
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP);
|
|
|
|
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
|
2018-07-20 06:58:11 +00:00
|
|
|
NVIC_DisableIRQ(TIMER1_IRQn);
|
2018-05-24 11:19:50 +00:00
|
|
|
us_ticker_initialized = false;
|
2016-07-22 12:58:17 +00:00
|
|
|
}
|