@@ -97,10 +97,11 @@ void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue) {
9797
9898// Reserve block in queue and return pointer to it.
9999uint8_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 )) {
101102 // have enough space on the rest of buffer to store full-length packet
102103 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 ) {
104105 // have enough space on the beginning of buffer to store full-length packet
105106 queue -> length = queue -> write ;
106107 queue -> write = 0 ;
@@ -114,14 +115,17 @@ uint8_t *CDC_ReceiveQueue_ReserveBlock(CDC_ReceiveQueue_TypeDef *queue) {
114115// Commits block in queue and make it available for reading
115116void CDC_ReceiveQueue_CommitBlock (CDC_ReceiveQueue_TypeDef * queue , uint16_t size ) {
116117 queue -> write += size ;
118+ if (queue -> write >= queue -> length ) {
119+ queue -> length = CDC_RECEIVE_QUEUE_BUFFER_SIZE ;
120+ }
117121}
118122
119123// Determine size, available for read
120124int CDC_ReceiveQueue_ReadSize (CDC_ReceiveQueue_TypeDef * queue ) {
121125 if (queue -> write >= queue -> read ) {
122126 return queue -> write - queue -> read ;
123127 } else {
124- return queue -> length - queue -> read ;
128+ return queue -> length + queue -> write - queue -> read ;
125129 }
126130}
127131
0 commit comments