mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2015 ARM Limited
AnnaBridge 189:f392fc9709a3 3 * SPDX-License-Identifier: Apache-2.0
<> 149:156823d33999 4 *
<> 149:156823d33999 5 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 6 * you may not use this file except in compliance with the License.
<> 149:156823d33999 7 * You may obtain a copy of the License at
<> 149:156823d33999 8 *
<> 149:156823d33999 9 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 10 *
<> 149:156823d33999 11 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 12 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 14 * See the License for the specific language governing permissions and
<> 149:156823d33999 15 * limitations under the License.
<> 149:156823d33999 16 */
<> 149:156823d33999 17 #ifndef MBED_CIRCULARBUFFER_H
<> 149:156823d33999 18 #define MBED_CIRCULARBUFFER_H
<> 149:156823d33999 19
<> 160:d5399cc887bb 20 #include "platform/mbed_critical.h"
AnnaBridge 184:08ed48f1de7f 21 #include "platform/mbed_assert.h"
<> 149:156823d33999 22
<> 149:156823d33999 23 namespace mbed {
AnnaBridge 184:08ed48f1de7f 24
AnnaBridge 184:08ed48f1de7f 25 namespace internal {
AnnaBridge 184:08ed48f1de7f 26 /* Detect if CounterType of the Circular buffer is of unsigned type. */
AnnaBridge 184:08ed48f1de7f 27 template<typename T>
AnnaBridge 187:0387e8f68319 28 struct is_unsigned {
AnnaBridge 187:0387e8f68319 29 static const bool value = false;
AnnaBridge 187:0387e8f68319 30 };
AnnaBridge 184:08ed48f1de7f 31 template<>
AnnaBridge 187:0387e8f68319 32 struct is_unsigned<unsigned char> {
AnnaBridge 187:0387e8f68319 33 static const bool value = true;
AnnaBridge 187:0387e8f68319 34 };
AnnaBridge 184:08ed48f1de7f 35 template<>
AnnaBridge 187:0387e8f68319 36 struct is_unsigned<unsigned short> {
AnnaBridge 187:0387e8f68319 37 static const bool value = true;
AnnaBridge 187:0387e8f68319 38 };
AnnaBridge 184:08ed48f1de7f 39 template<>
AnnaBridge 187:0387e8f68319 40 struct is_unsigned<unsigned int> {
AnnaBridge 187:0387e8f68319 41 static const bool value = true;
AnnaBridge 187:0387e8f68319 42 };
AnnaBridge 184:08ed48f1de7f 43 template<>
AnnaBridge 187:0387e8f68319 44 struct is_unsigned<unsigned long> {
AnnaBridge 187:0387e8f68319 45 static const bool value = true;
AnnaBridge 187:0387e8f68319 46 };
AnnaBridge 184:08ed48f1de7f 47 template<>
AnnaBridge 187:0387e8f68319 48 struct is_unsigned<unsigned long long> {
AnnaBridge 187:0387e8f68319 49 static const bool value = true;
AnnaBridge 187:0387e8f68319 50 };
AnnaBridge 184:08ed48f1de7f 51 };
AnnaBridge 184:08ed48f1de7f 52
<> 149:156823d33999 53 /** \addtogroup platform */
AnnaBridge 178:79309dc6340a 54 /** @{*/
AnnaBridge 178:79309dc6340a 55 /**
AnnaBridge 178:79309dc6340a 56 * \defgroup platform_CircularBuffer CircularBuffer functions
AnnaBridge 178:79309dc6340a 57 * @{
AnnaBridge 178:79309dc6340a 58 */
<> 149:156823d33999 59
<> 149:156823d33999 60 /** Templated Circular buffer class
<> 149:156823d33999 61 *
AnnaBridge 167:e84263d55307 62 * @note Synchronization level: Interrupt safe
AnnaBridge 184:08ed48f1de7f 63 * @note CounterType must be unsigned and consistent with BufferSize
<> 149:156823d33999 64 */
<> 149:156823d33999 65 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
<> 149:156823d33999 66 class CircularBuffer {
<> 149:156823d33999 67 public:
AnnaBridge 187:0387e8f68319 68 CircularBuffer() : _head(0), _tail(0), _full(false)
AnnaBridge 187:0387e8f68319 69 {
AnnaBridge 184:08ed48f1de7f 70 MBED_STATIC_ASSERT(
AnnaBridge 184:08ed48f1de7f 71 internal::is_unsigned<CounterType>::value,
AnnaBridge 184:08ed48f1de7f 72 "CounterType must be unsigned"
AnnaBridge 184:08ed48f1de7f 73 );
AnnaBridge 184:08ed48f1de7f 74
AnnaBridge 184:08ed48f1de7f 75 MBED_STATIC_ASSERT(
AnnaBridge 184:08ed48f1de7f 76 (sizeof(CounterType) >= sizeof(uint32_t)) ||
AnnaBridge 184:08ed48f1de7f 77 (BufferSize < (((uint64_t) 1) << (sizeof(CounterType) * 8))),
AnnaBridge 184:08ed48f1de7f 78 "Invalid BufferSize for the CounterType"
AnnaBridge 184:08ed48f1de7f 79 );
<> 149:156823d33999 80 }
<> 149:156823d33999 81
AnnaBridge 187:0387e8f68319 82 ~CircularBuffer()
AnnaBridge 187:0387e8f68319 83 {
<> 149:156823d33999 84 }
<> 149:156823d33999 85
<> 149:156823d33999 86 /** Push the transaction to the buffer. This overwrites the buffer if it's
<> 149:156823d33999 87 * full
<> 149:156823d33999 88 *
<> 149:156823d33999 89 * @param data Data to be pushed to the buffer
<> 149:156823d33999 90 */
AnnaBridge 187:0387e8f68319 91 void push(const T &data)
AnnaBridge 187:0387e8f68319 92 {
<> 149:156823d33999 93 core_util_critical_section_enter();
<> 149:156823d33999 94 if (full()) {
<> 149:156823d33999 95 _tail++;
AnnaBridge 188:bcfe06ba3d64 96 if (_tail == BufferSize) {
AnnaBridge 188:bcfe06ba3d64 97 _tail = 0;
AnnaBridge 188:bcfe06ba3d64 98 }
<> 149:156823d33999 99 }
<> 149:156823d33999 100 _pool[_head++] = data;
AnnaBridge 188:bcfe06ba3d64 101 if (_head == BufferSize) {
AnnaBridge 189:f392fc9709a3 102 _head = 0;
AnnaBridge 188:bcfe06ba3d64 103 }
<> 149:156823d33999 104 if (_head == _tail) {
<> 149:156823d33999 105 _full = true;
<> 149:156823d33999 106 }
<> 149:156823d33999 107 core_util_critical_section_exit();
<> 149:156823d33999 108 }
<> 149:156823d33999 109
<> 149:156823d33999 110 /** Pop the transaction from the buffer
<> 149:156823d33999 111 *
Anna Bridge 186:707f6e361f3e 112 * @param data Data to be popped from the buffer
<> 149:156823d33999 113 * @return True if the buffer is not empty and data contains a transaction, false otherwise
<> 149:156823d33999 114 */
AnnaBridge 187:0387e8f68319 115 bool pop(T &data)
AnnaBridge 187:0387e8f68319 116 {
<> 149:156823d33999 117 bool data_popped = false;
<> 149:156823d33999 118 core_util_critical_section_enter();
<> 149:156823d33999 119 if (!empty()) {
<> 149:156823d33999 120 data = _pool[_tail++];
AnnaBridge 188:bcfe06ba3d64 121 if (_tail == BufferSize) {
AnnaBridge 188:bcfe06ba3d64 122 _tail = 0;
AnnaBridge 188:bcfe06ba3d64 123 }
<> 149:156823d33999 124 _full = false;
<> 149:156823d33999 125 data_popped = true;
<> 149:156823d33999 126 }
<> 149:156823d33999 127 core_util_critical_section_exit();
<> 149:156823d33999 128 return data_popped;
<> 149:156823d33999 129 }
<> 149:156823d33999 130
<> 149:156823d33999 131 /** Check if the buffer is empty
<> 149:156823d33999 132 *
<> 149:156823d33999 133 * @return True if the buffer is empty, false if not
<> 149:156823d33999 134 */
AnnaBridge 187:0387e8f68319 135 bool empty() const
AnnaBridge 187:0387e8f68319 136 {
<> 149:156823d33999 137 core_util_critical_section_enter();
<> 149:156823d33999 138 bool is_empty = (_head == _tail) && !_full;
<> 149:156823d33999 139 core_util_critical_section_exit();
<> 149:156823d33999 140 return is_empty;
<> 149:156823d33999 141 }
<> 149:156823d33999 142
<> 149:156823d33999 143 /** Check if the buffer is full
<> 149:156823d33999 144 *
<> 149:156823d33999 145 * @return True if the buffer is full, false if not
<> 149:156823d33999 146 */
AnnaBridge 187:0387e8f68319 147 bool full() const
AnnaBridge 187:0387e8f68319 148 {
<> 149:156823d33999 149 core_util_critical_section_enter();
<> 149:156823d33999 150 bool full = _full;
<> 149:156823d33999 151 core_util_critical_section_exit();
<> 149:156823d33999 152 return full;
<> 149:156823d33999 153 }
<> 149:156823d33999 154
<> 149:156823d33999 155 /** Reset the buffer
<> 149:156823d33999 156 *
<> 149:156823d33999 157 */
AnnaBridge 187:0387e8f68319 158 void reset()
AnnaBridge 187:0387e8f68319 159 {
<> 149:156823d33999 160 core_util_critical_section_enter();
<> 149:156823d33999 161 _head = 0;
<> 149:156823d33999 162 _tail = 0;
<> 149:156823d33999 163 _full = false;
<> 149:156823d33999 164 core_util_critical_section_exit();
<> 149:156823d33999 165 }
<> 149:156823d33999 166
Anna Bridge 180:96ed750bd169 167 /** Get the number of elements currently stored in the circular_buffer */
AnnaBridge 187:0387e8f68319 168 CounterType size() const
AnnaBridge 187:0387e8f68319 169 {
Anna Bridge 180:96ed750bd169 170 core_util_critical_section_enter();
Anna Bridge 180:96ed750bd169 171 CounterType elements;
Anna Bridge 180:96ed750bd169 172 if (!_full) {
Anna Bridge 180:96ed750bd169 173 if (_head < _tail) {
Anna Bridge 180:96ed750bd169 174 elements = BufferSize + _head - _tail;
Anna Bridge 180:96ed750bd169 175 } else {
Anna Bridge 180:96ed750bd169 176 elements = _head - _tail;
Anna Bridge 180:96ed750bd169 177 }
Anna Bridge 180:96ed750bd169 178 } else {
Anna Bridge 180:96ed750bd169 179 elements = BufferSize;
Anna Bridge 180:96ed750bd169 180 }
Anna Bridge 180:96ed750bd169 181 core_util_critical_section_exit();
Anna Bridge 180:96ed750bd169 182 return elements;
Anna Bridge 180:96ed750bd169 183 }
Anna Bridge 186:707f6e361f3e 184
Anna Bridge 186:707f6e361f3e 185 /** Peek into circular buffer without popping
Anna Bridge 186:707f6e361f3e 186 *
Anna Bridge 186:707f6e361f3e 187 * @param data Data to be peeked from the buffer
Anna Bridge 186:707f6e361f3e 188 * @return True if the buffer is not empty and data contains a transaction, false otherwise
Anna Bridge 186:707f6e361f3e 189 */
AnnaBridge 187:0387e8f68319 190 bool peek(T &data) const
AnnaBridge 187:0387e8f68319 191 {
Anna Bridge 186:707f6e361f3e 192 bool data_updated = false;
Anna Bridge 186:707f6e361f3e 193 core_util_critical_section_enter();
Anna Bridge 186:707f6e361f3e 194 if (!empty()) {
Anna Bridge 186:707f6e361f3e 195 data = _pool[_tail];
Anna Bridge 186:707f6e361f3e 196 data_updated = true;
Anna Bridge 186:707f6e361f3e 197 }
Anna Bridge 186:707f6e361f3e 198 core_util_critical_section_exit();
Anna Bridge 186:707f6e361f3e 199 return data_updated;
Anna Bridge 186:707f6e361f3e 200 }
AnnaBridge 187:0387e8f68319 201
<> 149:156823d33999 202 private:
<> 149:156823d33999 203 T _pool[BufferSize];
AnnaBridge 188:bcfe06ba3d64 204 CounterType _head;
AnnaBridge 188:bcfe06ba3d64 205 CounterType _tail;
AnnaBridge 188:bcfe06ba3d64 206 bool _full;
<> 149:156823d33999 207 };
<> 149:156823d33999 208
AnnaBridge 178:79309dc6340a 209 /**@}*/
AnnaBridge 178:79309dc6340a 210
AnnaBridge 178:79309dc6340a 211 /**@}*/
AnnaBridge 178:79309dc6340a 212
<> 149:156823d33999 213 }
<> 149:156823d33999 214
<> 149:156823d33999 215 #endif