@@ -67,32 +67,34 @@ void CDC_TransmitQueue_Enqueue(CDC_TransmitQueue_TypeDef *queue,
67
67
{
68
68
uint32_t sizeToEnd = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue -> write ;
69
69
if (sizeToEnd > size ) {
70
- memcpy (& queue -> buffer [ queue -> write ], & buffer [ 0 ] , size );
70
+ memcpy (queue -> buffer + queue -> write , buffer , size );
71
71
} else {
72
- memcpy (& queue -> buffer [ queue -> write ], & buffer [ 0 ] , sizeToEnd );
73
- memcpy (& queue -> buffer [ 0 ], & buffer [ sizeToEnd ] , size - sizeToEnd );
72
+ memcpy (queue -> buffer + queue -> write , buffer , sizeToEnd );
73
+ memcpy (queue -> buffer , buffer + sizeToEnd , size - sizeToEnd );
74
74
}
75
- queue -> write = (uint16_t )((queue -> write + size ) %
76
- CDC_TRANSMIT_QUEUE_BUFFER_SIZE );
75
+ queue -> write = (queue -> write + size ) % CDC_TRANSMIT_QUEUE_BUFFER_SIZE ;
77
76
}
78
77
79
78
// Read flat block from queue biggest as possible, but max QUEUE_MAX_PACKET_SIZE
80
79
uint8_t * CDC_TransmitQueue_ReadBlock (CDC_TransmitQueue_TypeDef * queue ,
81
- uint16_t * size )
80
+ uint32_t * size )
82
81
{
83
82
if (queue -> write >= queue -> read ) {
84
83
* size = queue -> write - queue -> read ;
85
84
} else {
86
85
* size = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue -> read ;
87
86
}
87
+ if (* size > CDC_TRANSMIT_MAX_BUFFER_SIZE ) {
88
+ * size = CDC_TRANSMIT_MAX_BUFFER_SIZE ;
89
+ }
90
+
88
91
queue -> reserved = * size ;
89
92
return & queue -> buffer [queue -> read ];
90
93
}
91
94
92
95
void CDC_TransmitQueue_CommitRead (CDC_TransmitQueue_TypeDef * queue )
93
96
{
94
- queue -> read = (queue -> read + queue -> reserved ) %
95
- CDC_TRANSMIT_QUEUE_BUFFER_SIZE ;
97
+ queue -> read = (queue -> read + queue -> reserved ) % CDC_TRANSMIT_QUEUE_BUFFER_SIZE ;
96
98
}
97
99
98
100
// Initialize read and write position of queue.
@@ -106,23 +108,23 @@ void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue)
106
108
// Reserve block in queue and return pointer to it.
107
109
uint8_t * CDC_ReceiveQueue_ReserveBlock (CDC_ReceiveQueue_TypeDef * queue )
108
110
{
109
- const uint16_t limit =
110
- CDC_RECEIVE_QUEUE_BUFFER_SIZE - CDC_QUEUE_MAX_PACKET_SIZE ;
111
- volatile uint16_t read = queue -> read ;
111
+ const uint32_t limit =
112
+ CDC_RECEIVE_QUEUE_BUFFER_SIZE - CDC_RECEIVE_MAX_BUFFER_SIZE ;
113
+ volatile uint32_t read = queue -> read ;
112
114
113
115
if (read <= queue -> write ) {
114
116
// if write is limited only by buffer size.
115
117
if (queue -> write < limit || (queue -> write == limit && read > 0 )) {
116
118
// if size in the rest of buffer is enough for full packet plus 1 byte
117
119
// or if it tight enough and write position can be set to 0
118
120
return queue -> buffer + queue -> write ;
119
- } else if (read > CDC_QUEUE_MAX_PACKET_SIZE ) {
121
+ } else if (read > CDC_RECEIVE_MAX_BUFFER_SIZE ) {
120
122
// if size in the rest is not enough, but enough size in head
121
123
queue -> length = queue -> write ;
122
124
queue -> write = 0 ;
123
125
return queue -> buffer + queue -> write ;
124
126
}
125
- } else if (queue -> write + CDC_QUEUE_MAX_PACKET_SIZE < read ) {
127
+ } else if (queue -> write + CDC_RECEIVE_MAX_BUFFER_SIZE < read ) {
126
128
// write position must be less than read position
127
129
// after reading largest possible packet
128
130
return queue -> buffer + queue -> write ;
@@ -132,7 +134,7 @@ uint8_t *CDC_ReceiveQueue_ReserveBlock(CDC_ReceiveQueue_TypeDef *queue)
132
134
133
135
// Commits block in queue and make it available for reading
134
136
void CDC_ReceiveQueue_CommitBlock (CDC_ReceiveQueue_TypeDef * queue ,
135
- uint16_t size )
137
+ uint32_t size )
136
138
{
137
139
queue -> write += size ;
138
140
if (queue -> write >= queue -> length ) {
@@ -148,8 +150,8 @@ int CDC_ReceiveQueue_ReadSize(CDC_ReceiveQueue_TypeDef *queue)
148
150
{
149
151
// reading length after write make guarantee, that length >= write
150
152
// and determined reading size will be smaller or equal than real one.
151
- volatile uint16_t write = queue -> write ;
152
- volatile uint16_t length = queue -> length ;
153
+ volatile uint32_t write = queue -> write ;
154
+ volatile uint32_t length = queue -> length ;
153
155
if (write >= queue -> read ) {
154
156
return write - queue -> read ;
155
157
}
@@ -159,8 +161,8 @@ int CDC_ReceiveQueue_ReadSize(CDC_ReceiveQueue_TypeDef *queue)
159
161
// Read one byte from queue.
160
162
int CDC_ReceiveQueue_Dequeue (CDC_ReceiveQueue_TypeDef * queue )
161
163
{
162
- volatile uint16_t write = queue -> write ;
163
- volatile uint16_t length = queue -> length ;
164
+ volatile uint32_t write = queue -> write ;
165
+ volatile uint32_t length = queue -> length ;
164
166
if (queue -> read == length ) {
165
167
queue -> read = 0 ;
166
168
}
@@ -177,8 +179,8 @@ int CDC_ReceiveQueue_Dequeue(CDC_ReceiveQueue_TypeDef *queue)
177
179
// Peek byte from queue.
178
180
int CDC_ReceiveQueue_Peek (CDC_ReceiveQueue_TypeDef * queue )
179
181
{
180
- volatile uint16_t write = queue -> write ;
181
- volatile uint16_t length = queue -> length ;
182
+ volatile uint32_t write = queue -> write ;
183
+ volatile uint32_t length = queue -> length ;
182
184
if (queue -> read >= length ) {
183
185
queue -> read = 0 ;
184
186
}
@@ -188,12 +190,12 @@ int CDC_ReceiveQueue_Peek(CDC_ReceiveQueue_TypeDef *queue)
188
190
return queue -> buffer [queue -> read ];
189
191
}
190
192
191
- uint16_t CDC_ReceiveQueue_Read (CDC_ReceiveQueue_TypeDef * queue ,
192
- uint8_t * buffer , uint16_t size )
193
+ uint32_t CDC_ReceiveQueue_Read (CDC_ReceiveQueue_TypeDef * queue ,
194
+ uint8_t * buffer , uint32_t size )
193
195
{
194
- volatile uint16_t write = queue -> write ;
195
- volatile uint16_t length = queue -> length ;
196
- uint16_t available ;
196
+ volatile uint32_t write = queue -> write ;
197
+ volatile uint32_t length = queue -> length ;
198
+ uint32_t available ;
197
199
198
200
if (queue -> read >= length ) {
199
201
queue -> read = 0 ;
@@ -216,11 +218,11 @@ uint16_t CDC_ReceiveQueue_Read(CDC_ReceiveQueue_TypeDef *queue,
216
218
}
217
219
218
220
bool CDC_ReceiveQueue_ReadUntil (CDC_ReceiveQueue_TypeDef * queue ,
219
- uint8_t terminator , uint8_t * buffer , uint16_t size , uint16_t * fetched )
221
+ uint8_t terminator , uint8_t * buffer , uint32_t size , uint32_t * fetched )
220
222
{
221
- volatile uint16_t write = queue -> write ;
222
- volatile uint16_t length = queue -> length ;
223
- uint16_t available ;
223
+ volatile uint32_t write = queue -> write ;
224
+ volatile uint32_t length = queue -> length ;
225
+ uint32_t available ;
224
226
225
227
if (queue -> read >= length ) {
226
228
queue -> read = 0 ;
@@ -235,10 +237,10 @@ bool CDC_ReceiveQueue_ReadUntil(CDC_ReceiveQueue_TypeDef *queue,
235
237
}
236
238
237
239
uint8_t * start = & queue -> buffer [queue -> read ];
238
- for (uint16_t i = 0 ; i < size ; i ++ ) {
240
+ for (uint32_t i = 0 ; i < size ; i ++ ) {
239
241
uint8_t ch = start [i ];
240
242
if (ch == terminator ) {
241
- queue -> read += (uint16_t )(i + 1 );
243
+ queue -> read += (uint32_t )(i + 1 );
242
244
if (queue -> read >= length ) {
243
245
queue -> read = 0 ;
244
246
}
0 commit comments