Biomimetics MBED Library w/ Added Support for CAN3

Dependents:   CAN_TEST SPIne_Plus_DYNO_SENSORS SPIne_Plus_v2 SPIne_Plus_Dyno_v2

Committer:
saloutos
Date:
Thu Nov 26 04:08:56 2020 +0000
Revision:
0:083111ae2a11
first commit of leaned mbed dev lib

Who changed what in which revision?

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