Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Tue Mar 09 13:10:40 2021 +0000
Revision:
3:6fe17b8a6d62
Parent:
0:4beb2ea291ec
SDBlockDevice added

Who changed what in which revision?

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