[DeviceDrivers] Enhanced serial DMA rx driver reliability.
This commit is contained in:
parent
282c2f22f2
commit
afe3b16106
|
@ -166,8 +166,9 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
|
|||
*
|
||||
* @return length
|
||||
*/
|
||||
static rt_size_t rt_dma_calc_recved_len(struct rt_serial_device *serial) {
|
||||
static rt_size_t rx_length;
|
||||
static rt_size_t rt_dma_calc_recved_len(struct rt_serial_device *serial)
|
||||
{
|
||||
rt_size_t rx_length;
|
||||
struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
|
||||
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
@ -183,14 +184,16 @@ static rt_size_t rt_dma_calc_recved_len(struct rt_serial_device *serial) {
|
|||
* @param serial serial device
|
||||
* @param len get data length for this operate
|
||||
*/
|
||||
static void rt_dma_recv_update_get_index(struct rt_serial_device *serial, rt_size_t len) {
|
||||
static void rt_dma_recv_update_get_index(struct rt_serial_device *serial, rt_size_t len)
|
||||
{
|
||||
struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
|
||||
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
RT_ASSERT(len <= rt_dma_calc_recved_len(serial));
|
||||
|
||||
rx_fifo->get_index += len;
|
||||
if (rx_fifo->get_index > serial->config.bufsz ) {
|
||||
if (rx_fifo->get_index > serial->config.bufsz)
|
||||
{
|
||||
rx_fifo->get_index -= serial->config.bufsz;
|
||||
}
|
||||
}
|
||||
|
@ -201,37 +204,41 @@ static void rt_dma_recv_update_get_index(struct rt_serial_device *serial, rt_siz
|
|||
* @param serial serial device
|
||||
* @param len received length for this transmit
|
||||
*/
|
||||
static void rt_dma_recv_update_put_index(struct rt_serial_device *serial, rt_size_t len) {
|
||||
static void rt_dma_recv_update_put_index(struct rt_serial_device *serial, rt_size_t len)
|
||||
{
|
||||
struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
|
||||
rt_size_t i;
|
||||
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
||||
if (rx_fifo->get_index <= rx_fifo->put_index) {
|
||||
if (rx_fifo->get_index <= rx_fifo->put_index)
|
||||
{
|
||||
rx_fifo->put_index += len;
|
||||
/* beyond the fifo end */
|
||||
if (rx_fifo->put_index >= serial->config.bufsz) {
|
||||
for (i = 0; i <= len / serial->config.bufsz; i++) {
|
||||
rx_fifo->put_index -= serial->config.bufsz;
|
||||
}
|
||||
if (rx_fifo->put_index >= serial->config.bufsz)
|
||||
{
|
||||
rx_fifo->put_index %= serial->config.bufsz;
|
||||
/* force overwrite get index */
|
||||
if (rx_fifo->put_index >= rx_fifo->get_index) {
|
||||
if (rx_fifo->put_index >= rx_fifo->get_index)
|
||||
{
|
||||
rx_fifo->get_index = rx_fifo->put_index + 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
rx_fifo->put_index += len;
|
||||
if(rx_fifo->put_index >= rx_fifo->get_index) {
|
||||
/* beyond the fifo end */
|
||||
if(rx_fifo->put_index >= serial->config.bufsz) {
|
||||
for (i = 0; i <= len / serial->config.bufsz; i++) {
|
||||
rx_fifo->put_index -= serial->config.bufsz;
|
||||
}
|
||||
else
|
||||
{
|
||||
rx_fifo->put_index += len;
|
||||
if (rx_fifo->put_index >= rx_fifo->get_index)
|
||||
{
|
||||
/* beyond the fifo end */
|
||||
if (rx_fifo->put_index >= serial->config.bufsz)
|
||||
{
|
||||
rx_fifo->put_index %= serial->config.bufsz;
|
||||
}
|
||||
/* force overwrite get index */
|
||||
rx_fifo->get_index = rx_fifo->put_index + 1;
|
||||
}
|
||||
}
|
||||
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -754,11 +761,13 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
|
|||
case RT_SERIAL_EVENT_RX_DMADONE:
|
||||
{
|
||||
int length;
|
||||
rt_base_t level;
|
||||
|
||||
/* get DMA rx length */
|
||||
length = (event & (~0xff)) >> 8;
|
||||
|
||||
if (serial->config.bufsz == 0) {
|
||||
if (serial->config.bufsz == 0)
|
||||
{
|
||||
struct rt_serial_rx_dma* rx_dma;
|
||||
|
||||
rx_dma = (struct rt_serial_rx_dma*) serial->serial_rx;
|
||||
|
@ -767,12 +776,21 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
|
|||
RT_ASSERT(serial->parent.rx_indicate != RT_NULL);
|
||||
serial->parent.rx_indicate(&(serial->parent), length);
|
||||
rx_dma->activated = RT_FALSE;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
/* update fifo put index */
|
||||
rt_dma_recv_update_put_index(serial, length);
|
||||
/* calculate received total length */
|
||||
length = rt_dma_calc_recved_len(serial);
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
/* invoke callback */
|
||||
if (serial->parent.rx_indicate != RT_NULL) {
|
||||
serial->parent.rx_indicate(&(serial->parent), rt_dma_calc_recved_len(serial));
|
||||
if (serial->parent.rx_indicate != RT_NULL)
|
||||
{
|
||||
serial->parent.rx_indicate(&(serial->parent), length);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue