http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_ble_gzll.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_ble_gzll.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_ble_gzll.c deleted file mode 100644 index 08a2066..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_ble_gzll.c +++ /dev/null @@ -1,1119 +0,0 @@ -/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -#include "app_timer.h" -#include <stdlib.h> -#include "nrf.h" -#include "nrf_soc.h" -#include "nrf_delay.h" -#include "app_util_platform.h" -#include "sdk_common.h" - -#define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the RTC1 interrupt (used for checking for timeouts and executing timeout handlers). */ -#define SWI_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the SWI interrupt (used for updating the timer list). */ - -// The current design assumes that both interrupt handlers run at the same interrupt level. -// If this is to be changed, protection must be added to prevent them from interrupting each other -// (e.g. by using guard/trigger flags). -STATIC_ASSERT(RTC1_IRQ_PRI == SWI_IRQ_PRI); - -#define MAX_RTC_COUNTER_VAL 0x00FFFFFF /**< Maximum value of the RTC counter. */ - -#define APP_HIGH_USER_ID 0 /**< User Id for the Application High "user". */ -#define APP_LOW_USER_ID 1 /**< User Id for the Application Low "user". */ -#define THREAD_MODE_USER_ID 2 /**< User Id for the Thread Mode "user". */ - -#define RTC_COMPARE_OFFSET_MIN 3 /**< Minimum offset between the current RTC counter value and the Capture Compare register. Although the nRF51 Series User Specification recommends this value to be 2, we use 3 to be safer.*/ - -#define MAX_RTC_TASKS_DELAY 47 /**< Maximum delay until an RTC task is executed. */ - -#ifdef NRF51 -#define SWI_IRQn SWI1_IRQn -#define SWI_IRQHandler SWI1_IRQHandler -#elif defined NRF52 -#define SWI_IRQn SWI1_EGU1_IRQn -#define SWI_IRQHandler SWI1_EGU1_IRQHandler -#endif - -/**@brief Timer node type. The nodes will be used form a linked list of running timers. */ -typedef struct -{ - uint32_t ticks_to_expire; /**< Number of ticks from previous timer interrupt to timer expiry. */ - uint32_t ticks_at_start; /**< Current RTC counter value when the timer was started. */ - uint32_t ticks_first_interval; /**< Number of ticks in the first timer interval. */ - uint32_t ticks_periodic_interval; /**< Timer period (for repeating timers). */ - bool is_running; /**< True if timer is running, False otherwise. */ - app_timer_mode_t mode; /**< Timer mode. */ - app_timer_timeout_handler_t p_timeout_handler; /**< Pointer to function to be executed when the timer expires. */ - void * p_context; /**< General purpose pointer. Will be passed to the timeout handler when the timer expires. */ - void * next; /**< Pointer to the next node. */ -} timer_node_t; - -STATIC_ASSERT(sizeof(timer_node_t) == APP_TIMER_NODE_SIZE); - -/**@brief Set of available timer operation types. */ -typedef enum -{ - TIMER_USER_OP_TYPE_NONE, /**< Invalid timer operation type. */ - TIMER_USER_OP_TYPE_START, /**< Timer operation type Start. */ - TIMER_USER_OP_TYPE_STOP, /**< Timer operation type Stop. */ - TIMER_USER_OP_TYPE_STOP_ALL /**< Timer operation type Stop All. */ -} timer_user_op_type_t; - -/**@brief Structure describing a timer start operation. */ -typedef struct -{ - uint32_t ticks_at_start; /**< Current RTC counter value when the timer was started. */ - uint32_t ticks_first_interval; /**< Number of ticks in the first timer interval. */ - uint32_t ticks_periodic_interval; /**< Timer period (for repeating timers). */ - void * p_context; /**< General purpose pointer. Will be passed to the timeout handler when the timer expires. */ -} timer_user_op_start_t; - -/**@brief Structure describing a timer operation. */ -typedef struct -{ - timer_user_op_type_t op_type; /**< Id of timer on which the operation is to be performed. */ - timer_node_t * p_node; - union - { - timer_user_op_start_t start; /**< Structure describing a timer start operation. */ - } params; -} timer_user_op_t; - -STATIC_ASSERT(sizeof(timer_user_op_t) <= APP_TIMER_USER_OP_SIZE); -STATIC_ASSERT(sizeof(timer_user_op_t) % 4 == 0); - -/**@brief Structure describing a timer user. - * - * @details For each user of the timer module, there will be a timer operations queue. This queue - * will hold timer operations issued by this user until the timer interrupt handler - * processes these operations. For the current implementation, there will be one user for - * each interrupt level available to the application (APP_HIGH, APP_LOW and THREAD_MODE), - * but the module can easily be modified to e.g. have one queue per process when using an - * RTOS. The purpose of the queues is to be able to have a completely lockless timer - * implementation. - */ -typedef struct -{ - uint8_t first; /**< Index of first entry to have been inserted in the queue (i.e. the next entry to be executed). */ - uint8_t last; /**< Index of last entry to have been inserted in the queue. */ - uint8_t user_op_queue_size; /**< Queue size. */ - timer_user_op_t * p_user_op_queue; /**< Queue buffer. */ -} timer_user_t; - -STATIC_ASSERT(sizeof(timer_user_t) == APP_TIMER_USER_SIZE); -STATIC_ASSERT(sizeof(timer_user_t) % 4 == 0); - -/**@brief User id type. - * - * @details In the current implementation, this will automatically be generated from the current - * interrupt level. - */ -typedef uint32_t timer_user_id_t; - -#define CONTEXT_QUEUE_SIZE_MAX (2) - -static uint8_t m_user_array_size; /**< Size of timer user array. */ -static timer_user_t * mp_users = NULL; /**< Array of timer users. */ -static timer_node_t * mp_timer_id_head; /**< First timer in list of running timers. */ -static uint32_t m_ticks_latest; /**< Last known RTC counter value. */ -static uint32_t m_ticks_elapsed[CONTEXT_QUEUE_SIZE_MAX]; /**< Timer internal elapsed ticks queue. */ -static uint8_t m_ticks_elapsed_q_read_ind; /**< Timer internal elapsed ticks queue read index. */ -static uint8_t m_ticks_elapsed_q_write_ind; /**< Timer internal elapsed ticks queue write index. */ -static app_timer_evt_schedule_func_t m_evt_schedule_func; /**< Pointer to function for propagating timeout events to the scheduler. */ -static bool m_rtc1_running; /**< Boolean indicating if RTC1 is running. */ -static bool m_rtc1_reset; /**< Boolean indicating if RTC1 counter has been reset due to last timer removed from timer list during the timer list handling. */ - - -#define MODULE_INITIALIZED (mp_users != NULL) -#include "sdk_macros.h" - - -/**@brief Function for initializing the RTC1 counter. - * - * @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling. - */ -static void rtc1_init(uint32_t prescaler) -{ - NRF_RTC1->PRESCALER = prescaler; - NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI); -} - - -/**@brief Function for starting the RTC1 timer. - */ -static void rtc1_start(void) -{ - NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk; - NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk; - - NVIC_ClearPendingIRQ(RTC1_IRQn); - NVIC_EnableIRQ(RTC1_IRQn); - - NRF_RTC1->TASKS_START = 1; - nrf_delay_us(MAX_RTC_TASKS_DELAY); - - m_rtc1_running = true; -} - - -/**@brief Function for stopping the RTC1 timer. - */ -static void rtc1_stop(void) -{ - NVIC_DisableIRQ(RTC1_IRQn); - - NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk; - NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk; - - NRF_RTC1->TASKS_STOP = 1; - nrf_delay_us(MAX_RTC_TASKS_DELAY); - - NRF_RTC1->TASKS_CLEAR = 1; - m_ticks_latest = 0; - nrf_delay_us(MAX_RTC_TASKS_DELAY); - - m_rtc1_running = false; -} - - -/**@brief Function for returning the current value of the RTC1 counter. - * - * @return Current value of the RTC1 counter. - */ -static __INLINE uint32_t rtc1_counter_get(void) -{ - return NRF_RTC1->COUNTER; -} - - -/**@brief Function for computing the difference between two RTC1 counter values. - * - * @return Number of ticks elapsed from ticks_old to ticks_now. - */ -static __INLINE uint32_t ticks_diff_get(uint32_t ticks_now, uint32_t ticks_old) -{ - return ((ticks_now - ticks_old) & MAX_RTC_COUNTER_VAL); -} - - -/**@brief Function for setting the RTC1 Capture Compare register 0, and enabling the corresponding - * event. - * - * @param[in] value New value of Capture Compare register 0. - */ -static __INLINE void rtc1_compare0_set(uint32_t value) -{ - NRF_RTC1->CC[0] = value; -} - - -/**@brief Function for inserting a timer in the timer list. - * - * @param[in] timer_id Id of timer to insert. - */ -static void timer_list_insert(timer_node_t * p_timer) -{ - if (mp_timer_id_head == NULL) - { - mp_timer_id_head = p_timer; - } - else - { - if (p_timer->ticks_to_expire <= mp_timer_id_head->ticks_to_expire) - { - mp_timer_id_head->ticks_to_expire -= p_timer->ticks_to_expire; - - p_timer->next = mp_timer_id_head; - mp_timer_id_head = p_timer; - } - else - { - timer_node_t * p_previous; - timer_node_t * p_current; - uint32_t ticks_to_expire; - - ticks_to_expire = p_timer->ticks_to_expire; - p_previous = mp_timer_id_head; - p_current = mp_timer_id_head; - - while ((p_current != NULL) && (ticks_to_expire > p_current->ticks_to_expire)) - { - ticks_to_expire -= p_current->ticks_to_expire; - p_previous = p_current; - p_current = p_current->next; - } - - if (p_current != NULL) - { - p_current->ticks_to_expire -= ticks_to_expire; - } - - p_timer->ticks_to_expire = ticks_to_expire; - p_timer->next = p_current; - p_previous->next = p_timer; - } - } -} - - -/**@brief Function for removing a timer from the timer queue. - * - * @param[in] timer_id Id of timer to remove. - */ -static void timer_list_remove(timer_node_t * p_timer) -{ - timer_node_t * p_previous; - timer_node_t * p_current; - uint32_t timeout; - - // Find the timer's position in timer list. - p_previous = mp_timer_id_head; - p_current = p_previous; - - while (p_current != NULL) - { - if (p_current == p_timer) - { - break; - } - p_previous = p_current; - p_current = p_current->next; - } - - // Timer not in active list. - if (p_current == NULL) - { - return; - } - - // Timer is the first in the list - if (p_previous == p_current) - { - mp_timer_id_head = mp_timer_id_head->next; - - // No more timers in the list. Reset RTC1 in case Start timer operations are present in the queue. - if (mp_timer_id_head == NULL) - { - NRF_RTC1->TASKS_CLEAR = 1; - m_ticks_latest = 0; - m_rtc1_reset = true; - } - } - - // Remaining timeout between next timeout. - timeout = p_current->ticks_to_expire; - - // Link previous timer with next of this timer, i.e. removing the timer from list. - p_previous->next = p_current->next; - - // If this is not the last timer, increment the next timer by this timer timeout. - p_current = p_previous->next; - if (p_current != NULL) - { - p_current->ticks_to_expire += timeout; - } -} - - -/**@brief Function for scheduling a check for timeouts by generating a RTC1 interrupt. - */ -static void timer_timeouts_check_sched(void) -{ - NVIC_SetPendingIRQ(RTC1_IRQn); -} - - -/**@brief Function for scheduling a timer list update by generating a SWI interrupt. - */ -static void timer_list_handler_sched(void) -{ - NVIC_SetPendingIRQ(SWI_IRQn); -} - - -/**@brief Function for executing an application timeout handler, either by calling it directly, or - * by passing an event to the @ref app_scheduler. - * - * @param[in] p_timer Pointer to expired timer. - */ -static void timeout_handler_exec(timer_node_t * p_timer) -{ - if (m_evt_schedule_func != NULL) - { - uint32_t err_code = m_evt_schedule_func(p_timer->p_timeout_handler, p_timer->p_context); - APP_ERROR_CHECK(err_code); - } - else - { - p_timer->p_timeout_handler(p_timer->p_context); - } -} - - -/**@brief Function for checking for expired timers. - */ -static void timer_timeouts_check(void) -{ - // Handle expired of timer - if (mp_timer_id_head != NULL) - { - timer_node_t * p_timer; - timer_node_t * p_previous_timer; - uint32_t ticks_elapsed; - uint32_t ticks_expired; - - // Initialize actual elapsed ticks being consumed to 0. - ticks_expired = 0; - - // ticks_elapsed is collected here, job will use it. - ticks_elapsed = ticks_diff_get(rtc1_counter_get(), m_ticks_latest); - - // Auto variable containing the head of timers expiring. - p_timer = mp_timer_id_head; - - // Expire all timers within ticks_elapsed and collect ticks_expired. - while (p_timer != NULL) - { - // Do nothing if timer did not expire. - if (ticks_elapsed < p_timer->ticks_to_expire) - { - break; - } - - // Decrement ticks_elapsed and collect expired ticks. - ticks_elapsed -= p_timer->ticks_to_expire; - ticks_expired += p_timer->ticks_to_expire; - - // Move to next timer. - p_previous_timer = p_timer; - p_timer = p_timer->next; - - // Execute Task. - timeout_handler_exec(p_previous_timer); - } - - // Prepare to queue the ticks expired in the m_ticks_elapsed queue. - if (m_ticks_elapsed_q_read_ind == m_ticks_elapsed_q_write_ind) - { - // The read index of the queue is equal to the write index. This means the new - // value of ticks_expired should be stored at a new location in the m_ticks_elapsed - // queue (which is implemented as a double buffer). - - // Check if there will be a queue overflow. - if (++m_ticks_elapsed_q_write_ind == CONTEXT_QUEUE_SIZE_MAX) - { - // There will be a queue overflow. Hence the write index should point to the start - // of the queue. - m_ticks_elapsed_q_write_ind = 0; - } - } - - // Queue the ticks expired. - m_ticks_elapsed[m_ticks_elapsed_q_write_ind] = ticks_expired; - - timer_list_handler_sched(); - } -} - - -/**@brief Function for acquiring the number of ticks elapsed. - * - * @param[out] p_ticks_elapsed Number of ticks elapsed. - * - * @return TRUE if elapsed ticks was read from queue, FALSE otherwise. - */ -static bool elapsed_ticks_acquire(uint32_t * p_ticks_elapsed) -{ - // Pick the elapsed value from queue. - if (m_ticks_elapsed_q_read_ind != m_ticks_elapsed_q_write_ind) - { - // Dequeue elapsed value. - m_ticks_elapsed_q_read_ind++; - if (m_ticks_elapsed_q_read_ind == CONTEXT_QUEUE_SIZE_MAX) - { - m_ticks_elapsed_q_read_ind = 0; - } - - *p_ticks_elapsed = m_ticks_elapsed[m_ticks_elapsed_q_read_ind]; - - m_ticks_latest += *p_ticks_elapsed; - m_ticks_latest &= MAX_RTC_COUNTER_VAL; - - return true; - } - else - { - // No elapsed value in queue. - *p_ticks_elapsed = 0; - return false; - } -} - - -/**@brief Function for handling the timer list deletions. - * - * @return TRUE if Capture Compare register must be updated, FALSE otherwise. - */ -static bool list_deletions_handler(void) -{ - timer_node_t * p_timer_old_head; - uint8_t user_id; - - // Remember the old head, so as to decide if new compare needs to be set. - p_timer_old_head = mp_timer_id_head; - - user_id = m_user_array_size; - while (user_id--) - { - timer_user_t * p_user = &mp_users[user_id]; - uint8_t user_ops_first = p_user->first; - - while (user_ops_first != p_user->last) - { - timer_node_t * p_timer; - timer_user_op_t * p_user_op = &p_user->p_user_op_queue[user_ops_first]; - - // Traverse to next operation in queue. - user_ops_first++; - if (user_ops_first == p_user->user_op_queue_size) - { - user_ops_first = 0; - } - - switch (p_user_op->op_type) - { - case TIMER_USER_OP_TYPE_STOP: - // Delete node if timer is running. - p_timer = p_user_op->p_node; - if (p_timer->is_running) - { - timer_list_remove(p_user_op->p_node); - p_timer->is_running = false; - } - break; - - case TIMER_USER_OP_TYPE_STOP_ALL: - // Delete list of running timers, and mark all timers as not running. - while (mp_timer_id_head != NULL) - { - timer_node_t * p_head = mp_timer_id_head; - - p_head->is_running = false; - mp_timer_id_head = p_head->next; - } - break; - - default: - // No implementation needed. - break; - } - } - } - - // Detect change in head of the list. - return (mp_timer_id_head != p_timer_old_head); -} - - -/**@brief Function for updating the timer list for expired timers. - * - * @param[in] ticks_elapsed Number of elapsed ticks. - * @param[in] ticks_previous Previous known value of the RTC counter. - * @param[out] p_restart_list_head List of repeating timers to be restarted. - */ -static void expired_timers_handler(uint32_t ticks_elapsed, - uint32_t ticks_previous, - timer_node_t ** p_restart_list_head) -{ - uint32_t ticks_expired = 0; - - while (mp_timer_id_head != NULL) - { - timer_node_t * p_timer; - timer_node_t * p_timer_expired; - - // Auto variable for current timer node. - p_timer = mp_timer_id_head; - - // Do nothing if timer did not expire - if (ticks_elapsed < p_timer->ticks_to_expire) - { - p_timer->ticks_to_expire -= ticks_elapsed; - break; - } - - // Decrement ticks_elapsed and collect expired ticks. - ticks_elapsed -= p_timer->ticks_to_expire; - ticks_expired += p_timer->ticks_to_expire; - - // Timer expired, set ticks_to_expire zero. - p_timer->ticks_to_expire = 0; - p_timer->is_running = false; - - // Remove the expired timer from head. - p_timer_expired = mp_timer_id_head; - mp_timer_id_head = p_timer->next; - - // Timer will be restarted if periodic. - if (p_timer->ticks_periodic_interval != 0) - { - p_timer->ticks_at_start = (ticks_previous + ticks_expired) & MAX_RTC_COUNTER_VAL; - p_timer->ticks_first_interval = p_timer->ticks_periodic_interval; - p_timer->next = *p_restart_list_head; - *p_restart_list_head = p_timer_expired; - } - } -} - - -/**@brief Function for handling timer list insertions. - * - * @param[in] p_restart_list_head List of repeating timers to be restarted. - * - * @return TRUE if Capture Compare register must be updated, FALSE otherwise. - */ -static bool list_insertions_handler(timer_node_t * p_restart_list_head) -{ - timer_node_t * p_timer_id_old_head; - uint8_t user_id; - - // Remember the old head, so as to decide if new compare needs to be set. - p_timer_id_old_head = mp_timer_id_head; - - user_id = m_user_array_size; - while (user_id--) - { - timer_user_t * p_user = &mp_users[user_id]; - - // Handle insertions of timers. - while ((p_restart_list_head != NULL) || (p_user->first != p_user->last)) - { - timer_node_t * p_timer; - - if (p_restart_list_head != NULL) - { - p_timer = p_restart_list_head; - p_restart_list_head = p_timer->next; - } - else - { - timer_user_op_t * p_user_op = &p_user->p_user_op_queue[p_user->first]; - - p_user->first++; - if (p_user->first == p_user->user_op_queue_size) - { - p_user->first = 0; - } - - p_timer = p_user_op->p_node; - - if ((p_user_op->op_type != TIMER_USER_OP_TYPE_START) || p_timer->is_running) - { - continue; - } - - p_timer->ticks_at_start = p_user_op->params.start.ticks_at_start; - p_timer->ticks_first_interval = p_user_op->params.start.ticks_first_interval; - p_timer->ticks_periodic_interval = p_user_op->params.start.ticks_periodic_interval; - p_timer->p_context = p_user_op->params.start.p_context; - - if (m_rtc1_reset) - { - p_timer->ticks_at_start = 0; - } - } - - // Prepare the node to be inserted. - if ( - ((p_timer->ticks_at_start - m_ticks_latest) & MAX_RTC_COUNTER_VAL) - < - (MAX_RTC_COUNTER_VAL / 2) - ) - { - p_timer->ticks_to_expire = ticks_diff_get(p_timer->ticks_at_start, m_ticks_latest) + - p_timer->ticks_first_interval; - } - else - { - uint32_t delta_current_start; - - delta_current_start = ticks_diff_get(m_ticks_latest, p_timer->ticks_at_start); - if (p_timer->ticks_first_interval > delta_current_start) - { - p_timer->ticks_to_expire = p_timer->ticks_first_interval - delta_current_start; - } - else - { - p_timer->ticks_to_expire = 0; - } - } - - p_timer->ticks_at_start = 0; - p_timer->ticks_first_interval = 0; - p_timer->is_running = true; - p_timer->next = NULL; - - // Insert into list - timer_list_insert(p_timer); - } - } - - return (mp_timer_id_head != p_timer_id_old_head); -} - - -/**@brief Function for updating the Capture Compare register. - */ -static void compare_reg_update(timer_node_t * p_timer_id_head_old) -{ - // Setup the timeout for timers on the head of the list - if (mp_timer_id_head != NULL) - { - uint32_t ticks_to_expire = mp_timer_id_head->ticks_to_expire; - uint32_t pre_counter_val = rtc1_counter_get(); - uint32_t cc = m_ticks_latest; - uint32_t ticks_elapsed = ticks_diff_get(pre_counter_val, cc) + RTC_COMPARE_OFFSET_MIN; - - if (!m_rtc1_running) - { - // No timers were already running, start RTC - rtc1_start(); - } - - cc += (ticks_elapsed < ticks_to_expire) ? ticks_to_expire : ticks_elapsed; - cc &= MAX_RTC_COUNTER_VAL; - - rtc1_compare0_set(cc); - - uint32_t post_counter_val = rtc1_counter_get(); - - if ( - (ticks_diff_get(post_counter_val, pre_counter_val) + RTC_COMPARE_OFFSET_MIN) - > - ticks_diff_get(cc, pre_counter_val) - ) - { - // When this happens the COMPARE event may not be triggered by the RTC. - // The nRF51 Series User Specification states that if the COUNTER value is N - // (i.e post_counter_val = N), writing N or N+1 to a CC register may not trigger a - // COMPARE event. Hence the RTC interrupt is forcefully pended by calling the following - // function. - timer_timeouts_check_sched(); - } - } - else - { - // No timers are running, stop RTC - rtc1_stop(); - } -} - - -/**@brief Function for handling changes to the timer list. - */ -static void timer_list_handler(void) -{ - timer_node_t * p_restart_list_head = NULL; - - uint32_t ticks_elapsed; - uint32_t ticks_previous; - bool ticks_have_elapsed; - bool compare_update; - timer_node_t * p_timer_id_head_old; - - // Back up the previous known tick and previous list head - ticks_previous = m_ticks_latest; - p_timer_id_head_old = mp_timer_id_head; - - // Get number of elapsed ticks - ticks_have_elapsed = elapsed_ticks_acquire(&ticks_elapsed); - - // Handle list deletions - compare_update = list_deletions_handler(); - - // Handle expired timers - if (ticks_have_elapsed) - { - expired_timers_handler(ticks_elapsed, ticks_previous, &p_restart_list_head); - compare_update = true; - } - - // Handle list insertions - if (list_insertions_handler(p_restart_list_head)) - { - compare_update = true; - } - - // Update compare register if necessary - if (compare_update) - { - compare_reg_update(p_timer_id_head_old); - } - m_rtc1_reset = false; -} - - -/**@brief Function for enqueueing a new operations queue entry. - * - * @param[in] p_user User that the entry is to be enqueued for. - * @param[in] last_index Index of the next last index to be enqueued. - */ -static void user_op_enque(timer_user_t * p_user, uint8_t last_index) -{ - p_user->last = last_index; -} - - -/**@brief Function for allocating a new operations queue entry. - * - * @param[in] p_user User that the entry is to be allocated for. - * @param[out] p_last_index Index of the next last index to be enqueued. - * - * @return Pointer to allocated queue entry, or NULL if queue is full. - */ -static timer_user_op_t * user_op_alloc(timer_user_t * p_user, uint8_t * p_last_index) -{ - uint8_t last; - timer_user_op_t * p_user_op; - - last = p_user->last + 1; - if (last == p_user->user_op_queue_size) - { - // Overflow case. - last = 0; - } - if (last == p_user->first) - { - // Queue is full. - return NULL; - } - - *p_last_index = last; - p_user_op = &p_user->p_user_op_queue[p_user->last]; - - return p_user_op; -} - - -/**@brief Function for scheduling a Timer Start operation. - * - * @param[in] user_id Id of user calling this function. - * @param[in] timer_id Id of timer to start. - * @param[in] timeout_initial Time (in ticks) to first timer expiry. - * @param[in] timeout_periodic Time (in ticks) between periodic expiries. - * @param[in] p_context General purpose pointer. Will be passed to the timeout handler when - * the timer expires. - * @return NRF_SUCCESS on success, otherwise an error code. - */ -static uint32_t timer_start_op_schedule(timer_user_id_t user_id, - timer_node_t * p_node, - uint32_t timeout_initial, - uint32_t timeout_periodic, - void * p_context) -{ - uint8_t last_index; - - timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); - if (p_user_op == NULL) - { - return NRF_ERROR_NO_MEM; - } - - p_user_op->op_type = TIMER_USER_OP_TYPE_START; - p_user_op->p_node = p_node; - p_user_op->params.start.ticks_at_start = rtc1_counter_get(); - p_user_op->params.start.ticks_first_interval = timeout_initial; - p_user_op->params.start.ticks_periodic_interval = timeout_periodic; - p_user_op->params.start.p_context = p_context; - - user_op_enque(&mp_users[user_id], last_index); - - timer_list_handler_sched(); - - return NRF_SUCCESS; -} - - -/**@brief Function for scheduling a Timer Stop operation. - * - * @param[in] user_id Id of user calling this function. - * @param[in] timer_id Id of timer to stop. - * - * @return NRF_SUCCESS on successful scheduling a timer stop operation. NRF_ERROR_NO_MEM when there - * is no memory left to schedule the timer stop operation. - */ -static uint32_t timer_stop_op_schedule(timer_user_id_t user_id, timer_node_t * p_node) -{ - uint8_t last_index; - - timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); - if (p_user_op == NULL) - { - return NRF_ERROR_NO_MEM; - } - - p_user_op->op_type = TIMER_USER_OP_TYPE_STOP; - p_user_op->p_node = p_node; - - user_op_enque(&mp_users[user_id], last_index); - - timer_list_handler_sched(); - - return NRF_SUCCESS; -} - - -/**@brief Function for scheduling a Timer Stop All operation. - * - * @param[in] user_id Id of user calling this function. - */ -static uint32_t timer_stop_all_op_schedule(timer_user_id_t user_id) -{ - uint8_t last_index; - - timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); - if (p_user_op == NULL) - { - return NRF_ERROR_NO_MEM; - } - - p_user_op->op_type = TIMER_USER_OP_TYPE_STOP_ALL; - p_user_op->p_node = NULL; - - user_op_enque(&mp_users[user_id], last_index); - - timer_list_handler_sched(); - - return NRF_SUCCESS; -} - - -/**@brief Function for handling the RTC1 interrupt. - * - * @details Checks for timeouts, and executes timeout handlers for expired timers. - */ -void RTC1_IRQHandler(void) -{ - // Clear all events (also unexpected ones) - NRF_RTC1->EVENTS_COMPARE[0] = 0; - NRF_RTC1->EVENTS_COMPARE[1] = 0; - NRF_RTC1->EVENTS_COMPARE[2] = 0; - NRF_RTC1->EVENTS_COMPARE[3] = 0; - NRF_RTC1->EVENTS_TICK = 0; - NRF_RTC1->EVENTS_OVRFLW = 0; - - // Check for expired timers - timer_timeouts_check(); -} - - -/**@brief Function for handling the SWI interrupt. - * - * @details Performs all updates to the timer list. - */ -void SWI_IRQHandler(void) -{ - timer_list_handler(); -} - - -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) -{ - int i; - - // Check that buffer is correctly aligned - if (!is_word_aligned(p_buffer)) - { - return NRF_ERROR_INVALID_PARAM; - } - // Check for NULL buffer - if (p_buffer == NULL) - { - mp_users = NULL; - return NRF_ERROR_INVALID_PARAM; - } - - // Stop RTC to prevent any running timers from expiring (in case of reinitialization) - rtc1_stop(); - - m_evt_schedule_func = evt_schedule_func; - - // Initialize users array - m_user_array_size = APP_TIMER_INT_LEVELS; - mp_users = p_buffer; - - // Skip user array - p_buffer = &((uint8_t *)p_buffer)[APP_TIMER_INT_LEVELS * sizeof(timer_user_t)]; - - // Initialize operation queues - for (i = 0; i < APP_TIMER_INT_LEVELS; i++) - { - timer_user_t * p_user = &mp_users[i]; - - p_user->first = 0; - p_user->last = 0; - p_user->user_op_queue_size = op_queues_size; - p_user->p_user_op_queue = p_buffer; - - // Skip operation queue - p_buffer = &((uint8_t *)p_buffer)[op_queues_size * sizeof(timer_user_op_t)]; - } - - mp_timer_id_head = NULL; - m_ticks_elapsed_q_read_ind = 0; - m_ticks_elapsed_q_write_ind = 0; - - NVIC_ClearPendingIRQ(SWI_IRQn); - NVIC_SetPriority(SWI_IRQn, SWI_IRQ_PRI); - NVIC_EnableIRQ(SWI_IRQn); - - rtc1_init(prescaler); - - m_ticks_latest = rtc1_counter_get(); - - 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) -{ - // Check state and parameters - VERIFY_MODULE_INITIALIZED(); - - if (timeout_handler == NULL) - { - return NRF_ERROR_INVALID_PARAM; - } - if (p_timer_id == NULL) - { - return NRF_ERROR_INVALID_PARAM; - } - if (((timer_node_t*)*p_timer_id)->is_running) - { - return NRF_ERROR_INVALID_STATE; - } - - timer_node_t * p_node = (timer_node_t *)*p_timer_id; - p_node->is_running = false; - p_node->mode = mode; - p_node->p_timeout_handler = timeout_handler; - return NRF_SUCCESS; -} - - -/**@brief Function for creating a timer user id from the current interrupt level. - * - * @return Timer user id. -*/ -static timer_user_id_t user_id_get(void) -{ - timer_user_id_t ret; - - STATIC_ASSERT(APP_TIMER_INT_LEVELS == 3); - - switch (current_int_priority_get()) - { - case APP_IRQ_PRIORITY_HIGH: - ret = APP_HIGH_USER_ID; - break; - - case APP_IRQ_PRIORITY_LOW: - ret = APP_LOW_USER_ID; - break; - - default: - ret = THREAD_MODE_USER_ID; - break; - } - - return ret; -} - - -uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context) -{ - uint32_t timeout_periodic; - timer_node_t * p_node = (timer_node_t*)timer_id; - - // Check state and parameters - VERIFY_MODULE_INITIALIZED(); - - if (timer_id == 0) - { - return NRF_ERROR_INVALID_STATE; - } - if (timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS) - { - return NRF_ERROR_INVALID_PARAM; - } - if (p_node->p_timeout_handler == NULL) - { - return NRF_ERROR_INVALID_STATE; - } - - // Schedule timer start operation - timeout_periodic = (p_node->mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0; - - return timer_start_op_schedule(user_id_get(), - p_node, - timeout_ticks, - timeout_periodic, - p_context); -} - - -uint32_t app_timer_stop(app_timer_id_t timer_id) -{ - timer_node_t * p_node = (timer_node_t*)timer_id; - // Check state and parameters - VERIFY_MODULE_INITIALIZED(); - - if ((timer_id == NULL) || (p_node->p_timeout_handler == NULL)) - { - return NRF_ERROR_INVALID_STATE; - } - - // Schedule timer stop operation - return timer_stop_op_schedule(user_id_get(), p_node); -} - - -uint32_t app_timer_stop_all(void) -{ - // Check state - VERIFY_MODULE_INITIALIZED(); - - return timer_stop_all_op_schedule(user_id_get()); -} - - -uint32_t app_timer_cnt_get(uint32_t * p_ticks) -{ - *p_ticks = rtc1_counter_get(); - return NRF_SUCCESS; -} - - -uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to, - uint32_t ticks_from, - uint32_t * p_ticks_diff) -{ - *p_ticks_diff = ticks_diff_get(ticks_to, ticks_from); - return NRF_SUCCESS; -} -
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_freertos.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_freertos.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_freertos.c deleted file mode 100644 index 9c68bfb..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/timer/app_timer_freertos.c +++ /dev/null @@ -1,230 +0,0 @@ -/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -#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" -#include "app_util.h" -#include "nordic_common.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(pinfo)); - - 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; -} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/trace/app_trace.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/trace/app_trace.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/trace/app_trace.c deleted file mode 100644 index 2f510f8..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/trace/app_trace.c +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -#include <stdio.h> -#include <stdint.h> -#include <string.h> -#include <stdarg.h> - - -#ifdef ENABLE_DEBUG_LOG_SUPPORT -#include "app_trace.h" -#include "nrf_log.h" - -void app_trace_init(void) -{ - (void)NRF_LOG_INIT(); -} - -void app_trace_dump(uint8_t * p_buffer, uint32_t len) -{ - app_trace_log("\r\n"); - for (uint32_t index = 0; index < len; index++) - { - app_trace_log("0x%02X ", p_buffer[index]); - } - app_trace_log("\r\n"); -} - -#endif // ENABLE_DEBUG_LOG_SUPPORT - -/** - *@} - **/ - http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.c deleted file mode 100644 index 9402d28..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.c +++ /dev/null @@ -1,376 +0,0 @@ -/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -#include "app_twi.h" -#include "nrf_assert.h" -#include "app_util_platform.h" -#include "sdk_common.h" - - -// Increase specified queue index and when it goes outside the queue move it -// on the beginning of the queue. -#define INCREASE_IDX(idx, p_queue) \ - do { \ - ++idx; \ - p_queue->idx = (idx > p_queue->size) ? 0 : idx; \ - } while (0) - - -static bool queue_put(app_twi_queue_t * p_queue, - app_twi_transaction_t const * p_transaction) -{ - // [use a local variable to avoid using two volatile variables in one - // expression] - uint8_t write_idx = p_queue->write_idx; - - // If the queue is already full, we cannot put any more elements into it. - if ((write_idx == p_queue->size && p_queue->read_idx == 0) || - write_idx == p_queue->read_idx-1) - { - return false; - } - - // Write the new element on the position specified by the write index. - p_queue->p_buffer[write_idx] = p_transaction; - // Increase the write index and when it goes outside the queue move it - // on the beginning. - INCREASE_IDX(write_idx, p_queue); - - return true; -} - - -static app_twi_transaction_t const * queue_get(app_twi_queue_t * p_queue) -{ - // [use a local variable to avoid using two volatile variables in one - // expression] - uint8_t read_idx = p_queue->read_idx; - - // If the queue is empty, we cannot return any more elements from it. - if (read_idx == p_queue->write_idx) - { - return NULL; - } - - // Read the element from the position specified by the read index. - app_twi_transaction_t const * p_transaction = p_queue->p_buffer[read_idx]; - // Increase the read index and when it goes outside the queue move it - // on the beginning. - INCREASE_IDX(read_idx, p_queue); - - return p_transaction; -} - - -static ret_code_t start_transfer(app_twi_t * p_app_twi) -{ - ASSERT(p_app_twi != NULL); - - // [use a local variable to avoid using two volatile variables in one - // expression] - uint8_t current_transfer_idx = p_app_twi->current_transfer_idx; - app_twi_transfer_t const * p_transfer = - &p_app_twi->p_current_transaction->p_transfers[current_transfer_idx]; - uint8_t address = APP_TWI_OP_ADDRESS(p_transfer->operation); - - nrf_drv_twi_xfer_desc_t xfer_desc; - uint32_t flags; - - xfer_desc.address = address; - xfer_desc.p_primary_buf = p_transfer->p_data; - xfer_desc.primary_length = p_transfer->length; - - /* If it is possible try to bind two transfers together. They can be combined if: - * - there is no stop condition after current transfer. - * - current transfer is TX. - * - there is at least one more transfer in the transaction. - * - address of next trnasfer is the same as current transfer. - */ - if ((p_transfer->flags & APP_TWI_NO_STOP) && - !APP_TWI_IS_READ_OP(p_transfer->operation) && - ((current_transfer_idx+1) < p_app_twi->p_current_transaction->number_of_transfers) && - APP_TWI_OP_ADDRESS(p_transfer->operation) == - APP_TWI_OP_ADDRESS(p_app_twi->p_current_transaction->p_transfers[current_transfer_idx+1].operation) - ) - { - app_twi_transfer_t const * p_second_transfer = - &p_app_twi->p_current_transaction->p_transfers[current_transfer_idx+1]; - xfer_desc.p_secondary_buf = p_second_transfer->p_data; - xfer_desc.secondary_length = p_second_transfer->length; - xfer_desc.type = APP_TWI_IS_READ_OP(p_second_transfer->operation) ? NRF_DRV_TWI_XFER_TXRX : - NRF_DRV_TWI_XFER_TXTX; - flags = (p_second_transfer->flags & APP_TWI_NO_STOP) ? NRF_DRV_TWI_FLAG_TX_NO_STOP : 0; - p_app_twi->current_transfer_idx++; - } - else - { - xfer_desc.type = APP_TWI_IS_READ_OP(p_transfer->operation) ? NRF_DRV_TWI_XFER_RX : - NRF_DRV_TWI_XFER_TX; - xfer_desc.p_secondary_buf = NULL; - xfer_desc.secondary_length = 0; - flags = (p_transfer->flags & APP_TWI_NO_STOP) ? NRF_DRV_TWI_FLAG_TX_NO_STOP : 0; - } - - return nrf_drv_twi_xfer(&p_app_twi->twi, &xfer_desc, flags); -} - - -static void signal_end_of_transaction(app_twi_t const * p_app_twi, - ret_code_t result) -{ - ASSERT(p_app_twi != NULL); - - if (p_app_twi->p_current_transaction->callback) - { - // [use a local variable to avoid using two volatile variables in one - // expression] - void * p_user_data = p_app_twi->p_current_transaction->p_user_data; - p_app_twi->p_current_transaction->callback(result, p_user_data); - } -} - - -// This function starts pending transaction if there is no current one or -// when 'switch_transaction' parameter is set to true. It is important to -// switch to new transaction without setting 'p_app_twi->p_current_transaction' -// to NULL in between, since this pointer is used to check idle status - see -// 'app_twi_is_idle()'. -static void start_pending_transaction(app_twi_t * p_app_twi, - bool switch_transaction) -{ - ASSERT(p_app_twi != NULL); - - for (;;) - { - bool start_transaction = false; - - CRITICAL_REGION_ENTER(); - if (switch_transaction || app_twi_is_idle(p_app_twi)) - { - p_app_twi->p_current_transaction = queue_get(&p_app_twi->queue); - if (p_app_twi->p_current_transaction != NULL) - { - start_transaction = true; - } - } - CRITICAL_REGION_EXIT(); - - if (!start_transaction) - { - return; - } - else - { - ret_code_t result; - - // Try to start first transfer for this new transaction. - p_app_twi->current_transfer_idx = 0; - result = start_transfer(p_app_twi); - - // If it started successfully there is nothing more to do here now. - if (result == NRF_SUCCESS) - { - return; - } - - // Transfer failed to start - notify user that this transaction - // cannot be started and try with next one (in next iteration of - // the loop). - signal_end_of_transaction(p_app_twi, result); - - switch_transaction = true; - } - } -} - - -static void twi_event_handler(nrf_drv_twi_evt_t const * p_event, - void * p_context) -{ - ASSERT(p_event != NULL); - - app_twi_t * p_app_twi = (app_twi_t *)p_context; - ret_code_t result; - - // This callback should be called only during transaction. - ASSERT(p_app_twi->p_current_transaction != NULL); - - if (p_event->type == NRF_DRV_TWI_EVT_DONE) - { - result = NRF_SUCCESS; - - // Transfer finished successfully. If there is another one to be - // performed in the current transaction, start it now. - // [use a local variable to avoid using two volatile variables in one - // expression] - uint8_t current_transfer_idx = p_app_twi->current_transfer_idx; - ++current_transfer_idx; - if (current_transfer_idx < - p_app_twi->p_current_transaction->number_of_transfers) - { - p_app_twi->current_transfer_idx = current_transfer_idx; - - result = start_transfer(p_app_twi); - - if (result == NRF_SUCCESS) - { - // The current transaction goes on and we've successfully - // started its next transfer -> there is nothing more to do. - return; - } - - // [if the next transfer could not be started due to some error - // we finish the transaction with this error code as the result] - } - } - else - { - result = NRF_ERROR_INTERNAL; - } - - // The current transaction has been completed or interrupted by some error. - // Notify the user and start next one (if there is any). - signal_end_of_transaction(p_app_twi, result); - // [we switch transactions here ('p_app_twi->p_current_transaction' is set - // to NULL only if there is nothing more to do) in order to not generate - // spurious idle status (even for a moment)] - start_pending_transaction(p_app_twi, true); -} - - -ret_code_t app_twi_init(app_twi_t * p_app_twi, - nrf_drv_twi_config_t const * p_twi_config, - uint8_t queue_size, - app_twi_transaction_t const * * p_queue_buffer) -{ - ASSERT(p_app_twi != NULL); - ASSERT(queue_size != 0); - ASSERT(p_queue_buffer != NULL); - - ret_code_t err_code; - - err_code = nrf_drv_twi_init(&p_app_twi->twi, - p_twi_config, - twi_event_handler, - p_app_twi); - VERIFY_SUCCESS(err_code); - - nrf_drv_twi_enable(&p_app_twi->twi); - - p_app_twi->queue.p_buffer = p_queue_buffer; - p_app_twi->queue.size = queue_size; - p_app_twi->queue.read_idx = 0; - p_app_twi->queue.write_idx = 0; - - p_app_twi->internal_transaction_in_progress = false; - p_app_twi->p_current_transaction = NULL; - - return NRF_SUCCESS; -} - - -void app_twi_uninit(app_twi_t * p_app_twi) -{ - ASSERT(p_app_twi != NULL); - - nrf_drv_twi_uninit(&(p_app_twi->twi)); - - p_app_twi->p_current_transaction = NULL; -} - - -ret_code_t app_twi_schedule(app_twi_t * p_app_twi, - app_twi_transaction_t const * p_transaction) -{ - ASSERT(p_app_twi != NULL); - ASSERT(p_transaction != NULL); - ASSERT(p_transaction->p_transfers != NULL); - ASSERT(p_transaction->number_of_transfers != 0); - - ret_code_t result = NRF_SUCCESS; - - CRITICAL_REGION_ENTER(); - if (!queue_put(&p_app_twi->queue, p_transaction)) - { - result = NRF_ERROR_BUSY; - } - CRITICAL_REGION_EXIT(); - - if (result == NRF_SUCCESS) - { - // New transaction has been successfully added to queue, - // so if we are currently idle it's time to start the job. - start_pending_transaction(p_app_twi, false); - } - - return result; -} - - -static void internal_transaction_cb(ret_code_t result, void * p_user_data) -{ - app_twi_t * p_app_twi = (app_twi_t *)p_user_data; - - p_app_twi->internal_transaction_result = result; - p_app_twi->internal_transaction_in_progress = false; -} - - -ret_code_t app_twi_perform(app_twi_t * p_app_twi, - app_twi_transfer_t const * p_transfers, - uint8_t number_of_transfers, - void (* user_function)(void)) -{ - ASSERT(p_app_twi != NULL); - ASSERT(p_transfers != NULL); - ASSERT(number_of_transfers != 0); - - bool busy = false; - - CRITICAL_REGION_ENTER(); - if (p_app_twi->internal_transaction_in_progress) - { - busy = true; - } - else - { - p_app_twi->internal_transaction_in_progress = true; - } - CRITICAL_REGION_EXIT(); - - if (busy) - { - return NRF_ERROR_BUSY; - } - else - { - app_twi_transaction_t internal_transaction = - { - .callback = internal_transaction_cb, - .p_user_data = p_app_twi, - .p_transfers = p_transfers, - .number_of_transfers = number_of_transfers, - }; - ret_code_t result = app_twi_schedule(p_app_twi, &internal_transaction); - VERIFY_SUCCESS(result); - - while (p_app_twi->internal_transaction_in_progress) - { - if (user_function) - { - user_function(); - } - } - - return p_app_twi->internal_transaction_result; - } -} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.h ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.h b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.h deleted file mode 100644 index 65bbf45..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/twi/app_twi.h +++ /dev/null @@ -1,298 +0,0 @@ -/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -#ifndef APP_TWI_H__ -#define APP_TWI_H__ - -#include <stdint.h> -#include "nrf_drv_twi.h" -#include "sdk_errors.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @defgroup app_twi TWI transaction manager - * @{ - * @ingroup app_common - * - * @brief Module for scheduling TWI transactions. - */ - -/** - * @brief Flag indicating that a given transfer should not be ended - * with a stop condition. - * - * Use this flag when a stop condition is undesirable between two transfers, - * for example, when the first transfer is a write that sets an address in the slave - * device and the second one is a read that fetches certain data using this - * address. In this case, the second transfer should follow directly after the - * first transfer, with a repeated start condition instead of a stop and then - * a new start condition. - */ -#define APP_TWI_NO_STOP 0x01 - -/** - * @brief Macro for creating a write transfer. - * - * @param address Slave address. - * @param[in] p_data Pointer to the data to be sent. - * @param length Number of bytes to transfer. - * @param flags Transfer flags (see @ref APP_TWI_NO_STOP). - */ -#define APP_TWI_WRITE(address, p_data, length, flags) \ - APP_TWI_TRANSFER(APP_TWI_WRITE_OP(address), p_data, length, flags) - -/** - * @brief Macro for creating a read transfer. - * - * @param address Slave address. - * @param[in] p_data Pointer to the buffer where received data should be placed. - * @param length Number of bytes to transfer. - * @param flags Transfer flags (see @ref APP_TWI_NO_STOP). - */ -#define APP_TWI_READ(address, p_data, length, flags) \ - APP_TWI_TRANSFER(APP_TWI_READ_OP(address), p_data, length, flags) - -/** - * @brief Helper macro, should not be used directly. - */ -#define APP_TWI_TRANSFER(_operation, _p_data, _length, _flags) \ -{ \ - .p_data = (uint8_t *)(_p_data), \ - .length = _length, \ - .operation = _operation, \ - .flags = _flags \ -} -/** - * @brief Helper macro, should not be used directly. - */ -#define APP_TWI_WRITE_OP(address) (((address) << 1) | 0) -/** - * @brief Helper macro, should not be used directly. - */ -#define APP_TWI_READ_OP(address) (((address) << 1) | 1) -/** - * @brief Helper macro, should not be used directly. - */ -#define APP_TWI_IS_READ_OP(operation) ((operation) & 1) -/** - * @brief Helper macro, should not be used directly. - */ -#define APP_TWI_OP_ADDRESS(operation) ((operation) >> 1) - -/** - * @brief TWI transaction callback prototype. - * - * @param result Result of operation (NRF_SUCCESS on success, - * otherwise a relevant error code). - * @param[in] p_user_data Pointer to user data defined in transaction - * descriptor. - */ -typedef void (* app_twi_callback_t)(ret_code_t result, void * p_user_data); - -/** - * @brief TWI transfer descriptor. - */ -typedef struct { - uint8_t * p_data; ///< Pointer to the buffer holding the data. - uint8_t length; ///< Number of bytes to transfer. - uint8_t operation; ///< Device address combined with transfer direction. - uint8_t flags; ///< Transfer flags (see @ref APP_TWI_NO_STOP). -} app_twi_transfer_t; - -/** - * @brief TWI transaction descriptor. - */ -typedef struct { - app_twi_callback_t callback; - ///< User-specified function to be called after the transaction is finished. - - void * p_user_data; - ///< Pointer to user data to be passed to the callback. - - app_twi_transfer_t const * p_transfers; - ///< Pointer to the array of transfers that make up the transaction. - - uint8_t number_of_transfers; - ///< Number of transfers that make up the transaction. -} app_twi_transaction_t; - -/** - * @brief TWI transaction queue. - */ -typedef struct { - app_twi_transaction_t const * volatile * p_buffer; - uint8_t size; - uint8_t volatile read_idx; - uint8_t volatile write_idx; -} app_twi_queue_t; - -/** - * @brief TWI transaction manager instance. - */ -typedef struct { - app_twi_queue_t queue; - ///< Transaction queue. - - uint8_t volatile current_transfer_idx; - ///< Index of currently performed transfer (within current transaction). - - bool volatile internal_transaction_in_progress; - ///< Informs that an internal transaction is being performed (by app_twi_perform()). - - uint8_t volatile internal_transaction_result; - ///< Used to pass the result of the internal transaction realized by app_twi_perform(). - - app_twi_transaction_t const * volatile p_current_transaction; - ///< Currently realized transaction. - - nrf_drv_twi_t const twi; - ///< TWI master driver instance. -} app_twi_t; - -/** - * @brief Macro for creating an instance of the TWI transaction manager. - * - * @param[in] twi_idx Index of the TWI master driver instance to be utilized - * by this manager instance. - */ -#define APP_TWI_INSTANCE(twi_idx) \ -{ \ - .twi = NRF_DRV_TWI_INSTANCE(twi_idx) \ -} - -/** - * @brief Macro that simplifies the initialization of a TWI transaction manager - * instance. - * - * This macro allocates a static buffer for the transaction queue. - * Therefore, it should be used in only one place in the code for a given - * instance. - * - * @param[in] p_app_twi Pointer to the instance to be initialized. - * @param[in] p_twi_config Pointer to the TWI master driver configuration. - * @param queue_size Size of the transaction queue (maximum number - * of pending transactions). - * See @ref app_twi_init_note "this note". - * @param[out] err_code The result of the app_twi_init() function call - * is written to this parameter. - */ -#define APP_TWI_INIT(p_app_twi, p_twi_config, queue_size, err_code) \ - do { \ - static app_twi_transaction_t const * queue_buffer[queue_size + 1]; \ - err_code = app_twi_init(p_app_twi, p_twi_config, \ - queue_size, queue_buffer); \ - } while (0) - -/** - * @brief Function for initializing a TWI transaction manager instance. - * - * This function initializes the utilized TWI master driver instance and - * prepares the transaction queue. - * - * @anchor app_twi_init_note - * @note The queue size is the maximum number of pending transactions - * not counting the one that is currently realized. This means that - * for an empty queue with size of, for example, 4 elements, it is - * possible to schedule up to 5 transactions. - * - * @param[in] p_app_twi Pointer to the instance to be initialized. - * @param[in] p_twi_config Pointer to the TWI master driver configuration. - * @param queue_size Size of the transaction queue (maximum number - * of pending transactions). - * @param[in] p_queue_buffer Pointer to a buffer for queued transactions - * storage. Due to the queue implementation, the buffer must - * be big enough to hold queue_size + 1 entries - * (pointers to transaction descriptors). - * - * @retval NRF_SUCCESS If initialization was successful. Otherwise, the error code - * returned by the nrf_drv_twi_init() function is returned. - */ -ret_code_t app_twi_init(app_twi_t * p_app_twi, - nrf_drv_twi_config_t const * p_twi_config, - uint8_t queue_size, - app_twi_transaction_t const * * p_queue_buffer); - -/** - * @brief Function for uninitializing a TWI transaction manager instance. - * - * @param[in] p_app_twi Pointer to the instance to be uninitialized. - */ -void app_twi_uninit(app_twi_t * p_app_twi); - -/** - * @brief Function for scheduling a TWI transaction. - * - * The transaction is enqueued and started as soon as the TWI bus is - * available, thus when all previously scheduled transactions have been - * finished (possibly immediately). - * - * @param[in] p_app_twi Pointer to the TWI transaction manager instance. - * @param[in] p_transaction Pointer to the descriptor of the transaction to be - * scheduled. - * - * @retval NRF_SUCCESS If the transaction has been successfully scheduled. - * @retval NRF_ERROR_BUSY If the limit of pending transactions has been reached - * (the transaction queue is full). - */ -ret_code_t app_twi_schedule(app_twi_t * p_app_twi, - app_twi_transaction_t const * p_transaction); - -/** - * @brief Function for scheduling a transaction and waiting until it is finished. - * - * This function schedules a transaction that consists of one or more transfers - * and waits until it is finished. - * - * @param[in] p_app_twi Pointer to the TWI transaction manager instance. - * @param[in] p_transfers Pointer to an array of transfers to be performed. - * @param number_of_transfers Number of transfers to be performed. - * @param user_function User-specified function to be called while - * waiting. NULL if such functionality - * is not needed. - * - * @retval NRF_SUCCESS If the transfers have been successfully realized. - * @retval NRF_ERROR_BUSY If some transfers are already performed (if this function - * was called from another context). - * @retval - Other error codes mean that the transaction has ended - * with the error that is specified in the error code. - */ -ret_code_t app_twi_perform(app_twi_t * p_app_twi, - app_twi_transfer_t const * p_transfers, - uint8_t number_of_transfers, - void (* user_function)(void)); - -/** - * @brief Function for getting the current state of a TWI transaction manager - * instance. - * - * @param[in] p_app_twi Pointer to the TWI transaction manager instance. - * - * @retval true If all scheduled transactions have been finished. - * @retval false Otherwise. - */ -__STATIC_INLINE bool app_twi_is_idle(app_twi_t * p_app_twi) -{ - return (p_app_twi->p_current_transaction == NULL); -} - -/** - *@} - **/ - -#ifdef __cplusplus -} -#endif - -#endif // APP_TWI_H__ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a1481cb2/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/uart/app_uart.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/uart/app_uart.c b/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/uart/app_uart.c deleted file mode 100644 index 944ff21..0000000 --- a/hw/mcu/nordic/src/ext/nRF5_SDK_11.0.0_89a8197/components/libraries/uart/app_uart.c +++ /dev/null @@ -1,132 +0,0 @@ -/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -#include "app_uart.h" -#include "nrf_drv_uart.h" -#include "nrf_assert.h" -#include "sdk_common.h" - -static uint8_t tx_buffer[1]; -static uint8_t rx_buffer[1]; -static volatile bool rx_done; -static app_uart_event_handler_t m_event_handler; /**< Event handler function. */ - -void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context) -{ - if (p_event->type == NRF_DRV_UART_EVT_RX_DONE) - { - app_uart_evt_t app_uart_event; - app_uart_event.evt_type = APP_UART_DATA; - app_uart_event.data.value = p_event->data.rxtx.p_data[0]; - (void)nrf_drv_uart_rx(rx_buffer,1); - rx_done = true; - m_event_handler(&app_uart_event); - } - else if (p_event->type == NRF_DRV_UART_EVT_ERROR) - { - app_uart_evt_t app_uart_event; - app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR; - app_uart_event.data.error_communication = p_event->data.error.error_mask; - (void)nrf_drv_uart_rx(rx_buffer,1); - m_event_handler(&app_uart_event); - } - else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE) - { - // Last byte from FIFO transmitted, notify the application. - // Notify that new data is available if this was first byte put in the buffer. - app_uart_evt_t app_uart_event; - app_uart_event.evt_type = APP_UART_TX_EMPTY; - m_event_handler(&app_uart_event); - } -} - -uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params, - app_uart_buffers_t * p_buffers, - app_uart_event_handler_t event_handler, - app_irq_priority_t irq_priority) -{ - nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG; - config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate; - config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ? - NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED; - config.interrupt_priority = irq_priority; - config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED; - config.pselcts = p_comm_params->cts_pin_no; - config.pselrts = p_comm_params->rts_pin_no; - config.pselrxd = p_comm_params->rx_pin_no; - config.pseltxd = p_comm_params->tx_pin_no; - - m_event_handler = event_handler; - - rx_done = false; - - if (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_LOW_POWER) - { - return NRF_ERROR_NOT_SUPPORTED; - } - - uint32_t err_code = nrf_drv_uart_init(&config, uart_event_handler); - VERIFY_SUCCESS(err_code); - -#ifdef NRF52 - if (!config.use_easy_dma) -#endif - { - nrf_drv_uart_rx_enable(); - } - return nrf_drv_uart_rx(rx_buffer,1); -} - - -uint32_t app_uart_get(uint8_t * p_byte) -{ - ASSERT(p_byte); - uint32_t err_code = NRF_SUCCESS; - if (rx_done) - { - *p_byte = rx_buffer[0]; - } - else - { - err_code = NRF_ERROR_NOT_FOUND; - } - return err_code; -} - -uint32_t app_uart_put(uint8_t byte) -{ - tx_buffer[0] = byte; - ret_code_t ret = nrf_drv_uart_tx(tx_buffer,1); - if (NRF_ERROR_BUSY == ret) - { - return NRF_ERROR_NO_MEM; - } - else if (ret != NRF_SUCCESS) - { - return NRF_ERROR_INTERNAL; - } - else - { - return NRF_SUCCESS; - } -} - -uint32_t app_uart_flush(void) -{ - return NRF_SUCCESS; -} - -uint32_t app_uart_close(void) -{ - nrf_drv_uart_uninit(); - return NRF_SUCCESS; -}
