Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:56 2016 +0100
Revision:
28:041dac1366b2
Parent:
20:a90c48eb1d30
Child:
29:286940b7ee5a
Synchronized with git rev 012b8118
Author: Liyou Zhou
Pull in files from sdk 10.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 28:041dac1366b2 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
vcoubard 28:041dac1366b2 2 *
vcoubard 28:041dac1366b2 3 * The information contained herein is property of Nordic Semiconductor ASA.
vcoubard 28:041dac1366b2 4 * Terms and conditions of usage are described in detail in NORDIC
vcoubard 28:041dac1366b2 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
vcoubard 28:041dac1366b2 6 *
vcoubard 28:041dac1366b2 7 * Licensees are granted free, non-transferable use of the information. NO
vcoubard 28:041dac1366b2 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
vcoubard 28:041dac1366b2 9 * the file.
vcoubard 28:041dac1366b2 10 *
Vincent Coubard 0:f2542974c862 11 */
Vincent Coubard 0:f2542974c862 12
Vincent Coubard 0:f2542974c862 13 #include "hci_mem_pool.h"
Vincent Coubard 0:f2542974c862 14 #include "hci_mem_pool_internal.h"
Vincent Coubard 0:f2542974c862 15 #include <stdbool.h>
Vincent Coubard 0:f2542974c862 16 #include <stdio.h>
Vincent Coubard 0:f2542974c862 17
Vincent Coubard 0:f2542974c862 18 /**@brief RX buffer element instance structure.
Vincent Coubard 0:f2542974c862 19 */
Vincent Coubard 0:f2542974c862 20 typedef struct
Vincent Coubard 0:f2542974c862 21 {
Vincent Coubard 0:f2542974c862 22 uint8_t rx_buffer[RX_BUF_SIZE]; /**< RX buffer memory array. */
Vincent Coubard 0:f2542974c862 23 uint32_t length; /**< Length of the RX buffer memory array. */
Vincent Coubard 0:f2542974c862 24 } rx_buffer_elem_t;
Vincent Coubard 0:f2542974c862 25
Vincent Coubard 0:f2542974c862 26 /**@brief RX buffer queue element instance structure.
Vincent Coubard 0:f2542974c862 27 */
Vincent Coubard 0:f2542974c862 28 typedef struct
Vincent Coubard 0:f2542974c862 29 {
Vincent Coubard 0:f2542974c862 30 rx_buffer_elem_t * p_buffer; /**< Pointer to RX buffer element. */
Vincent Coubard 0:f2542974c862 31 uint32_t free_window_count; /**< Free space element count. */
Vincent Coubard 0:f2542974c862 32 uint32_t free_available_count; /**< Free area element count. */
Vincent Coubard 0:f2542974c862 33 uint32_t read_available_count; /**< Read area element count. */
Vincent Coubard 0:f2542974c862 34 uint32_t write_index; /**< Write position index. */
Vincent Coubard 0:f2542974c862 35 uint32_t read_index; /**< Read position index. */
Vincent Coubard 0:f2542974c862 36 uint32_t free_index; /**< Free position index. */
Vincent Coubard 0:f2542974c862 37 } rx_buffer_queue_t;
Vincent Coubard 0:f2542974c862 38
Vincent Coubard 0:f2542974c862 39 static bool m_is_tx_allocated; /**< Boolean value to determine if the TX buffer is allocated. */
Vincent Coubard 0:f2542974c862 40 static rx_buffer_elem_t m_rx_buffer_elem_queue[RX_BUF_QUEUE_SIZE]; /**< RX buffer element instances. */
Vincent Coubard 0:f2542974c862 41 static rx_buffer_queue_t m_rx_buffer_queue; /**< RX buffer queue element instance. */
Vincent Coubard 0:f2542974c862 42
Vincent Coubard 0:f2542974c862 43
Vincent Coubard 0:f2542974c862 44 uint32_t hci_mem_pool_open(void)
Vincent Coubard 0:f2542974c862 45 {
Vincent Coubard 0:f2542974c862 46 m_is_tx_allocated = false;
Vincent Coubard 0:f2542974c862 47 m_rx_buffer_queue.p_buffer = m_rx_buffer_elem_queue;
Vincent Coubard 0:f2542974c862 48 m_rx_buffer_queue.free_window_count = RX_BUF_QUEUE_SIZE;
Vincent Coubard 0:f2542974c862 49 m_rx_buffer_queue.free_available_count = 0;
Vincent Coubard 0:f2542974c862 50 m_rx_buffer_queue.read_available_count = 0;
Vincent Coubard 0:f2542974c862 51 m_rx_buffer_queue.write_index = 0;
Vincent Coubard 0:f2542974c862 52 m_rx_buffer_queue.read_index = 0;
Vincent Coubard 0:f2542974c862 53 m_rx_buffer_queue.free_index = 0;
Vincent Coubard 0:f2542974c862 54
Vincent Coubard 0:f2542974c862 55 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 56 }
Vincent Coubard 0:f2542974c862 57
Vincent Coubard 0:f2542974c862 58
Vincent Coubard 0:f2542974c862 59 uint32_t hci_mem_pool_close(void)
Vincent Coubard 0:f2542974c862 60 {
Vincent Coubard 0:f2542974c862 61 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 62 }
Vincent Coubard 0:f2542974c862 63
Vincent Coubard 0:f2542974c862 64
Vincent Coubard 0:f2542974c862 65 uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer)
Vincent Coubard 0:f2542974c862 66 {
Vincent Coubard 0:f2542974c862 67 static uint8_t tx_buffer[TX_BUF_SIZE];
Vincent Coubard 0:f2542974c862 68
Vincent Coubard 0:f2542974c862 69 uint32_t err_code;
Vincent Coubard 0:f2542974c862 70
Vincent Coubard 0:f2542974c862 71 if (pp_buffer == NULL)
Vincent Coubard 0:f2542974c862 72 {
Vincent Coubard 0:f2542974c862 73 return NRF_ERROR_NULL;
Vincent Coubard 0:f2542974c862 74 }
Vincent Coubard 0:f2542974c862 75
Vincent Coubard 0:f2542974c862 76 if (!m_is_tx_allocated)
Vincent Coubard 0:f2542974c862 77 {
Vincent Coubard 0:f2542974c862 78 m_is_tx_allocated = true;
Vincent Coubard 0:f2542974c862 79 *pp_buffer = tx_buffer;
Vincent Coubard 0:f2542974c862 80 err_code = NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 81 }
Vincent Coubard 0:f2542974c862 82 else
Vincent Coubard 0:f2542974c862 83 {
Vincent Coubard 0:f2542974c862 84 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 0:f2542974c862 85 }
Vincent Coubard 0:f2542974c862 86
Vincent Coubard 0:f2542974c862 87 return err_code;
Vincent Coubard 0:f2542974c862 88 }
Vincent Coubard 0:f2542974c862 89
Vincent Coubard 0:f2542974c862 90
Vincent Coubard 0:f2542974c862 91 uint32_t hci_mem_pool_tx_free(void)
Vincent Coubard 0:f2542974c862 92 {
Vincent Coubard 0:f2542974c862 93 m_is_tx_allocated = false;
Vincent Coubard 0:f2542974c862 94
Vincent Coubard 0:f2542974c862 95 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 96 }
Vincent Coubard 0:f2542974c862 97
Vincent Coubard 0:f2542974c862 98
Vincent Coubard 0:f2542974c862 99 uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer)
Vincent Coubard 0:f2542974c862 100 {
Vincent Coubard 0:f2542974c862 101 uint32_t err_code;
Vincent Coubard 0:f2542974c862 102
Vincent Coubard 0:f2542974c862 103 if (pp_buffer == NULL)
Vincent Coubard 0:f2542974c862 104 {
Vincent Coubard 0:f2542974c862 105 return NRF_ERROR_NULL;
Vincent Coubard 0:f2542974c862 106 }
Vincent Coubard 0:f2542974c862 107 *pp_buffer = NULL;
Vincent Coubard 0:f2542974c862 108
Vincent Coubard 0:f2542974c862 109 if (m_rx_buffer_queue.free_window_count != 0)
Vincent Coubard 0:f2542974c862 110 {
Vincent Coubard 0:f2542974c862 111 if (length <= RX_BUF_SIZE)
Vincent Coubard 0:f2542974c862 112 {
Vincent Coubard 0:f2542974c862 113 --(m_rx_buffer_queue.free_window_count);
Vincent Coubard 0:f2542974c862 114 ++(m_rx_buffer_queue.read_available_count);
Vincent Coubard 0:f2542974c862 115
Vincent Coubard 0:f2542974c862 116 *pp_buffer =
Vincent Coubard 0:f2542974c862 117 m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.write_index].rx_buffer;
Vincent Coubard 0:f2542974c862 118
Vincent Coubard 0:f2542974c862 119 m_rx_buffer_queue.free_index |= (1u << m_rx_buffer_queue.write_index);
Vincent Coubard 0:f2542974c862 120
Vincent Coubard 0:f2542974c862 121 // @note: Adjust the write_index making use of the fact that the buffer size is of
Vincent Coubard 0:f2542974c862 122 // power of two and two's complement arithmetic. For details refer example to book
Vincent Coubard 0:f2542974c862 123 // "Making embedded systems: Elicia White".
Vincent Coubard 0:f2542974c862 124 m_rx_buffer_queue.write_index =
Vincent Coubard 0:f2542974c862 125 (m_rx_buffer_queue.write_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 0:f2542974c862 126
Vincent Coubard 0:f2542974c862 127 err_code = NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 128 }
Vincent Coubard 0:f2542974c862 129 else
Vincent Coubard 0:f2542974c862 130 {
Vincent Coubard 0:f2542974c862 131 err_code = NRF_ERROR_DATA_SIZE;
Vincent Coubard 0:f2542974c862 132 }
Vincent Coubard 0:f2542974c862 133 }
Vincent Coubard 0:f2542974c862 134 else
Vincent Coubard 0:f2542974c862 135 {
Vincent Coubard 0:f2542974c862 136 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 0:f2542974c862 137 }
Vincent Coubard 0:f2542974c862 138
Vincent Coubard 0:f2542974c862 139 return err_code;
Vincent Coubard 0:f2542974c862 140 }
Vincent Coubard 0:f2542974c862 141
Vincent Coubard 0:f2542974c862 142
Vincent Coubard 0:f2542974c862 143 uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer)
Vincent Coubard 0:f2542974c862 144 {
Vincent Coubard 0:f2542974c862 145 uint32_t err_code;
Vincent Coubard 0:f2542974c862 146 uint32_t consume_index;
Vincent Coubard 0:f2542974c862 147 uint32_t start_index;
Vincent Coubard 0:f2542974c862 148
Vincent Coubard 0:f2542974c862 149 if (m_rx_buffer_queue.free_available_count != 0)
Vincent Coubard 0:f2542974c862 150 {
Vincent Coubard 0:f2542974c862 151 // Find the buffer that has been freed -
Vincent Coubard 0:f2542974c862 152 // Start at read_index minus free_available_count and then increment until read index.
Vincent Coubard 0:f2542974c862 153 err_code = NRF_ERROR_INVALID_ADDR;
Vincent Coubard 0:f2542974c862 154 consume_index = (m_rx_buffer_queue.read_index - m_rx_buffer_queue.free_available_count) &
Vincent Coubard 0:f2542974c862 155 (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 0:f2542974c862 156 start_index = consume_index;
Vincent Coubard 0:f2542974c862 157
Vincent Coubard 0:f2542974c862 158 do
Vincent Coubard 0:f2542974c862 159 {
Vincent Coubard 0:f2542974c862 160 if (m_rx_buffer_queue.p_buffer[consume_index].rx_buffer == p_buffer)
Vincent Coubard 0:f2542974c862 161 {
Vincent Coubard 0:f2542974c862 162 m_rx_buffer_queue.free_index ^= (1u << consume_index);
Vincent Coubard 0:f2542974c862 163 err_code = NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 164 break;
Vincent Coubard 0:f2542974c862 165 }
Vincent Coubard 0:f2542974c862 166 else
Vincent Coubard 0:f2542974c862 167 {
Vincent Coubard 0:f2542974c862 168 consume_index = (consume_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 0:f2542974c862 169 }
Vincent Coubard 0:f2542974c862 170 }
Vincent Coubard 0:f2542974c862 171 while (consume_index != m_rx_buffer_queue.read_index);
Vincent Coubard 0:f2542974c862 172
Vincent Coubard 0:f2542974c862 173 while (!(m_rx_buffer_queue.free_index & (1 << start_index)) &&
Vincent Coubard 0:f2542974c862 174 (m_rx_buffer_queue.free_available_count != 0))
Vincent Coubard 0:f2542974c862 175 {
Vincent Coubard 0:f2542974c862 176 --(m_rx_buffer_queue.free_available_count);
Vincent Coubard 0:f2542974c862 177 ++(m_rx_buffer_queue.free_window_count);
Vincent Coubard 0:f2542974c862 178 start_index = (consume_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 0:f2542974c862 179 }
Vincent Coubard 0:f2542974c862 180 }
Vincent Coubard 0:f2542974c862 181 else
Vincent Coubard 0:f2542974c862 182 {
Vincent Coubard 0:f2542974c862 183 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 0:f2542974c862 184 }
Vincent Coubard 0:f2542974c862 185
Vincent Coubard 0:f2542974c862 186 return err_code;
Vincent Coubard 0:f2542974c862 187 }
Vincent Coubard 0:f2542974c862 188
Vincent Coubard 0:f2542974c862 189
Vincent Coubard 0:f2542974c862 190 uint32_t hci_mem_pool_rx_data_size_set(uint32_t length)
Vincent Coubard 0:f2542974c862 191 {
Vincent Coubard 0:f2542974c862 192 // @note: Adjust the write_index making use of the fact that the buffer size is of power
Vincent Coubard 0:f2542974c862 193 // of two and two's complement arithmetic. For details refer example to book
Vincent Coubard 0:f2542974c862 194 // "Making embedded systems: Elicia White".
Vincent Coubard 0:f2542974c862 195 const uint32_t index = (m_rx_buffer_queue.write_index - 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 0:f2542974c862 196 m_rx_buffer_queue.p_buffer[index].length = length;
Vincent Coubard 0:f2542974c862 197
Vincent Coubard 0:f2542974c862 198 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 199 }
Vincent Coubard 0:f2542974c862 200
Vincent Coubard 0:f2542974c862 201
Vincent Coubard 0:f2542974c862 202 uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length)
Vincent Coubard 0:f2542974c862 203 {
Vincent Coubard 0:f2542974c862 204 uint32_t err_code;
Vincent Coubard 0:f2542974c862 205
Vincent Coubard 0:f2542974c862 206 if ((pp_buffer == NULL) || (p_length == NULL))
Vincent Coubard 0:f2542974c862 207 {
Vincent Coubard 0:f2542974c862 208 return NRF_ERROR_NULL;
Vincent Coubard 0:f2542974c862 209 }
Vincent Coubard 0:f2542974c862 210
Vincent Coubard 0:f2542974c862 211 if (m_rx_buffer_queue.read_available_count != 0)
Vincent Coubard 0:f2542974c862 212 {
Vincent Coubard 0:f2542974c862 213 --(m_rx_buffer_queue.read_available_count);
Vincent Coubard 0:f2542974c862 214 ++(m_rx_buffer_queue.free_available_count);
Vincent Coubard 0:f2542974c862 215
Vincent Coubard 0:f2542974c862 216 *pp_buffer =
Vincent Coubard 0:f2542974c862 217 m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].rx_buffer;
Vincent Coubard 0:f2542974c862 218 *p_length =
Vincent Coubard 0:f2542974c862 219 m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].length;
Vincent Coubard 0:f2542974c862 220
Vincent Coubard 0:f2542974c862 221 // @note: Adjust the write_index making use of the fact that the buffer size is of power
Vincent Coubard 0:f2542974c862 222 // of two and two's complement arithmetic. For details refer example to book
Vincent Coubard 0:f2542974c862 223 // "Making embedded systems: Elicia White".
Vincent Coubard 0:f2542974c862 224 m_rx_buffer_queue.read_index =
Vincent Coubard 0:f2542974c862 225 (m_rx_buffer_queue.read_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 0:f2542974c862 226
Vincent Coubard 0:f2542974c862 227 err_code = NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 228 }
Vincent Coubard 0:f2542974c862 229 else
Vincent Coubard 0:f2542974c862 230 {
Vincent Coubard 0:f2542974c862 231 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 0:f2542974c862 232 }
Vincent Coubard 0:f2542974c862 233
Vincent Coubard 0:f2542974c862 234 return err_code;
vcoubard 1:ebc0e0ef0a11 235 }