Adapted to LoRa Semtech + Nucleo
Dependencies: LMiC SX1276Lib cantcoap lwip mbed-rtos mbed
Fork of LoRaWAN-lmic-app by
core/MtxCircBuffer.h@5:97b629581096, 2015-11-23 (annotated)
- Committer:
- pnysten
- Date:
- Mon Nov 23 09:54:36 2015 +0000
- Revision:
- 5:97b629581096
- Parent:
- 4:5e274bf85bf0
Encryption bypassed if port>0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pnysten | 4:5e274bf85bf0 | 1 | /* MtxCircBuf.h */ |
pnysten | 4:5e274bf85bf0 | 2 | /* |
pnysten | 4:5e274bf85bf0 | 3 | Copyright (C) 2012 ARM Limited. |
pnysten | 4:5e274bf85bf0 | 4 | |
pnysten | 4:5e274bf85bf0 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
pnysten | 4:5e274bf85bf0 | 6 | this software and associated documentation files (the "Software"), to deal in |
pnysten | 4:5e274bf85bf0 | 7 | the Software without restriction, including without limitation the rights to |
pnysten | 4:5e274bf85bf0 | 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
pnysten | 4:5e274bf85bf0 | 9 | of the Software, and to permit persons to whom the Software is furnished to do |
pnysten | 4:5e274bf85bf0 | 10 | so, subject to the following conditions: |
pnysten | 4:5e274bf85bf0 | 11 | |
pnysten | 4:5e274bf85bf0 | 12 | The above copyright notice and this permission notice shall be included in all |
pnysten | 4:5e274bf85bf0 | 13 | copies or substantial portions of the Software. |
pnysten | 4:5e274bf85bf0 | 14 | |
pnysten | 4:5e274bf85bf0 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pnysten | 4:5e274bf85bf0 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pnysten | 4:5e274bf85bf0 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pnysten | 4:5e274bf85bf0 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pnysten | 4:5e274bf85bf0 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pnysten | 4:5e274bf85bf0 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
pnysten | 4:5e274bf85bf0 | 21 | SOFTWARE. |
pnysten | 4:5e274bf85bf0 | 22 | */ |
pnysten | 4:5e274bf85bf0 | 23 | |
pnysten | 4:5e274bf85bf0 | 24 | #ifndef MTXCIRCBUFFER_H |
pnysten | 4:5e274bf85bf0 | 25 | #define MTXCIRCBUFFER_H |
pnysten | 4:5e274bf85bf0 | 26 | |
pnysten | 4:5e274bf85bf0 | 27 | #include "fwk.h" |
pnysten | 4:5e274bf85bf0 | 28 | |
pnysten | 4:5e274bf85bf0 | 29 | #include "rtos.h" |
pnysten | 4:5e274bf85bf0 | 30 | |
pnysten | 4:5e274bf85bf0 | 31 | //Mutex protected circualr buffer |
pnysten | 4:5e274bf85bf0 | 32 | template<typename T, int size> |
pnysten | 4:5e274bf85bf0 | 33 | class MtxCircBuffer |
pnysten | 4:5e274bf85bf0 | 34 | { |
pnysten | 4:5e274bf85bf0 | 35 | public: |
pnysten | 4:5e274bf85bf0 | 36 | MtxCircBuffer() //: |
pnysten | 4:5e274bf85bf0 | 37 | //mtx() |
pnysten | 4:5e274bf85bf0 | 38 | { |
pnysten | 4:5e274bf85bf0 | 39 | write = 0; |
pnysten | 4:5e274bf85bf0 | 40 | read = 0; |
pnysten | 4:5e274bf85bf0 | 41 | } |
pnysten | 4:5e274bf85bf0 | 42 | |
pnysten | 4:5e274bf85bf0 | 43 | bool isFull() |
pnysten | 4:5e274bf85bf0 | 44 | { |
pnysten | 4:5e274bf85bf0 | 45 | mtx.lock(); |
pnysten | 4:5e274bf85bf0 | 46 | bool r = (((write + 1) % size) == read); |
pnysten | 4:5e274bf85bf0 | 47 | mtx.unlock(); |
pnysten | 4:5e274bf85bf0 | 48 | return r; |
pnysten | 4:5e274bf85bf0 | 49 | } |
pnysten | 4:5e274bf85bf0 | 50 | |
pnysten | 4:5e274bf85bf0 | 51 | bool isEmpty() |
pnysten | 4:5e274bf85bf0 | 52 | { |
pnysten | 4:5e274bf85bf0 | 53 | mtx.lock(); |
pnysten | 4:5e274bf85bf0 | 54 | bool r = (read == write); |
pnysten | 4:5e274bf85bf0 | 55 | mtx.unlock(); |
pnysten | 4:5e274bf85bf0 | 56 | return r; |
pnysten | 4:5e274bf85bf0 | 57 | } |
pnysten | 4:5e274bf85bf0 | 58 | |
pnysten | 4:5e274bf85bf0 | 59 | void queue(T k) |
pnysten | 4:5e274bf85bf0 | 60 | { |
pnysten | 4:5e274bf85bf0 | 61 | mtx.lock(); |
pnysten | 4:5e274bf85bf0 | 62 | while (((write + 1) % size) == read) //if (isFull()) |
pnysten | 4:5e274bf85bf0 | 63 | { |
pnysten | 4:5e274bf85bf0 | 64 | /*while((((write + 1) % size) == read)) |
pnysten | 4:5e274bf85bf0 | 65 | {*/ |
pnysten | 4:5e274bf85bf0 | 66 | mtx.unlock(); |
pnysten | 4:5e274bf85bf0 | 67 | Thread::wait(10); |
pnysten | 4:5e274bf85bf0 | 68 | mtx.lock(); |
pnysten | 4:5e274bf85bf0 | 69 | /*}*/ |
pnysten | 4:5e274bf85bf0 | 70 | //read++; |
pnysten | 4:5e274bf85bf0 | 71 | //read %= size; |
pnysten | 4:5e274bf85bf0 | 72 | } |
pnysten | 4:5e274bf85bf0 | 73 | buf[write++] = k; |
pnysten | 4:5e274bf85bf0 | 74 | write %= size; |
pnysten | 4:5e274bf85bf0 | 75 | mtx.unlock(); |
pnysten | 4:5e274bf85bf0 | 76 | } |
pnysten | 4:5e274bf85bf0 | 77 | |
pnysten | 4:5e274bf85bf0 | 78 | uint16_t available() |
pnysten | 4:5e274bf85bf0 | 79 | { |
pnysten | 4:5e274bf85bf0 | 80 | mtx.lock(); |
pnysten | 4:5e274bf85bf0 | 81 | uint16_t a = (write >= read) ? (write - read) : (size - read + write); |
pnysten | 4:5e274bf85bf0 | 82 | mtx.unlock(); |
pnysten | 4:5e274bf85bf0 | 83 | return a; |
pnysten | 4:5e274bf85bf0 | 84 | } |
pnysten | 4:5e274bf85bf0 | 85 | |
pnysten | 4:5e274bf85bf0 | 86 | bool dequeue(T * c) |
pnysten | 4:5e274bf85bf0 | 87 | { |
pnysten | 4:5e274bf85bf0 | 88 | mtx.lock(); |
pnysten | 4:5e274bf85bf0 | 89 | bool empty = (read == write); |
pnysten | 4:5e274bf85bf0 | 90 | if (!empty) |
pnysten | 4:5e274bf85bf0 | 91 | { |
pnysten | 4:5e274bf85bf0 | 92 | *c = buf[read++]; |
pnysten | 4:5e274bf85bf0 | 93 | read %= size; |
pnysten | 4:5e274bf85bf0 | 94 | } |
pnysten | 4:5e274bf85bf0 | 95 | mtx.unlock(); |
pnysten | 4:5e274bf85bf0 | 96 | return (!empty); |
pnysten | 4:5e274bf85bf0 | 97 | } |
pnysten | 4:5e274bf85bf0 | 98 | |
pnysten | 4:5e274bf85bf0 | 99 | private: |
pnysten | 4:5e274bf85bf0 | 100 | volatile uint16_t write; |
pnysten | 4:5e274bf85bf0 | 101 | volatile uint16_t read; |
pnysten | 4:5e274bf85bf0 | 102 | volatile T buf[size]; |
pnysten | 4:5e274bf85bf0 | 103 | Mutex mtx; |
pnysten | 4:5e274bf85bf0 | 104 | }; |
pnysten | 4:5e274bf85bf0 | 105 | |
pnysten | 4:5e274bf85bf0 | 106 | #endif |
pnysten | 4:5e274bf85bf0 | 107 |