this hurts

Dependencies:   FFT

Committer:
annieluo2
Date:
Wed Dec 02 18:02:03 2020 +0000
Revision:
0:d6c9b09b4042
boo

Who changed what in which revision?

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