added debugging
Fork of BLE_nRF8001 by
BLE_nRF8001/utility/aci_queue.cpp@2:7805a5595aab, 2014-11-10 (annotated)
- Committer:
- jn80842
- Date:
- Mon Nov 10 01:24:23 2014 +0000
- Revision:
- 2:7805a5595aab
- Parent:
- 0:075ea2812998
just added debugging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:075ea2812998 | 1 | /* Copyright (c) 2014, Nordic Semiconductor ASA |
RedBearLab | 0:075ea2812998 | 2 | * |
RedBearLab | 0:075ea2812998 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
RedBearLab | 0:075ea2812998 | 4 | * of this software and associated documentation files (the "Software"), to deal |
RedBearLab | 0:075ea2812998 | 5 | * in the Software without restriction, including without limitation the rights |
RedBearLab | 0:075ea2812998 | 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
RedBearLab | 0:075ea2812998 | 7 | * copies of the Software, and to permit persons to whom the Software is |
RedBearLab | 0:075ea2812998 | 8 | * furnished to do so, subject to the following conditions: |
RedBearLab | 0:075ea2812998 | 9 | * |
RedBearLab | 0:075ea2812998 | 10 | * The above copyright notice and this permission notice shall be included in all |
RedBearLab | 0:075ea2812998 | 11 | * copies or substantial portions of the Software. |
RedBearLab | 0:075ea2812998 | 12 | * |
RedBearLab | 0:075ea2812998 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
RedBearLab | 0:075ea2812998 | 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
RedBearLab | 0:075ea2812998 | 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
RedBearLab | 0:075ea2812998 | 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
RedBearLab | 0:075ea2812998 | 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
RedBearLab | 0:075ea2812998 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
RedBearLab | 0:075ea2812998 | 19 | * SOFTWARE. |
RedBearLab | 0:075ea2812998 | 20 | */ |
RedBearLab | 0:075ea2812998 | 21 | |
RedBearLab | 0:075ea2812998 | 22 | /** @file |
RedBearLab | 0:075ea2812998 | 23 | @brief Implementation of a circular queue for ACI data |
RedBearLab | 0:075ea2812998 | 24 | */ |
RedBearLab | 0:075ea2812998 | 25 | |
RedBearLab | 0:075ea2812998 | 26 | #include "hal_aci_tl.h" |
RedBearLab | 0:075ea2812998 | 27 | #include "aci_queue.h" |
RedBearLab | 0:075ea2812998 | 28 | #include "ble_assert.h" |
RedBearLab | 0:075ea2812998 | 29 | |
RedBearLab | 0:075ea2812998 | 30 | void aci_queue_init(aci_queue_t *aci_q) |
RedBearLab | 0:075ea2812998 | 31 | { |
RedBearLab | 0:075ea2812998 | 32 | uint8_t loop; |
RedBearLab | 0:075ea2812998 | 33 | |
RedBearLab | 0:075ea2812998 | 34 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 35 | |
RedBearLab | 0:075ea2812998 | 36 | aci_q->head = 0; |
RedBearLab | 0:075ea2812998 | 37 | aci_q->tail = 0; |
RedBearLab | 0:075ea2812998 | 38 | for(loop=0; loop<ACI_QUEUE_SIZE; loop++) |
RedBearLab | 0:075ea2812998 | 39 | { |
RedBearLab | 0:075ea2812998 | 40 | aci_q->aci_data[loop].buffer[0] = 0x00; |
RedBearLab | 0:075ea2812998 | 41 | aci_q->aci_data[loop].buffer[1] = 0x00; |
RedBearLab | 0:075ea2812998 | 42 | } |
RedBearLab | 0:075ea2812998 | 43 | } |
RedBearLab | 0:075ea2812998 | 44 | |
RedBearLab | 0:075ea2812998 | 45 | bool aci_queue_dequeue(aci_queue_t *aci_q, hal_aci_data_t *p_data) |
RedBearLab | 0:075ea2812998 | 46 | { |
RedBearLab | 0:075ea2812998 | 47 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 48 | ble_assert(NULL != p_data); |
RedBearLab | 0:075ea2812998 | 49 | |
RedBearLab | 0:075ea2812998 | 50 | if (aci_queue_is_empty(aci_q)) |
RedBearLab | 0:075ea2812998 | 51 | { |
RedBearLab | 0:075ea2812998 | 52 | return false; |
RedBearLab | 0:075ea2812998 | 53 | } |
RedBearLab | 0:075ea2812998 | 54 | |
RedBearLab | 0:075ea2812998 | 55 | memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t)); |
RedBearLab | 0:075ea2812998 | 56 | aci_q->head = (aci_q->head + 1) % ACI_QUEUE_SIZE; |
RedBearLab | 0:075ea2812998 | 57 | |
RedBearLab | 0:075ea2812998 | 58 | return true; |
RedBearLab | 0:075ea2812998 | 59 | } |
RedBearLab | 0:075ea2812998 | 60 | |
RedBearLab | 0:075ea2812998 | 61 | bool aci_queue_dequeue_from_isr(aci_queue_t *aci_q, hal_aci_data_t *p_data) |
RedBearLab | 0:075ea2812998 | 62 | { |
RedBearLab | 0:075ea2812998 | 63 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 64 | ble_assert(NULL != p_data); |
RedBearLab | 0:075ea2812998 | 65 | |
RedBearLab | 0:075ea2812998 | 66 | if (aci_queue_is_empty_from_isr(aci_q)) |
RedBearLab | 0:075ea2812998 | 67 | { |
RedBearLab | 0:075ea2812998 | 68 | return false; |
RedBearLab | 0:075ea2812998 | 69 | } |
RedBearLab | 0:075ea2812998 | 70 | |
RedBearLab | 0:075ea2812998 | 71 | memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t)); |
RedBearLab | 0:075ea2812998 | 72 | aci_q->head = (aci_q->head + 1) % ACI_QUEUE_SIZE; |
RedBearLab | 0:075ea2812998 | 73 | |
RedBearLab | 0:075ea2812998 | 74 | return true; |
RedBearLab | 0:075ea2812998 | 75 | } |
RedBearLab | 0:075ea2812998 | 76 | |
RedBearLab | 0:075ea2812998 | 77 | bool aci_queue_enqueue(aci_queue_t *aci_q, hal_aci_data_t *p_data) |
RedBearLab | 0:075ea2812998 | 78 | { |
RedBearLab | 0:075ea2812998 | 79 | const uint8_t length = p_data->buffer[0]; |
RedBearLab | 0:075ea2812998 | 80 | |
RedBearLab | 0:075ea2812998 | 81 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 82 | ble_assert(NULL != p_data); |
RedBearLab | 0:075ea2812998 | 83 | |
RedBearLab | 0:075ea2812998 | 84 | if (aci_queue_is_full(aci_q)) |
RedBearLab | 0:075ea2812998 | 85 | { |
RedBearLab | 0:075ea2812998 | 86 | return false; |
RedBearLab | 0:075ea2812998 | 87 | } |
RedBearLab | 0:075ea2812998 | 88 | |
RedBearLab | 0:075ea2812998 | 89 | aci_q->aci_data[aci_q->tail].status_byte = 0; |
RedBearLab | 0:075ea2812998 | 90 | memcpy((uint8_t *)&(aci_q->aci_data[aci_q->tail].buffer[0]), (uint8_t *)&p_data->buffer[0], length + 1); |
RedBearLab | 0:075ea2812998 | 91 | aci_q->tail = (aci_q->tail + 1) % ACI_QUEUE_SIZE; |
RedBearLab | 0:075ea2812998 | 92 | |
RedBearLab | 0:075ea2812998 | 93 | return true; |
RedBearLab | 0:075ea2812998 | 94 | } |
RedBearLab | 0:075ea2812998 | 95 | |
RedBearLab | 0:075ea2812998 | 96 | bool aci_queue_enqueue_from_isr(aci_queue_t *aci_q, hal_aci_data_t *p_data) |
RedBearLab | 0:075ea2812998 | 97 | { |
RedBearLab | 0:075ea2812998 | 98 | const uint8_t length = p_data->buffer[0]; |
RedBearLab | 0:075ea2812998 | 99 | |
RedBearLab | 0:075ea2812998 | 100 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 101 | ble_assert(NULL != p_data); |
RedBearLab | 0:075ea2812998 | 102 | |
RedBearLab | 0:075ea2812998 | 103 | if (aci_queue_is_full_from_isr(aci_q)) |
RedBearLab | 0:075ea2812998 | 104 | { |
RedBearLab | 0:075ea2812998 | 105 | return false; |
RedBearLab | 0:075ea2812998 | 106 | } |
RedBearLab | 0:075ea2812998 | 107 | |
RedBearLab | 0:075ea2812998 | 108 | aci_q->aci_data[aci_q->tail].status_byte = 0; |
RedBearLab | 0:075ea2812998 | 109 | memcpy((uint8_t *)&(aci_q->aci_data[aci_q->tail].buffer[0]), (uint8_t *)&p_data->buffer[0], length + 1); |
RedBearLab | 0:075ea2812998 | 110 | aci_q->tail = (aci_q->tail + 1) % ACI_QUEUE_SIZE; |
RedBearLab | 0:075ea2812998 | 111 | |
RedBearLab | 0:075ea2812998 | 112 | return true; |
RedBearLab | 0:075ea2812998 | 113 | } |
RedBearLab | 0:075ea2812998 | 114 | |
RedBearLab | 0:075ea2812998 | 115 | bool aci_queue_is_empty(aci_queue_t *aci_q) |
RedBearLab | 0:075ea2812998 | 116 | { |
RedBearLab | 0:075ea2812998 | 117 | bool state = false; |
RedBearLab | 0:075ea2812998 | 118 | |
RedBearLab | 0:075ea2812998 | 119 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 120 | |
RedBearLab | 0:075ea2812998 | 121 | //Critical section |
RedBearLab | 0:075ea2812998 | 122 | //noInterrupts(); |
RedBearLab | 0:075ea2812998 | 123 | if (aci_q->head == aci_q->tail) |
RedBearLab | 0:075ea2812998 | 124 | { |
RedBearLab | 0:075ea2812998 | 125 | state = true; |
RedBearLab | 0:075ea2812998 | 126 | } |
RedBearLab | 0:075ea2812998 | 127 | //interrupts(); |
RedBearLab | 0:075ea2812998 | 128 | |
RedBearLab | 0:075ea2812998 | 129 | return state; |
RedBearLab | 0:075ea2812998 | 130 | } |
RedBearLab | 0:075ea2812998 | 131 | |
RedBearLab | 0:075ea2812998 | 132 | bool aci_queue_is_empty_from_isr(aci_queue_t *aci_q) |
RedBearLab | 0:075ea2812998 | 133 | { |
RedBearLab | 0:075ea2812998 | 134 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 135 | |
RedBearLab | 0:075ea2812998 | 136 | return aci_q->head == aci_q->tail; |
RedBearLab | 0:075ea2812998 | 137 | } |
RedBearLab | 0:075ea2812998 | 138 | |
RedBearLab | 0:075ea2812998 | 139 | bool aci_queue_is_full(aci_queue_t *aci_q) |
RedBearLab | 0:075ea2812998 | 140 | { |
RedBearLab | 0:075ea2812998 | 141 | uint8_t next; |
RedBearLab | 0:075ea2812998 | 142 | bool state; |
RedBearLab | 0:075ea2812998 | 143 | |
RedBearLab | 0:075ea2812998 | 144 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 145 | |
RedBearLab | 0:075ea2812998 | 146 | //This should be done in a critical section |
RedBearLab | 0:075ea2812998 | 147 | //noInterrupts(); |
RedBearLab | 0:075ea2812998 | 148 | next = (aci_q->tail + 1) % ACI_QUEUE_SIZE; |
RedBearLab | 0:075ea2812998 | 149 | |
RedBearLab | 0:075ea2812998 | 150 | if (next == aci_q->head) |
RedBearLab | 0:075ea2812998 | 151 | { |
RedBearLab | 0:075ea2812998 | 152 | state = true; |
RedBearLab | 0:075ea2812998 | 153 | } |
RedBearLab | 0:075ea2812998 | 154 | else |
RedBearLab | 0:075ea2812998 | 155 | { |
RedBearLab | 0:075ea2812998 | 156 | state = false; |
RedBearLab | 0:075ea2812998 | 157 | } |
RedBearLab | 0:075ea2812998 | 158 | |
RedBearLab | 0:075ea2812998 | 159 | //interrupts(); |
RedBearLab | 0:075ea2812998 | 160 | //end |
RedBearLab | 0:075ea2812998 | 161 | |
RedBearLab | 0:075ea2812998 | 162 | return state; |
RedBearLab | 0:075ea2812998 | 163 | } |
RedBearLab | 0:075ea2812998 | 164 | |
RedBearLab | 0:075ea2812998 | 165 | bool aci_queue_is_full_from_isr(aci_queue_t *aci_q) |
RedBearLab | 0:075ea2812998 | 166 | { |
RedBearLab | 0:075ea2812998 | 167 | const uint8_t next = (aci_q->tail + 1) % ACI_QUEUE_SIZE; |
RedBearLab | 0:075ea2812998 | 168 | |
RedBearLab | 0:075ea2812998 | 169 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 170 | |
RedBearLab | 0:075ea2812998 | 171 | return next == aci_q->head; |
RedBearLab | 0:075ea2812998 | 172 | } |
RedBearLab | 0:075ea2812998 | 173 | |
RedBearLab | 0:075ea2812998 | 174 | bool aci_queue_peek(aci_queue_t *aci_q, hal_aci_data_t *p_data) |
RedBearLab | 0:075ea2812998 | 175 | { |
RedBearLab | 0:075ea2812998 | 176 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 177 | ble_assert(NULL != p_data); |
RedBearLab | 0:075ea2812998 | 178 | |
RedBearLab | 0:075ea2812998 | 179 | if (aci_queue_is_empty(aci_q)) |
RedBearLab | 0:075ea2812998 | 180 | { |
RedBearLab | 0:075ea2812998 | 181 | return false; |
RedBearLab | 0:075ea2812998 | 182 | } |
RedBearLab | 0:075ea2812998 | 183 | |
RedBearLab | 0:075ea2812998 | 184 | memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t)); |
RedBearLab | 0:075ea2812998 | 185 | |
RedBearLab | 0:075ea2812998 | 186 | return true; |
RedBearLab | 0:075ea2812998 | 187 | } |
RedBearLab | 0:075ea2812998 | 188 | |
RedBearLab | 0:075ea2812998 | 189 | bool aci_queue_peek_from_isr(aci_queue_t *aci_q, hal_aci_data_t *p_data) |
RedBearLab | 0:075ea2812998 | 190 | { |
RedBearLab | 0:075ea2812998 | 191 | ble_assert(NULL != aci_q); |
RedBearLab | 0:075ea2812998 | 192 | ble_assert(NULL != p_data); |
RedBearLab | 0:075ea2812998 | 193 | |
RedBearLab | 0:075ea2812998 | 194 | if (aci_queue_is_empty_from_isr(aci_q)) |
RedBearLab | 0:075ea2812998 | 195 | { |
RedBearLab | 0:075ea2812998 | 196 | return false; |
RedBearLab | 0:075ea2812998 | 197 | } |
RedBearLab | 0:075ea2812998 | 198 | |
RedBearLab | 0:075ea2812998 | 199 | memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t)); |
RedBearLab | 0:075ea2812998 | 200 | |
RedBearLab | 0:075ea2812998 | 201 | return true; |
RedBearLab | 0:075ea2812998 | 202 | } |