初始版本
This commit is contained in:
1075
components/libraries/timer/app_timer.c
Normal file
1075
components/libraries/timer/app_timer.c
Normal file
File diff suppressed because it is too large
Load Diff
313
components/libraries/timer/app_timer.h
Normal file
313
components/libraries/timer/app_timer.h
Normal file
@@ -0,0 +1,313 @@
|
||||
/**
|
||||
* Copyright (c) 2012 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 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 the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 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 written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
|
||||
*
|
||||
*/
|
||||
/** @file
|
||||
*
|
||||
* @defgroup app_timer Application Timer
|
||||
* @{
|
||||
* @ingroup app_common
|
||||
*
|
||||
* @brief Application timer functionality.
|
||||
*
|
||||
* @details This module enables the application to create multiple timer instances based on the RTC1
|
||||
* peripheral. Checking for time-outs and invocation of user time-out handlers is performed
|
||||
* in the RTC1 interrupt handler. List handling is done using a software interrupt (SWI0).
|
||||
* Both interrupt handlers are running in APP_LOW priority level.
|
||||
*
|
||||
* @details When calling app_timer_start() or app_timer_stop(), the timer operation is just queued,
|
||||
* and the software interrupt is triggered. The actual timer start/stop operation is
|
||||
* executed by the SWI0 interrupt handler. Since the SWI0 interrupt is running in APP_LOW,
|
||||
* if the application code calling the timer function is running in APP_LOW or APP_HIGH,
|
||||
* the timer operation will not be performed until the application handler has returned.
|
||||
* This will be the case, for example, when stopping a timer from a time-out handler when not using
|
||||
* the scheduler.
|
||||
*
|
||||
* @details Use the USE_SCHEDULER parameter of the APP_TIMER_INIT() macro to select if the
|
||||
* @ref app_scheduler should be used or not. Even if the scheduler is
|
||||
* not used, app_timer.h will include app_scheduler.h, so when
|
||||
* compiling, app_scheduler.h must be available in one of the compiler include paths.
|
||||
*/
|
||||
|
||||
#ifndef APP_TIMER_H__
|
||||
#define APP_TIMER_H__
|
||||
#include "sdk_config.h"
|
||||
#include "app_error.h"
|
||||
#include "app_util.h"
|
||||
#include "compiler_abstraction.h"
|
||||
#include "nordic_common.h"
|
||||
#ifdef APP_TIMER_V2
|
||||
#include "nrf_log_instance.h"
|
||||
#include "nrf_sortlist.h"
|
||||
#endif
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief Name of the module used for logger messaging.
|
||||
*/
|
||||
#define APP_TIMER_LOG_NAME app_timer
|
||||
|
||||
#define APP_TIMER_CLOCK_FREQ 32768 /**< Clock frequency of the RTC timer used to implement the app timer module. */
|
||||
#define APP_TIMER_MIN_TIMEOUT_TICKS 5 /**< Minimum value of the timeout_ticks parameter of app_timer_start(). */
|
||||
|
||||
#ifdef RTX
|
||||
#define APP_TIMER_NODE_SIZE 40 /**< Size of app_timer.timer_node_t (used to allocate data). */
|
||||
#else
|
||||
#define APP_TIMER_NODE_SIZE 32 /**< Size of app_timer.timer_node_t (used to allocate data). */
|
||||
#endif // RTX
|
||||
|
||||
#define APP_TIMER_SCHED_EVENT_DATA_SIZE sizeof(app_timer_event_t) /**< Size of event data when scheduler is used. */
|
||||
|
||||
#define APP_TIMER_MAX_CNT_VAL RTC_COUNTER_COUNTER_Msk /**< Maximum counter value that can be returned by @ref app_timer_cnt_get. */
|
||||
|
||||
/**@brief Convert milliseconds to timer ticks.
|
||||
*
|
||||
* This macro uses 64-bit integer arithmetic, but as long as the macro parameters are
|
||||
* constants (i.e. defines), the computation will be done by the preprocessor.
|
||||
*
|
||||
* @param[in] MS Milliseconds.
|
||||
*
|
||||
* @return Number of timer ticks.
|
||||
*/
|
||||
#ifndef FREERTOS
|
||||
#define APP_TIMER_TICKS(MS) \
|
||||
((uint32_t)ROUNDED_DIV( \
|
||||
(MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, \
|
||||
1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
|
||||
#else
|
||||
#include "FreeRTOSConfig.h"
|
||||
#define APP_TIMER_TICKS(MS) (uint32_t)ROUNDED_DIV((MS)*configTICK_RATE_HZ,1000)
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @brief Create a timer identifier and statically allocate memory for the timer.
|
||||
*
|
||||
* @param timer_id Name of the timer identifier variable that will be used to control the timer.
|
||||
*/
|
||||
#define APP_TIMER_DEF(timer_id) _APP_TIMER_DEF(timer_id)
|
||||
|
||||
/**@brief Application time-out handler type. */
|
||||
typedef void (*app_timer_timeout_handler_t)(void * p_context);
|
||||
|
||||
#ifdef APP_TIMER_V2
|
||||
/**
|
||||
* @brief app_timer control block
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nrf_sortlist_item_t list_item; /**< Token used by sortlist. */
|
||||
uint64_t end_val; /**< RTC counter value when timer expires. */
|
||||
uint32_t repeat_period; /**< Repeat period (0 if single shot mode). */
|
||||
app_timer_timeout_handler_t handler; /**< User handler. */
|
||||
void * p_context; /**< User context. */
|
||||
NRF_LOG_INSTANCE_PTR_DECLARE(p_log) /**< Pointer to instance of the logger object (Conditionally compiled). */
|
||||
volatile bool active; /**< Flag indicating that timer is active. */
|
||||
} app_timer_t;
|
||||
|
||||
/**@brief Timer ID type.
|
||||
* Never declare a variable of this type, but use the macro @ref APP_TIMER_DEF instead.*/
|
||||
typedef app_timer_t * app_timer_id_t;
|
||||
|
||||
#define _APP_TIMER_DEF(timer_id) \
|
||||
NRF_LOG_INSTANCE_REGISTER(APP_TIMER_LOG_NAME, timer_id, \
|
||||
APP_TIMER_CONFIG_INFO_COLOR, \
|
||||
APP_TIMER_CONFIG_DEBUG_COLOR, \
|
||||
APP_TIMER_CONFIG_INITIAL_LOG_LEVEL, \
|
||||
APP_TIMER_CONFIG_LOG_ENABLED ? \
|
||||
APP_TIMER_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
|
||||
static app_timer_t CONCAT_2(timer_id,_data) = { \
|
||||
.active = false, \
|
||||
NRF_LOG_INSTANCE_PTR_INIT(p_log, APP_TIMER_LOG_NAME, timer_id) \
|
||||
}; \
|
||||
static const app_timer_id_t timer_id = &CONCAT_2(timer_id,_data)
|
||||
|
||||
#else //APP_TIMER_V2
|
||||
typedef struct app_timer_t { uint32_t data[CEIL_DIV(APP_TIMER_NODE_SIZE, sizeof(uint32_t))]; } app_timer_t;
|
||||
|
||||
/**@brief Timer ID type.
|
||||
* Never declare a variable of this type, but use the macro @ref APP_TIMER_DEF instead.*/
|
||||
typedef app_timer_t * app_timer_id_t;
|
||||
|
||||
#define _APP_TIMER_DEF(timer_id) \
|
||||
static app_timer_t CONCAT_2(timer_id,_data) = { {0} }; \
|
||||
static const app_timer_id_t timer_id = &CONCAT_2(timer_id,_data)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/**@brief Structure passed to app_scheduler. */
|
||||
typedef struct
|
||||
{
|
||||
app_timer_timeout_handler_t timeout_handler;
|
||||
void * p_context;
|
||||
} app_timer_event_t;
|
||||
|
||||
/**@brief Timer modes. */
|
||||
typedef enum
|
||||
{
|
||||
APP_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */
|
||||
APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
|
||||
} app_timer_mode_t;
|
||||
|
||||
/**@brief Function for initializing the timer module.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the module was initialized successfully.
|
||||
*/
|
||||
ret_code_t app_timer_init(void);
|
||||
|
||||
/**@brief Function for creating a timer instance.
|
||||
*
|
||||
* @param[in] p_timer_id Pointer to timer identifier.
|
||||
* @param[in] mode Timer mode.
|
||||
* @param[in] timeout_handler Function to be executed when the timer expires.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the timer was successfully created.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
|
||||
* @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or
|
||||
* the timer is running.
|
||||
*
|
||||
* @note This function does the timer allocation in the caller's context. It is also not protected
|
||||
* by a critical region. Therefore care must be taken not to call it from several interrupt
|
||||
* levels simultaneously.
|
||||
* @note The function can be called again on the timer instance and will re-initialize the instance if
|
||||
* the timer is not running.
|
||||
* @attention The FreeRTOS and RTX app_timer implementation does not allow app_timer_create to
|
||||
* be called on the previously initialized instance.
|
||||
*/
|
||||
ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
|
||||
app_timer_mode_t mode,
|
||||
app_timer_timeout_handler_t timeout_handler);
|
||||
|
||||
/**@brief Function for starting a timer.
|
||||
*
|
||||
* @param[in] timer_id Timer identifier.
|
||||
* @param[in] timeout_ticks Number of ticks (of RTC1, including prescaling) to time-out event
|
||||
* (minimum 5 ticks).
|
||||
* @param[in] p_context General purpose pointer. Will be passed to the time-out handler when
|
||||
* the timer expires.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the timer was successfully started.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
|
||||
* @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or the timer
|
||||
* has not been created.
|
||||
* @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
|
||||
*
|
||||
* @note The minimum timeout_ticks value is 5.
|
||||
* @note For multiple active timers, time-outs occurring in close proximity to each other (in the
|
||||
* range of 1 to 3 ticks) will have a positive jitter of maximum 3 ticks.
|
||||
* @note When calling this method on a timer that is already running, the second start operation
|
||||
* is ignored.
|
||||
*/
|
||||
ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context);
|
||||
|
||||
/**@brief Function for stopping the specified timer.
|
||||
*
|
||||
* @param[in] timer_id Timer identifier.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the timer was successfully stopped.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
|
||||
* @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or the timer
|
||||
* has not been created.
|
||||
* @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
|
||||
*/
|
||||
ret_code_t app_timer_stop(app_timer_id_t timer_id);
|
||||
|
||||
/**@brief Function for stopping all running timers.
|
||||
*
|
||||
* @retval NRF_SUCCESS If all timers were successfully stopped.
|
||||
* @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized.
|
||||
* @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
|
||||
*/
|
||||
ret_code_t app_timer_stop_all(void);
|
||||
|
||||
/**@brief Function for returning the current value of the RTC1 counter.
|
||||
*
|
||||
* @return Current value of the RTC1 counter.
|
||||
*/
|
||||
uint32_t app_timer_cnt_get(void);
|
||||
|
||||
/**@brief Function for computing the difference between two RTC1 counter values.
|
||||
*
|
||||
* @param[in] ticks_to Value returned by app_timer_cnt_get().
|
||||
* @param[in] ticks_from Value returned by app_timer_cnt_get().
|
||||
*
|
||||
* @return Number of ticks from ticks_from to ticks_to.
|
||||
*/
|
||||
uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
|
||||
uint32_t ticks_from);
|
||||
|
||||
|
||||
/**@brief Function for getting the maximum observed operation queue utilization.
|
||||
*
|
||||
* Function for tuning the module and determining OP_QUEUE_SIZE value and thus module RAM usage.
|
||||
*
|
||||
* @note APP_TIMER_WITH_PROFILER must be enabled to use this functionality.
|
||||
*
|
||||
* @return Maximum number of events in queue observed so far.
|
||||
*/
|
||||
uint8_t app_timer_op_queue_utilization_get(void);
|
||||
|
||||
/**
|
||||
* @brief Function for pausing RTC activity which drives app_timer.
|
||||
*
|
||||
* @note This function can be used for debugging purposes to ensure
|
||||
* that application is halted when entering a breakpoint.
|
||||
*/
|
||||
void app_timer_pause(void);
|
||||
|
||||
/**
|
||||
* @brief Function for resuming RTC activity which drives app_timer.
|
||||
*
|
||||
* @note This function can be used for debugging purposes to resume
|
||||
* application activity.
|
||||
*/
|
||||
void app_timer_resume(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // APP_TIMER_H__
|
||||
|
||||
/** @} */
|
||||
637
components/libraries/timer/app_timer2.c
Normal file
637
components/libraries/timer/app_timer2.c
Normal file
@@ -0,0 +1,637 @@
|
||||
/**
|
||||
* Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 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 the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 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 written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
|
||||
*
|
||||
*/
|
||||
#include "app_timer.h"
|
||||
#include "nrf_atfifo.h"
|
||||
#include "nrf_sortlist.h"
|
||||
#include "nrf_delay.h"
|
||||
#if APP_TIMER_WITH_PROFILER
|
||||
#include "app_util_platform.h"
|
||||
#endif
|
||||
#if APP_TIMER_CONFIG_USE_SCHEDULER
|
||||
#include "app_scheduler.h"
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#define NRF_LOG_MODULE_NAME APP_TIMER_LOG_NAME
|
||||
#if APP_TIMER_CONFIG_LOG_ENABLED
|
||||
#define NRF_LOG_LEVEL APP_TIMER_CONFIG_LOG_LEVEL
|
||||
#define NRF_LOG_INFO_COLOR APP_TIMER_CONFIG_INFO_COLOR
|
||||
#define NRF_LOG_DEBUG_COLOR APP_TIMER_CONFIG_DEBUG_COLOR
|
||||
#else //APP_TIMER_CONFIG_LOG_ENABLED
|
||||
#define NRF_LOG_LEVEL 0
|
||||
#endif //APP_TIMER_CONFIG_LOG_ENABLED
|
||||
#include "nrf_log.h"
|
||||
NRF_LOG_MODULE_REGISTER();
|
||||
|
||||
#include "drv_rtc.h"
|
||||
|
||||
/**
|
||||
* Maximum possible relative value is limited by safe window to detect cases when requested
|
||||
* compare event has already occured.
|
||||
*/
|
||||
#define APP_TIMER_SAFE_WINDOW APP_TIMER_TICKS(APP_TIMER_SAFE_WINDOW_MS)
|
||||
|
||||
#define APP_TIMER_RTC_MAX_VALUE (DRV_RTC_MAX_CNT - APP_TIMER_SAFE_WINDOW)
|
||||
|
||||
static drv_rtc_t m_rtc_inst = DRV_RTC_INSTANCE(1);
|
||||
|
||||
#if APP_TIMER_WITH_PROFILER
|
||||
static uint8_t m_max_user_op_queue_utilization; /**< Maximum observed timer user operations queue utilization. */
|
||||
static uint8_t m_current_user_op_queue_utilization; /**< Currently observed timer user operations queue utilization. */
|
||||
#endif /* APP_TIMER_WITH_PROFILER */
|
||||
|
||||
/**
|
||||
* @brief Timer requests types.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
TIMER_REQ_START,
|
||||
TIMER_REQ_STOP,
|
||||
TIMER_REQ_STOP_ALL
|
||||
} app_timer_req_type_t;
|
||||
|
||||
/**
|
||||
* @brief Operation request structure.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
app_timer_req_type_t type; /**< Request type. */
|
||||
app_timer_t * p_timer; /**< Timer instance. */
|
||||
} timer_req_t;
|
||||
|
||||
static app_timer_t * volatile mp_active_timer; /**< Timer currently handled by RTC driver. */
|
||||
static bool m_global_active; /**< Flag used to globally disable all timers. */
|
||||
static uint64_t m_base_counter;
|
||||
static uint64_t m_stamp64;
|
||||
|
||||
/* Request FIFO instance. */
|
||||
NRF_ATFIFO_DEF(m_req_fifo, timer_req_t, APP_TIMER_CONFIG_OP_QUEUE_SIZE);
|
||||
|
||||
/* Sortlist instance. */
|
||||
static bool compare_func(nrf_sortlist_item_t * p_item0, nrf_sortlist_item_t *p_item1);
|
||||
NRF_SORTLIST_DEF(m_app_timer_sortlist, compare_func); /**< Sortlist used for storing queued timers. */
|
||||
|
||||
/**
|
||||
* @brief Return current 64 bit timestamp
|
||||
*/
|
||||
static uint64_t get_now(void)
|
||||
{
|
||||
uint64_t now = m_base_counter + drv_rtc_counter_get(&m_rtc_inst);
|
||||
|
||||
/* it is possible that base was not updated and overflow occured, in that case 'now' will be
|
||||
* 24bit value behind. Additional timestamp updated on every 24 bit period is used to detect
|
||||
* that case. Apart from that 'now' should never be behind previously read timestamp.
|
||||
*/
|
||||
if (now < m_stamp64) {
|
||||
now += (DRV_RTC_MAX_CNT + 1);
|
||||
}
|
||||
|
||||
return now;
|
||||
}
|
||||
/**
|
||||
* @brief Function used for comparing items in sorted list.
|
||||
*/
|
||||
static inline bool compare_func(nrf_sortlist_item_t * p_item0, nrf_sortlist_item_t *p_item1)
|
||||
{
|
||||
app_timer_t * p0 = CONTAINER_OF(p_item0, app_timer_t, list_item);
|
||||
app_timer_t * p1 = CONTAINER_OF(p_item1, app_timer_t, list_item);
|
||||
|
||||
uint64_t p0_end = p0->end_val;
|
||||
uint64_t p1_end = p1->end_val;
|
||||
return (p0_end <= p1_end) ? true : false;
|
||||
}
|
||||
|
||||
#if APP_TIMER_CONFIG_USE_SCHEDULER
|
||||
static void scheduled_timeout_handler(void * p_event_data, uint16_t event_size)
|
||||
{
|
||||
ASSERT(event_size == sizeof(app_timer_event_t));
|
||||
app_timer_event_t const * p_timer_event = (app_timer_event_t *)p_event_data;
|
||||
|
||||
p_timer_event->timeout_handler(p_timer_event->p_context);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Function called on timer expiration
|
||||
* If end value is not reached it is assumed that it was partial expiration and time is put back
|
||||
* into the list. Otherwise function calls user handler if timer was not stopped before. If timer
|
||||
* is in repeated mode then timer is rescheduled.
|
||||
*
|
||||
* @param p_timer Timer instance.
|
||||
*
|
||||
* @return True if reevaluation of sortlist needed (becasue it was updated).
|
||||
*/
|
||||
static bool timer_expire(app_timer_t * p_timer)
|
||||
{
|
||||
ASSERT(p_timer->handler);
|
||||
bool ret = false;
|
||||
|
||||
if ((m_global_active == true) && (p_timer != NULL) && (p_timer->active))
|
||||
{
|
||||
if (get_now() >= p_timer->end_val) {
|
||||
/* timer expired */
|
||||
if (p_timer->repeat_period == 0)
|
||||
{
|
||||
p_timer->active = false;
|
||||
}
|
||||
#if APP_TIMER_CONFIG_USE_SCHEDULER
|
||||
app_timer_event_t timer_event;
|
||||
|
||||
timer_event.timeout_handler = p_timer->handler;
|
||||
timer_event.p_context = p_timer->p_context;
|
||||
uint32_t err_code = app_sched_event_put(&timer_event,
|
||||
sizeof(timer_event),
|
||||
scheduled_timeout_handler);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
#else
|
||||
NRF_LOG_DEBUG("Timer expired (context: %d)", (uint32_t)p_timer->p_context)
|
||||
p_timer->handler(p_timer->p_context);
|
||||
#endif
|
||||
/* check active flag as it may have been stopped in the user handler */
|
||||
if ((p_timer->repeat_period) && (p_timer->active))
|
||||
{
|
||||
p_timer->end_val += p_timer->repeat_period;
|
||||
nrf_sortlist_add(&m_app_timer_sortlist, &p_timer->list_item);
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
nrf_sortlist_add(&m_app_timer_sortlist, &p_timer->list_item);
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function is configuring RTC driver to trigger timeout interrupt for given timer.
|
||||
*
|
||||
* It is possible that RTC driver will indicate that timeout already occured. In that case timer
|
||||
* expires and function indicates that RTC was not configured.
|
||||
*
|
||||
* @param p_timer Timer instance.
|
||||
* @param [in,out] p_rerun Flag indicating that sortlist reevaluation is required.
|
||||
*
|
||||
* @return True if RTC was successfully configured, false if timer already expired and RTC was not
|
||||
* configured.
|
||||
*
|
||||
*/
|
||||
static bool rtc_schedule(app_timer_t * p_timer, bool * p_rerun)
|
||||
{
|
||||
ret_code_t ret = NRF_ERROR_TIMEOUT;
|
||||
*p_rerun = false;
|
||||
int64_t remaining = (int64_t)(p_timer->end_val - get_now());
|
||||
|
||||
if (remaining > 0) {
|
||||
uint32_t cc_val = ((uint32_t)remaining > APP_TIMER_RTC_MAX_VALUE) ?
|
||||
(app_timer_cnt_get() + APP_TIMER_RTC_MAX_VALUE) : p_timer->end_val;
|
||||
|
||||
ret = drv_rtc_windowed_compare_set(&m_rtc_inst, 0, cc_val, APP_TIMER_SAFE_WINDOW);
|
||||
NRF_LOG_DEBUG("Setting CC to 0x%08x (err: %d)", cc_val & DRV_RTC_MAX_CNT, ret);
|
||||
if (ret == NRF_SUCCESS)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
drv_rtc_compare_disable(&m_rtc_inst, 0);
|
||||
}
|
||||
|
||||
if (ret == NRF_ERROR_TIMEOUT)
|
||||
{
|
||||
*p_rerun = timer_expire(p_timer);
|
||||
}
|
||||
else
|
||||
{
|
||||
NRF_LOG_ERROR("Unexpected error: %d", ret);
|
||||
ASSERT(0);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline app_timer_t * sortlist_pop(void)
|
||||
{
|
||||
nrf_sortlist_item_t * p_next_item = nrf_sortlist_pop(&m_app_timer_sortlist);
|
||||
return p_next_item ? CONTAINER_OF(p_next_item, app_timer_t, list_item) : NULL;
|
||||
}
|
||||
|
||||
static inline app_timer_t * sortlist_peek(void)
|
||||
{
|
||||
nrf_sortlist_item_t const * p_next_item = nrf_sortlist_peek(&m_app_timer_sortlist);
|
||||
return p_next_item ? CONTAINER_OF(p_next_item, app_timer_t, list_item) : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for deactivating all timers which are in the sorted list (active timers).
|
||||
*/
|
||||
static void sorted_list_stop_all(void)
|
||||
{
|
||||
app_timer_t * p_next;
|
||||
do
|
||||
{
|
||||
p_next = sortlist_pop();
|
||||
if (p_next)
|
||||
{
|
||||
p_next->active = false;
|
||||
}
|
||||
} while (p_next);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for handling RTC counter overflow.
|
||||
*
|
||||
* Increment base counter used to calculate 64 bit timestamp.
|
||||
*/
|
||||
static void on_overflow_evt(void)
|
||||
{
|
||||
NRF_LOG_DEBUG("Overflow EVT");
|
||||
m_base_counter += (DRV_RTC_MAX_CNT + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* #brief Function for handling RTC compare event - active timer expiration.
|
||||
*/
|
||||
static void on_compare_evt(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
if (mp_active_timer)
|
||||
{
|
||||
/* If assert fails it suggests that safe window should be increased. */
|
||||
ASSERT(app_timer_cnt_diff_compute(drv_rtc_counter_get(p_instance),
|
||||
drv_rtc_compare_get(p_instance, 0)) < APP_TIMER_SAFE_WINDOW);
|
||||
|
||||
NRF_LOG_INST_DEBUG(mp_active_timer->p_log, "Compare EVT");
|
||||
UNUSED_RETURN_VALUE(timer_expire(mp_active_timer));
|
||||
mp_active_timer = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
NRF_LOG_WARNING("Compare event but no active timer (already stopped?)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Channel 1 is triggered in the middle of 24 bit period to updated control timestamp in
|
||||
* place where there is no risk of overflow.
|
||||
*/
|
||||
static void on_compare1_evt(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
m_stamp64 = get_now();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function updates RTC.
|
||||
*
|
||||
* Function is called at the end of RTC interrupt when all new user request and/or timer expiration
|
||||
* occured. It configures RTC if there is any pending timer, reconfigures if the are timers with
|
||||
* shorted timeout than active one or stops RTC if there is no active timers.
|
||||
*/
|
||||
static void rtc_update(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
app_timer_t * p_next = sortlist_peek();
|
||||
bool rtc_reconf = false;
|
||||
if (p_next) //Candidate for active timer
|
||||
{
|
||||
if (mp_active_timer == NULL)
|
||||
{
|
||||
//There is no active timer so candidate will become active timer.
|
||||
rtc_reconf = true;
|
||||
}
|
||||
else if (p_next->end_val < mp_active_timer->end_val)
|
||||
{
|
||||
//Candidate has shorter timeout than current active timer. Candidate will replace active timer.
|
||||
//Active timer is put back into sorted list.
|
||||
rtc_reconf = true;
|
||||
if (mp_active_timer->active)
|
||||
{
|
||||
NRF_LOG_INST_DEBUG(mp_active_timer->p_log, "Timer preempted.");
|
||||
nrf_sortlist_add(&m_app_timer_sortlist, &mp_active_timer->list_item);
|
||||
}
|
||||
}
|
||||
|
||||
if (rtc_reconf)
|
||||
{
|
||||
bool rerun;
|
||||
p_next = sortlist_pop();
|
||||
NRF_LOG_INST_DEBUG(p_next->p_log, "Activating timer (CC:%d/%08x).", p_next->end_val, p_next->end_val);
|
||||
if (rtc_schedule(p_next, &rerun))
|
||||
{
|
||||
if (!APP_TIMER_KEEPS_RTC_ACTIVE && (mp_active_timer == NULL))
|
||||
{
|
||||
drv_rtc_start(p_instance);
|
||||
}
|
||||
mp_active_timer = p_next;
|
||||
|
||||
if (rerun == false)
|
||||
{
|
||||
//RTC was successfully updated and sortlist was not updated. Function can be terminated.
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//If RTC driver indicated that timeout already occured a new candidate will be taken from sorted list.
|
||||
NRF_LOG_INST_DEBUG(p_next->p_log,"Timer expired before scheduled to RTC.");
|
||||
mp_active_timer = NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//RTC will not be updated. Function can terminate.
|
||||
break;
|
||||
}
|
||||
}
|
||||
else //No candidate for active timer.
|
||||
{
|
||||
if (!APP_TIMER_KEEPS_RTC_ACTIVE && (mp_active_timer == NULL))
|
||||
{
|
||||
drv_rtc_stop(p_instance);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for processing user requests.
|
||||
*
|
||||
* Function is called only in the context of RTC interrupt.
|
||||
*/
|
||||
static void timer_req_process(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
nrf_atfifo_item_get_t fifo_ctx;
|
||||
timer_req_t * p_req = nrf_atfifo_item_get(m_req_fifo, &fifo_ctx);
|
||||
|
||||
while (p_req)
|
||||
{
|
||||
switch (p_req->type)
|
||||
{
|
||||
case TIMER_REQ_START:
|
||||
if (!p_req->p_timer->active)
|
||||
{
|
||||
p_req->p_timer->active = true;
|
||||
nrf_sortlist_add(&m_app_timer_sortlist, &(p_req->p_timer->list_item));
|
||||
NRF_LOG_INST_DEBUG(p_req->p_timer->p_log,"Start request (expiring at %d/0x%08x).",
|
||||
p_req->p_timer->end_val, p_req->p_timer->end_val);
|
||||
}
|
||||
break;
|
||||
case TIMER_REQ_STOP:
|
||||
if (p_req->p_timer == mp_active_timer)
|
||||
{
|
||||
mp_active_timer = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
bool found = nrf_sortlist_remove(&m_app_timer_sortlist, &(p_req->p_timer->list_item));
|
||||
if (!found)
|
||||
{
|
||||
NRF_LOG_INFO("Timer not found on sortlist (stopping expired timer).");
|
||||
}
|
||||
}
|
||||
NRF_LOG_INST_DEBUG(p_req->p_timer->p_log,"Stop request.");
|
||||
break;
|
||||
case TIMER_REQ_STOP_ALL:
|
||||
sorted_list_stop_all();
|
||||
m_global_active = true;
|
||||
NRF_LOG_INFO("Stop all request.");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#if APP_TIMER_WITH_PROFILER
|
||||
CRITICAL_REGION_ENTER();
|
||||
#endif
|
||||
UNUSED_RETURN_VALUE(nrf_atfifo_item_free(m_req_fifo, &fifo_ctx));
|
||||
#if APP_TIMER_WITH_PROFILER
|
||||
if (m_max_user_op_queue_utilization < m_current_user_op_queue_utilization)
|
||||
{
|
||||
m_max_user_op_queue_utilization = m_current_user_op_queue_utilization;
|
||||
}
|
||||
--m_current_user_op_queue_utilization;
|
||||
CRITICAL_REGION_EXIT();
|
||||
#endif /* APP_TIMER_WITH_PROFILER */
|
||||
p_req = nrf_atfifo_item_get(m_req_fifo, &fifo_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void rtc_irq(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
if (drv_rtc_overflow_pending(p_instance))
|
||||
{
|
||||
on_overflow_evt();
|
||||
}
|
||||
if (drv_rtc_compare_pending(p_instance, 0))
|
||||
{
|
||||
on_compare_evt(p_instance);
|
||||
}
|
||||
if (drv_rtc_compare_pending(p_instance, 1))
|
||||
{
|
||||
on_compare1_evt(p_instance);
|
||||
}
|
||||
|
||||
timer_req_process(p_instance);
|
||||
rtc_update(p_instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for triggering processing user requests.
|
||||
*
|
||||
* @note All user requests are processed in a single context - RTC interrupt.
|
||||
*/
|
||||
static inline void timer_request_proc_trigger(void)
|
||||
{
|
||||
drv_rtc_irq_trigger(&m_rtc_inst);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for putting user request into the request queue
|
||||
*/
|
||||
static ret_code_t timer_req_schedule(app_timer_req_type_t type, app_timer_t * p_timer)
|
||||
{
|
||||
nrf_atfifo_item_put_t fifo_ctx;
|
||||
timer_req_t * p_req;
|
||||
#if APP_TIMER_WITH_PROFILER
|
||||
CRITICAL_REGION_ENTER();
|
||||
#endif
|
||||
p_req = nrf_atfifo_item_alloc(m_req_fifo, &fifo_ctx);
|
||||
#if APP_TIMER_WITH_PROFILER
|
||||
if (p_req)
|
||||
{
|
||||
++m_current_user_op_queue_utilization;
|
||||
}
|
||||
CRITICAL_REGION_EXIT();
|
||||
#endif /* APP_TIMER_WITH_PROFILER */
|
||||
if (p_req)
|
||||
{
|
||||
p_req->type = type;
|
||||
p_req->p_timer = p_timer;
|
||||
if (nrf_atfifo_item_put(m_req_fifo, &fifo_ctx))
|
||||
{
|
||||
timer_request_proc_trigger();
|
||||
}
|
||||
else
|
||||
{
|
||||
NRF_LOG_WARNING("Scheduling interrupted another scheduling.");
|
||||
}
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
ret_code_t app_timer_init(void)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
drv_rtc_config_t config = {
|
||||
.prescaler = APP_TIMER_CONFIG_RTC_FREQUENCY,
|
||||
.interrupt_priority = APP_TIMER_CONFIG_IRQ_PRIORITY
|
||||
};
|
||||
|
||||
err_code = NRF_ATFIFO_INIT(m_req_fifo);
|
||||
if (err_code != NRFX_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
err_code = drv_rtc_init(&m_rtc_inst, &config, rtc_irq);
|
||||
if (err_code != NRFX_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
drv_rtc_overflow_enable(&m_rtc_inst, true);
|
||||
drv_rtc_compare_set(&m_rtc_inst, 1, DRV_RTC_MAX_CNT >> 1, true);
|
||||
if (APP_TIMER_KEEPS_RTC_ACTIVE)
|
||||
{
|
||||
drv_rtc_start(&m_rtc_inst);
|
||||
}
|
||||
|
||||
m_global_active = true;
|
||||
return err_code;
|
||||
}
|
||||
|
||||
ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
|
||||
app_timer_mode_t mode,
|
||||
app_timer_timeout_handler_t timeout_handler)
|
||||
{
|
||||
ASSERT(p_timer_id);
|
||||
ASSERT(timeout_handler);
|
||||
|
||||
if (timeout_handler == NULL)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
app_timer_t * p_t = (app_timer_t *) *p_timer_id;
|
||||
p_t->handler = timeout_handler;
|
||||
p_t->repeat_period = (mode == APP_TIMER_MODE_REPEATED) ? 1 : 0;
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
ret_code_t app_timer_start(app_timer_t * p_timer, uint32_t timeout_ticks, void * p_context)
|
||||
{
|
||||
ASSERT(p_timer);
|
||||
app_timer_t * p_t = (app_timer_t *) p_timer;
|
||||
|
||||
if (p_t->active)
|
||||
{
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
p_t->p_context = p_context;
|
||||
p_t->end_val = get_now() + timeout_ticks;
|
||||
|
||||
if (p_t->repeat_period)
|
||||
{
|
||||
p_t->repeat_period = timeout_ticks;
|
||||
}
|
||||
|
||||
return timer_req_schedule(TIMER_REQ_START, p_t);
|
||||
}
|
||||
|
||||
|
||||
ret_code_t app_timer_stop(app_timer_t * p_timer)
|
||||
{
|
||||
ASSERT(p_timer);
|
||||
app_timer_t * p_t = (app_timer_t *) p_timer;
|
||||
p_t->active = false;
|
||||
|
||||
return timer_req_schedule(TIMER_REQ_STOP, p_t);
|
||||
}
|
||||
|
||||
ret_code_t app_timer_stop_all(void)
|
||||
{
|
||||
//block timer globally
|
||||
m_global_active = false;
|
||||
|
||||
return timer_req_schedule(TIMER_REQ_STOP_ALL, NULL);
|
||||
}
|
||||
|
||||
#if APP_TIMER_WITH_PROFILER
|
||||
uint8_t app_timer_op_queue_utilization_get(void)
|
||||
{
|
||||
return m_max_user_op_queue_utilization;
|
||||
}
|
||||
#endif /* APP_TIMER_WITH_PROFILER */
|
||||
|
||||
uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
|
||||
uint32_t ticks_from)
|
||||
{
|
||||
return ((ticks_to - ticks_from) & RTC_COUNTER_COUNTER_Msk);
|
||||
}
|
||||
|
||||
uint32_t app_timer_cnt_get(void)
|
||||
{
|
||||
return drv_rtc_counter_get(&m_rtc_inst);
|
||||
}
|
||||
|
||||
void app_timer_pause(void)
|
||||
{
|
||||
drv_rtc_stop(&m_rtc_inst);
|
||||
}
|
||||
|
||||
void app_timer_resume(void)
|
||||
{
|
||||
drv_rtc_start(&m_rtc_inst);
|
||||
}
|
||||
243
components/libraries/timer/app_timer_freertos.c
Normal file
243
components/libraries/timer/app_timer_freertos.c
Normal file
@@ -0,0 +1,243 @@
|
||||
/**
|
||||
* Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 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 the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 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 written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
|
||||
*
|
||||
*/
|
||||
#include "sdk_common.h"
|
||||
#if NRF_MODULE_ENABLED(APP_TIMER)
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "timers.h"
|
||||
|
||||
#include "app_timer.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "nrf.h"
|
||||
#include "app_error.h"
|
||||
|
||||
/**
|
||||
* Note that this implementation is made only for enable SDK components which interacts with app_timer to work with FreeRTOS.
|
||||
* It is more suitable to use native FreeRTOS timer for other purposes.
|
||||
*/
|
||||
/* Check if RTC FreeRTOS version is used */
|
||||
#if configTICK_SOURCE != FREERTOS_USE_RTC
|
||||
#error app_timer in FreeRTOS variant have to be used with RTC tick source configuration. Default configuration have to be used in other case.
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Waiting time for the timer queue
|
||||
*
|
||||
* Number of system ticks to wait for the timer queue to put the message.
|
||||
* It is strongly recommended to set this to the value bigger than 1.
|
||||
* In other case if timer message queue is full - any operation on timer may fail.
|
||||
* @note
|
||||
* Timer functions called from interrupt context would never wait.
|
||||
*/
|
||||
#define APP_TIMER_WAIT_FOR_QUEUE 2
|
||||
|
||||
/**@brief This structure keeps information about osTimer.*/
|
||||
typedef struct
|
||||
{
|
||||
void * argument;
|
||||
TimerHandle_t osHandle;
|
||||
app_timer_timeout_handler_t func;
|
||||
/**
|
||||
* This member is to make sure that timer function is only called if timer is running.
|
||||
* FreeRTOS may have timer running even after stop function is called,
|
||||
* because it processes commands in Timer task and stopping function only puts command into the queue. */
|
||||
bool active;
|
||||
bool single_shot;
|
||||
}app_timer_info_t;
|
||||
|
||||
|
||||
/* Check if freeRTOS timers are activated */
|
||||
#if configUSE_TIMERS == 0
|
||||
#error app_timer for freeRTOS requires configUSE_TIMERS option to be activated.
|
||||
#endif
|
||||
|
||||
/* Check if app_timer_t variable type can held our app_timer_info_t structure */
|
||||
STATIC_ASSERT(sizeof(app_timer_info_t) <= sizeof(app_timer_t));
|
||||
|
||||
|
||||
/**
|
||||
* @brief Internal callback function for the system timer
|
||||
*
|
||||
* Internal function that is called from the system timer.
|
||||
* It gets our parameter from timer data and sends it to user function.
|
||||
* @param[in] xTimer Timer handler
|
||||
*/
|
||||
static void app_timer_callback(TimerHandle_t xTimer)
|
||||
{
|
||||
app_timer_info_t * pinfo = (app_timer_info_t*)(pvTimerGetTimerID(xTimer));
|
||||
ASSERT(pinfo->osHandle == xTimer);
|
||||
ASSERT(pinfo->func != NULL);
|
||||
|
||||
if (pinfo->active)
|
||||
{
|
||||
pinfo->active = (pinfo->single_shot) ? false : true;
|
||||
pinfo->func(pinfo->argument);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_timer_init(void)
|
||||
{
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
|
||||
app_timer_mode_t mode,
|
||||
app_timer_timeout_handler_t timeout_handler)
|
||||
{
|
||||
app_timer_info_t * pinfo = (app_timer_info_t*)(*p_timer_id);
|
||||
uint32_t err_code = NRF_SUCCESS;
|
||||
unsigned long timer_mode;
|
||||
|
||||
if ((timeout_handler == NULL) || (p_timer_id == NULL))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
if (pinfo->active)
|
||||
{
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (pinfo->osHandle == NULL)
|
||||
{
|
||||
/* New timer is created */
|
||||
memset(pinfo, 0, sizeof(app_timer_info_t));
|
||||
|
||||
timer_mode = (mode == APP_TIMER_MODE_SINGLE_SHOT) ? pdFALSE : pdTRUE;
|
||||
pinfo->single_shot = (mode == APP_TIMER_MODE_SINGLE_SHOT);
|
||||
pinfo->func = timeout_handler;
|
||||
pinfo->osHandle = xTimerCreate(" ", 1000, timer_mode, pinfo, app_timer_callback);
|
||||
|
||||
if (pinfo->osHandle == NULL)
|
||||
err_code = NRF_ERROR_NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Timer cannot be reinitialized using FreeRTOS API */
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
|
||||
{
|
||||
app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
|
||||
TimerHandle_t hTimer = pinfo->osHandle;
|
||||
|
||||
if (hTimer == NULL)
|
||||
{
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
}
|
||||
if (pinfo->active)
|
||||
{
|
||||
// Timer already running - exit silently
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
pinfo->argument = p_context;
|
||||
|
||||
if (__get_IPSR() != 0)
|
||||
{
|
||||
BaseType_t yieldReq = pdFALSE;
|
||||
|
||||
if (xTimerChangePeriodFromISR(hTimer, timeout_ticks, &yieldReq) != pdPASS)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
if ( xTimerStartFromISR(hTimer, &yieldReq) != pdPASS )
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
portYIELD_FROM_ISR(yieldReq);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (xTimerChangePeriod(hTimer, timeout_ticks, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
if (xTimerStart(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
pinfo->active = true;
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_timer_stop(app_timer_id_t timer_id)
|
||||
{
|
||||
app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
|
||||
TimerHandle_t hTimer = pinfo->osHandle;
|
||||
if (hTimer == NULL)
|
||||
{
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (__get_IPSR() != 0)
|
||||
{
|
||||
BaseType_t yieldReq = pdFALSE;
|
||||
if (xTimerStopFromISR(hTimer, &yieldReq) != pdPASS)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
portYIELD_FROM_ISR(yieldReq);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (xTimerStop(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
pinfo->active = false;
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
#endif //NRF_MODULE_ENABLED(APP_TIMER)
|
||||
278
components/libraries/timer/app_timer_rtx.c
Normal file
278
components/libraries/timer/app_timer_rtx.c
Normal file
@@ -0,0 +1,278 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 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 the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 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 written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
|
||||
*
|
||||
*/
|
||||
#include "sdk_common.h"
|
||||
#if NRF_MODULE_ENABLED(APP_TIMER)
|
||||
#include "app_timer.h"
|
||||
#include <stdlib.h>
|
||||
#include "nrf.h"
|
||||
#include "nrf_soc.h"
|
||||
#include "app_error.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "app_util_platform.h"
|
||||
|
||||
#define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOWEST /**< Priority of the RTC1 interrupt. */
|
||||
|
||||
#define MAX_RTC_COUNTER_VAL 0x00FFFFFF /**< Maximum value of the RTC counter. */
|
||||
|
||||
/**@brief This structure keeps information about osTimer.*/
|
||||
typedef struct
|
||||
{
|
||||
osTimerDef_t timerDef;
|
||||
uint32_t buffer[6];
|
||||
osTimerId id;
|
||||
}app_timer_info_t;
|
||||
|
||||
/**@brief Store an array of timers with configuration. */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t max_timers; /**< The maximum number of timers*/
|
||||
uint32_t prescaler;
|
||||
app_timer_info_t * app_timers; /**< Pointer to table of timers*/
|
||||
}app_timer_control_t;
|
||||
|
||||
app_timer_control_t app_timer_control;
|
||||
|
||||
/**@brief This structure is defined by RTX. It keeps information about created osTimers. It is used in app_timer_start(). */
|
||||
typedef struct os_timer_cb_
|
||||
{
|
||||
struct os_timer_cb_ * next; /**< Pointer to next active Timer */
|
||||
uint8_t state; /**< Timer State */
|
||||
uint8_t type; /**< Timer Type (Periodic/One-shot). */
|
||||
uint16_t reserved; /**< Reserved. */
|
||||
uint32_t tcnt; /**< Timer Delay Count. */
|
||||
uint32_t icnt; /**< Timer Initial Count. */
|
||||
void * arg; /**< Timer Function Argument. */
|
||||
const osTimerDef_t * timer; /**< Pointer to Timer definition. */
|
||||
} os_timer_cb;
|
||||
|
||||
/**@brief This functions are defined by RTX.*/
|
||||
//lint --save -e10 -e19 -e526
|
||||
extern osStatus svcTimerStop(osTimerId timer_id); /**< Used in app_timer_stop(). */
|
||||
extern osStatus svcTimerStart(osTimerId timer_id, uint32_t millisec); /**< Used in app_timer_start(). */
|
||||
// lint --restore
|
||||
static void * rt_id2obj (void *id) /**< Used in app_timer_start(). This function gives information if osTimerID is valid */
|
||||
{
|
||||
if ((uint32_t)id & 3U)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef OS_SECTIONS_LINK_INFO
|
||||
|
||||
if ((os_section_id$$Base != 0U) && (os_section_id$$Limit != 0U))
|
||||
{
|
||||
if (id < (void *)os_section_id$$Base)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (id >= (void *)os_section_id$$Limit)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ret_code_t app_timer_init(void)
|
||||
{
|
||||
if (p_buffer == NULL)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
app_timer_control.app_timers = p_buffer;
|
||||
NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
|
||||
app_timer_mode_t mode,
|
||||
app_timer_timeout_handler_t timeout_handler)
|
||||
{
|
||||
|
||||
if ((timeout_handler == NULL) || (p_timer_id == NULL))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
app_timer_info_t * p_timer_info = (app_timer_info_t *)*p_timer_id;
|
||||
p_timer_info->timerDef.timer = p_timer_info->buffer;
|
||||
p_timer_info->timerDef.ptimer = (os_ptimer)timeout_handler;
|
||||
|
||||
p_timer_info->id = osTimerCreate(&(p_timer_info->timerDef), (os_timer_type)mode, NULL);
|
||||
|
||||
if (p_timer_info->id)
|
||||
return NRF_SUCCESS;
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM; // This error is unspecified by rtx
|
||||
}
|
||||
}
|
||||
|
||||
#define osTimerRunning 2
|
||||
ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
|
||||
{
|
||||
if ((timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
uint32_t timeout_ms =
|
||||
((uint32_t)ROUNDED_DIV(timeout_ticks * 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1),
|
||||
(uint32_t)APP_TIMER_CLOCK_FREQ));
|
||||
|
||||
app_timer_info_t * p_timer_info = (app_timer_info_t *)timer_id;
|
||||
if (rt_id2obj((void *)p_timer_info->id) == NULL)
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
|
||||
// Pass p_context to timer_timeout_handler
|
||||
((os_timer_cb *)(p_timer_info->id))->arg = p_context;
|
||||
|
||||
if (((os_timer_cb *)(p_timer_info->id))->state == osTimerRunning)
|
||||
{
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
// osTimerStart() returns osErrorISR if it is called in interrupt routine.
|
||||
switch (osTimerStart((osTimerId)p_timer_info->id, timeout_ms) )
|
||||
{
|
||||
case osOK:
|
||||
return NRF_SUCCESS;
|
||||
|
||||
case osErrorISR:
|
||||
break;
|
||||
|
||||
case osErrorParameter:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
|
||||
default:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// Start timer without svcCall
|
||||
switch (svcTimerStart((osTimerId)p_timer_info->id, timeout_ms))
|
||||
{
|
||||
case osOK:
|
||||
return NRF_SUCCESS;
|
||||
|
||||
case osErrorISR:
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
|
||||
case osErrorParameter:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
|
||||
default:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
}
|
||||
|
||||
ret_code_t app_timer_stop(app_timer_id_t timer_id)
|
||||
{
|
||||
app_timer_info_t * p_timer_info = (app_timer_info_t *)timer_id;
|
||||
switch (osTimerStop((osTimerId)p_timer_info->id) )
|
||||
{
|
||||
case osOK:
|
||||
return NRF_SUCCESS;
|
||||
|
||||
case osErrorISR:
|
||||
break;
|
||||
|
||||
case osErrorParameter:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
|
||||
case osErrorResource:
|
||||
return NRF_SUCCESS;
|
||||
|
||||
default:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// Stop timer without svcCall
|
||||
switch (svcTimerStop((osTimerId)p_timer_info->id))
|
||||
{
|
||||
case osOK:
|
||||
return NRF_SUCCESS;
|
||||
|
||||
case osErrorISR:
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
|
||||
case osErrorParameter:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
|
||||
case osErrorResource:
|
||||
return NRF_SUCCESS;
|
||||
|
||||
default:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ret_code_t app_timer_stop_all(void)
|
||||
{
|
||||
for (int i = 0; i < app_timer_control.max_timers; i++)
|
||||
{
|
||||
if (app_timer_control.app_timers[i].id)
|
||||
{
|
||||
(void)app_timer_stop((app_timer_id_t)app_timer_control.app_timers[i].id);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
extern uint32_t os_tick_val(void);
|
||||
uint32_t app_timer_cnt_get(void)
|
||||
{
|
||||
return os_tick_val();
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
|
||||
uint32_t ticks_from)
|
||||
{
|
||||
return ((ticks_to - ticks_from) & MAX_RTC_COUNTER_VAL);
|
||||
}
|
||||
#endif //NRF_MODULE_ENABLED(APP_TIMER)
|
||||
363
components/libraries/timer/drv_rtc.c
Normal file
363
components/libraries/timer/drv_rtc.c
Normal file
@@ -0,0 +1,363 @@
|
||||
/**
|
||||
* Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 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 the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 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 written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <nrfx.h>
|
||||
#include <nrf_delay.h>
|
||||
#include <drv_rtc.h>
|
||||
|
||||
/* Module is integral part of app_timer implementation. */
|
||||
#define NRF_LOG_MODULE_NAME app_timer
|
||||
#include <nrf_log.h>
|
||||
|
||||
#define EVT_TO_STR(event) \
|
||||
(event == NRF_RTC_EVENT_TICK ? "NRF_RTC_EVENT_TICK" : \
|
||||
(event == NRF_RTC_EVENT_OVERFLOW ? "NRF_RTC_EVENT_OVERFLOW" : \
|
||||
(event == NRF_RTC_EVENT_COMPARE_0 ? "NRF_RTC_EVENT_COMPARE_0" : \
|
||||
(event == NRF_RTC_EVENT_COMPARE_1 ? "NRF_RTC_EVENT_COMPARE_1" : \
|
||||
(event == NRF_RTC_EVENT_COMPARE_2 ? "NRF_RTC_EVENT_COMPARE_2" : \
|
||||
(event == NRF_RTC_EVENT_COMPARE_3 ? "NRF_RTC_EVENT_COMPARE_3" : \
|
||||
"UNKNOWN EVENT"))))))
|
||||
#if defined ( __ICCARM__ )
|
||||
/* IAR gives warning for offsetof with non-constant expression.*/
|
||||
#define CC_IDX_TO_CC_EVENT(_cc) \
|
||||
((nrf_rtc_event_t)(offsetof(NRF_RTC_Type, EVENTS_COMPARE[0]) + sizeof(uint32_t)*_cc))
|
||||
#else
|
||||
#define CC_IDX_TO_CC_EVENT(_cc) \
|
||||
((nrf_rtc_event_t)(offsetof(NRF_RTC_Type, EVENTS_COMPARE[_cc])))
|
||||
#endif
|
||||
|
||||
/**@brief RTC driver instance control block structure. */
|
||||
typedef struct
|
||||
{
|
||||
drv_rtc_t const * p_instance;
|
||||
nrfx_drv_state_t state; /**< Instance state. */
|
||||
} drv_rtc_cb_t;
|
||||
|
||||
// User callbacks local storage.
|
||||
static drv_rtc_handler_t m_handlers[DRV_RTC_ENABLED_COUNT];
|
||||
static drv_rtc_cb_t m_cb[DRV_RTC_ENABLED_COUNT];
|
||||
|
||||
// According to Produce Specification RTC may not trigger COMPARE event if CC value set is equal to
|
||||
// COUNTER value or COUNTER+1.
|
||||
#define COUNTER_TO_CC_MIN_DISTANCE 2
|
||||
|
||||
ret_code_t drv_rtc_init(drv_rtc_t const * const p_instance,
|
||||
drv_rtc_config_t const * p_config,
|
||||
drv_rtc_handler_t handler)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
ASSERT(p_config);
|
||||
ASSERT(handler);
|
||||
|
||||
ret_code_t err_code;
|
||||
|
||||
m_handlers[p_instance->instance_id] = handler;
|
||||
|
||||
if (m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED)
|
||||
{
|
||||
err_code = NRF_ERROR_INVALID_STATE;
|
||||
NRF_LOG_WARNING("RTC instance already initialized.");
|
||||
return err_code;
|
||||
}
|
||||
|
||||
nrf_rtc_prescaler_set(p_instance->p_reg, p_config->prescaler);
|
||||
NRFX_IRQ_PRIORITY_SET(p_instance->irq, p_config->interrupt_priority);
|
||||
NRFX_IRQ_ENABLE(p_instance->irq);
|
||||
|
||||
m_cb[p_instance->instance_id].state = NRFX_DRV_STATE_INITIALIZED;
|
||||
m_cb[p_instance->instance_id].p_instance = p_instance;
|
||||
|
||||
err_code = NRF_SUCCESS;
|
||||
NRF_LOG_INFO("RTC: initialized.");
|
||||
return err_code;
|
||||
}
|
||||
|
||||
void drv_rtc_uninit(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
uint32_t mask = NRF_RTC_INT_TICK_MASK |
|
||||
NRF_RTC_INT_OVERFLOW_MASK |
|
||||
NRF_RTC_INT_COMPARE0_MASK |
|
||||
NRF_RTC_INT_COMPARE1_MASK |
|
||||
NRF_RTC_INT_COMPARE2_MASK |
|
||||
NRF_RTC_INT_COMPARE3_MASK;
|
||||
ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
|
||||
|
||||
NRFX_IRQ_DISABLE(p_instance->irq);
|
||||
|
||||
drv_rtc_stop(p_instance);
|
||||
nrf_rtc_event_disable(p_instance->p_reg, mask);
|
||||
nrf_rtc_int_disable(p_instance->p_reg, mask);
|
||||
|
||||
m_cb[p_instance->instance_id].state = NRFX_DRV_STATE_UNINITIALIZED;
|
||||
NRF_LOG_INFO("RTC: Uninitialized.");
|
||||
}
|
||||
|
||||
void drv_rtc_start(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
nrf_rtc_task_trigger(p_instance->p_reg, NRF_RTC_TASK_START);
|
||||
}
|
||||
|
||||
void drv_rtc_stop(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
nrf_rtc_task_trigger(p_instance->p_reg, NRF_RTC_TASK_STOP);
|
||||
}
|
||||
|
||||
void drv_rtc_compare_set(drv_rtc_t const * const p_instance,
|
||||
uint32_t cc,
|
||||
uint32_t abs_value,
|
||||
bool irq_enable)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
nrf_rtc_int_t cc_int_mask = (nrf_rtc_int_t)(NRF_RTC_INT_COMPARE0_MASK << cc);
|
||||
nrf_rtc_event_t cc_evt = CC_IDX_TO_CC_EVENT(cc);
|
||||
abs_value &= RTC_COUNTER_COUNTER_Msk;
|
||||
|
||||
nrf_rtc_int_disable(p_instance->p_reg, cc_int_mask);
|
||||
nrf_rtc_event_disable(p_instance->p_reg, cc_int_mask);
|
||||
nrf_rtc_event_clear(p_instance->p_reg, cc_evt);
|
||||
nrf_rtc_cc_set(p_instance->p_reg, cc,abs_value);
|
||||
nrf_rtc_event_enable(p_instance->p_reg, cc_int_mask);
|
||||
|
||||
if (irq_enable)
|
||||
{
|
||||
nrf_rtc_int_enable(p_instance->p_reg, cc_int_mask);
|
||||
}
|
||||
}
|
||||
|
||||
static void evt_enable(drv_rtc_t const * const p_instance, uint32_t mask, bool irq_enable)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
nrf_rtc_event_enable(p_instance->p_reg, mask);
|
||||
if (irq_enable)
|
||||
{
|
||||
nrf_rtc_int_enable(p_instance->p_reg, mask);
|
||||
}
|
||||
}
|
||||
|
||||
static void evt_disable(drv_rtc_t const * const p_instance, uint32_t mask)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
nrf_rtc_event_disable(p_instance->p_reg, mask);
|
||||
nrf_rtc_int_disable(p_instance->p_reg, mask);
|
||||
}
|
||||
|
||||
static bool evt_pending(drv_rtc_t const * const p_instance, nrf_rtc_event_t event)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
if (nrf_rtc_event_pending(p_instance->p_reg, event))
|
||||
{
|
||||
nrf_rtc_event_clear(p_instance->p_reg, event);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static uint32_t ticks_sub(uint32_t a, uint32_t b)
|
||||
{
|
||||
return (a - b) & RTC_COUNTER_COUNTER_Msk;
|
||||
}
|
||||
|
||||
ret_code_t drv_rtc_windowed_compare_set(drv_rtc_t const * const p_instance,
|
||||
uint32_t cc,
|
||||
uint32_t abs_value,
|
||||
uint32_t safe_window)
|
||||
{
|
||||
ASSERT(p_instance);
|
||||
uint32_t prev_cc_set;
|
||||
uint32_t now;
|
||||
uint32_t diff;
|
||||
nrf_rtc_int_t cc_int_mask = (nrf_rtc_int_t)(NRF_RTC_INT_COMPARE0_MASK << cc);
|
||||
nrf_rtc_event_t cc_evt = CC_IDX_TO_CC_EVENT(cc);;
|
||||
abs_value &=RTC_COUNTER_COUNTER_Msk;
|
||||
|
||||
evt_disable(p_instance, cc_int_mask);
|
||||
|
||||
/* First handle potential prefiring caused by CC being set to next tick. Even if CC is
|
||||
* overwritten it may happen that event will be generated for previous CC in next tick.
|
||||
* Following algorith is applied:
|
||||
* - read previous CC
|
||||
* - write current counter value to CC (furtherest in future)
|
||||
* - if previous CC was in one tick from now wait half of the 32k tick and clear event which
|
||||
* may be set. Half tick delay is used because CC is latched in the middle of the 32k tick.
|
||||
*/
|
||||
now = nrf_rtc_counter_get(p_instance->p_reg);
|
||||
prev_cc_set = nrf_rtc_cc_get(p_instance->p_reg, cc);
|
||||
nrf_rtc_cc_set(p_instance->p_reg, cc, now);
|
||||
nrf_rtc_event_clear(p_instance->p_reg, cc_evt);
|
||||
|
||||
if (ticks_sub(prev_cc_set, now) == 1)
|
||||
{
|
||||
nrf_delay_us(16);
|
||||
nrf_rtc_event_clear(p_instance->p_reg, cc_evt);
|
||||
}
|
||||
|
||||
now = nrf_rtc_counter_get(p_instance->p_reg);
|
||||
diff = ticks_sub(abs_value, now);
|
||||
|
||||
nrf_rtc_event_enable(p_instance->p_reg, cc_int_mask);
|
||||
|
||||
/* Setting CC for +1 from now may not generate event. In that case set CC+2 and check if counter
|
||||
* changed during that process. If changed it means that 1 tick expired. */
|
||||
if (diff == 1)
|
||||
{
|
||||
nrf_rtc_cc_set(p_instance->p_reg, cc, abs_value + 1);
|
||||
nrf_delay_us(16);
|
||||
if (now != nrf_rtc_counter_get(p_instance->p_reg))
|
||||
{
|
||||
/* one tick elapsed already. */
|
||||
return NRF_ERROR_TIMEOUT;
|
||||
}
|
||||
} else {
|
||||
nrf_rtc_cc_set(p_instance->p_reg, cc, abs_value);
|
||||
now = nrf_rtc_counter_get(p_instance->p_reg);
|
||||
diff = ticks_sub(abs_value - 1, now);
|
||||
/* Check if counter equals cc value or is behind in the safe window. If yes it means that
|
||||
* CC expired. */
|
||||
if (diff > (RTC_COUNTER_COUNTER_Msk - safe_window))
|
||||
{
|
||||
return NRF_ERROR_TIMEOUT;
|
||||
}
|
||||
else if (diff == 0)
|
||||
{
|
||||
/* If cc value == counter + 1, it may hit +1 case. */
|
||||
nrf_rtc_cc_set(p_instance->p_reg, cc, abs_value + 1);
|
||||
if (now != nrf_rtc_counter_get(p_instance->p_reg))
|
||||
{
|
||||
/* one tick elapsed already. */
|
||||
return NRF_ERROR_TIMEOUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
evt_enable(p_instance, cc_int_mask, true);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
void drv_rtc_overflow_enable(drv_rtc_t const * const p_instance, bool irq_enable)
|
||||
{
|
||||
evt_enable(p_instance, NRF_RTC_INT_OVERFLOW_MASK, irq_enable);
|
||||
}
|
||||
|
||||
void drv_rtc_overflow_disable(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
evt_disable(p_instance, NRF_RTC_INT_OVERFLOW_MASK);
|
||||
}
|
||||
|
||||
bool drv_rtc_overflow_pending(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
return evt_pending(p_instance, NRF_RTC_EVENT_OVERFLOW);
|
||||
}
|
||||
|
||||
void drv_rtc_tick_enable(drv_rtc_t const * const p_instance, bool irq_enable)
|
||||
{
|
||||
evt_enable(p_instance, NRF_RTC_INT_TICK_MASK, irq_enable);
|
||||
}
|
||||
|
||||
void drv_rtc_tick_disable(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
evt_disable(p_instance, NRF_RTC_INT_TICK_MASK);
|
||||
}
|
||||
|
||||
bool drv_rtc_tick_pending(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
return evt_pending(p_instance, NRF_RTC_EVENT_TICK);
|
||||
}
|
||||
|
||||
void drv_rtc_compare_enable(drv_rtc_t const * const p_instance,
|
||||
uint32_t cc,
|
||||
bool irq_enable)
|
||||
{
|
||||
evt_enable(p_instance, (uint32_t)NRF_RTC_INT_COMPARE0_MASK << cc, irq_enable);
|
||||
}
|
||||
|
||||
void drv_rtc_compare_disable(drv_rtc_t const * const p_instance, uint32_t cc)
|
||||
{
|
||||
evt_disable(p_instance, (uint32_t)NRF_RTC_INT_COMPARE0_MASK << cc);
|
||||
}
|
||||
|
||||
bool drv_rtc_compare_pending(drv_rtc_t const * const p_instance, uint32_t cc)
|
||||
{
|
||||
nrf_rtc_event_t cc_evt = CC_IDX_TO_CC_EVENT(cc);
|
||||
return evt_pending(p_instance, cc_evt);
|
||||
}
|
||||
|
||||
uint32_t drv_rtc_compare_get(drv_rtc_t const * const p_instance, uint32_t cc)
|
||||
{
|
||||
return nrf_rtc_cc_get(p_instance->p_reg, cc);
|
||||
}
|
||||
|
||||
uint32_t drv_rtc_counter_get(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
return nrf_rtc_counter_get(p_instance->p_reg);
|
||||
}
|
||||
|
||||
void drv_rtc_irq_trigger(drv_rtc_t const * const p_instance)
|
||||
{
|
||||
NVIC_SetPendingIRQ(p_instance->irq);
|
||||
}
|
||||
|
||||
#define drv_rtc_rtc_0_irq_handler RTC0_IRQHandler
|
||||
#define drv_rtc_rtc_1_irq_handler RTC1_IRQHandler
|
||||
#define drv_rtc_rtc_2_irq_handler RTC2_IRQHandler
|
||||
|
||||
#if defined(APP_TIMER_V2_RTC0_ENABLED)
|
||||
void drv_rtc_rtc_0_irq_handler(void)
|
||||
{
|
||||
m_handlers[DRV_RTC_RTC0_INST_IDX](m_cb[DRV_RTC_RTC0_INST_IDX].p_instance);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(APP_TIMER_V2_RTC1_ENABLED)
|
||||
void drv_rtc_rtc_1_irq_handler(void)
|
||||
{
|
||||
m_handlers[DRV_RTC_RTC1_INST_IDX](m_cb[DRV_RTC_RTC1_INST_IDX].p_instance);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(APP_TIMER_V2_RTC2_ENABLED)
|
||||
void drv_rtc_rtc_2_irq_handler(void)
|
||||
{
|
||||
m_handlers[DRV_RTC_RTC2_INST_IDX](m_cb[DRV_RTC_RTC2_INST_IDX].p_instance);
|
||||
}
|
||||
#endif
|
||||
315
components/libraries/timer/drv_rtc.h
Normal file
315
components/libraries/timer/drv_rtc.h
Normal file
@@ -0,0 +1,315 @@
|
||||
/**
|
||||
* Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 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 the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 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 written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DRV_RTC_H__
|
||||
#define DRV_RTC_H__
|
||||
|
||||
#include <nrfx.h>
|
||||
#include <hal/nrf_rtc.h>
|
||||
#include "sdk_errors.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @defgroup drv_rtc RTC driver
|
||||
* @{
|
||||
* @ingroup app_timer
|
||||
* @brief Real Timer Counter (RTC) peripheral driver for app_timer.
|
||||
*/
|
||||
|
||||
/** @brief Maximum RTC counter value. */
|
||||
#define DRV_RTC_MAX_CNT RTC_COUNTER_COUNTER_Msk
|
||||
|
||||
/** @brief Time requires to update registers between RTC and MCU domains. */
|
||||
#define DRV_RTC_CONFIG_APPLY_TIME_US 33
|
||||
|
||||
/**
|
||||
* @brief Minimum delta value between set value and counter value.
|
||||
*
|
||||
* RTC peripheral requires two ticks to be sure that value it properly set in RTC value. Compare
|
||||
* channel function requires additional one tick to avoid problematic situations (lack or additional
|
||||
* unspecified event) when Compare Channel register is reseting or setting to N+2 value.
|
||||
*/
|
||||
#define DRV_RTC_MIN_TICK_HANDLED 3
|
||||
|
||||
/** @brief Macro to convert microseconds into ticks. */
|
||||
#define DRV_RTC_US_TO_TICKS(us,freq) (us >= 2^17 ? \
|
||||
((((us)/1000)*(freq))/1000U) : (((us)*(freq))/1000000U) )
|
||||
|
||||
|
||||
/** @brief RTC driver instance structure. */
|
||||
typedef struct
|
||||
{
|
||||
NRF_RTC_Type * p_reg; /**< Pointer to instance register set. */
|
||||
IRQn_Type irq; /**< Instance IRQ ID. */
|
||||
uint8_t instance_id; /**< Instance index. */
|
||||
uint8_t cc_channel_count; /**< Number of capture/compare channels. */
|
||||
} drv_rtc_t;
|
||||
|
||||
/** @brief Macro for creating RTC driver instance.*/
|
||||
#define DRV_RTC_INSTANCE(id) \
|
||||
{ \
|
||||
.p_reg = NRFX_CONCAT_2(NRF_RTC, id), \
|
||||
.irq = NRFX_CONCAT_3(RTC, id, _IRQn), \
|
||||
.instance_id = NRFX_CONCAT_3(DRV_RTC_RTC, id, _INST_IDX), \
|
||||
.cc_channel_count = NRF_RTC_CC_CHANNEL_COUNT(id), \
|
||||
}
|
||||
|
||||
enum {
|
||||
#if defined(APP_TIMER_V2_RTC0_ENABLED)
|
||||
DRV_RTC_RTC0_INST_IDX,
|
||||
#endif
|
||||
#if defined(APP_TIMER_V2_RTC1_ENABLED)
|
||||
DRV_RTC_RTC1_INST_IDX,
|
||||
#endif
|
||||
#if defined(APP_TIMER_V2_RTC2_ENABLED)
|
||||
DRV_RTC_RTC2_INST_IDX,
|
||||
#endif
|
||||
DRV_RTC_ENABLED_COUNT
|
||||
};
|
||||
|
||||
/** @brief RTC driver instance configuration structure. */
|
||||
typedef struct
|
||||
{
|
||||
uint16_t prescaler; /**< Prescaler. */
|
||||
uint8_t interrupt_priority; /**< Interrupt priority. */
|
||||
} drv_rtc_config_t;
|
||||
|
||||
/** @brief RTC instance default configuration. */
|
||||
#define DRV_RTC_DEFAULT_CONFIG \
|
||||
{ \
|
||||
.prescaler = RTC_FREQ_TO_PRESCALER(DRV_RTC_DEFAULT_CONFIG_FREQUENCY), \
|
||||
.interrupt_priority = DRV_RTC_DEFAULT_CONFIG_IRQ_PRIORITY, \
|
||||
}
|
||||
|
||||
/** @brief RTC driver instance handler type. */
|
||||
typedef void (*drv_rtc_handler_t)(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for initializing the RTC driver instance.
|
||||
*
|
||||
* After initialization, the instance is in power off state. The LFCLK (@ref nrfx_clock)
|
||||
* has to be started before using @ref drv_rtc.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] p_config Pointer to the structure with initial configuration.
|
||||
* @param[in] handler Event handler provided by the user. Must not be NULL.
|
||||
*
|
||||
* @retval NRF_SUCCESS If successfully initialized.
|
||||
* @retval NRF_ERROR_INVALID_STATE If the instance is already initialized.
|
||||
*/
|
||||
ret_code_t drv_rtc_init(drv_rtc_t const * const p_instance,
|
||||
drv_rtc_config_t const * p_config,
|
||||
drv_rtc_handler_t handler);
|
||||
|
||||
/**
|
||||
* @brief Function for uninitializing the RTC driver instance.
|
||||
*
|
||||
* After uninitialization, the instance is in idle state. The hardware should return to the state
|
||||
* before initialization. The function asserts if the instance is in idle state.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*/
|
||||
void drv_rtc_uninit(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for starting RTC clock.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*/
|
||||
void drv_rtc_start(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for stopping RTC clock.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*/
|
||||
void drv_rtc_stop(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for configuring compare channel.
|
||||
*
|
||||
* @note Function disables interrupts and only enable compare events. Remember to enable interrupt
|
||||
* using @ref drv_rtc_compare_enable in case of using it.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] cc Compare channel index.
|
||||
* @param[in] abs_value Absolute value to be set in the compare register.
|
||||
* @param[in] irq_enable True to enable interrupt.
|
||||
*/
|
||||
void drv_rtc_compare_set(drv_rtc_t const * const p_instance,
|
||||
uint32_t cc,
|
||||
uint32_t abs_value,
|
||||
bool irq_enable);
|
||||
|
||||
/**
|
||||
* @brief Function for configuring compare channel with safe window.
|
||||
*
|
||||
* Maximum possible relative value is limited by safe window to detect
|
||||
* cases when requested compare event has already occured.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] cc Compare channel index.
|
||||
* @param[in] abs_value Absolute value to be set in the compare register.
|
||||
* @param[in] safe_window Width of the safe window.
|
||||
*
|
||||
* @retval NRF_ERROR_TIMEOUT If @par abs_value is in safe window of event occured before
|
||||
* enabling compare channel intterupt.
|
||||
* @retval NRF_SUCCESS If successfully set.
|
||||
*/
|
||||
ret_code_t drv_rtc_windowed_compare_set(drv_rtc_t const * const p_instance,
|
||||
uint32_t cc,
|
||||
uint32_t abs_value,
|
||||
uint32_t safe_window);
|
||||
|
||||
/**
|
||||
* @brief Function for enabling overflow event and interrupt.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] irq_enable True to enable interrupt.
|
||||
*/
|
||||
void drv_rtc_overflow_enable(drv_rtc_t const * const p_instance, bool irq_enable);
|
||||
|
||||
/**
|
||||
* @brief Function for diabling overflow event and interrupt.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*/
|
||||
void drv_rtc_overflow_disable(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for checking if overflow event has occured.
|
||||
*
|
||||
* @note Event is cleared after reading.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*
|
||||
* @return True if interrupt pending, false otherwise.
|
||||
*/
|
||||
bool drv_rtc_overflow_pending(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for enabling tick event and interrupt.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] irq_enable True to enable interrupt.
|
||||
*/
|
||||
void drv_rtc_tick_enable(drv_rtc_t const * const p_instance, bool irq_enable);
|
||||
|
||||
/**
|
||||
* @brief Function for disabling tick event and interrupt.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*/
|
||||
void drv_rtc_tick_disable(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for checking if tick event has occured.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*
|
||||
* @return True if interrupt pending, false otherwise.
|
||||
*/
|
||||
bool drv_rtc_tick_pending(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for enabling compare channel event and interrupt.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] cc Compare channel index.
|
||||
* @param[in] irq_enable True to enable interrupt.
|
||||
*/
|
||||
void drv_rtc_compare_enable(drv_rtc_t const * const p_instance,
|
||||
uint32_t cc,
|
||||
bool irq_enable);
|
||||
|
||||
/**
|
||||
* @brief Function for disabling compare channel event and interrupt.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] cc Compare channel index.
|
||||
*/
|
||||
void drv_rtc_compare_disable(drv_rtc_t const * const p_instance, uint32_t cc);
|
||||
|
||||
/**
|
||||
* @brief Function for checking if compare channel event has occured.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] cc Compare channel index.
|
||||
*
|
||||
* @return True if interrupt pending, false otherwise.
|
||||
*/
|
||||
bool drv_rtc_compare_pending(drv_rtc_t const * const p_instance, uint32_t cc);
|
||||
|
||||
/**
|
||||
* @brief Function for reading compare value.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
* @param[in] cc Compare channel index.
|
||||
*
|
||||
* @return Compare value set for given channel.
|
||||
*/
|
||||
uint32_t drv_rtc_compare_get(drv_rtc_t const * const p_instance, uint32_t cc);
|
||||
|
||||
/**
|
||||
* @brief Function for getting current value of RTC counter.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*
|
||||
* @return Counter value.
|
||||
*/
|
||||
uint32_t drv_rtc_counter_get(drv_rtc_t const * const p_instance);
|
||||
|
||||
/**
|
||||
* @brief Function for triggering RTC interrupt.
|
||||
*
|
||||
* @param[in] p_instance Pointer to the driver instance structure.
|
||||
*/
|
||||
void drv_rtc_irq_trigger(drv_rtc_t const * const p_instance);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DRV_RTC_H__
|
||||
Reference in New Issue
Block a user