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