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