Repository navigation
Expand file tree
/
Copy pathcdc_queue.c
More file actions
262 lines (239 loc) · 8.48 KB
/
Copy pathcdc_queue.c
File metadata and controls
262 lines (239 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
******************************************************************************
* @file cdc_queue.c
* @author makarenya
* @version V1.0.0
* @date 23-December-2018
* @brief Provides methods to manipulate with CDC cyclic buffers
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifdef USBCON
#ifdef USBD_USE_CDC
#include "cdc_queue.h"
// Both queues are single-producer/single-consumer ring buffers accessed
// without locks: the producer only moves `write`, the consumer only moves `read`.
// Transmit queue: `read == write` means empty. One byte is always left free
// so that a full queue can be told from an empty one.
void CDC_TransmitQueue_Init(CDC_TransmitQueue_TypeDef *queue)
{
queue->read = 0;
queue->write = 0;
}
int CDC_TransmitQueue_WriteSize(CDC_TransmitQueue_TypeDef *queue)
{
return (queue->read + CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->write - 1)
% CDC_TRANSMIT_QUEUE_BUFFER_SIZE;
}
int CDC_TransmitQueue_ReadSize(CDC_TransmitQueue_TypeDef *queue)
{
return (queue->write + CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->read)
% CDC_TRANSMIT_QUEUE_BUFFER_SIZE;
}
void CDC_TransmitQueue_Enqueue(CDC_TransmitQueue_TypeDef *queue,
const uint8_t *buffer, uint32_t size)
{
uint32_t sizeToEnd = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->write;
if (sizeToEnd > size) {
memcpy(&queue->buffer[queue->write], &buffer[0], size);
} else {
memcpy(&queue->buffer[queue->write], &buffer[0], sizeToEnd);
memcpy(&queue->buffer[0], &buffer[sizeToEnd], size - sizeToEnd);
}
queue->write = (uint16_t)((queue->write + size) %
CDC_TRANSMIT_QUEUE_BUFFER_SIZE);
}
// Transmit read side: `reserved` remembers the size of the block handed out
// by ReadBlock, `read` only advances when CommitRead is called, so the block
// stays valid while USB is sending it.
uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue,
uint16_t *size)
{
if (queue->write >= queue->read) {
*size = queue->write - queue->read;
} else {
*size = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->read;
}
queue->reserved = *size;
return &queue->buffer[queue->read];
}
void CDC_TransmitQueue_CommitRead(CDC_TransmitQueue_TypeDef *queue)
{
queue->read = (queue->read + queue->reserved) %
CDC_TRANSMIT_QUEUE_BUFFER_SIZE;
}
// Receive queue: USB writes whole packets at `write` (ReserveBlock then
// CommitBlock) while the application consumes from `read`. A packet is never
// split, so when the end of the buffer is too close for a full packet the
// writer wraps early and `length` records where the valid data ends. `read`
// wraps to 0 when it reaches `length`. `write == read` means empty.
void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue)
{
queue->read = 0;
queue->write = 0;
queue->length = CDC_RECEIVE_QUEUE_BUFFER_SIZE;
}
uint8_t *CDC_ReceiveQueue_ReserveBlock(CDC_ReceiveQueue_TypeDef *queue)
{
const uint16_t limit =
CDC_RECEIVE_QUEUE_BUFFER_SIZE - CDC_QUEUE_MAX_PACKET_SIZE;
volatile uint16_t read = queue->read;
if (read <= queue->write) {
// if write is limited only by buffer size.
if (queue->write < limit || (queue->write == limit && read > 0)) {
// if size in the rest of buffer is enough for full packet plus 1 byte
// or if it tight enough and write position can be set to 0
return queue->buffer + queue->write;
} else if (read > CDC_QUEUE_MAX_PACKET_SIZE) {
// if size in the rest is not enough, but enough size in head
queue->length = queue->write;
queue->write = 0;
return queue->buffer + queue->write;
}
} else if (queue->write + CDC_QUEUE_MAX_PACKET_SIZE < read) {
// write position must be less than read position
// after reading largest possible packet
return queue->buffer + queue->write;
}
return 0;
}
void CDC_ReceiveQueue_CommitBlock(CDC_ReceiveQueue_TypeDef *queue,
uint16_t size)
{
queue->write += size;
if (queue->write >= queue->length) {
queue->length = CDC_RECEIVE_QUEUE_BUFFER_SIZE;
}
if (queue->write >= CDC_RECEIVE_QUEUE_BUFFER_SIZE) {
queue->write = 0;
}
}
int CDC_ReceiveQueue_ReadSize(CDC_ReceiveQueue_TypeDef *queue)
{
// reading length after write make guarantee, that length >= write
// and determined reading size will be smaller or equal than real one.
volatile uint16_t write = queue->write;
volatile uint16_t length = queue->length;
if (write >= queue->read) {
return write - queue->read;
}
return length + write - queue->read;
}
int CDC_ReceiveQueue_Dequeue(CDC_ReceiveQueue_TypeDef *queue)
{
volatile uint16_t write = queue->write;
volatile uint16_t length = queue->length;
if (queue->read == length) {
queue->read = 0;
}
if (write == queue->read) {
return -1;
}
uint8_t ch = queue->buffer[queue->read++];
if (queue->read >= length) {
queue->read = 0;
}
return ch;
}
int CDC_ReceiveQueue_Peek(CDC_ReceiveQueue_TypeDef *queue)
{
volatile uint16_t write = queue->write;
volatile uint16_t length = queue->length;
if (queue->read >= length) {
queue->read = 0;
}
if (write == queue->read) {
return -1;
}
return queue->buffer[queue->read];
}
uint16_t CDC_ReceiveQueue_Read(CDC_ReceiveQueue_TypeDef *queue,
uint8_t *buffer, uint16_t size)
{
volatile uint16_t write = queue->write;
volatile uint16_t length = queue->length;
uint16_t available;
if (queue->read >= length) {
queue->read = 0;
}
if (write >= queue->read) {
available = write - queue->read;
} else {
available = length - queue->read;
}
if (available < size) {
size = available;
}
memcpy(buffer, &queue->buffer[queue->read], size);
queue->read = queue->read + size;
if (queue->read >= length) {
queue->read = 0;
}
return size;
}
bool CDC_ReceiveQueue_ReadUntil(CDC_ReceiveQueue_TypeDef *queue,
uint8_t terminator, uint8_t *buffer, uint16_t size, uint16_t *fetched)
{
volatile uint16_t write = queue->write;
volatile uint16_t length = queue->length;
uint16_t available;
if (queue->read >= length) {
queue->read = 0;
}
if (write >= queue->read) {
available = write - queue->read;
} else {
available = length - queue->read;
}
if (available < size) {
size = available;
}
uint8_t *start = &queue->buffer[queue->read];
for (uint16_t i = 0; i < size; i++) {
uint8_t ch = start[i];
if (ch == terminator) {
queue->read += (uint16_t)(i + 1);
if (queue->read >= length) {
queue->read = 0;
}
*fetched = i;
return true;
} else {
buffer[i] = ch;
}
}
*fetched = size;
queue->read += size;
if (queue->read >= length) {
queue->read = 0;
}
return false;
}
#endif /* USBD_USE_CDC */
#endif /* USBCON */