Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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