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