USBHost custom library.
Dependents: GR-PEACH_HVC-P2_sample_client mbed-os-storage-access GR-PEACH_Digital_Signage GR-PEACH_Audio_Playback_Sample ... more
Fork of USBHost by
USBHostSerial/CircBufferHostSerial.h@38:dd168a3e4194, 2017-03-14 (annotated)
- Committer:
- dkato
- Date:
- Tue Mar 14 05:38:25 2017 +0000
- Revision:
- 38:dd168a3e4194
Delete FATFileSystem.lib.
; Fix stack size.
; Change to get USBHostSerial's buffer from heap.
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 38:dd168a3e4194 | 1 | /* mbed USBHost Library |
dkato | 38:dd168a3e4194 | 2 | * Copyright (c) 2006-2013 ARM Limited |
dkato | 38:dd168a3e4194 | 3 | * |
dkato | 38:dd168a3e4194 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 38:dd168a3e4194 | 5 | * you may not use this file except in compliance with the License. |
dkato | 38:dd168a3e4194 | 6 | * You may obtain a copy of the License at |
dkato | 38:dd168a3e4194 | 7 | * |
dkato | 38:dd168a3e4194 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 38:dd168a3e4194 | 9 | * |
dkato | 38:dd168a3e4194 | 10 | * Unless required by applicable law or agreed to in writing, software |
dkato | 38:dd168a3e4194 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 38:dd168a3e4194 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 38:dd168a3e4194 | 13 | * See the License for the specific language governing permissions and |
dkato | 38:dd168a3e4194 | 14 | * limitations under the License. |
dkato | 38:dd168a3e4194 | 15 | */ |
dkato | 38:dd168a3e4194 | 16 | |
dkato | 38:dd168a3e4194 | 17 | #ifndef CIRCBUFFERHOSTSERIAL_H |
dkato | 38:dd168a3e4194 | 18 | #define CIRCBUFFERHOSTSERIAL_H |
dkato | 38:dd168a3e4194 | 19 | |
dkato | 38:dd168a3e4194 | 20 | #include "stdint.h" |
dkato | 38:dd168a3e4194 | 21 | |
dkato | 38:dd168a3e4194 | 22 | template<typename T, int size> |
dkato | 38:dd168a3e4194 | 23 | class CircBufferHostSerial { |
dkato | 38:dd168a3e4194 | 24 | public: |
dkato | 38:dd168a3e4194 | 25 | |
dkato | 38:dd168a3e4194 | 26 | CircBufferHostSerial() { |
dkato | 38:dd168a3e4194 | 27 | write = 0; |
dkato | 38:dd168a3e4194 | 28 | read = 0; |
dkato | 38:dd168a3e4194 | 29 | } |
dkato | 38:dd168a3e4194 | 30 | |
dkato | 38:dd168a3e4194 | 31 | bool isFull() { |
dkato | 38:dd168a3e4194 | 32 | return (((write + 1) % size) == read); |
dkato | 38:dd168a3e4194 | 33 | } |
dkato | 38:dd168a3e4194 | 34 | |
dkato | 38:dd168a3e4194 | 35 | bool isEmpty() { |
dkato | 38:dd168a3e4194 | 36 | return (read == write); |
dkato | 38:dd168a3e4194 | 37 | } |
dkato | 38:dd168a3e4194 | 38 | |
dkato | 38:dd168a3e4194 | 39 | void flush() { |
dkato | 38:dd168a3e4194 | 40 | write = 0; |
dkato | 38:dd168a3e4194 | 41 | read = 0; |
dkato | 38:dd168a3e4194 | 42 | } |
dkato | 38:dd168a3e4194 | 43 | |
dkato | 38:dd168a3e4194 | 44 | bool queue(T k) { |
dkato | 38:dd168a3e4194 | 45 | if (isFull()) { |
dkato | 38:dd168a3e4194 | 46 | return false; |
dkato | 38:dd168a3e4194 | 47 | } |
dkato | 38:dd168a3e4194 | 48 | buf[write] = k; |
dkato | 38:dd168a3e4194 | 49 | write = (write + 1) % size; |
dkato | 38:dd168a3e4194 | 50 | return true; |
dkato | 38:dd168a3e4194 | 51 | } |
dkato | 38:dd168a3e4194 | 52 | |
dkato | 38:dd168a3e4194 | 53 | uint32_t available() { |
dkato | 38:dd168a3e4194 | 54 | uint32_t a = (write >= read) ? (write - read) : (size - read + write); |
dkato | 38:dd168a3e4194 | 55 | return a; |
dkato | 38:dd168a3e4194 | 56 | } |
dkato | 38:dd168a3e4194 | 57 | |
dkato | 38:dd168a3e4194 | 58 | bool dequeue(T * c) { |
dkato | 38:dd168a3e4194 | 59 | if (isEmpty()) { |
dkato | 38:dd168a3e4194 | 60 | return false; |
dkato | 38:dd168a3e4194 | 61 | } |
dkato | 38:dd168a3e4194 | 62 | *c = buf[read]; |
dkato | 38:dd168a3e4194 | 63 | read = (read + 1) % size; |
dkato | 38:dd168a3e4194 | 64 | return true; |
dkato | 38:dd168a3e4194 | 65 | } |
dkato | 38:dd168a3e4194 | 66 | |
dkato | 38:dd168a3e4194 | 67 | private: |
dkato | 38:dd168a3e4194 | 68 | volatile uint32_t write; |
dkato | 38:dd168a3e4194 | 69 | volatile uint32_t read; |
dkato | 38:dd168a3e4194 | 70 | volatile T buf[size]; |
dkato | 38:dd168a3e4194 | 71 | }; |
dkato | 38:dd168a3e4194 | 72 | |
dkato | 38:dd168a3e4194 | 73 | #endif |
dkato | 38:dd168a3e4194 | 74 |