mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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