Fork of https://developer.mbed.org/users/bscott/code/STM32_USBDevice/

Fork of STM32_USBDevice by Bradley Scott

Committer:
Troels Nilsson
Date:
Wed Jul 25 14:04:48 2018 +0200
Revision:
79:d28244984385
Parent:
77:a98f786d05d4
Merge feature_WebUSB into default

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:80ab0d068708 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 1:80ab0d068708 2 *
samux 1:80ab0d068708 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:80ab0d068708 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 1:80ab0d068708 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 1:80ab0d068708 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 1:80ab0d068708 7 * Software is furnished to do so, subject to the following conditions:
samux 1:80ab0d068708 8 *
samux 1:80ab0d068708 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:80ab0d068708 10 * substantial portions of the Software.
samux 1:80ab0d068708 11 *
samux 1:80ab0d068708 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:80ab0d068708 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:80ab0d068708 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:80ab0d068708 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:80ab0d068708 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:80ab0d068708 17 */
samux 1:80ab0d068708 18
samux 1:80ab0d068708 19 #ifndef CIRCBUFFER_H
samux 1:80ab0d068708 20 #define CIRCBUFFER_H
samux 1:80ab0d068708 21
mbed_official 51:deafa44182d9 22 template <class T, int Size>
samux 1:80ab0d068708 23 class CircBuffer {
samux 1:80ab0d068708 24 public:
mbed_official 51:deafa44182d9 25 CircBuffer():write(0), read(0){}
samux 1:80ab0d068708 26 bool isFull() {
samux 1:80ab0d068708 27 return ((write + 1) % size == read);
samux 1:80ab0d068708 28 };
samux 1:80ab0d068708 29
samux 1:80ab0d068708 30 bool isEmpty() {
samux 1:80ab0d068708 31 return (read == write);
samux 1:80ab0d068708 32 };
samux 1:80ab0d068708 33
samux 1:80ab0d068708 34 void queue(T k) {
samux 1:80ab0d068708 35 if (isFull()) {
samux 1:80ab0d068708 36 read++;
samux 1:80ab0d068708 37 read %= size;
samux 1:80ab0d068708 38 }
samux 1:80ab0d068708 39 buf[write++] = k;
samux 1:80ab0d068708 40 write %= size;
samux 1:80ab0d068708 41 }
samux 1:80ab0d068708 42
samux 1:80ab0d068708 43 uint16_t available() {
samux 1:80ab0d068708 44 return (write >= read) ? write - read : size - read + write;
samux 1:80ab0d068708 45 };
samux 1:80ab0d068708 46
samux 1:80ab0d068708 47 bool dequeue(T * c) {
samux 1:80ab0d068708 48 bool empty = isEmpty();
samux 1:80ab0d068708 49 if (!empty) {
samux 1:80ab0d068708 50 *c = buf[read++];
samux 1:80ab0d068708 51 read %= size;
samux 1:80ab0d068708 52 }
samux 1:80ab0d068708 53 return(!empty);
samux 1:80ab0d068708 54 };
samux 1:80ab0d068708 55
Troels Nilsson 77:a98f786d05d4 56 void flush()
Troels Nilsson 77:a98f786d05d4 57 {
Troels Nilsson 77:a98f786d05d4 58 write = 0;
Troels Nilsson 77:a98f786d05d4 59 read = 0;
Troels Nilsson 77:a98f786d05d4 60 }
Troels Nilsson 77:a98f786d05d4 61
Troels Nilsson 77:a98f786d05d4 62 // Queue a block of data of blockSize items
Troels Nilsson 77:a98f786d05d4 63 void queue(const T *block, uint16_t blockSize)
Troels Nilsson 77:a98f786d05d4 64 {
Troels Nilsson 77:a98f786d05d4 65 if (blockSize >= size)
Troels Nilsson 77:a98f786d05d4 66 {
Troels Nilsson 77:a98f786d05d4 67 // Block is too big to fit in buffer, take the last size-1 items
Troels Nilsson 77:a98f786d05d4 68 block = &block[blockSize - (size-1)];
Troels Nilsson 77:a98f786d05d4 69 blockSize = size-1;
Troels Nilsson 77:a98f786d05d4 70 }
Troels Nilsson 77:a98f786d05d4 71
Troels Nilsson 77:a98f786d05d4 72 if (write + blockSize > size)
Troels Nilsson 77:a98f786d05d4 73 {
Troels Nilsson 77:a98f786d05d4 74 // Need to wrap around
Troels Nilsson 77:a98f786d05d4 75 std::memcpy(&buf[write], block, sizeof(T)*(size-write));
Troels Nilsson 77:a98f786d05d4 76 std::memcpy(buf, &block[size-write], sizeof(T)*(blockSize - (size-write)));
Troels Nilsson 77:a98f786d05d4 77 }
Troels Nilsson 77:a98f786d05d4 78 else
Troels Nilsson 77:a98f786d05d4 79 {
Troels Nilsson 77:a98f786d05d4 80 std::memcpy(&buf[write], block, sizeof(T)*blockSize);
Troels Nilsson 77:a98f786d05d4 81 }
Troels Nilsson 77:a98f786d05d4 82
Troels Nilsson 77:a98f786d05d4 83 // Update write position
Troels Nilsson 77:a98f786d05d4 84 uint16_t wasFree = available() - size - 1;
Troels Nilsson 77:a98f786d05d4 85 write = write + blockSize;
Troels Nilsson 77:a98f786d05d4 86 write %= size;
Troels Nilsson 77:a98f786d05d4 87 if (wasFree < blockSize)
Troels Nilsson 77:a98f786d05d4 88 {
Troels Nilsson 77:a98f786d05d4 89 // Update read position as well
Troels Nilsson 77:a98f786d05d4 90 read = write + 1;
Troels Nilsson 77:a98f786d05d4 91 read %= size;
Troels Nilsson 77:a98f786d05d4 92 }
Troels Nilsson 77:a98f786d05d4 93 }
Troels Nilsson 77:a98f786d05d4 94
Troels Nilsson 77:a98f786d05d4 95 // Dequeue a block of data of at most blockSize items, writing them into block
Troels Nilsson 77:a98f786d05d4 96 // Returns the number of items dequeued
Troels Nilsson 77:a98f786d05d4 97 uint16_t dequeue(T *block, uint16_t blockSize)
Troels Nilsson 77:a98f786d05d4 98 {
Troels Nilsson 77:a98f786d05d4 99 if (isEmpty())
Troels Nilsson 77:a98f786d05d4 100 {
Troels Nilsson 77:a98f786d05d4 101 return 0;
Troels Nilsson 77:a98f786d05d4 102 }
Troels Nilsson 77:a98f786d05d4 103
Troels Nilsson 77:a98f786d05d4 104 uint16_t isAvailable = available();
Troels Nilsson 77:a98f786d05d4 105 if (isAvailable < blockSize)
Troels Nilsson 77:a98f786d05d4 106 {
Troels Nilsson 77:a98f786d05d4 107 // Only return what we have
Troels Nilsson 77:a98f786d05d4 108 blockSize = isAvailable;
Troels Nilsson 77:a98f786d05d4 109 }
Troels Nilsson 77:a98f786d05d4 110
Troels Nilsson 77:a98f786d05d4 111 if (read + blockSize > size)
Troels Nilsson 77:a98f786d05d4 112 {
Troels Nilsson 77:a98f786d05d4 113 // Need to wrap around
Troels Nilsson 77:a98f786d05d4 114 std::memcpy(block, &buf[read], sizeof(T)*(size-read));
Troels Nilsson 77:a98f786d05d4 115 std::memcpy(&block[size-read], buf, sizeof(T)*(blockSize - (size-read)));
Troels Nilsson 77:a98f786d05d4 116 }
Troels Nilsson 77:a98f786d05d4 117 else
Troels Nilsson 77:a98f786d05d4 118 {
Troels Nilsson 77:a98f786d05d4 119 std::memcpy(block, &buf[read], sizeof(T)*blockSize);
Troels Nilsson 77:a98f786d05d4 120 }
Troels Nilsson 77:a98f786d05d4 121
Troels Nilsson 77:a98f786d05d4 122 // Update read position
Troels Nilsson 77:a98f786d05d4 123 read = read + blockSize;
Troels Nilsson 77:a98f786d05d4 124 read %= size;
Troels Nilsson 77:a98f786d05d4 125
Troels Nilsson 77:a98f786d05d4 126 return blockSize;
Troels Nilsson 77:a98f786d05d4 127 }
Troels Nilsson 77:a98f786d05d4 128
samux 1:80ab0d068708 129 private:
samux 1:80ab0d068708 130 volatile uint16_t write;
samux 1:80ab0d068708 131 volatile uint16_t read;
mbed_official 51:deafa44182d9 132 static const int size = Size+1; //a modern optimizer should be able to remove this so it uses no ram.
mbed_official 56:151ba33713ff 133 T buf[Size+1];
samux 1:80ab0d068708 134 };
samux 1:80ab0d068708 135
samux 1:80ab0d068708 136 #endif