Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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