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