2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 04:20:29 2018 +0000
Revision:
44:ef52682c423e
Parent:
43:1675750cca08
2018.07.26;

Who changed what in which revision?

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