mbed library sources
Fork of mbed-src by
Revision 304:89b9c3a9a045, committed 2014-08-29
- Comitter:
- mbed_official
- Date:
- Fri Aug 29 17:15:07 2014 +0100
- Parent:
- 303:78e31413b129
- Child:
- 305:1f0269907d8b
- Commit message:
- Synchronized with git revision 734f365d7da26ef199751f4b0d91611479b495ea
Full URL: https://github.com/mbedmicro/mbed/commit/734f365d7da26ef199751f4b0d91611479b495ea/
1. timestamp_t as an abstraction for time values managed by
Ticker. Using uint64_t for timestamp_t allows a wraparound-free
Ticker. This change forces us to update the definitions of usTicker
for all platforms; but the changes beyond nRF51822 aren't major.
2. reduce power consumption on the nRF51822 by removing the need for
the high-frequency processor timer; and reimplementing it using the
RTC.
I've also replaced high-frequency clock with low-frequency external
clock during system startup of the nRF51822. This brings a major win
in power consumption.
Changed in this revision
--- a/api/Ticker.h Fri Aug 29 11:45:07 2014 +0100 +++ b/api/Ticker.h Fri Aug 29 17:15:07 2014 +0100 @@ -83,7 +83,7 @@ * @param fptr pointer to the function to be called * @param t the time between calls in micro-seconds */ - void attach_us(void (*fptr)(void), unsigned int t) { + void attach_us(void (*fptr)(void), timestamp_t t) { _function.attach(fptr); setup(t); } @@ -95,21 +95,26 @@ * @param t the time between calls in micro-seconds */ template<typename T> - void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { + void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) { _function.attach(tptr, mptr); setup(t); } + virtual ~Ticker() { + detach(); + } + /** Detach the function */ void detach(); protected: - void setup(unsigned int t); + void setup(timestamp_t t); virtual void handler(); - unsigned int _delay; - FunctionPointer _function; +protected: + timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ + FunctionPointer _function; /**< Callback. */ }; } // namespace mbed
--- a/api/TimerEvent.h Fri Aug 29 11:45:07 2014 +0100 +++ b/api/TimerEvent.h Fri Aug 29 17:15:07 2014 +0100 @@ -39,7 +39,7 @@ virtual void handler() = 0; // insert in to linked list - void insert(unsigned int timestamp); + void insert(timestamp_t timestamp); // remove from linked list, if in it void remove();
--- a/common/Ticker.cpp Fri Aug 29 11:45:07 2014 +0100 +++ b/common/Ticker.cpp Fri Aug 29 17:15:07 2014 +0100 @@ -25,7 +25,7 @@ _function.attach(0); } -void Ticker::setup(unsigned int t) { +void Ticker::setup(timestamp_t t) { remove(); _delay = t; insert(_delay + us_ticker_read());
--- a/common/TimerEvent.cpp Fri Aug 29 11:45:07 2014 +0100 +++ b/common/TimerEvent.cpp Fri Aug 29 17:15:07 2014 +0100 @@ -34,7 +34,7 @@ } // insert in to linked list -void TimerEvent::insert(unsigned int timestamp) { +void TimerEvent::insert(timestamp_t timestamp) { us_ticker_insert_event(&event, timestamp, (uint32_t)this); }
--- a/common/us_ticker_api.c Fri Aug 29 11:45:07 2014 +0100 +++ b/common/us_ticker_api.c Fri Aug 29 17:15:07 2014 +0100 @@ -45,6 +45,8 @@ if (event_handler != NULL) { event_handler(p->id); // NOTE: the handler can set new events } + /* Note: We continue back to examining the head because calling the + * event handler may have altered the chain of pending events. */ } else { // This event and the following ones in the list are in the future: // set it as next interrupt and return @@ -54,7 +56,7 @@ } } -void us_ticker_insert_event(ticker_event_t *obj, unsigned int timestamp, uint32_t id) { +void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t id) { /* disable interrupts for the duration of the function */ __disable_irq(); @@ -95,7 +97,9 @@ if (head == obj) { // first in the list, so just drop me head = obj->next; - if (obj->next != NULL) { + if (head == NULL) { + us_ticker_disable_interrupt(); + } else { us_ticker_set_interrupt(head->timestamp); } } else {
--- a/hal/us_ticker_api.h Fri Aug 29 11:45:07 2014 +0100 +++ b/hal/us_ticker_api.h Fri Aug 29 17:15:07 2014 +0100 @@ -22,24 +22,26 @@ extern "C" { #endif +typedef uint64_t timestamp_t; + uint32_t us_ticker_read(void); typedef void (*ticker_event_handler)(uint32_t id); void us_ticker_set_handler(ticker_event_handler handler); typedef struct ticker_event_s { - uint32_t timestamp; - uint32_t id; + timestamp_t timestamp; + uint32_t id; struct ticker_event_s *next; } ticker_event_t; void us_ticker_init(void); -void us_ticker_set_interrupt(unsigned int timestamp); +void us_ticker_set_interrupt(timestamp_t timestamp); void us_ticker_disable_interrupt(void); void us_ticker_clear_interrupt(void); void us_ticker_irq_handler(void); -void us_ticker_insert_event(ticker_event_t *obj, unsigned int timestamp, uint32_t id); +void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t id); void us_ticker_remove_event(ticker_event_t *obj); #ifdef __cplusplus
--- a/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51822.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51822.c Fri Aug 29 17:15:07 2014 +0100 @@ -29,7 +29,7 @@ #if defined ( __CC_ARM ) - uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; + uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; #elif defined ( __ICCARM__ ) __root uint32_t SystemCoreClock = __SYSTEM_CLOCK; #elif defined ( __GNUC__ ) @@ -43,35 +43,36 @@ } void SystemInit(void) -{ +{ // Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required // to enable the use of peripherals" found at Product Anomaly document for your device found at - // https://www.nordicsemi.com/. The side effect of executing these instructions in the devices + // https://www.nordicsemi.com/. The side effect of executing these instructions in the devices // that do not need it is that the new peripherals in the second generation devices (LPCOMP for // example) will not be available. if (is_manual_peripheral_setup_needed()){ *(uint32_t volatile *)0x40000504 = 0xC007FFDF; *(uint32_t volatile *)0x40006C18 = 0x00008000; } - + // Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG - // register is incorrect" found at Product Anomaly document four your device found at - // https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed. + // register is incorrect" found at Product Anomaly document four your device found at + // https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed. if (is_disabled_in_debug_needed()){ NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos; } - - // Start 16 MHz crystal oscillator. - NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; - NRF_CLOCK->TASKS_HFCLKSTART = 1; + + // Start the external 32khz crystal oscillator. + NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos); + NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; + NRF_CLOCK->TASKS_LFCLKSTART = 1; // Wait for the external oscillator to start up. - while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { + while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) { // Do nothing. } } -static bool is_manual_peripheral_setup_needed(void) +static bool is_manual_peripheral_setup_needed(void) { if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) { @@ -88,11 +89,11 @@ return true; } } - + return false; } -static bool is_disabled_in_debug_needed(void) +static bool is_disabled_in_debug_needed(void) { if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) { @@ -101,7 +102,7 @@ return true; } } - + return false; }
--- a/targets/hal/TARGET_Freescale/TARGET_K20D50M/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D50M/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -135,8 +135,8 @@ } } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); if (delta <= 0) { // This event was in the past: us_ticker_irq_handler();
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -167,8 +167,8 @@ } } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); if (delta <= 0) { // This event was in the past: us_ticker_irq_handler();
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -133,8 +133,8 @@ } } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); if (delta <= 0) { // This event was in the past: us_ticker_irq_handler();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/app_common/app_timer.c Fri Aug 29 17:15:07 2014 +0100 @@ -0,0 +1,1151 @@ +/* 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 "nrf51.h" +#include "nrf51_bitfields.h" +#include "nrf_soc.h" +#include "app_error.h" +#include "nrf_delay.h" +#include "app_util.h" +#include "app_util_platform.h" + + +#define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the RTC1 interrupt (used for checking for timeouts and executing timeout handlers). */ +#define SWI0_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the SWI0 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 == SWI0_IRQ_PRI); + +#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. */ + +/**@brief Timer allocation state type. */ +typedef enum +{ + STATE_FREE, /**< The timer node is available. */ + STATE_ALLOCATED /**< The timer node has been allocated. */ +} timer_alloc_state_t; + +/**@brief Timer node type. The nodes will be used form a linked list of running timers. */ +typedef struct +{ + timer_alloc_state_t state; /**< Timer allocation state. */ + app_timer_mode_t mode; /**< Timer mode. */ + 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_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. */ + app_timer_id_t next; /**< Id of next timer in list of running timers. */ +} timer_node_t; + +STATIC_ASSERT(sizeof(timer_node_t) <= APP_TIMER_NODE_SIZE); +STATIC_ASSERT(sizeof(timer_node_t) % 4 == 0); + +/**@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; /**< Timer operation type. */ + app_timer_id_t timer_id; /**< Id of timer on which the operation is to be performed. */ + 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) /**< Timer internal elapsed ticks queue size. */ + +static uint8_t m_node_array_size; /**< Size of timer node array. */ +static timer_node_t * mp_nodes = NULL; /**< Array of timer nodes. */ +static uint8_t m_user_array_size; /**< Size of timer user array. */ +static timer_user_t * mp_users; /**< Array of timer users. */ +static app_timer_id_t m_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 volatile uint64_t overflowBits; /**< The upper 40 bits of the 64-bit value returned by cnt_get() */ + + +/**@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) +{ + if (m_rtc1_running) { + return; + } + + NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk; + NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk | RTC_INTENSET_OVRFLW_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) +{ + if (!m_rtc1_running) { + return; + } + + NVIC_DisableIRQ(RTC1_IRQn); + + NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk; + NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk | RTC_INTENSET_OVRFLW_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(app_timer_id_t timer_id) +{ + timer_node_t * p_timer = &mp_nodes[timer_id]; + + if (m_timer_id_head == TIMER_NULL) + { + m_timer_id_head = timer_id; + } + else + { + if (p_timer->ticks_to_expire <= mp_nodes[m_timer_id_head].ticks_to_expire) + { + mp_nodes[m_timer_id_head].ticks_to_expire -= p_timer->ticks_to_expire; + + p_timer->next = m_timer_id_head; + m_timer_id_head = timer_id; + } + else + { + app_timer_id_t previous; + app_timer_id_t current; + uint32_t ticks_to_expire; + + ticks_to_expire = p_timer->ticks_to_expire; + previous = m_timer_id_head; + current = m_timer_id_head; + + while ((current != TIMER_NULL) && (ticks_to_expire > mp_nodes[current].ticks_to_expire)) + { + ticks_to_expire -= mp_nodes[current].ticks_to_expire; + previous = current; + current = mp_nodes[current].next; + } + + if (current != TIMER_NULL) + { + mp_nodes[current].ticks_to_expire -= ticks_to_expire; + } + + p_timer->ticks_to_expire = ticks_to_expire; + p_timer->next = current; + mp_nodes[previous].next = timer_id; + } + } +} + + +/**@brief Function for removing a timer from the timer queue. + * + * @param[in] timer_id Id of timer to remove. + */ +static void timer_list_remove(app_timer_id_t timer_id) +{ + app_timer_id_t previous; + app_timer_id_t current; + uint32_t timeout; + + // Find the timer's position in timer list + previous = m_timer_id_head; + current = previous; + + while (current != TIMER_NULL) + { + if (current == timer_id) + { + break; + } + previous = current; + current = mp_nodes[current].next; + } + + // Timer not in active list + if (current == TIMER_NULL) + { + return; + } + + // Timer is the first in the list + if (previous == current) + { + m_timer_id_head = mp_nodes[m_timer_id_head].next; + + // No more timers in the list. Disable RTC1. + if (m_timer_id_head == TIMER_NULL) + { + rtc1_stop(); + } + } + + // Remaining timeout between next timeout + timeout = mp_nodes[current].ticks_to_expire; + + // Link previous timer with next of this timer, i.e. removing the timer from list + mp_nodes[previous].next = mp_nodes[current].next; + + // If this is not the last timer, increment the next timer by this timer timeout + current = mp_nodes[previous].next; + if (current != TIMER_NULL) + { + mp_nodes[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 SWI0 interrupt. + */ +static void timer_list_handler_sched(void) +{ + NVIC_SetPendingIRQ(SWI0_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 (m_timer_id_head != TIMER_NULL) + { + app_timer_id_t timer_id; + 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 + timer_id = m_timer_id_head; + + // Expire all timers within ticks_elapsed and collect ticks_expired + while (timer_id != TIMER_NULL) + { + timer_node_t * p_timer; + + // Auto variable for current timer node + p_timer = &mp_nodes[timer_id]; + + // 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 + timer_id = p_timer->next; + + // Execute Task + timeout_handler_exec(p_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) +{ + app_timer_id_t timer_id_old_head; + uint8_t user_id; + + // Remember the old head, so as to decide if new compare needs to be set + timer_id_old_head = m_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 = &mp_nodes[p_user_op->timer_id]; + if (p_timer->is_running) + { + timer_list_remove(p_user_op->timer_id); + 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 (m_timer_id_head != TIMER_NULL) + { + timer_node_t * p_head = &mp_nodes[m_timer_id_head]; + + p_head->is_running = false; + m_timer_id_head = p_head->next; + } + break; + + default: + // No implementation needed. + break; + } + } + } + + // Detect change in head of the list + return (m_timer_id_head != timer_id_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, + app_timer_id_t * p_restart_list_head) +{ + uint32_t ticks_expired = 0; + + while (m_timer_id_head != TIMER_NULL) + { + timer_node_t * p_timer; + app_timer_id_t id_expired; + + // Auto variable for current timer node + p_timer = &mp_nodes[m_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 + id_expired = m_timer_id_head; + m_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 = id_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(app_timer_id_t restart_list_head) +{ + app_timer_id_t timer_id_old_head; + uint8_t user_id; + + // Remember the old head, so as to decide if new compare needs to be set + timer_id_old_head = m_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 ((restart_list_head != TIMER_NULL) || (p_user->first != p_user->last)) + { + app_timer_id_t id_start; + timer_node_t * p_timer; + + if (restart_list_head != TIMER_NULL) + { + id_start = restart_list_head; + p_timer = &mp_nodes[id_start]; + 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; + } + + id_start = p_user_op->timer_id; + p_timer = &mp_nodes[id_start]; + + 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; + } + + // 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 = TIMER_NULL; + + // Insert into list + timer_list_insert(id_start); + } + } + + return (m_timer_id_head != timer_id_old_head); +} + + +/**@brief Function for updating the Capture Compare register. + */ +static void compare_reg_update(app_timer_id_t timer_id_head_old) +{ + // Setup the timeout for timers on the head of the list + if (m_timer_id_head != TIMER_NULL) + { + uint32_t ticks_to_expire = mp_nodes[m_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) +{ + app_timer_id_t restart_list_head = TIMER_NULL; + uint32_t ticks_elapsed; + uint32_t ticks_previous; + bool ticks_have_elapsed; + bool compare_update; + app_timer_id_t timer_id_head_old; + + // Back up the previous known tick and previous list head + ticks_previous = m_ticks_latest; + timer_id_head_old = m_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, &restart_list_head); + compare_update = true; + } + + // Handle list insertions + if (list_insertions_handler(restart_list_head)) + { + compare_update = true; + } + + // Update compare register if necessary + if (compare_update) + { + compare_reg_update(timer_id_head_old); + } +} + + +/**@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, app_timer_id_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, app_timer_id_t * p_last_index) +{ + app_timer_id_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, + app_timer_id_t timer_id, + uint32_t timeout_initial, + uint32_t timeout_periodic, + void * p_context) +{ + app_timer_id_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->timer_id = timer_id; + 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, app_timer_id_t timer_id) +{ + app_timer_id_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->timer_id = timer_id; + + 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) +{ + app_timer_id_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->timer_id = TIMER_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; + if (NRF_RTC1->EVENTS_OVRFLW) { + overflowBits += (1 << 24); + } + NRF_RTC1->EVENTS_OVRFLW = 0; + + // Check for expired timers + timer_timeouts_check(); +} + +/**@brief Function for handling the SWI0 interrupt. + * + * @details Performs all updates to the timer list. + */ +void SWI0_IRQHandler(void) +{ + timer_list_handler(); +} + +uint32_t app_timer_init(uint32_t prescaler, + uint8_t max_timers, + 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) + { + 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 timer node array + m_node_array_size = max_timers; + mp_nodes = (timer_node_t *) p_buffer; + + for (i = 0; i < max_timers; i++) + { + mp_nodes[i].state = STATE_FREE; + mp_nodes[i].is_running = false; + } + + // Skip timer node array + p_buffer = &((uint8_t *)p_buffer)[max_timers * sizeof(timer_node_t)]; + + // Initialize users array + m_user_array_size = APP_TIMER_INT_LEVELS; + mp_users = (timer_user_t *) 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 = (timer_user_op_t *) p_buffer; + + // Skip operation queue + p_buffer = &((uint8_t *)p_buffer)[op_queues_size * sizeof(timer_user_op_t)]; + } + + m_timer_id_head = TIMER_NULL; + m_ticks_elapsed_q_read_ind = 0; + m_ticks_elapsed_q_write_ind = 0; + + NVIC_ClearPendingIRQ(SWI0_IRQn); + NVIC_SetPriority(SWI0_IRQn, SWI0_IRQ_PRI); + NVIC_EnableIRQ(SWI0_IRQn); + + rtc1_init(prescaler); + rtc1_start(); + + m_ticks_latest = rtc1_counter_get(); + + return NRF_SUCCESS; +} + + +uint32_t app_timer_create(app_timer_id_t * p_timer_id, + app_timer_mode_t mode, + app_timer_timeout_handler_t timeout_handler) +{ + int i; + + // Check state and parameters + if (mp_nodes == NULL) + { + return NRF_ERROR_INVALID_STATE; + } + if (timeout_handler == NULL) + { + return NRF_ERROR_INVALID_PARAM; + } + if (p_timer_id == NULL) + { + return NRF_ERROR_INVALID_PARAM; + } + + // Find free timer + for (i = 0; i < m_node_array_size; i++) + { + if (mp_nodes[i].state == STATE_FREE) + { + mp_nodes[i].state = STATE_ALLOCATED; + mp_nodes[i].mode = mode; + mp_nodes[i].p_timeout_handler = timeout_handler; + + *p_timer_id = i; + return NRF_SUCCESS; + } + } + + return NRF_ERROR_NO_MEM; +} + + +/**@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; + + // Check state and parameters + if (mp_nodes == NULL) + { + return NRF_ERROR_INVALID_STATE; + } + if ((timer_id >= m_node_array_size) || (timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS)) + { + return NRF_ERROR_INVALID_PARAM; + } + if (mp_nodes[timer_id].state != STATE_ALLOCATED) + { + return NRF_ERROR_INVALID_STATE; + } + + // Schedule timer start operation + timeout_periodic = (mp_nodes[timer_id].mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0; + + return timer_start_op_schedule(user_id_get(), + timer_id, + timeout_ticks, + timeout_periodic, + p_context); +} + + +uint32_t app_timer_stop(app_timer_id_t timer_id) +{ + // Check state and parameters + if (mp_nodes == NULL) + { + return NRF_ERROR_INVALID_STATE; + } + if (timer_id >= m_node_array_size) + { + return NRF_ERROR_INVALID_PARAM; + } + if (mp_nodes[timer_id].state != STATE_ALLOCATED) + { + return NRF_ERROR_INVALID_STATE; + } + + // Schedule timer stop operation + return timer_stop_op_schedule(user_id_get(), timer_id); +} + + +uint32_t app_timer_stop_all(void) +{ + // Check state + if (mp_nodes == NULL) + { + return NRF_ERROR_INVALID_STATE; + } + + return timer_stop_all_op_schedule(user_id_get()); +} + + +uint32_t app_timer_cnt_get(uint64_t * p_ticks) +{ + *p_ticks = overflowBits | 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; +}
--- a/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk/app_common/app_error.h Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk/app_common/app_error.h Fri Aug 29 17:15:07 2014 +0100 @@ -48,7 +48,7 @@ #define APP_ERROR_HANDLER(ERR_CODE) \ do \ { \ - app_error_handler((ERR_CODE), __LINE__, (uint8_t*) __FILE__); \ + /* app_error_handler((ERR_CODE), __LINE__, (uint8_t*) __FILE__); */ \ } while (0) /**@brief Macro for calling error handler function if supplied error code any other than NRF_SUCCESS.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk/nrf_delay.h Fri Aug 29 17:15:07 2014 +0100 @@ -0,0 +1,74 @@ +#ifndef _NRF_DELAY_H +#define _NRF_DELAY_H + +// #include "nrf.h" + +/*lint --e{438, 522} "Variable not used" "Function lacks side-effects" */ +#if defined ( __CC_ARM ) +static __ASM void __INLINE nrf_delay_us(uint32_t volatile number_of_us) +{ +loop + SUBS R0, R0, #1 + NOP + NOP + NOP + NOP + NOP + NOP + NOP + NOP + NOP + NOP + NOP + NOP + BNE loop + BX LR +} +#elif defined ( __ICCARM__ ) +static void __INLINE nrf_delay_us(uint32_t volatile number_of_us) +{ +__ASM ( +"loop:\n\t" + " SUBS R0, R0, #1\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " NOP\n\t" + " BNE loop\n\t"); +} +#elif defined ( __GNUC__ ) +static void __INLINE nrf_delay_us(uint32_t volatile number_of_us) +{ + do + { + __ASM volatile ( + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + "NOP\n\t" + ); + } while (--number_of_us); +} +#endif + +void nrf_delay_ms(uint32_t volatile number_of_ms); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk/sd_common/app_util_platform.h Fri Aug 29 17:15:07 2014 +0100 @@ -0,0 +1,110 @@ +/* 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. + * + */ + +/**@file + * + * @defgroup app_util_platform Utility Functions and Definitions (Platform) + * @{ + * @ingroup app_common + * + * @brief Various types and definitions available to all applications when using SoftDevice. + */ + +#ifndef APP_UTIL_PLATFORM_H__ +#define APP_UTIL_PLATFORM_H__ + +#include <stdint.h> +#include "compiler_abstraction.h" +#include "nrf51.h" +#include "app_error.h" + +/**@brief The interrupt priorities available to the application while the SoftDevice is active. */ +typedef enum +{ + APP_IRQ_PRIORITY_HIGH = 1, + APP_IRQ_PRIORITY_LOW = 3 +} app_irq_priority_t; + +#define NRF_APP_PRIORITY_THREAD 4 /**< "Interrupt level" when running in Thread Mode. */ + +/**@cond NO_DOXYGEN */ +#define EXTERNAL_INT_VECTOR_OFFSET 16 +/**@endcond */ + +#define PACKED(TYPE) __packed TYPE + +/**@brief Macro for entering a critical region. + * + * @note Due to implementation details, there must exist one and only one call to + * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located + * in the same scope. + */ +#define CRITICAL_REGION_ENTER() \ + { \ + uint8_t IS_NESTED_CRITICAL_REGION = 0; \ + uint32_t CURRENT_INT_PRI = current_int_priority_get(); \ + if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ + { \ + uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION); \ + if (ERR_CODE == NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ + { \ + __disable_irq(); \ + } \ + else \ + { \ + APP_ERROR_CHECK(ERR_CODE); \ + } \ + } + +/**@brief Macro for leaving a critical region. + * + * @note Due to implementation details, there must exist one and only one call to + * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located + * in the same scope. + */ +#define CRITICAL_REGION_EXIT() \ + if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ + { \ + uint32_t ERR_CODE; \ + __enable_irq(); \ + ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION); \ + if (ERR_CODE != NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ + { \ + APP_ERROR_CHECK(ERR_CODE); \ + } \ + } \ + } + +/**@brief Function for finding the current interrupt level. + * + * @return Current interrupt level. + * @retval APP_IRQ_PRIORITY_HIGH We are running in Application High interrupt level. + * @retval APP_IRQ_PRIORITY_LOW We are running in Application Low interrupt level. + * @retval APP_IRQ_PRIORITY_THREAD We are running in Thread Mode. + */ +static __INLINE uint8_t current_int_priority_get(void) +{ + uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk); + if (isr_vector_num > 0) + { + int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET); + return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF); + } + else + { + return NRF_APP_PRIORITY_THREAD; + } +} + +#endif // APP_UTIL_PLATFORM_H__ + +/** @} */
--- a/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -17,113 +17,88 @@ #include "us_ticker_api.h" #include "cmsis.h" #include "PeripheralNames.h" - -#define US_TICKER_TIMER NRF_TIMER1 -#define US_TICKER_TIMER_IRQn TIMER1_IRQn - -int us_ticker_inited = 0; -volatile uint16_t overflow = 0; //overflow value that forms the upper 16 bits of the counter -volatile uint16_t timeStamp = 0; - -#ifdef __cplusplus -extern "C" { -#endif +#include "app_timer.h" -void TIMER1_IRQHandler(void) -{ - if ((US_TICKER_TIMER->EVENTS_COMPARE[1] != 0) && - ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0)) { - US_TICKER_TIMER->EVENTS_COMPARE[1] = 0; - overflow++; - US_TICKER_TIMER->CC[1] = 0xFFFF; - if (timeStamp>0) { - timeStamp--; - if (timeStamp==0) { - us_ticker_clear_interrupt(); - us_ticker_disable_interrupt(); - us_ticker_irq_handler(); - return; - } - } - } - if ((US_TICKER_TIMER->EVENTS_COMPARE[0] != 0) && - ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) { - us_ticker_clear_interrupt(); - us_ticker_disable_interrupt(); - if (timeStamp==0) { - us_ticker_irq_handler(); - } - } -} +static bool us_ticker_inited = false; +static volatile bool us_ticker_appTimerRunning = false; +static app_timer_id_t us_ticker_appTimerID = TIMER_NULL; -#ifdef __cplusplus -} -#endif void us_ticker_init(void) { - if (us_ticker_inited && US_TICKER_TIMER->POWER) { + if (us_ticker_inited) { return; } - us_ticker_inited = 1; - - US_TICKER_TIMER->POWER = 0; - US_TICKER_TIMER->POWER = 1; - - US_TICKER_TIMER->MODE = TIMER_MODE_MODE_Timer; + APP_TIMER_INIT(0 /*CFG_TIMER_PRESCALER*/ , 1 /*CFG_TIMER_MAX_INSTANCE*/, 1 /*CFG_TIMER_OPERATION_QUEUE_SIZE*/, false /*CFG_SCHEDULER_ENABLE*/); - US_TICKER_TIMER->PRESCALER = 4; - US_TICKER_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit; - US_TICKER_TIMER->TASKS_CLEAR = 1; - US_TICKER_TIMER->CC[1] = 0xFFFF; - US_TICKER_TIMER->INTENSET = TIMER_INTENSET_COMPARE1_Set << TIMER_INTENSET_COMPARE1_Pos; - - NVIC_SetPriority(US_TICKER_TIMER_IRQn, 3); - NVIC_EnableIRQ(US_TICKER_TIMER_IRQn); - - US_TICKER_TIMER->TASKS_START = 0x01; + us_ticker_inited = true; } uint32_t us_ticker_read() { - if (!us_ticker_inited || (US_TICKER_TIMER->POWER==0)) { + if (!us_ticker_inited) { + us_ticker_init(); + } + + timestamp_t value; + app_timer_cnt_get(&value); /* This returns the RTC counter (which is fed by the 32khz crystal clock source) */ + return (uint32_t)((value * 1000000) / APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value. + * This is only as precise as the 32khz low-freq + * clock source, but could be adequate.*/ +} + +/* An adaptor to interface us_ticker_irq_handler with the app_timer callback. + * Needed because the irq_handler() doesn't take any parameter.*/ +static void us_ticker_app_timer_callback(void *context) +{ + us_ticker_appTimerRunning = false; + us_ticker_irq_handler(); +} + +void us_ticker_set_interrupt(timestamp_t timestamp) +{ + if (!us_ticker_inited) { us_ticker_init(); } - uint16_t bufferedOverFlow = overflow; - US_TICKER_TIMER->TASKS_CAPTURE[2] = 1; - - if (overflow!=bufferedOverFlow) { - bufferedOverFlow = overflow; - US_TICKER_TIMER->TASKS_CAPTURE[2] = 1; + if (us_ticker_appTimerID == TIMER_NULL) { + if (app_timer_create(&us_ticker_appTimerID, APP_TIMER_MODE_SINGLE_SHOT, us_ticker_app_timer_callback) != NRF_SUCCESS) { + /* placeholder to do something to recover from error */ + return; + } } - return (((uint32_t)bufferedOverFlow << 16) | US_TICKER_TIMER->CC[2]); -} -void us_ticker_set_interrupt(unsigned int timestamp) -{ - if (!us_ticker_inited || (US_TICKER_TIMER->POWER == 0)) { - us_ticker_init(); + if (us_ticker_appTimerRunning) { + return; } - US_TICKER_TIMER->TASKS_CAPTURE[0] = 1; - uint16_t tsUpper16 = (uint16_t)((timestamp - us_ticker_read()) >> 16); - if (tsUpper16>0) { - if ((timeStamp ==0) || (timeStamp> tsUpper16)) { - timeStamp = tsUpper16; + timestamp_t currentCounter64; + app_timer_cnt_get(¤tCounter64); + uint32_t currentCounter = currentCounter64 & MAX_RTC_COUNTER_VAL; + uint32_t targetCounter = ((uint32_t)((timestamp * (uint64_t)APP_TIMER_CLOCK_FREQ) / 1000000) + 1) & MAX_RTC_COUNTER_VAL; + uint32_t ticksToCount = (targetCounter >= currentCounter) ? + (targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter); + if (ticksToCount > 0) { + uint32_t rc; + rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/); + if (rc != NRF_SUCCESS) { + /* placeholder to do something to recover from error */ + return; } - } else { - US_TICKER_TIMER->INTENSET |= TIMER_INTENSET_COMPARE0_Set << TIMER_INTENSET_COMPARE0_Pos; - US_TICKER_TIMER->CC[0] += timestamp - us_ticker_read(); + us_ticker_appTimerRunning = true; } } void us_ticker_disable_interrupt(void) { - US_TICKER_TIMER->INTENCLR = TIMER_INTENCLR_COMPARE0_Clear << TIMER_INTENCLR_COMPARE0_Pos; + if (us_ticker_appTimerRunning) { + app_timer_stop(us_ticker_appTimerID); + } } void us_ticker_clear_interrupt(void) { - US_TICKER_TIMER->EVENTS_COMPARE[0] = 0; + if (us_ticker_appTimerRunning) { + app_timer_stop(us_ticker_appTimerID); + } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -46,9 +46,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR0 = timestamp; + US_TICKER_TIMER->MR0 = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -46,9 +46,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR0 = timestamp; + US_TICKER_TIMER->MR0 = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -46,9 +46,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR0 = timestamp; + US_TICKER_TIMER->MR0 = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -46,9 +46,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR0 = timestamp; + US_TICKER_TIMER->MR0 = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -58,7 +58,7 @@ return (uint32_t)temp; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { uint64_t temp = ((uint64_t)timestamp * (SystemCoreClock/1000000)); LPC_RIT->COMPVAL = (temp & 0xFFFFFFFFL); LPC_RIT->COMPVAL_H = ((temp >> 32)& 0x0000FFFFL);
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,9 +48,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR0 = timestamp; + US_TICKER_TIMER->MR0 = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,9 +48,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR0 = timestamp; + US_TICKER_TIMER->MR0 = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,9 +48,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR0 = timestamp; + US_TICKER_TIMER->MR0 = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,9 +48,9 @@ return US_TICKER_TIMER->TC; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->MR[0] = timestamp; + US_TICKER_TIMER->MR[0] = (uint32_t)timestamp; // enable match interrupt US_TICKER_TIMER->MCR |= 1; }
--- a/targets/hal/TARGET_NXP/TARGET_LPC81X/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC81X/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -55,13 +55,13 @@ return LPC_SCT->COUNT_U; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // halt the counter: // - setting bit 2 of the CTRL register LPC_SCT->CTRL_L |= (1 << 2); // set timestamp in compare register - LPC_SCT->MATCH[0].U = timestamp; + LPC_SCT->MATCH[0].U = (uint32_t)timestamp; // unhalt the counter: // - clearing bit 2 of the CTRL register
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -90,7 +90,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -131,8 +131,8 @@ return counter2; } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); uint16_t cval = TIM_MST->CNT; if (delta <= 0) { // This event was in the past
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -92,7 +92,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -133,8 +133,8 @@ return counter2; } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); uint16_t cval = TIM_MST->CNT; if (delta <= 0) { // This event was in the past
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,7 +48,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -65,9 +65,9 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - TIM_SetCompare1(TIM_MST, timestamp); + TIM_SetCompare1(TIM_MST, (uint32_t)timestamp); // Enable IT TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -90,7 +90,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -131,8 +131,8 @@ return counter2; } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); uint16_t cval = TIM_MST->CNT; if (delta <= 0) { // This event was in the past
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -50,7 +50,7 @@ // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFFFFFF; - TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimMasterHandle.Init.RepetitionCounter = 0; @@ -68,9 +68,9 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, timestamp); + __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp); // Enable IT __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -83,7 +83,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -121,8 +121,8 @@ return counter2; } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); uint16_t cval = TIM_MST->CNT; if (delta <= 0) { // This event was in the past
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,7 +48,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -65,9 +65,9 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - TIM_SetCompare1(TIM_MST, timestamp); + TIM_SetCompare1(TIM_MST, (uint32_t)timestamp); // Enable IT TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F334R8/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F334R8/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -51,7 +51,7 @@ // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFFFFFF; - TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimMasterHandle.Init.RepetitionCounter = 0; @@ -70,10 +70,10 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, timestamp); + __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp); // Enable IT __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,9 +48,9 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, timestamp); + __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp); // Enable IT __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F411RE/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F411RE/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -50,10 +50,10 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, timestamp); + __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp); // Enable IT __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -85,7 +85,7 @@ // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFF; - TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TimMasterHandle); @@ -123,8 +123,8 @@ return counter2; } -void us_ticker_set_interrupt(unsigned int timestamp) { - int delta = (int)(timestamp - us_ticker_read()); +void us_ticker_set_interrupt(timestamp_t timestamp) { + int delta = (int)((uint32_t)timestamp - us_ticker_read()); uint16_t cval = TIM_MST->CNT; if (delta <= 0) { // This event was in the past
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,7 +48,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -65,9 +65,9 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - TIM_SetCompare1(TIM_MST, timestamp); + TIM_SetCompare1(TIM_MST, (uint32_t)timestamp); // Enable IT TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE); }
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,7 +48,7 @@ // Configure time base TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; - TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick + TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure); @@ -65,9 +65,9 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - TIM_SetCompare1(TIM_MST, timestamp); + TIM_SetCompare1(TIM_MST, (uint32_t)timestamp); // Enable IT TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE); }
--- a/targets/hal/TARGET_STM/TARGET_STM32F407VG/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F407VG/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -48,9 +48,9 @@ return TIM_MST->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // Set new output compare value - __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, timestamp); + __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp); // Enable IT __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); }
--- a/targets/hal/TARGET_STM/TARGET_STM32F4XX/us_ticker.c Fri Aug 29 11:45:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F4XX/us_ticker.c Fri Aug 29 17:15:07 2014 +0100 @@ -47,9 +47,9 @@ return US_TICKER_TIMER->CNT; } -void us_ticker_set_interrupt(unsigned int timestamp) { +void us_ticker_set_interrupt(timestamp_t timestamp) { // set match value - US_TICKER_TIMER->CCR1 = timestamp; + US_TICKER_TIMER->CCR1 = (uint32_t)timestamp; // enable compare interrupt US_TICKER_TIMER->DIER |= TIM_DIER_CC1IE; }