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