To get started with Seeed Tiny BLE, include detecting motion, button and battery level.

Dependencies:   BLE_API eMPL_MPU6050 mbed nRF51822

Committer:
yihui
Date:
Wed Apr 22 07:47:17 2015 +0000
Revision:
1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:fc2f9d636751 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
yihui 1:fc2f9d636751 2 *
yihui 1:fc2f9d636751 3 * The information contained herein is property of Nordic Semiconductor ASA.
yihui 1:fc2f9d636751 4 * Terms and conditions of usage are described in detail in NORDIC
yihui 1:fc2f9d636751 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
yihui 1:fc2f9d636751 6 *
yihui 1:fc2f9d636751 7 * Licensees are granted free, non-transferable use of the information. NO
yihui 1:fc2f9d636751 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
yihui 1:fc2f9d636751 9 * the file.
yihui 1:fc2f9d636751 10 *
yihui 1:fc2f9d636751 11 */
yihui 1:fc2f9d636751 12
yihui 1:fc2f9d636751 13 #include "app_scheduler.h"
yihui 1:fc2f9d636751 14 #include <stdlib.h>
yihui 1:fc2f9d636751 15 #include <stdint.h>
yihui 1:fc2f9d636751 16 #include <string.h>
yihui 1:fc2f9d636751 17 #include "nrf_soc.h"
yihui 1:fc2f9d636751 18 #include "nrf_assert.h"
yihui 1:fc2f9d636751 19 #include "app_util.h"
yihui 1:fc2f9d636751 20 #include "app_util_platform.h"
yihui 1:fc2f9d636751 21
yihui 1:fc2f9d636751 22 /**@brief Structure for holding a scheduled event header. */
yihui 1:fc2f9d636751 23 typedef struct
yihui 1:fc2f9d636751 24 {
yihui 1:fc2f9d636751 25 app_sched_event_handler_t handler; /**< Pointer to event handler to receive the event. */
yihui 1:fc2f9d636751 26 uint16_t event_data_size; /**< Size of event data. */
yihui 1:fc2f9d636751 27 } event_header_t;
yihui 1:fc2f9d636751 28
yihui 1:fc2f9d636751 29 STATIC_ASSERT(sizeof(event_header_t) <= APP_SCHED_EVENT_HEADER_SIZE);
yihui 1:fc2f9d636751 30
yihui 1:fc2f9d636751 31 static event_header_t * m_queue_event_headers; /**< Array for holding the queue event headers. */
yihui 1:fc2f9d636751 32 static uint8_t * m_queue_event_data; /**< Array for holding the queue event data. */
yihui 1:fc2f9d636751 33 static volatile uint8_t m_queue_start_index; /**< Index of queue entry at the start of the queue. */
yihui 1:fc2f9d636751 34 static volatile uint8_t m_queue_end_index; /**< Index of queue entry at the end of the queue. */
yihui 1:fc2f9d636751 35 static uint16_t m_queue_event_size; /**< Maximum event size in queue. */
yihui 1:fc2f9d636751 36 static uint16_t m_queue_size; /**< Number of queue entries. */
yihui 1:fc2f9d636751 37
yihui 1:fc2f9d636751 38 /**@brief Macro for checking if a queue is full. */
yihui 1:fc2f9d636751 39 #define APP_SCHED_QUEUE_FULL() (next_index(m_queue_end_index) == m_queue_start_index)
yihui 1:fc2f9d636751 40
yihui 1:fc2f9d636751 41 /**@brief Macro for checking if a queue is empty. */
yihui 1:fc2f9d636751 42 #define APP_SCHED_QUEUE_EMPTY() (m_queue_end_index == m_queue_start_index)
yihui 1:fc2f9d636751 43
yihui 1:fc2f9d636751 44
yihui 1:fc2f9d636751 45 /**@brief Function for incrementing a queue index, and handle wrap-around.
yihui 1:fc2f9d636751 46 *
yihui 1:fc2f9d636751 47 * @param[in] index Old index.
yihui 1:fc2f9d636751 48 *
yihui 1:fc2f9d636751 49 * @return New (incremented) index.
yihui 1:fc2f9d636751 50 */
yihui 1:fc2f9d636751 51 static __INLINE uint8_t next_index(uint8_t index)
yihui 1:fc2f9d636751 52 {
yihui 1:fc2f9d636751 53 return (index < m_queue_size) ? (index + 1) : 0;
yihui 1:fc2f9d636751 54 }
yihui 1:fc2f9d636751 55
yihui 1:fc2f9d636751 56
yihui 1:fc2f9d636751 57 uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event_buffer)
yihui 1:fc2f9d636751 58 {
yihui 1:fc2f9d636751 59 uint16_t data_start_index = (queue_size + 1) * sizeof(event_header_t);
yihui 1:fc2f9d636751 60
yihui 1:fc2f9d636751 61 // Check that buffer is correctly aligned
yihui 1:fc2f9d636751 62 if (!is_word_aligned(p_event_buffer))
yihui 1:fc2f9d636751 63 {
yihui 1:fc2f9d636751 64 return NRF_ERROR_INVALID_PARAM;
yihui 1:fc2f9d636751 65 }
yihui 1:fc2f9d636751 66
yihui 1:fc2f9d636751 67 // Initialize event scheduler
yihui 1:fc2f9d636751 68 m_queue_event_headers = p_event_buffer;
yihui 1:fc2f9d636751 69 m_queue_event_data = &((uint8_t *)p_event_buffer)[data_start_index];
yihui 1:fc2f9d636751 70 m_queue_end_index = 0;
yihui 1:fc2f9d636751 71 m_queue_start_index = 0;
yihui 1:fc2f9d636751 72 m_queue_event_size = event_size;
yihui 1:fc2f9d636751 73 m_queue_size = queue_size;
yihui 1:fc2f9d636751 74
yihui 1:fc2f9d636751 75 return NRF_SUCCESS;
yihui 1:fc2f9d636751 76 }
yihui 1:fc2f9d636751 77
yihui 1:fc2f9d636751 78
yihui 1:fc2f9d636751 79 uint32_t app_sched_event_put(void * p_event_data,
yihui 1:fc2f9d636751 80 uint16_t event_data_size,
yihui 1:fc2f9d636751 81 app_sched_event_handler_t handler)
yihui 1:fc2f9d636751 82 {
yihui 1:fc2f9d636751 83 uint32_t err_code;
yihui 1:fc2f9d636751 84
yihui 1:fc2f9d636751 85 if (event_data_size <= m_queue_event_size)
yihui 1:fc2f9d636751 86 {
yihui 1:fc2f9d636751 87 uint16_t event_index = 0xFFFF;
yihui 1:fc2f9d636751 88
yihui 1:fc2f9d636751 89 CRITICAL_REGION_ENTER();
yihui 1:fc2f9d636751 90
yihui 1:fc2f9d636751 91 if (!APP_SCHED_QUEUE_FULL())
yihui 1:fc2f9d636751 92 {
yihui 1:fc2f9d636751 93 event_index = m_queue_end_index;
yihui 1:fc2f9d636751 94 m_queue_end_index = next_index(m_queue_end_index);
yihui 1:fc2f9d636751 95 }
yihui 1:fc2f9d636751 96
yihui 1:fc2f9d636751 97 CRITICAL_REGION_EXIT();
yihui 1:fc2f9d636751 98
yihui 1:fc2f9d636751 99 if (event_index != 0xFFFF)
yihui 1:fc2f9d636751 100 {
yihui 1:fc2f9d636751 101 // NOTE: This can be done outside the critical region since the event consumer will
yihui 1:fc2f9d636751 102 // always be called from the main loop, and will thus never interrupt this code.
yihui 1:fc2f9d636751 103 m_queue_event_headers[event_index].handler = handler;
yihui 1:fc2f9d636751 104 if ((p_event_data != NULL) && (event_data_size > 0))
yihui 1:fc2f9d636751 105 {
yihui 1:fc2f9d636751 106 memcpy(&m_queue_event_data[event_index * m_queue_event_size],
yihui 1:fc2f9d636751 107 p_event_data,
yihui 1:fc2f9d636751 108 event_data_size);
yihui 1:fc2f9d636751 109 m_queue_event_headers[event_index].event_data_size = event_data_size;
yihui 1:fc2f9d636751 110 }
yihui 1:fc2f9d636751 111 else
yihui 1:fc2f9d636751 112 {
yihui 1:fc2f9d636751 113 m_queue_event_headers[event_index].event_data_size = 0;
yihui 1:fc2f9d636751 114 }
yihui 1:fc2f9d636751 115
yihui 1:fc2f9d636751 116 err_code = NRF_SUCCESS;
yihui 1:fc2f9d636751 117 }
yihui 1:fc2f9d636751 118 else
yihui 1:fc2f9d636751 119 {
yihui 1:fc2f9d636751 120 err_code = NRF_ERROR_NO_MEM;
yihui 1:fc2f9d636751 121 }
yihui 1:fc2f9d636751 122 }
yihui 1:fc2f9d636751 123 else
yihui 1:fc2f9d636751 124 {
yihui 1:fc2f9d636751 125 err_code = NRF_ERROR_INVALID_LENGTH;
yihui 1:fc2f9d636751 126 }
yihui 1:fc2f9d636751 127
yihui 1:fc2f9d636751 128 return err_code;
yihui 1:fc2f9d636751 129 }
yihui 1:fc2f9d636751 130
yihui 1:fc2f9d636751 131
yihui 1:fc2f9d636751 132 /**@brief Function for reading the next event from specified event queue.
yihui 1:fc2f9d636751 133 *
yihui 1:fc2f9d636751 134 * @param[out] pp_event_data Pointer to pointer to event data.
yihui 1:fc2f9d636751 135 * @param[out] p_event_data_size Pointer to size of event data.
yihui 1:fc2f9d636751 136 * @param[out] p_event_handler Pointer to event handler function pointer.
yihui 1:fc2f9d636751 137 *
yihui 1:fc2f9d636751 138 * @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty.
yihui 1:fc2f9d636751 139 */
yihui 1:fc2f9d636751 140 static uint32_t app_sched_event_get(void ** pp_event_data,
yihui 1:fc2f9d636751 141 uint16_t * p_event_data_size,
yihui 1:fc2f9d636751 142 app_sched_event_handler_t * p_event_handler)
yihui 1:fc2f9d636751 143 {
yihui 1:fc2f9d636751 144 uint32_t err_code = NRF_ERROR_NOT_FOUND;
yihui 1:fc2f9d636751 145
yihui 1:fc2f9d636751 146 if (!APP_SCHED_QUEUE_EMPTY())
yihui 1:fc2f9d636751 147 {
yihui 1:fc2f9d636751 148 uint16_t event_index;
yihui 1:fc2f9d636751 149
yihui 1:fc2f9d636751 150 // NOTE: There is no need for a critical region here, as this function will only be called
yihui 1:fc2f9d636751 151 // from app_sched_execute() from inside the main loop, so it will never interrupt
yihui 1:fc2f9d636751 152 // app_sched_event_put(). Also, updating of (i.e. writing to) the start index will be
yihui 1:fc2f9d636751 153 // an atomic operation.
yihui 1:fc2f9d636751 154 event_index = m_queue_start_index;
yihui 1:fc2f9d636751 155 m_queue_start_index = next_index(m_queue_start_index);
yihui 1:fc2f9d636751 156
yihui 1:fc2f9d636751 157 *pp_event_data = &m_queue_event_data[event_index * m_queue_event_size];
yihui 1:fc2f9d636751 158 *p_event_data_size = m_queue_event_headers[event_index].event_data_size;
yihui 1:fc2f9d636751 159 *p_event_handler = m_queue_event_headers[event_index].handler;
yihui 1:fc2f9d636751 160
yihui 1:fc2f9d636751 161 err_code = NRF_SUCCESS;
yihui 1:fc2f9d636751 162 }
yihui 1:fc2f9d636751 163
yihui 1:fc2f9d636751 164 return err_code;
yihui 1:fc2f9d636751 165 }
yihui 1:fc2f9d636751 166
yihui 1:fc2f9d636751 167
yihui 1:fc2f9d636751 168 void app_sched_execute(void)
yihui 1:fc2f9d636751 169 {
yihui 1:fc2f9d636751 170 void * p_event_data;
yihui 1:fc2f9d636751 171 uint16_t event_data_size;
yihui 1:fc2f9d636751 172 app_sched_event_handler_t event_handler;
yihui 1:fc2f9d636751 173
yihui 1:fc2f9d636751 174 // Get next event (if any), and execute handler
yihui 1:fc2f9d636751 175 while ((app_sched_event_get(&p_event_data, &event_data_size, &event_handler) == NRF_SUCCESS))
yihui 1:fc2f9d636751 176 {
yihui 1:fc2f9d636751 177 event_handler(p_event_data, event_data_size);
yihui 1:fc2f9d636751 178 }
yihui 1:fc2f9d636751 179 }