Merge pull request #739 from armink/fix_uart_rx_dma
[DeviceDrivers] Fix uart rx dma
This commit is contained in:
commit
a38144407e
|
@ -199,15 +199,7 @@ static void dma_uart_rx_idle_isr(struct rt_serial_device *serial) {
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
|
|
||||||
recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_ch);
|
recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_ch);
|
||||||
if (recv_total_index >= uart->dma.last_recv_index)
|
|
||||||
{
|
|
||||||
recv_len = recv_total_index - uart->dma.last_recv_index;
|
recv_len = recv_total_index - uart->dma.last_recv_index;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index + recv_total_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
uart->dma.last_recv_index = recv_total_index;
|
uart->dma.last_recv_index = recv_total_index;
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
@ -226,23 +218,15 @@ static void dma_uart_rx_idle_isr(struct rt_serial_device *serial) {
|
||||||
*/
|
*/
|
||||||
static void dma_rx_done_isr(struct rt_serial_device *serial) {
|
static void dma_rx_done_isr(struct rt_serial_device *serial) {
|
||||||
struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
|
struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
|
||||||
rt_size_t recv_total_index, recv_len;
|
rt_size_t recv_len;
|
||||||
rt_base_t level;
|
rt_base_t level;
|
||||||
|
|
||||||
/* disable interrupt */
|
/* disable interrupt */
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
|
|
||||||
recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_ch);
|
recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index;
|
||||||
if (recv_total_index >= uart->dma.last_recv_index)
|
/* reset last recv index */
|
||||||
{
|
uart->dma.last_recv_index = 0;
|
||||||
recv_len = recv_total_index - uart->dma.last_recv_index;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index + recv_total_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
uart->dma.last_recv_index = recv_total_index;
|
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
|
@ -535,7 +519,7 @@ static void NVIC_Configuration(struct stm32_uart* uart)
|
||||||
/* Enable the USART1 Interrupt */
|
/* Enable the USART1 Interrupt */
|
||||||
NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
|
NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||||
NVIC_Init(&NVIC_InitStructure);
|
NVIC_Init(&NVIC_InitStructure);
|
||||||
}
|
}
|
||||||
|
@ -577,7 +561,7 @@ static void DMA_Configuration(struct rt_serial_device *serial) {
|
||||||
/* rx dma interrupt config */
|
/* rx dma interrupt config */
|
||||||
NVIC_InitStructure.NVIC_IRQChannel = uart->dma.rx_irq_ch;
|
NVIC_InitStructure.NVIC_IRQChannel = uart->dma.rx_irq_ch;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||||
NVIC_Init(&NVIC_InitStructure);
|
NVIC_Init(&NVIC_InitStructure);
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,15 +242,7 @@ static void dma_uart_rx_idle_isr(struct rt_serial_device *serial) {
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
|
|
||||||
recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_stream);
|
recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_stream);
|
||||||
if (recv_total_index >= uart->dma.last_recv_index)
|
|
||||||
{
|
|
||||||
recv_len = recv_total_index - uart->dma.last_recv_index;
|
recv_len = recv_total_index - uart->dma.last_recv_index;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index + recv_total_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
uart->dma.last_recv_index = recv_total_index;
|
uart->dma.last_recv_index = recv_total_index;
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
@ -269,7 +261,7 @@ static void dma_uart_rx_idle_isr(struct rt_serial_device *serial) {
|
||||||
static void dma_rx_done_isr(struct rt_serial_device *serial)
|
static void dma_rx_done_isr(struct rt_serial_device *serial)
|
||||||
{
|
{
|
||||||
struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
|
struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
|
||||||
rt_size_t recv_total_index, recv_len;
|
rt_size_t recv_len;
|
||||||
rt_base_t level;
|
rt_base_t level;
|
||||||
|
|
||||||
if (DMA_GetFlagStatus(uart->dma.rx_stream, uart->dma.rx_flag) != RESET)
|
if (DMA_GetFlagStatus(uart->dma.rx_stream, uart->dma.rx_flag) != RESET)
|
||||||
|
@ -277,17 +269,9 @@ static void dma_rx_done_isr(struct rt_serial_device *serial)
|
||||||
/* disable interrupt */
|
/* disable interrupt */
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
|
|
||||||
recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_stream);
|
recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index;
|
||||||
if (recv_total_index >= uart->dma.last_recv_index)
|
/* reset last recv index */
|
||||||
{
|
uart->dma.last_recv_index = 0;
|
||||||
recv_len = recv_total_index - uart->dma.last_recv_index;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index + recv_total_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
uart->dma.last_recv_index = recv_total_index;
|
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
|
@ -639,8 +623,8 @@ static void NVIC_Configuration(struct stm32_uart *uart)
|
||||||
|
|
||||||
/* Enable the USART1 Interrupt */
|
/* Enable the USART1 Interrupt */
|
||||||
NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
|
NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||||
NVIC_Init(&NVIC_InitStructure);
|
NVIC_Init(&NVIC_InitStructure);
|
||||||
}
|
}
|
||||||
|
@ -668,7 +652,7 @@ static void DMA_Configuration(struct rt_serial_device *serial) {
|
||||||
/* rx dma interrupt config */
|
/* rx dma interrupt config */
|
||||||
NVIC_InitStructure.NVIC_IRQChannel = uart->dma.rx_irq_ch;
|
NVIC_InitStructure.NVIC_IRQChannel = uart->dma.rx_irq_ch;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||||
NVIC_Init(&NVIC_InitStructure);
|
NVIC_Init(&NVIC_InitStructure);
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,6 +122,8 @@ struct rt_serial_rx_fifo
|
||||||
rt_uint8_t *buffer;
|
rt_uint8_t *buffer;
|
||||||
|
|
||||||
rt_uint16_t put_index, get_index;
|
rt_uint16_t put_index, get_index;
|
||||||
|
|
||||||
|
rt_bool_t is_full;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct rt_serial_tx_fifo
|
struct rt_serial_tx_fifo
|
||||||
|
|
|
@ -168,14 +168,21 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
|
||||||
*/
|
*/
|
||||||
static rt_size_t rt_dma_calc_recved_len(struct rt_serial_device *serial)
|
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;
|
struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
|
||||||
|
|
||||||
RT_ASSERT(rx_fifo != RT_NULL);
|
RT_ASSERT(rx_fifo != RT_NULL);
|
||||||
|
|
||||||
rx_length = (rx_fifo->put_index >= rx_fifo->get_index) ? (rx_fifo->put_index - rx_fifo->get_index):
|
if (rx_fifo->put_index > rx_fifo->get_index)
|
||||||
(serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
|
return rx_fifo->put_index - rx_fifo->get_index;
|
||||||
return rx_length;
|
else if (rx_fifo->put_index < rx_fifo->get_index)
|
||||||
|
return serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (rx_fifo->is_full)
|
||||||
|
return serial->config.bufsz;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -191,10 +198,12 @@ static void rt_dma_recv_update_get_index(struct rt_serial_device *serial, rt_siz
|
||||||
RT_ASSERT(rx_fifo != RT_NULL);
|
RT_ASSERT(rx_fifo != RT_NULL);
|
||||||
RT_ASSERT(len <= rt_dma_calc_recved_len(serial));
|
RT_ASSERT(len <= rt_dma_calc_recved_len(serial));
|
||||||
|
|
||||||
|
if (rx_fifo->is_full && len != 0) rx_fifo->is_full = RT_FALSE;
|
||||||
|
|
||||||
rx_fifo->get_index += len;
|
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;
|
rx_fifo->get_index %= serial->config.bufsz;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,7 +229,8 @@ static void rt_dma_recv_update_put_index(struct rt_serial_device *serial, rt_siz
|
||||||
/* force overwrite get index */
|
/* 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;
|
rx_fifo->get_index = rx_fifo->put_index;
|
||||||
|
rx_fifo->is_full = RT_TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -235,7 +245,8 @@ static void rt_dma_recv_update_put_index(struct rt_serial_device *serial, rt_siz
|
||||||
rx_fifo->put_index %= serial->config.bufsz;
|
rx_fifo->put_index %= serial->config.bufsz;
|
||||||
}
|
}
|
||||||
/* force overwrite get index */
|
/* force overwrite get index */
|
||||||
rx_fifo->get_index = rx_fifo->put_index + 1;
|
rx_fifo->get_index = rx_fifo->put_index;
|
||||||
|
rx_fifo->is_full = RT_TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
|
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
|
||||||
|
@ -252,7 +263,8 @@ rt_inline int _serial_dma_rx(struct rt_serial_device *serial, rt_uint8_t *data,
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
|
|
||||||
if (serial->config.bufsz == 0) {
|
if (serial->config.bufsz == 0)
|
||||||
|
{
|
||||||
int result = RT_EOK;
|
int result = RT_EOK;
|
||||||
struct rt_serial_rx_dma *rx_dma;
|
struct rt_serial_rx_dma *rx_dma;
|
||||||
|
|
||||||
|
@ -272,21 +284,23 @@ rt_inline int _serial_dma_rx(struct rt_serial_device *serial, rt_uint8_t *data,
|
||||||
|
|
||||||
rt_set_errno(result);
|
rt_set_errno(result);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
|
struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
|
||||||
rt_size_t recv_len = 0, fifo_recved_len = rt_dma_calc_recved_len(serial);
|
rt_size_t recv_len = 0, fifo_recved_len = rt_dma_calc_recved_len(serial);
|
||||||
|
|
||||||
RT_ASSERT(rx_fifo != RT_NULL);
|
RT_ASSERT(rx_fifo != RT_NULL);
|
||||||
|
|
||||||
if (length < fifo_recved_len) {
|
if (length < fifo_recved_len)
|
||||||
recv_len = length;
|
recv_len = length;
|
||||||
} else {
|
else
|
||||||
recv_len = fifo_recved_len;
|
recv_len = fifo_recved_len;
|
||||||
}
|
|
||||||
|
|
||||||
if (rx_fifo->get_index + recv_len < serial->config.bufsz) {
|
if (rx_fifo->get_index + recv_len < serial->config.bufsz)
|
||||||
rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index, recv_len);
|
rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index, recv_len);
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index,
|
rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index,
|
||||||
serial->config.bufsz - rx_fifo->get_index);
|
serial->config.bufsz - rx_fifo->get_index);
|
||||||
rt_memcpy(data + serial->config.bufsz - rx_fifo->get_index, rx_fifo->buffer,
|
rt_memcpy(data + serial->config.bufsz - rx_fifo->get_index, rx_fifo->buffer,
|
||||||
|
@ -398,6 +412,7 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||||
rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
|
rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
|
||||||
rx_fifo->put_index = 0;
|
rx_fifo->put_index = 0;
|
||||||
rx_fifo->get_index = 0;
|
rx_fifo->get_index = 0;
|
||||||
|
rx_fifo->is_full = RT_FALSE;
|
||||||
serial->serial_rx = rx_fifo;
|
serial->serial_rx = rx_fifo;
|
||||||
/* configure fifo address and length to low level device */
|
/* configure fifo address and length to low level device */
|
||||||
serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *) RT_DEVICE_FLAG_DMA_RX);
|
serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *) RT_DEVICE_FLAG_DMA_RX);
|
||||||
|
|
Loading…
Reference in New Issue