Skip to content

Commit e4147a5

Browse files
committed
Optimize serial poll mode
1 parent f5321a5 commit e4147a5

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/hal/serial/serial.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ static rt_size_t serial_poll_rx(struct serial_device* serial, rt_uint8_t* data,
4646
rx_length = size - length;
4747

4848
/* invoke callback */
49-
if (serial->parent.rx_indicate != RT_NULL && rx_length) {
49+
if (serial->parent.rx_indicate != RT_NULL && rx_length > 0)
5050
serial->parent.rx_indicate(&serial->parent, rx_length);
51-
}
5251

5352
return rx_length;
5453
}
@@ -175,7 +174,7 @@ static void dma_recv_update_get_index(struct serial_device* serial, rt_size_t le
175174
}
176175

177176
if (len > dma_calc_recved_len(serial)) {
178-
HAL_DBG("length:%d exceeds the maxium received length\n", len);
177+
HAL_DBG("length:%ld exceeds the maxium received length\n", len);
179178
return;
180179
}
181180

@@ -337,11 +336,11 @@ static rt_err_t hal_serial_open(struct rt_device* dev, rt_uint16_t oflag)
337336

338337
/* create rx fifo to store received rx data */
339338
rx_fifo = (struct serial_rx_fifo*)rt_malloc(sizeof(struct serial_rx_fifo) + serial->config.bufsz);
340-
if(rx_fifo == RT_NULL) {
339+
if (rx_fifo == RT_NULL) {
341340
HAL_DBG("serial rx fifo create error\n");
342341
return RT_ENOMEM;
343342
}
344-
343+
345344
rx_fifo->buffer = (rt_uint8_t*)(rx_fifo + 1);
346345
rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
347346
rx_fifo->put_index = 0;
@@ -436,6 +435,8 @@ static rt_size_t hal_serial_read(struct rt_device* dev,
436435
rt_size_t size)
437436
{
438437
struct serial_device* serial;
438+
rt_size_t rx_cnt = 0;
439+
rt_int32_t timeout = pos;
439440

440441
if (size == 0) {
441442
return 0;
@@ -452,9 +453,6 @@ static rt_size_t hal_serial_read(struct rt_device* dev,
452453
serial = (struct serial_device*)dev;
453454

454455
if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX) {
455-
rt_size_t rx_cnt = 0;
456-
rt_int32_t timeout = pos;
457-
458456
/* try to read data */
459457
rx_cnt = serial_dma_rx(serial, buffer, size);
460458

@@ -483,9 +481,6 @@ static rt_size_t hal_serial_read(struct rt_device* dev,
483481

484482
return rx_cnt;
485483
} else if (dev->open_flag & RT_DEVICE_FLAG_INT_RX) {
486-
rt_size_t rx_cnt = 0;
487-
rt_int32_t timeout = pos;
488-
489484
/* try to read data */
490485
rx_cnt = serial_int_rx(serial, buffer, size);
491486

@@ -514,7 +509,29 @@ static rt_size_t hal_serial_read(struct rt_device* dev,
514509

515510
return rx_cnt;
516511
} else {
517-
return serial_poll_rx(serial, buffer, size);
512+
/* try to read data */
513+
rx_cnt = serial_poll_rx(serial, buffer, size);
514+
515+
/* if timeout is not 0, then check if required length data read */
516+
if (timeout != 0) {
517+
uint32_t time_start = systime_now_ms();
518+
519+
/* if not enough data reveived, wait it */
520+
while (rx_cnt < size) {
521+
if (timeout > 0) {
522+
if ((systime_now_ms() - time_start) >= timeout) {
523+
/* timeout */
524+
break;
525+
}
526+
}
527+
/* read rest data */
528+
rx_cnt += serial_poll_rx(serial, (void*)((uint32_t)buffer + rx_cnt), size - rx_cnt);
529+
/* sleep to do not block the system */
530+
sys_msleep(1);
531+
}
532+
}
533+
534+
return rx_cnt;
518535
}
519536
}
520537

@@ -759,4 +776,4 @@ rt_err_t hal_serial_register(struct serial_device* serial,
759776

760777
/* register character to system */
761778
return rt_device_register(device, name, flag);
762-
}
779+
}

0 commit comments

Comments
 (0)