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