Faster uart_receive() in UDP mode

Added some functions for writing data to the ring buffer directly
Increased the size of ring buffer
This commit is contained in:
wh201906 2023-10-16 20:05:42 +08:00
parent d738f3171c
commit 703a2c968a
No known key found for this signature in database
3 changed files with 34 additions and 4 deletions

View file

@ -100,3 +100,19 @@ void RingBuf_destroy(RingBuffer* buffer) {
free(buffer->data);
free(buffer);
}
inline int RingBuf_getContinousAvailableSize(RingBuffer* buffer) {
const int availableSize = RingBuf_getAvailableSize(buffer);
const int continousSize = (buffer->capacity) - (buffer->rear);
return (availableSize < continousSize) ? availableSize : continousSize;
}
inline void RingBuf_postEnqueueBatch(RingBuffer* buffer, int count) {
// no check there
buffer->rear = (buffer->rear + count) % buffer->capacity;
buffer->size += count;
}
inline uint8_t* RingBuf_getRearPtr(RingBuffer* buffer) {
return buffer->data + buffer->rear;
}

View file

@ -23,4 +23,9 @@ int RingBuf_getUsedSize(RingBuffer* buffer);
int RingBuf_getAvailableSize(RingBuffer* buffer);
void RingBuf_destroy(RingBuffer* buffer);
// for direct write
int RingBuf_getContinousAvailableSize(RingBuffer* buffer);
void RingBuf_postEnqueueBatch(RingBuffer* buffer, int count);
uint8_t* RingBuf_getRearPtr(RingBuffer* buffer);
#endif

View file

@ -242,7 +242,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
}
sp->fd = sfd;
sp->udpBuffer = RingBuf_create(MAX(sizeof(PacketResponseNGRaw), sizeof(PacketResponseOLD)) * 20);
sp->udpBuffer = RingBuf_create(MAX(sizeof(PacketResponseNGRaw), sizeof(PacketResponseOLD)) * 30);
return sp;
}
@ -497,9 +497,18 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin
// For UDP connection, put the incoming data into the buffer and handle them in the next round
if (spu->udpBuffer != NULL) {
uint8_t recvBuf[MAX(sizeof(PacketResponseNGRaw), sizeof(PacketResponseOLD)) * 20];
res = read(spu->fd, recvBuf, RingBuf_getAvailableSize(spu->udpBuffer));
RingBuf_enqueueBatch(spu->udpBuffer, recvBuf, res);
if (RingBuf_getContinousAvailableSize(spu->udpBuffer) >= byteCount) {
// write to the buffer directly
res = read(spu->fd, RingBuf_getRearPtr(spu->udpBuffer), RingBuf_getAvailableSize(spu->udpBuffer));
if (res >= 0) {
RingBuf_postEnqueueBatch(spu->udpBuffer, res);
}
} else {
// use transit buffer
uint8_t transitBuf[MAX(sizeof(PacketResponseNGRaw), sizeof(PacketResponseOLD)) * 30];
res = read(spu->fd, transitBuf, RingBuf_getAvailableSize(spu->udpBuffer));
RingBuf_enqueueBatch(spu->udpBuffer, transitBuf, res);
}
continue;
}