2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:19:16 2018 +0000
Revision:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sayzyas 43:78f328f311dc 1 /* mbed USBHost Library
sayzyas 43:78f328f311dc 2 * Copyright (c) 2006-2013 ARM Limited
sayzyas 43:78f328f311dc 3 *
sayzyas 43:78f328f311dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
sayzyas 43:78f328f311dc 5 * you may not use this file except in compliance with the License.
sayzyas 43:78f328f311dc 6 * You may obtain a copy of the License at
sayzyas 43:78f328f311dc 7 *
sayzyas 43:78f328f311dc 8 * http://www.apache.org/licenses/LICENSE-2.0
sayzyas 43:78f328f311dc 9 *
sayzyas 43:78f328f311dc 10 * Unless required by applicable law or agreed to in writing, software
sayzyas 43:78f328f311dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
sayzyas 43:78f328f311dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sayzyas 43:78f328f311dc 13 * See the License for the specific language governing permissions and
sayzyas 43:78f328f311dc 14 * limitations under the License.
sayzyas 43:78f328f311dc 15 */
sayzyas 43:78f328f311dc 16
sayzyas 43:78f328f311dc 17 #ifndef MTXCIRCBUFFER_H
sayzyas 43:78f328f311dc 18 #define MTXCIRCBUFFER_H
sayzyas 43:78f328f311dc 19
sayzyas 43:78f328f311dc 20 #include "stdint.h"
sayzyas 43:78f328f311dc 21 #include "rtos.h"
sayzyas 43:78f328f311dc 22
sayzyas 43:78f328f311dc 23 //Mutex protected circular buffer
sayzyas 43:78f328f311dc 24 template<typename T, int size>
sayzyas 43:78f328f311dc 25 class MtxCircBuffer {
sayzyas 43:78f328f311dc 26 public:
sayzyas 43:78f328f311dc 27
sayzyas 43:78f328f311dc 28 MtxCircBuffer() {
sayzyas 43:78f328f311dc 29 write = 0;
sayzyas 43:78f328f311dc 30 read = 0;
sayzyas 43:78f328f311dc 31 }
sayzyas 43:78f328f311dc 32
sayzyas 43:78f328f311dc 33 bool isFull() {
sayzyas 43:78f328f311dc 34 mtx.lock();
sayzyas 43:78f328f311dc 35 bool r = (((write + 1) % size) == read);
sayzyas 43:78f328f311dc 36 mtx.unlock();
sayzyas 43:78f328f311dc 37 return r;
sayzyas 43:78f328f311dc 38 }
sayzyas 43:78f328f311dc 39
sayzyas 43:78f328f311dc 40 bool isEmpty() {
sayzyas 43:78f328f311dc 41 mtx.lock();
sayzyas 43:78f328f311dc 42 bool r = (read == write);
sayzyas 43:78f328f311dc 43 mtx.unlock();
sayzyas 43:78f328f311dc 44 return r;
sayzyas 43:78f328f311dc 45 }
sayzyas 43:78f328f311dc 46
sayzyas 43:78f328f311dc 47 void flush() {
sayzyas 43:78f328f311dc 48 write = 0;
sayzyas 43:78f328f311dc 49 read = 0;
sayzyas 43:78f328f311dc 50 }
sayzyas 43:78f328f311dc 51
sayzyas 43:78f328f311dc 52 void queue(T k) {
sayzyas 43:78f328f311dc 53 mtx.lock();
sayzyas 43:78f328f311dc 54 while (((write + 1) % size) == read) {
sayzyas 43:78f328f311dc 55 mtx.unlock();
sayzyas 43:78f328f311dc 56 Thread::wait(10);
sayzyas 43:78f328f311dc 57 mtx.lock();
sayzyas 43:78f328f311dc 58 }
sayzyas 43:78f328f311dc 59 buf[write++] = k;
sayzyas 43:78f328f311dc 60 write %= size;
sayzyas 43:78f328f311dc 61 mtx.unlock();
sayzyas 43:78f328f311dc 62 }
sayzyas 43:78f328f311dc 63
sayzyas 43:78f328f311dc 64 uint16_t available() {
sayzyas 43:78f328f311dc 65 mtx.lock();
sayzyas 43:78f328f311dc 66 uint16_t a = (write >= read) ? (write - read) : (size - read + write);
sayzyas 43:78f328f311dc 67 mtx.unlock();
sayzyas 43:78f328f311dc 68 return a;
sayzyas 43:78f328f311dc 69 }
sayzyas 43:78f328f311dc 70
sayzyas 43:78f328f311dc 71 bool dequeue(T * c) {
sayzyas 43:78f328f311dc 72 mtx.lock();
sayzyas 43:78f328f311dc 73 bool empty = (read == write);
sayzyas 43:78f328f311dc 74 if (!empty) {
sayzyas 43:78f328f311dc 75 *c = buf[read++];
sayzyas 43:78f328f311dc 76 read %= size;
sayzyas 43:78f328f311dc 77 }
sayzyas 43:78f328f311dc 78 mtx.unlock();
sayzyas 43:78f328f311dc 79 return (!empty);
sayzyas 43:78f328f311dc 80 }
sayzyas 43:78f328f311dc 81
sayzyas 43:78f328f311dc 82 private:
sayzyas 43:78f328f311dc 83 volatile uint16_t write;
sayzyas 43:78f328f311dc 84 volatile uint16_t read;
sayzyas 43:78f328f311dc 85 volatile T buf[size];
sayzyas 43:78f328f311dc 86 Mutex mtx;
sayzyas 43:78f328f311dc 87 };
sayzyas 43:78f328f311dc 88
sayzyas 43:78f328f311dc 89 #endif