Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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