[DeviceDriver][serialv2] Support configure when serial is open.

This commit is contained in:
朱天龙 (Armink) 2022-10-07 00:13:51 +08:00 committed by guo
parent 250de80e9a
commit 748f6afd5e
1 changed files with 30 additions and 18 deletions

View File

@ -12,7 +12,7 @@
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "UART"
#define DBG_TAG "Serial"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
@ -254,7 +254,7 @@ static rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb,
}
static rt_size_t rt_serial_update_write_index(struct rt_ringbuffer *rb,
rt_uint16_t write_index)
rt_uint16_t write_size)
{
rt_uint16_t size;
RT_ASSERT(rb != RT_NULL);
@ -262,27 +262,28 @@ static rt_size_t rt_serial_update_write_index(struct rt_ringbuffer *rb,
/* whether has enough space */
size = rt_ringbuffer_space_len(rb);
/* no space */
if (size == 0)
return 0;
/* no space, drop some data */
if (size < write_size)
{
write_size = size;
#if !defined(RT_USING_ULOG) || defined(ULOG_USING_ISR_LOG)
LOG_W("The serial buffer (len %d) is overflow.", rb->buffer_size);
#endif
}
/* drop some data */
if (size < write_index)
write_index = size;
if (rb->buffer_size - rb->write_index > write_index)
if (rb->buffer_size - rb->write_index > write_size)
{
/* this should not cause overflow because there is enough space for
* length of data in current mirror */
rb->write_index += write_index;
return write_index;
rb->write_index += write_size;
return write_size;
}
/* we are going into the other side of the mirror */
rb->write_mirror = ~rb->write_mirror;
rb->write_index = write_index - (rb->buffer_size - rb->write_index);
rb->write_index = write_size - (rb->buffer_size - rb->write_index);
return write_index;
return write_size;
}
@ -463,6 +464,12 @@ static rt_size_t _serial_fifo_tx_blocking_nbuf(struct rt_device *dev,
tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
RT_ASSERT(tx_fifo != RT_NULL);
if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
{
/* using poll tx when the scheduler not startup or in stream mode */
return _serial_poll_tx(dev, pos, buffer, size);
}
/* When serial transmit in tx_blocking mode,
* if the activated mode is RT_TRUE, it will return directly */
if (tx_fifo->activated == RT_TRUE) return 0;
@ -1121,15 +1128,15 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
if (args != RT_NULL)
{
struct serial_configure *pconfig = (struct serial_configure *) args;
if (serial->parent.ref_count)
if (((pconfig->rx_bufsz != serial->config.rx_bufsz) || (pconfig->tx_bufsz != serial->config.tx_bufsz))
&& serial->parent.ref_count)
{
/*can not change buffer size*/
return -RT_EBUSY;
}
/* set serial configure */
/* set serial configure */
serial->config = *pconfig;
serial->ops->configure(serial,
(struct serial_configure *) args);
serial->ops->configure(serial, (struct serial_configure *) args);
}
break;
@ -1477,13 +1484,18 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
struct rt_serial_rx_fifo *rx_fifo;
rt_size_t rx_length = 0;
rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
rt_base_t level;
RT_ASSERT(rx_fifo != RT_NULL);
/* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
rx_length = (event & (~0xff)) >> 8;
if (rx_length)
{ /* RT_SERIAL_EVENT_RX_DMADONE MODE */
level = rt_hw_interrupt_disable();
rt_serial_update_write_index(&(rx_fifo->rb), rx_length);
rt_hw_interrupt_enable(level);
}
/* Get the length of the data from the ringbuffer */
rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);