added USB host
Dependencies: FATFileSystem mbed-rtos
Fork of USBHost by
USBHostSerial/MtxCircBuffer.h@0:a554658735bf, 2013-03-06 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Mar 06 16:27:14 2013 +0000
- Revision:
- 0:a554658735bf
- Child:
- 4:b320d68e98e7
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:a554658735bf | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
mbed_official | 0:a554658735bf | 2 | * |
mbed_official | 0:a554658735bf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 0:a554658735bf | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed_official | 0:a554658735bf | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed_official | 0:a554658735bf | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 0:a554658735bf | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 0:a554658735bf | 8 | * |
mbed_official | 0:a554658735bf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 0:a554658735bf | 10 | * substantial portions of the Software. |
mbed_official | 0:a554658735bf | 11 | * |
mbed_official | 0:a554658735bf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 0:a554658735bf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 0:a554658735bf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 0:a554658735bf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:a554658735bf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 0:a554658735bf | 17 | */ |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #ifndef MTXCIRCBUFFER_H |
mbed_official | 0:a554658735bf | 20 | #define MTXCIRCBUFFER_H |
mbed_official | 0:a554658735bf | 21 | |
mbed_official | 0:a554658735bf | 22 | #include "stdint.h" |
mbed_official | 0:a554658735bf | 23 | #include "rtos.h" |
mbed_official | 0:a554658735bf | 24 | |
mbed_official | 0:a554658735bf | 25 | //Mutex protected circular buffer |
mbed_official | 0:a554658735bf | 26 | template<typename T, int size> |
mbed_official | 0:a554658735bf | 27 | class MtxCircBuffer { |
mbed_official | 0:a554658735bf | 28 | public: |
mbed_official | 0:a554658735bf | 29 | |
mbed_official | 0:a554658735bf | 30 | MtxCircBuffer() { |
mbed_official | 0:a554658735bf | 31 | write = 0; |
mbed_official | 0:a554658735bf | 32 | read = 0; |
mbed_official | 0:a554658735bf | 33 | } |
mbed_official | 0:a554658735bf | 34 | |
mbed_official | 0:a554658735bf | 35 | bool isFull() { |
mbed_official | 0:a554658735bf | 36 | mtx.lock(); |
mbed_official | 0:a554658735bf | 37 | bool r = (((write + 1) % size) == read); |
mbed_official | 0:a554658735bf | 38 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 39 | return r; |
mbed_official | 0:a554658735bf | 40 | } |
mbed_official | 0:a554658735bf | 41 | |
mbed_official | 0:a554658735bf | 42 | bool isEmpty() { |
mbed_official | 0:a554658735bf | 43 | mtx.lock(); |
mbed_official | 0:a554658735bf | 44 | bool r = (read == write); |
mbed_official | 0:a554658735bf | 45 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 46 | return r; |
mbed_official | 0:a554658735bf | 47 | } |
mbed_official | 0:a554658735bf | 48 | |
mbed_official | 0:a554658735bf | 49 | void queue(T k) { |
mbed_official | 0:a554658735bf | 50 | mtx.lock(); |
mbed_official | 0:a554658735bf | 51 | while (((write + 1) % size) == read) { |
mbed_official | 0:a554658735bf | 52 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 53 | Thread::wait(10); |
mbed_official | 0:a554658735bf | 54 | mtx.lock(); |
mbed_official | 0:a554658735bf | 55 | } |
mbed_official | 0:a554658735bf | 56 | buf[write++] = k; |
mbed_official | 0:a554658735bf | 57 | write %= size; |
mbed_official | 0:a554658735bf | 58 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 59 | } |
mbed_official | 0:a554658735bf | 60 | |
mbed_official | 0:a554658735bf | 61 | uint16_t available() { |
mbed_official | 0:a554658735bf | 62 | mtx.lock(); |
mbed_official | 0:a554658735bf | 63 | uint16_t a = (write >= read) ? (write - read) : (size - read + write); |
mbed_official | 0:a554658735bf | 64 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 65 | return a; |
mbed_official | 0:a554658735bf | 66 | } |
mbed_official | 0:a554658735bf | 67 | |
mbed_official | 0:a554658735bf | 68 | bool dequeue(T * c) { |
mbed_official | 0:a554658735bf | 69 | mtx.lock(); |
mbed_official | 0:a554658735bf | 70 | bool empty = (read == write); |
mbed_official | 0:a554658735bf | 71 | if (!empty) { |
mbed_official | 0:a554658735bf | 72 | *c = buf[read++]; |
mbed_official | 0:a554658735bf | 73 | read %= size; |
mbed_official | 0:a554658735bf | 74 | } |
mbed_official | 0:a554658735bf | 75 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 76 | return (!empty); |
mbed_official | 0:a554658735bf | 77 | } |
mbed_official | 0:a554658735bf | 78 | |
mbed_official | 0:a554658735bf | 79 | private: |
mbed_official | 0:a554658735bf | 80 | volatile uint16_t write; |
mbed_official | 0:a554658735bf | 81 | volatile uint16_t read; |
mbed_official | 0:a554658735bf | 82 | volatile T buf[size]; |
mbed_official | 0:a554658735bf | 83 | Mutex mtx; |
mbed_official | 0:a554658735bf | 84 | }; |
mbed_official | 0:a554658735bf | 85 | |
mbed_official | 0:a554658735bf | 86 | #endif |
mbed_official | 0:a554658735bf | 87 | |
mbed_official | 0:a554658735bf | 88 |