move components to SDK dir

This commit is contained in:
Shuanglei Tao
2025-03-03 09:06:26 +08:00
parent 20d1297e57
commit f4f4c9e60d
1021 changed files with 58 additions and 35059 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,323 @@
/**
* Copyright (c) 2012 - 2017, 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 <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#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_USER_OP_SIZE 24 /**< Size of app_timer.timer_user_op_t (only for use inside APP_TIMER_BUF_SIZE()). */
/**@brief Compute number of bytes required to hold the application timer data structures.
*
* @param[in] OP_QUEUE_SIZE Size of the queue holding timer operations that are pending execution.
* Note that due to the queue implementation, this size must be one more
* than the size that is actually needed.
*
* @return Required application timer buffer size (in bytes).
*/
#define APP_TIMER_BUF_SIZE(OP_QUEUE_SIZE) \
( \
( \
(((OP_QUEUE_SIZE) + 1) * APP_TIMER_USER_OP_SIZE) \
) \
)
/**@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.
*
* When using this macro, ensure that the
* values provided as input result in an output value that is supported by the
* @ref app_timer_start function. For example, when the ticks for 1 ms is needed, the
* maximum possible value of PRESCALER must be 6, when @ref APP_TIMER_CLOCK_FREQ is 32768.
* This will result in a ticks value as 5. Any higher value for PRESCALER will result in a
* ticks value that is not supported by this module.
*
* @param[in] MS Milliseconds.
* @param[in] PRESCALER Value of the RTC1 PRESCALER register (must be the same value that was
* passed to APP_TIMER_INIT()).
*
* @return Number of timer ticks.
*/
#define APP_TIMER_TICKS(MS, PRESCALER)\
((uint32_t)ROUNDED_DIV((MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, ((PRESCALER) + 1) * 1000))
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;
/**
* @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) \
static app_timer_t timer_id##_data = { {0} }; \
static const app_timer_id_t timer_id = &timer_id##_data
/**@brief Application time-out handler type. */
typedef void (*app_timer_timeout_handler_t)(void * p_context);
/**@brief Type of function for passing events from the timer module to the scheduler. */
typedef uint32_t (*app_timer_evt_schedule_func_t) (app_timer_timeout_handler_t timeout_handler,
void * p_context);
/**@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 Initialize the application timer module.
*
* @details This macro handles dimensioning and allocation of the memory buffer required by the timer,
* making sure that the buffer is correctly aligned. It will also connect the timer module
* to the scheduler (if specified).
*
* @note This module assumes that the LFCLK is already running. If it is not, the module will
* be non-functional, since the RTC will not run. If you do not use a SoftDevice, you
* must start the LFCLK manually. See the rtc_example's lfclk_config() function
* for an example of how to do this. If you use a SoftDevice, the LFCLK is started on
* SoftDevice init.
*
*
* @param[in] PRESCALER Value of the RTC1 PRESCALER register. This will decide the
* timer tick rate. Set to 0 for no prescaling.
* @param[in] OP_QUEUE_SIZE Size of the queue holding timer operations that are pending execution.
* @param[in] SCHEDULER_FUNC Pointer to scheduler event handler
*
* @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
* several times as long as it is from the same location, for example, to do a re-initialization).
*/
/*lint -emacro(506, APP_TIMER_INIT) */ /* Suppress "Constant value Boolean */
#define APP_TIMER_INIT(PRESCALER, OP_QUEUE_SIZE, SCHEDULER_FUNC) \
do \
{ \
static uint32_t APP_TIMER_BUF[CEIL_DIV(APP_TIMER_BUF_SIZE(OP_QUEUE_SIZE), \
sizeof(uint32_t))]; \
uint32_t ERR_CODE = app_timer_init((PRESCALER), \
(OP_QUEUE_SIZE) + 1, \
APP_TIMER_BUF, \
SCHEDULER_FUNC); \
APP_ERROR_CHECK(ERR_CODE); \
} while (0)
/**@brief Function for initializing the timer module.
*
* Normally, initialization should be done using the APP_TIMER_INIT() macro, because that macro will both
* allocate the buffers needed by the timer module (including aligning the buffers correctly)
* and take care of connecting the timer module to the scheduler (if specified).
*
* @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling.
* @param[in] op_queue_size Size of the queue holding timer operations that are pending
* execution. Note that due to the queue implementation, this size must
* be one more than the size that is actually needed.
* @param[in] p_buffer Pointer to memory buffer for internal use in the app_timer
* module. The size of the buffer can be computed using the
* APP_TIMER_BUF_SIZE() macro. The buffer must be aligned to a
* 4 byte boundary.
* @param[in] evt_schedule_func Function for passing time-out events to the scheduler. Point to
* app_timer_evt_schedule() to connect to the scheduler. Set to NULL
* to make the timer module call the time-out handler directly from
* the timer interrupt handler.
*
* @retval NRF_SUCCESS If the module was initialized successfully.
* @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid (buffer not aligned to a 4 byte
* boundary or NULL).
*/
uint32_t app_timer_init(uint32_t prescaler,
uint8_t op_queue_size,
void * p_buffer,
app_timer_evt_schedule_func_t evt_schedule_func);
/**@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.
*/
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);
/**@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.
*/
uint32_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.
*/
uint32_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.
*/
uint32_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().
* @param[out] p_ticks_diff Number of ticks from ticks_from to ticks_to.
*
* @retval NRF_SUCCESS If the counter difference was successfully computed.
*/
uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
uint32_t ticks_from,
uint32_t * p_ticks_diff);
/**@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);
#ifdef __cplusplus
}
#endif
#endif // APP_TIMER_H__
/** @} */

View File

@@ -0,0 +1,62 @@
/**
* Copyright (c) 2014 - 2017, 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_appsh.h"
#include "app_scheduler.h"
static void app_timer_evt_get(void * p_event_data, uint16_t event_size)
{
app_timer_event_t * p_timer_event = (app_timer_event_t *)p_event_data;
APP_ERROR_CHECK_BOOL(event_size == sizeof(app_timer_event_t));
p_timer_event->timeout_handler(p_timer_event->p_context);
}
uint32_t app_timer_evt_schedule(app_timer_timeout_handler_t timeout_handler,
void * p_context)
{
app_timer_event_t timer_event;
timer_event.timeout_handler = timeout_handler;
timer_event.p_context = p_context;
return app_sched_event_put(&timer_event, sizeof(timer_event), app_timer_evt_get);
}

View File

@@ -0,0 +1,81 @@
/**
* Copyright (c) 2014 - 2017, 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 APP_TIMER_APPSH_H
#define APP_TIMER_APPSH_H
#include "app_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
#define APP_TIMER_SCHED_EVT_SIZE sizeof(app_timer_event_t) /**< Size of button events being passed through the scheduler (is to be used for computing the maximum size of scheduler events). */
/**@brief Macro for initializing the application timer module to use with app_scheduler.
*
* @param[in] PRESCALER Value of the RTC1 PRESCALER register. This will decide the
* timer tick rate. Set to 0 for no prescaling.
* @param[in] OP_QUEUES_SIZE Size of queues holding timer operations that are pending execution.
* @param[in] USE_SCHEDULER TRUE if the application is using the app_scheduler,
* FALSE otherwise.
*
* @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
* several times as long as it is from the same location, e.g. to do a reinitialization).
*/
#define APP_TIMER_APPSH_INIT(PRESCALER, OP_QUEUES_SIZE, USE_SCHEDULER) \
APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, \
(USE_SCHEDULER) ? app_timer_evt_schedule : NULL)
typedef struct
{
app_timer_timeout_handler_t timeout_handler;
void * p_context;
} app_timer_event_t;
uint32_t app_timer_evt_schedule(app_timer_timeout_handler_t timeout_handler,
void * p_context);
#ifdef __cplusplus
}
#endif
#endif // APP_TIMER_APPSH_H

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,258 @@
/**
* Copyright (c) 2014 - 2017, 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"
/* 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;
}app_timer_info_t;
/**
* @brief Prescaler that was set by the user
*
* In FreeRTOS version of app_timer the prescaler setting is constant and done by the operating system.
* But the application expect the prescaler to be set according to value given in setup and then
* calculate required ticks using this value.
* For compatibility we remember the value set and use it for recalculation of required timer setting.
*/
static uint32_t m_prescaler;
/* 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->func(pinfo->argument);
}
uint32_t app_timer_init(uint32_t prescaler,
uint8_t op_queues_size,
void * p_buffer,
app_timer_evt_schedule_func_t evt_schedule_func)
{
UNUSED_PARAMETER(op_queues_size);
UNUSED_PARAMETER(p_buffer);
UNUSED_PARAMETER(evt_schedule_func);
m_prescaler = prescaler + 1;
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));
if (mode == APP_TIMER_MODE_SINGLE_SHOT)
timer_mode = pdFALSE;
else
timer_mode = pdTRUE;
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;
uint32_t rtc_prescaler = portNRF_RTC_REG->PRESCALER + 1;
/* Get back the microseconds to wait */
uint32_t timeout_corrected = ROUNDED_DIV(timeout_ticks * m_prescaler, rtc_prescaler);
if (hTimer == NULL)
{
return NRF_ERROR_INVALID_STATE;
}
if (pinfo->active && (xTimerIsTimerActive(hTimer) != pdFALSE))
{
// Timer already running - exit silently
return NRF_SUCCESS;
}
pinfo->argument = p_context;
if (__get_IPSR() != 0)
{
BaseType_t yieldReq = pdFALSE;
if (xTimerChangePeriodFromISR(hTimer, timeout_corrected, &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_corrected, 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(timer_id, &yieldReq) != pdPASS)
{
return NRF_ERROR_NO_MEM;
}
portYIELD_FROM_ISR(yieldReq);
}
else
{
if (xTimerStop(timer_id, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
{
return NRF_ERROR_NO_MEM;
}
}
pinfo->active = false;
return NRF_SUCCESS;
}
#endif //NRF_MODULE_ENABLED(APP_TIMER)

View File

@@ -0,0 +1,284 @@
/**
* Copyright (c) 2016 - 2017, 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;
}
uint32_t app_timer_init(uint32_t prescaler,
uint8_t op_queues_size,
void * p_buffer,
app_timer_evt_schedule_func_t evt_schedule_func)
{
if (p_buffer == NULL)
{
return NRF_ERROR_INVALID_PARAM;
}
app_timer_control.prescaler = prescaler;
app_timer_control.app_timers = p_buffer;
NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI);
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)
{
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
uint32_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_control.prescaler + 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;
}
}
uint32_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;
}
}
uint32_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,
uint32_t * p_ticks_diff)
{
*p_ticks_diff = ((ticks_to - ticks_from) & MAX_RTC_COUNTER_VAL);
return NRF_SUCCESS;
}
#endif //NRF_MODULE_ENABLED(APP_TIMER)