@@ -97,10 +97,11 @@ void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue) {
97
97
98
98
// Reserve block in queue and return pointer to it.
99
99
uint8_t * CDC_ReceiveQueue_ReserveBlock (CDC_ReceiveQueue_TypeDef * queue ) {
100
- if ((uint16_t )(CDC_RECEIVE_QUEUE_BUFFER_SIZE - queue -> write ) >= CDC_QUEUE_MAX_PACKET_SIZE ) {
100
+ if ((uint16_t )(CDC_RECEIVE_QUEUE_BUFFER_SIZE - queue -> write ) >= CDC_QUEUE_MAX_PACKET_SIZE &&
101
+ (queue -> read <= queue -> write || (uint16_t )(queue -> read - queue -> write ) >= CDC_QUEUE_MAX_PACKET_SIZE )) {
101
102
// have enough space on the rest of buffer to store full-length packet
102
103
return & queue -> buffer [queue -> write ];
103
- } else if (queue -> read >= CDC_QUEUE_MAX_PACKET_SIZE ) {
104
+ } else if (queue -> read >= CDC_QUEUE_MAX_PACKET_SIZE && queue -> read <= queue -> write ) {
104
105
// have enough space on the beginning of buffer to store full-length packet
105
106
queue -> length = queue -> write ;
106
107
queue -> write = 0 ;
@@ -114,14 +115,17 @@ uint8_t *CDC_ReceiveQueue_ReserveBlock(CDC_ReceiveQueue_TypeDef *queue) {
114
115
// Commits block in queue and make it available for reading
115
116
void CDC_ReceiveQueue_CommitBlock (CDC_ReceiveQueue_TypeDef * queue , uint16_t size ) {
116
117
queue -> write += size ;
118
+ if (queue -> write >= queue -> length ) {
119
+ queue -> length = CDC_RECEIVE_QUEUE_BUFFER_SIZE ;
120
+ }
117
121
}
118
122
119
123
// Determine size, available for read
120
124
int CDC_ReceiveQueue_ReadSize (CDC_ReceiveQueue_TypeDef * queue ) {
121
125
if (queue -> write >= queue -> read ) {
122
126
return queue -> write - queue -> read ;
123
127
} else {
124
- return queue -> length - queue -> read ;
128
+ return queue -> length + queue -> write - queue -> read ;
125
129
}
126
130
}
127
131
0 commit comments