[457] | 1 | /* Copyright (c) 2014, Nordic Semiconductor ASA
|
---|
| 2 | *
|
---|
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 4 | * of this software and associated documentation files (the "Software"), to deal
|
---|
| 5 | * in the Software without restriction, including without limitation the rights
|
---|
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 7 | * copies of the Software, and to permit persons to whom the Software is
|
---|
| 8 | * furnished to do so, subject to the following conditions:
|
---|
| 9 | *
|
---|
| 10 | * The above copyright notice and this permission notice shall be included in all
|
---|
| 11 | * copies or substantial portions of the Software.
|
---|
| 12 | *
|
---|
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
---|
| 19 | * SOFTWARE.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | /** @file
|
---|
| 23 | @brief Implementation of a circular queue for ACI data
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | #include "hal_aci_tl.h"
|
---|
| 27 | #include "aci_queue.h"
|
---|
| 28 | #include "ble_assert.h"
|
---|
| 29 |
|
---|
| 30 | void aci_queue_init(aci_queue_t *aci_q)
|
---|
| 31 | {
|
---|
| 32 | uint8_t loop;
|
---|
| 33 |
|
---|
| 34 | ble_assert(NULL != aci_q);
|
---|
| 35 |
|
---|
| 36 | aci_q->head = 0;
|
---|
| 37 | aci_q->tail = 0;
|
---|
| 38 | for(loop=0; loop<ACI_QUEUE_SIZE; loop++)
|
---|
| 39 | {
|
---|
| 40 | aci_q->aci_data[loop].buffer[0] = 0x00;
|
---|
| 41 | aci_q->aci_data[loop].buffer[1] = 0x00;
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | bool aci_queue_dequeue(aci_queue_t *aci_q, hal_aci_data_t *p_data)
|
---|
| 46 | {
|
---|
| 47 | ble_assert(NULL != aci_q);
|
---|
| 48 | ble_assert(NULL != p_data);
|
---|
| 49 |
|
---|
| 50 | if (aci_queue_is_empty(aci_q))
|
---|
| 51 | {
|
---|
| 52 | return false;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | noInterrupts();
|
---|
| 56 | memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t));
|
---|
| 57 | aci_q->head = (aci_q->head + 1) % ACI_QUEUE_SIZE;
|
---|
| 58 | interrupts();
|
---|
| 59 |
|
---|
| 60 | return true;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | bool aci_queue_enqueue(aci_queue_t *aci_q, hal_aci_data_t *p_data)
|
---|
| 64 | {
|
---|
| 65 | const uint8_t length = p_data->buffer[0];
|
---|
| 66 |
|
---|
| 67 | ble_assert(NULL != aci_q);
|
---|
| 68 | ble_assert(NULL != p_data);
|
---|
| 69 |
|
---|
| 70 | if (aci_queue_is_full(aci_q))
|
---|
| 71 | {
|
---|
| 72 | return false;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | noInterrupts();
|
---|
| 76 | aci_q->aci_data[aci_q->tail].status_byte = 0;
|
---|
| 77 | memcpy((uint8_t *)&(aci_q->aci_data[aci_q->tail].buffer[0]), (uint8_t *)&p_data->buffer[0], length + 1);
|
---|
| 78 | aci_q->tail = (aci_q->tail + 1) % ACI_QUEUE_SIZE;
|
---|
| 79 | interrupts();
|
---|
| 80 |
|
---|
| 81 | return true;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | bool aci_queue_is_empty(aci_queue_t *aci_q)
|
---|
| 85 | {
|
---|
| 86 | bool state = false;
|
---|
| 87 |
|
---|
| 88 | ble_assert(NULL != aci_q);
|
---|
| 89 |
|
---|
| 90 | //Critical section
|
---|
| 91 | noInterrupts();
|
---|
| 92 | if (aci_q->head == aci_q->tail)
|
---|
| 93 | {
|
---|
| 94 | state = true;
|
---|
| 95 | }
|
---|
| 96 | interrupts();
|
---|
| 97 |
|
---|
| 98 | return state;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | bool aci_queue_is_full(aci_queue_t *aci_q)
|
---|
| 102 | {
|
---|
| 103 | uint8_t next;
|
---|
| 104 | bool state;
|
---|
| 105 |
|
---|
| 106 | ble_assert(NULL != aci_q);
|
---|
| 107 |
|
---|
| 108 | //This should be done in a critical section
|
---|
| 109 | noInterrupts();
|
---|
| 110 | next = (aci_q->tail + 1) % ACI_QUEUE_SIZE;
|
---|
| 111 |
|
---|
| 112 | if (next == aci_q->head)
|
---|
| 113 | {
|
---|
| 114 | state = true;
|
---|
| 115 | }
|
---|
| 116 | else
|
---|
| 117 | {
|
---|
| 118 | state = false;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | interrupts();
|
---|
| 122 | //end
|
---|
| 123 |
|
---|
| 124 | return state;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | bool aci_queue_peek(aci_queue_t *aci_q, hal_aci_data_t *p_data)
|
---|
| 128 | {
|
---|
| 129 | ble_assert(NULL != aci_q);
|
---|
| 130 | ble_assert(NULL != p_data);
|
---|
| 131 |
|
---|
| 132 | if (aci_queue_is_empty(aci_q))
|
---|
| 133 | {
|
---|
| 134 | return false;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t));
|
---|
| 138 |
|
---|
| 139 | return true;
|
---|
| 140 | }
|
---|
| 141 |
|
---|