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