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