partly working USB Device lib for STM32F746NG Discovery both Interface are working
Dependents: DISCO-F746NG-USB_Device McLighTT project_Keyboard_to_the_Keyboard MIDIInstrumentPADProject ... more
USBSerial/CircBuffer.h@0:0a2eaa300982, 2016-07-31 (annotated)
- Committer:
- DieterGraef
- Date:
- Sun Jul 31 17:47:35 2016 +0000
- Revision:
- 0:0a2eaa300982
partly working USB Device library - serial and MIDI is working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DieterGraef | 0:0a2eaa300982 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
DieterGraef | 0:0a2eaa300982 | 2 | * |
DieterGraef | 0:0a2eaa300982 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
DieterGraef | 0:0a2eaa300982 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
DieterGraef | 0:0a2eaa300982 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
DieterGraef | 0:0a2eaa300982 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
DieterGraef | 0:0a2eaa300982 | 7 | * Software is furnished to do so, subject to the following conditions: |
DieterGraef | 0:0a2eaa300982 | 8 | * |
DieterGraef | 0:0a2eaa300982 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
DieterGraef | 0:0a2eaa300982 | 10 | * substantial portions of the Software. |
DieterGraef | 0:0a2eaa300982 | 11 | * |
DieterGraef | 0:0a2eaa300982 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
DieterGraef | 0:0a2eaa300982 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
DieterGraef | 0:0a2eaa300982 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
DieterGraef | 0:0a2eaa300982 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
DieterGraef | 0:0a2eaa300982 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
DieterGraef | 0:0a2eaa300982 | 17 | */ |
DieterGraef | 0:0a2eaa300982 | 18 | |
DieterGraef | 0:0a2eaa300982 | 19 | #ifndef USBCIRCBUFFER_H |
DieterGraef | 0:0a2eaa300982 | 20 | #define USBCIRCBUFFER_H |
DieterGraef | 0:0a2eaa300982 | 21 | |
DieterGraef | 0:0a2eaa300982 | 22 | template <class T, int Size> |
DieterGraef | 0:0a2eaa300982 | 23 | class USBCircBuffer { |
DieterGraef | 0:0a2eaa300982 | 24 | public: |
DieterGraef | 0:0a2eaa300982 | 25 | USBCircBuffer():write(0), read(0){} |
DieterGraef | 0:0a2eaa300982 | 26 | bool isFull() { |
DieterGraef | 0:0a2eaa300982 | 27 | return ((write + 1) % size == read); |
DieterGraef | 0:0a2eaa300982 | 28 | }; |
DieterGraef | 0:0a2eaa300982 | 29 | |
DieterGraef | 0:0a2eaa300982 | 30 | bool isEmpty() { |
DieterGraef | 0:0a2eaa300982 | 31 | return (read == write); |
DieterGraef | 0:0a2eaa300982 | 32 | }; |
DieterGraef | 0:0a2eaa300982 | 33 | |
DieterGraef | 0:0a2eaa300982 | 34 | void queue(T k) { |
DieterGraef | 0:0a2eaa300982 | 35 | if (isFull()) { |
DieterGraef | 0:0a2eaa300982 | 36 | read++; |
DieterGraef | 0:0a2eaa300982 | 37 | read %= size; |
DieterGraef | 0:0a2eaa300982 | 38 | } |
DieterGraef | 0:0a2eaa300982 | 39 | buf[write++] = k; |
DieterGraef | 0:0a2eaa300982 | 40 | write %= size; |
DieterGraef | 0:0a2eaa300982 | 41 | } |
DieterGraef | 0:0a2eaa300982 | 42 | |
DieterGraef | 0:0a2eaa300982 | 43 | uint16_t available() { |
DieterGraef | 0:0a2eaa300982 | 44 | return (write >= read) ? write - read : size - read + write; |
DieterGraef | 0:0a2eaa300982 | 45 | }; |
DieterGraef | 0:0a2eaa300982 | 46 | |
DieterGraef | 0:0a2eaa300982 | 47 | bool dequeue(T * c) { |
DieterGraef | 0:0a2eaa300982 | 48 | bool empty = isEmpty(); |
DieterGraef | 0:0a2eaa300982 | 49 | if (!empty) { |
DieterGraef | 0:0a2eaa300982 | 50 | *c = buf[read++]; |
DieterGraef | 0:0a2eaa300982 | 51 | read %= size; |
DieterGraef | 0:0a2eaa300982 | 52 | } |
DieterGraef | 0:0a2eaa300982 | 53 | return(!empty); |
DieterGraef | 0:0a2eaa300982 | 54 | }; |
DieterGraef | 0:0a2eaa300982 | 55 | |
DieterGraef | 0:0a2eaa300982 | 56 | private: |
DieterGraef | 0:0a2eaa300982 | 57 | volatile uint16_t write; |
DieterGraef | 0:0a2eaa300982 | 58 | volatile uint16_t read; |
DieterGraef | 0:0a2eaa300982 | 59 | static const int size = Size+1; //a modern optimizer should be able to remove this so it uses no ram. |
DieterGraef | 0:0a2eaa300982 | 60 | T buf[Size+1] ; |
DieterGraef | 0:0a2eaa300982 | 61 | }; |
DieterGraef | 0:0a2eaa300982 | 62 | |
DieterGraef | 0:0a2eaa300982 | 63 | #endif |