テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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