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