Describes predefine macros for mbed online compiler (armcc)

Committer:
MACRUM
Date:
Thu Mar 16 21:58:09 2017 +0900
Revision:
6:40e873bbc5f7
Add licence header info

Who changed what in which revision?

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