Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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