2018-11-29 17:00:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2019-01-22 10:00:45 +08:00
|
|
|
* 2018-10-30 SummerGift first version
|
2020-03-23 15:35:27 +08:00
|
|
|
* 2020-03-16 SummerGift add device close feature
|
|
|
|
* 2020-03-20 SummerGift fix bug caused by ORE
|
2020-05-02 15:09:06 +08:00
|
|
|
* 2020-05-02 whj4674672 support stm32h7 uart dma
|
2018-11-29 17:00:22 +08:00
|
|
|
*/
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
#include "board.h"
|
|
|
|
#include "drv_usart.h"
|
|
|
|
#include "drv_config.h"
|
|
|
|
|
|
|
|
#ifdef RT_USING_SERIAL
|
|
|
|
|
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.usart"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
2019-09-10 19:17:38 +08:00
|
|
|
#if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && \
|
|
|
|
!defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && \
|
|
|
|
!defined(BSP_USING_UART7) && !defined(BSP_USING_UART8) && !defined(BSP_USING_LPUART1)
|
2020-03-23 15:35:27 +08:00
|
|
|
#error "Please define at least one BSP_USING_UARTx"
|
|
|
|
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
|
|
|
|
2018-12-26 10:43:16 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
2020-03-23 15:35:27 +08:00
|
|
|
static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag);
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
UART1_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART2
|
|
|
|
UART2_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART3
|
|
|
|
UART3_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART4
|
|
|
|
UART4_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART5
|
|
|
|
UART5_INDEX,
|
|
|
|
#endif
|
2019-04-02 19:13:51 +08:00
|
|
|
#ifdef BSP_USING_UART6
|
|
|
|
UART6_INDEX,
|
|
|
|
#endif
|
2019-09-10 19:17:38 +08:00
|
|
|
#ifdef BSP_USING_UART7
|
|
|
|
UART7_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART8
|
|
|
|
UART8_INDEX,
|
|
|
|
#endif
|
2019-01-22 15:07:51 +08:00
|
|
|
#ifdef BSP_USING_LPUART1
|
|
|
|
LPUART1_INDEX,
|
|
|
|
#endif
|
2018-11-29 17:00:22 +08:00
|
|
|
};
|
|
|
|
|
2019-01-08 11:58:57 +08:00
|
|
|
static struct stm32_uart_config uart_config[] =
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
#ifdef BSP_USING_UART1
|
2019-05-03 20:52:31 +08:00
|
|
|
UART1_CONFIG,
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART2
|
2019-05-03 20:52:31 +08:00
|
|
|
UART2_CONFIG,
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART3
|
2019-05-03 20:52:31 +08:00
|
|
|
UART3_CONFIG,
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART4
|
2019-05-03 20:52:31 +08:00
|
|
|
UART4_CONFIG,
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART5
|
2019-05-03 20:52:31 +08:00
|
|
|
UART5_CONFIG,
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
2019-04-02 19:13:51 +08:00
|
|
|
#ifdef BSP_USING_UART6
|
2019-05-03 20:52:31 +08:00
|
|
|
UART6_CONFIG,
|
2019-04-02 19:13:51 +08:00
|
|
|
#endif
|
2019-09-10 19:17:38 +08:00
|
|
|
#ifdef BSP_USING_UART7
|
|
|
|
UART7_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART8
|
|
|
|
UART8_CONFIG,
|
|
|
|
#endif
|
2019-01-22 15:07:51 +08:00
|
|
|
#ifdef BSP_USING_LPUART1
|
2019-05-03 20:52:31 +08:00
|
|
|
LPUART1_CONFIG,
|
2019-01-22 15:07:51 +08:00
|
|
|
#endif
|
2018-11-29 17:00:22 +08:00
|
|
|
};
|
|
|
|
|
2019-01-08 11:58:57 +08:00
|
|
|
static struct stm32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
2019-09-21 13:51:04 +08:00
|
|
|
|
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2018-11-29 17:00:22 +08:00
|
|
|
uart->handle.Instance = uart->config->Instance;
|
|
|
|
uart->handle.Init.BaudRate = cfg->baud_rate;
|
|
|
|
uart->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
|
|
uart->handle.Init.Mode = UART_MODE_TX_RX;
|
|
|
|
uart->handle.Init.OverSampling = UART_OVERSAMPLING_16;
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
switch (cfg->data_bits)
|
|
|
|
{
|
|
|
|
case DATA_BITS_8:
|
|
|
|
uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
|
|
|
|
break;
|
|
|
|
case DATA_BITS_9:
|
|
|
|
uart->handle.Init.WordLength = UART_WORDLENGTH_9B;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
switch (cfg->stop_bits)
|
|
|
|
{
|
|
|
|
case STOP_BITS_1:
|
|
|
|
uart->handle.Init.StopBits = UART_STOPBITS_1;
|
|
|
|
break;
|
|
|
|
case STOP_BITS_2:
|
|
|
|
uart->handle.Init.StopBits = UART_STOPBITS_2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
uart->handle.Init.StopBits = UART_STOPBITS_1;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
switch (cfg->parity)
|
|
|
|
{
|
|
|
|
case PARITY_NONE:
|
|
|
|
uart->handle.Init.Parity = UART_PARITY_NONE;
|
|
|
|
break;
|
|
|
|
case PARITY_ODD:
|
|
|
|
uart->handle.Init.Parity = UART_PARITY_ODD;
|
|
|
|
break;
|
|
|
|
case PARITY_EVEN:
|
|
|
|
uart->handle.Init.Parity = UART_PARITY_EVEN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
uart->handle.Init.Parity = UART_PARITY_NONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:35:27 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
|
|
|
uart->dma_rx.last_index = 0;
|
|
|
|
#endif
|
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
if (HAL_UART_Init(&uart->handle) != HAL_OK)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2018-12-10 09:48:01 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
2018-12-26 10:43:16 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
2018-11-29 17:00:22 +08:00
|
|
|
rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
|
|
|
|
#endif
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2019-09-21 13:51:04 +08:00
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
|
|
|
/* disable rx irq */
|
|
|
|
NVIC_DisableIRQ(uart->config->irq_type);
|
|
|
|
/* disable interrupt */
|
|
|
|
__HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE);
|
2020-03-23 15:35:27 +08:00
|
|
|
|
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
|
|
|
/* disable DMA */
|
|
|
|
if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX)
|
|
|
|
{
|
|
|
|
HAL_NVIC_DisableIRQ(uart->config->dma_rx->dma_irq);
|
|
|
|
if (HAL_DMA_Abort(&(uart->dma_rx.handle)) != HAL_OK)
|
|
|
|
{
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HAL_DMA_DeInit(&(uart->dma_rx.handle)) != HAL_OK)
|
|
|
|
{
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ctrl_arg == RT_DEVICE_FLAG_DMA_TX)
|
|
|
|
{
|
|
|
|
HAL_NVIC_DisableIRQ(uart->config->dma_tx->dma_irq);
|
|
|
|
if (HAL_DMA_DeInit(&(uart->dma_tx.handle)) != HAL_OK)
|
|
|
|
{
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-29 17:00:22 +08:00
|
|
|
break;
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
/* enable rx irq */
|
2020-06-04 10:16:18 +08:00
|
|
|
HAL_NVIC_SetPriority(uart->config->irq_type, 1, 0);
|
|
|
|
HAL_NVIC_EnableIRQ(uart->config->irq_type);
|
2018-11-29 17:00:22 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
__HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_RXNE);
|
|
|
|
break;
|
|
|
|
|
2018-12-26 10:43:16 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
2018-11-29 17:00:22 +08:00
|
|
|
case RT_DEVICE_CTRL_CONFIG:
|
2019-05-03 20:52:31 +08:00
|
|
|
stm32_dma_config(serial, ctrl_arg);
|
2018-11-29 17:00:22 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2020-03-23 15:35:27 +08:00
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_CLOSE:
|
|
|
|
if (HAL_UART_DeInit(&(uart->handle)) != HAL_OK )
|
|
|
|
{
|
|
|
|
RT_ASSERT(0)
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_putc(struct rt_serial_device *serial, char c)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
2019-09-21 13:51:04 +08:00
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2018-12-10 21:39:28 +08:00
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
2019-01-22 15:07:51 +08:00
|
|
|
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
|
2019-10-03 22:29:13 +08:00
|
|
|
|| defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
|
|
|
|
|| defined(SOC_SERIES_STM32G4)
|
2018-11-29 17:00:22 +08:00
|
|
|
uart->handle.Instance->TDR = c;
|
|
|
|
#else
|
|
|
|
uart->handle.Instance->DR = c;
|
|
|
|
#endif
|
|
|
|
while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_getc(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2019-09-21 13:51:04 +08:00
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
ch = -1;
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
|
|
|
|
{
|
2019-01-22 15:07:51 +08:00
|
|
|
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
|
2019-10-03 22:29:13 +08:00
|
|
|
|| defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
|
|
|
|
|| defined(SOC_SERIES_STM32G4)
|
2018-11-29 17:00:22 +08:00
|
|
|
ch = uart->handle.Instance->RDR & 0xff;
|
|
|
|
#else
|
|
|
|
ch = uart->handle.Instance->DR & 0xff;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
static rt_size_t stm32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2020-03-23 15:35:27 +08:00
|
|
|
RT_ASSERT(buf != RT_NULL);
|
2019-09-21 13:51:04 +08:00
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
if (RT_SERIAL_DMA_TX == direction)
|
|
|
|
{
|
|
|
|
if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) == HAL_OK)
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
/**
|
|
|
|
* Uart common interrupt process. This need add to uart ISR.
|
|
|
|
*
|
|
|
|
* @param serial serial device
|
|
|
|
*/
|
|
|
|
static void uart_isr(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
2018-12-26 10:43:16 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
2018-11-29 17:00:22 +08:00
|
|
|
rt_size_t recv_total_index, recv_len;
|
|
|
|
rt_base_t level;
|
|
|
|
#endif
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2019-09-21 13:51:04 +08:00
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
/* UART in mode Receiver -------------------------------------------------*/
|
|
|
|
if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&
|
2019-05-03 18:16:16 +08:00
|
|
|
(__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
}
|
2018-12-26 10:43:16 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
2019-05-03 20:52:31 +08:00
|
|
|
else if ((uart->uart_dma_flag) && (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_IDLE) != RESET)
|
|
|
|
&& (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_IDLE) != RESET))
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
level = rt_hw_interrupt_disable();
|
2019-05-03 20:52:31 +08:00
|
|
|
recv_total_index = serial->config.bufsz - __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
|
|
|
|
recv_len = recv_total_index - uart->dma_rx.last_index;
|
|
|
|
uart->dma_rx.last_index = recv_total_index;
|
2018-11-29 17:00:22 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
if (recv_len)
|
|
|
|
{
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
|
|
|
|
}
|
2018-12-26 10:43:16 +08:00
|
|
|
__HAL_UART_CLEAR_IDLEFLAG(&uart->handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
2020-03-23 15:35:27 +08:00
|
|
|
else if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) &&
|
|
|
|
(__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_TC) != RESET))
|
2019-05-03 20:52:31 +08:00
|
|
|
{
|
|
|
|
if ((serial->parent.open_flag & RT_DEVICE_FLAG_DMA_TX) != 0)
|
|
|
|
{
|
|
|
|
HAL_UART_IRQHandler(&(uart->handle));
|
|
|
|
}
|
2020-03-23 15:35:27 +08:00
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
2019-05-03 20:52:31 +08:00
|
|
|
}
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_ORE) != RESET)
|
|
|
|
{
|
2018-12-26 10:43:16 +08:00
|
|
|
__HAL_UART_CLEAR_OREFLAG(&uart->handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_NE) != RESET)
|
|
|
|
{
|
2018-12-26 10:43:16 +08:00
|
|
|
__HAL_UART_CLEAR_NEFLAG(&uart->handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_FE) != RESET)
|
|
|
|
{
|
2018-12-26 10:43:16 +08:00
|
|
|
__HAL_UART_CLEAR_FEFLAG(&uart->handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_PE) != RESET)
|
|
|
|
{
|
2018-12-26 10:43:16 +08:00
|
|
|
__HAL_UART_CLEAR_PEFLAG(&uart->handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
2019-01-22 15:07:51 +08:00
|
|
|
#if !defined(SOC_SERIES_STM32L4) && !defined(SOC_SERIES_STM32F7) && !defined(SOC_SERIES_STM32F0) \
|
2019-10-03 22:29:13 +08:00
|
|
|
&& !defined(SOC_SERIES_STM32L0) && !defined(SOC_SERIES_STM32G0) && !defined(SOC_SERIES_STM32H7) \
|
|
|
|
&& !defined(SOC_SERIES_STM32G4)
|
2018-11-29 17:00:22 +08:00
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBD) != RESET)
|
|
|
|
{
|
2018-12-10 21:39:28 +08:00
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBD);
|
|
|
|
}
|
2018-12-26 10:43:16 +08:00
|
|
|
#endif
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_CTS) != RESET)
|
2018-12-10 21:39:28 +08:00
|
|
|
{
|
2018-12-26 10:43:16 +08:00
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_CTS);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
|
|
|
|
{
|
2018-12-10 21:39:28 +08:00
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
|
|
|
|
{
|
2018-12-10 21:39:28 +08:00
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
|
|
|
|
{
|
2018-12-10 21:39:28 +08:00
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:16:16 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
|
|
|
static void dma_isr(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
rt_size_t recv_total_index, recv_len;
|
|
|
|
rt_base_t level;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2019-09-21 13:51:04 +08:00
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2019-05-03 18:16:16 +08:00
|
|
|
|
2019-05-19 22:26:56 +08:00
|
|
|
if ((__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_TC) != RESET) ||
|
|
|
|
(__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_HT) != RESET))
|
2019-05-03 18:16:16 +08:00
|
|
|
{
|
|
|
|
level = rt_hw_interrupt_disable();
|
2019-05-19 22:26:56 +08:00
|
|
|
recv_total_index = serial->config.bufsz - __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
|
2019-05-03 18:16:16 +08:00
|
|
|
if (recv_total_index == 0)
|
|
|
|
{
|
2019-05-19 22:26:56 +08:00
|
|
|
recv_len = serial->config.bufsz - uart->dma_rx.last_index;
|
2019-05-03 18:16:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-19 22:26:56 +08:00
|
|
|
recv_len = recv_total_index - uart->dma_rx.last_index;
|
2019-05-03 18:16:16 +08:00
|
|
|
}
|
2019-05-19 22:26:56 +08:00
|
|
|
uart->dma_rx.last_index = recv_total_index;
|
2019-05-03 18:16:16 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
if (recv_len)
|
|
|
|
{
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
#if defined(BSP_USING_UART1)
|
|
|
|
void USART1_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART1_INDEX].serial));
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
|
2019-01-17 12:53:52 +08:00
|
|
|
void UART1_DMA_RX_IRQHandler(void)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_rx.handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA) */
|
2019-05-03 20:52:31 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA)
|
|
|
|
void UART1_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA) */
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif /* BSP_USING_UART1 */
|
|
|
|
|
|
|
|
#if defined(BSP_USING_UART2)
|
|
|
|
void USART2_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART2_INDEX].serial));
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA)
|
2019-01-17 12:53:52 +08:00
|
|
|
void UART2_DMA_RX_IRQHandler(void)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART2_INDEX].dma_rx.handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA) */
|
2019-05-03 20:52:31 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_TX_USING_DMA)
|
|
|
|
void UART2_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART2_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_TX_USING_DMA) */
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif /* BSP_USING_UART2 */
|
|
|
|
|
|
|
|
#if defined(BSP_USING_UART3)
|
|
|
|
void USART3_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART3_INDEX].serial));
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_RX_USING_DMA)
|
2019-01-17 12:53:52 +08:00
|
|
|
void UART3_DMA_RX_IRQHandler(void)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART3_INDEX].dma_rx.handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#endif /* defined(BSP_UART_USING_DMA_RX) && defined(BSP_UART3_RX_USING_DMA) */
|
2019-05-03 20:52:31 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_TX_USING_DMA)
|
|
|
|
void UART3_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART3_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(BSP_UART_USING_DMA_TX) && defined(BSP_UART3_TX_USING_DMA) */
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif /* BSP_USING_UART3*/
|
|
|
|
|
|
|
|
#if defined(BSP_USING_UART4)
|
|
|
|
void UART4_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART4_INDEX].serial));
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART4_RX_USING_DMA)
|
2019-01-17 12:53:52 +08:00
|
|
|
void UART4_DMA_RX_IRQHandler(void)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART4_INDEX].dma_rx.handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#endif /* defined(BSP_UART_USING_DMA_RX) && defined(BSP_UART4_RX_USING_DMA) */
|
2019-05-03 20:52:31 +08:00
|
|
|
|
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART4_TX_USING_DMA)
|
|
|
|
void UART4_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART4_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(BSP_UART_USING_DMA_TX) && defined(BSP_UART4_TX_USING_DMA) */
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif /* BSP_USING_UART4*/
|
|
|
|
|
|
|
|
#if defined(BSP_USING_UART5)
|
|
|
|
void UART5_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART5_INDEX].serial));
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA)
|
2019-01-17 12:53:52 +08:00
|
|
|
void UART5_DMA_RX_IRQHandler(void)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART5_INDEX].dma_rx.handle);
|
2018-11-29 17:00:22 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-01-08 11:58:57 +08:00
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA) */
|
2019-05-03 20:52:31 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_TX_USING_DMA)
|
|
|
|
void UART5_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART5_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_TX_USING_DMA) */
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif /* BSP_USING_UART5*/
|
|
|
|
|
2019-04-02 19:13:51 +08:00
|
|
|
#if defined(BSP_USING_UART6)
|
|
|
|
void USART6_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART6_INDEX].serial));
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2019-04-02 19:13:51 +08:00
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_RX_USING_DMA)
|
|
|
|
void UART6_DMA_RX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART6_INDEX].dma_rx.handle);
|
2019-04-02 19:13:51 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_RX_USING_DMA) */
|
2019-05-03 20:52:31 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_TX_USING_DMA)
|
|
|
|
void UART6_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART6_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_TX_USING_DMA) */
|
2019-04-02 19:13:51 +08:00
|
|
|
#endif /* BSP_USING_UART6*/
|
|
|
|
|
2019-09-10 19:17:38 +08:00
|
|
|
#if defined(BSP_USING_UART7)
|
|
|
|
void UART7_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART7_INDEX].serial));
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_RX_USING_DMA)
|
|
|
|
void UART7_DMA_RX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART7_INDEX].dma_rx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_RX_USING_DMA) */
|
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_TX_USING_DMA)
|
|
|
|
void UART7_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART7_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_TX_USING_DMA) */
|
|
|
|
#endif /* BSP_USING_UART7*/
|
|
|
|
|
|
|
|
#if defined(BSP_USING_UART8)
|
|
|
|
void UART8_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[UART8_INDEX].serial));
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_RX_USING_DMA)
|
|
|
|
void UART8_DMA_RX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART8_INDEX].dma_rx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_RX_USING_DMA) */
|
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_TX_USING_DMA)
|
|
|
|
void UART8_DMA_TX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_DMA_IRQHandler(&uart_obj[UART8_INDEX].dma_tx.handle);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_TX_USING_DMA) */
|
|
|
|
#endif /* BSP_USING_UART8*/
|
|
|
|
|
2019-01-22 15:07:51 +08:00
|
|
|
#if defined(BSP_USING_LPUART1)
|
2019-02-16 12:40:43 +08:00
|
|
|
void LPUART1_IRQHandler(void)
|
2019-01-22 15:07:51 +08:00
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
uart_isr(&(uart_obj[LPUART1_INDEX].serial));
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2019-01-22 15:07:51 +08:00
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
2019-02-16 12:40:43 +08:00
|
|
|
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_LPUART1_RX_USING_DMA)
|
|
|
|
void LPUART1_DMA_RX_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_DMA_IRQHandler(&uart_obj[LPUART1_INDEX].dma_rx.handle);
|
2019-02-16 12:40:43 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_LPUART1_RX_USING_DMA) */
|
|
|
|
#endif /* BSP_USING_LPUART1*/
|
2019-01-22 15:07:51 +08:00
|
|
|
|
2020-03-23 15:35:27 +08:00
|
|
|
static void stm32_uart_get_dma_config(void)
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
uart_obj[UART1_INDEX].uart_dma_flag = 0;
|
|
|
|
#ifdef BSP_UART1_RX_USING_DMA
|
|
|
|
uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
|
|
|
|
static struct dma_config uart1_dma_rx = UART1_DMA_RX_CONFIG;
|
|
|
|
uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx;
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_UART1_TX_USING_DMA
|
|
|
|
uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
|
|
|
|
static struct dma_config uart1_dma_tx = UART1_DMA_TX_CONFIG;
|
|
|
|
uart_config[UART1_INDEX].dma_tx = &uart1_dma_tx;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART2
|
|
|
|
uart_obj[UART2_INDEX].uart_dma_flag = 0;
|
|
|
|
#ifdef BSP_UART2_RX_USING_DMA
|
|
|
|
uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
|
|
|
|
static struct dma_config uart2_dma_rx = UART2_DMA_RX_CONFIG;
|
|
|
|
uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx;
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_UART2_TX_USING_DMA
|
|
|
|
uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
|
|
|
|
static struct dma_config uart2_dma_tx = UART2_DMA_TX_CONFIG;
|
|
|
|
uart_config[UART2_INDEX].dma_tx = &uart2_dma_tx;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART3
|
|
|
|
uart_obj[UART3_INDEX].uart_dma_flag = 0;
|
|
|
|
#ifdef BSP_UART3_RX_USING_DMA
|
|
|
|
uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
|
|
|
|
static struct dma_config uart3_dma_rx = UART3_DMA_RX_CONFIG;
|
|
|
|
uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx;
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_UART3_TX_USING_DMA
|
|
|
|
uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
|
|
|
|
static struct dma_config uart3_dma_tx = UART3_DMA_TX_CONFIG;
|
|
|
|
uart_config[UART3_INDEX].dma_tx = &uart3_dma_tx;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART4
|
|
|
|
uart_obj[UART4_INDEX].uart_dma_flag = 0;
|
|
|
|
#ifdef BSP_UART4_RX_USING_DMA
|
|
|
|
uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
|
|
|
|
static struct dma_config uart4_dma_rx = UART4_DMA_RX_CONFIG;
|
|
|
|
uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx;
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_UART4_TX_USING_DMA
|
|
|
|
uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
|
|
|
|
static struct dma_config uart4_dma_tx = UART4_DMA_TX_CONFIG;
|
|
|
|
uart_config[UART4_INDEX].dma_tx = &uart4_dma_tx;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART5
|
|
|
|
uart_obj[UART5_INDEX].uart_dma_flag = 0;
|
|
|
|
#ifdef BSP_UART5_RX_USING_DMA
|
|
|
|
uart_obj[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
|
|
|
|
static struct dma_config uart5_dma_rx = UART5_DMA_RX_CONFIG;
|
|
|
|
uart_config[UART5_INDEX].dma_rx = &uart5_dma_rx;
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_UART5_TX_USING_DMA
|
|
|
|
uart_obj[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
|
|
|
|
static struct dma_config uart5_dma_tx = UART5_DMA_TX_CONFIG;
|
|
|
|
uart_config[UART5_INDEX].dma_tx = &uart5_dma_tx;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_UART6
|
|
|
|
uart_obj[UART6_INDEX].uart_dma_flag = 0;
|
|
|
|
#ifdef BSP_UART6_RX_USING_DMA
|
|
|
|
uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
|
|
|
|
static struct dma_config uart6_dma_rx = UART6_DMA_RX_CONFIG;
|
|
|
|
uart_config[UART6_INDEX].dma_rx = &uart6_dma_rx;
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_UART6_TX_USING_DMA
|
|
|
|
uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
|
|
|
|
static struct dma_config uart6_dma_tx = UART6_DMA_TX_CONFIG;
|
|
|
|
uart_config[UART6_INDEX].dma_tx = &uart6_dma_tx;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-26 10:43:16 +08:00
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
2019-05-03 20:52:31 +08:00
|
|
|
static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
2019-05-20 10:44:08 +08:00
|
|
|
struct rt_serial_rx_fifo *rx_fifo;
|
2019-05-03 20:52:31 +08:00
|
|
|
DMA_HandleTypeDef *DMA_Handle;
|
|
|
|
struct dma_config *dma_config;
|
2019-05-20 10:21:00 +08:00
|
|
|
struct stm32_uart *uart;
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2019-05-20 10:21:00 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2019-09-21 13:51:04 +08:00
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
2019-05-03 20:52:31 +08:00
|
|
|
|
|
|
|
if (RT_DEVICE_FLAG_DMA_RX == flag)
|
|
|
|
{
|
|
|
|
DMA_Handle = &uart->dma_rx.handle;
|
|
|
|
dma_config = uart->config->dma_rx;
|
|
|
|
}
|
|
|
|
else if (RT_DEVICE_FLAG_DMA_TX == flag)
|
|
|
|
{
|
|
|
|
DMA_Handle = &uart->dma_tx.handle;
|
|
|
|
dma_config = uart->config->dma_tx;
|
|
|
|
}
|
2018-11-29 17:00:22 +08:00
|
|
|
LOG_D("%s dma config start", uart->config->name);
|
|
|
|
|
|
|
|
{
|
2019-05-03 20:52:31 +08:00
|
|
|
rt_uint32_t tmpreg = 0x00U;
|
2019-01-27 13:11:15 +08:00
|
|
|
#if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \
|
2019-05-03 20:52:31 +08:00
|
|
|
|| defined(SOC_SERIES_STM32L0)
|
2018-11-29 17:00:22 +08:00
|
|
|
/* enable DMA clock && Delay after an RCC peripheral clock enabling*/
|
2019-05-22 09:48:13 +08:00
|
|
|
SET_BIT(RCC->AHBENR, dma_config->dma_rcc);
|
|
|
|
tmpreg = READ_BIT(RCC->AHBENR, dma_config->dma_rcc);
|
2019-10-03 22:29:13 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) \
|
2020-05-02 15:09:06 +08:00
|
|
|
|| defined(SOC_SERIES_STM32G4)|| defined(SOC_SERIES_STM32H7)
|
2018-11-29 17:00:22 +08:00
|
|
|
/* enable DMA clock && Delay after an RCC peripheral clock enabling*/
|
2019-05-22 09:48:13 +08:00
|
|
|
SET_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
|
|
|
|
tmpreg = READ_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
|
2019-12-01 15:47:36 +08:00
|
|
|
|
|
|
|
#if (defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G4)) && defined(DMAMUX1)
|
|
|
|
/* enable DMAMUX clock for L4+ and G4 */
|
|
|
|
__HAL_RCC_DMAMUX1_CLK_ENABLE();
|
|
|
|
#endif
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
#endif
|
2018-11-29 17:00:22 +08:00
|
|
|
UNUSED(tmpreg); /* To avoid compiler warnings */
|
|
|
|
}
|
2019-01-22 15:07:51 +08:00
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
if (RT_DEVICE_FLAG_DMA_RX == flag)
|
|
|
|
{
|
|
|
|
__HAL_LINKDMA(&(uart->handle), hdmarx, uart->dma_rx.handle);
|
|
|
|
}
|
|
|
|
else if (RT_DEVICE_FLAG_DMA_TX == flag)
|
|
|
|
{
|
|
|
|
__HAL_LINKDMA(&(uart->handle), hdmatx, uart->dma_tx.handle);
|
|
|
|
}
|
2018-11-29 17:00:22 +08:00
|
|
|
|
2019-02-15 16:44:51 +08:00
|
|
|
#if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0)
|
2019-05-03 20:52:31 +08:00
|
|
|
DMA_Handle->Instance = dma_config->Instance;
|
2018-12-10 21:39:28 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
|
2019-05-03 20:52:31 +08:00
|
|
|
DMA_Handle->Instance = dma_config->Instance;
|
|
|
|
DMA_Handle->Init.Channel = dma_config->channel;
|
2020-05-02 15:09:06 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32G4)\
|
|
|
|
|| defined(SOC_SERIES_STM32H7)
|
2019-05-03 20:52:31 +08:00
|
|
|
DMA_Handle->Instance = dma_config->Instance;
|
|
|
|
DMA_Handle->Init.Request = dma_config->request;
|
|
|
|
#endif
|
|
|
|
DMA_Handle->Init.PeriphInc = DMA_PINC_DISABLE;
|
|
|
|
DMA_Handle->Init.MemInc = DMA_MINC_ENABLE;
|
|
|
|
DMA_Handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
|
|
DMA_Handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
if (RT_DEVICE_FLAG_DMA_RX == flag)
|
|
|
|
{
|
|
|
|
DMA_Handle->Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
|
|
DMA_Handle->Init.Mode = DMA_CIRCULAR;
|
|
|
|
}
|
|
|
|
else if (RT_DEVICE_FLAG_DMA_TX == flag)
|
|
|
|
{
|
|
|
|
DMA_Handle->Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
|
|
DMA_Handle->Init.Mode = DMA_NORMAL;
|
|
|
|
}
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
DMA_Handle->Init.Priority = DMA_PRIORITY_MEDIUM;
|
2020-05-02 15:09:06 +08:00
|
|
|
#if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32H7)
|
2019-05-03 20:52:31 +08:00
|
|
|
DMA_Handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
2018-11-29 17:00:22 +08:00
|
|
|
#endif
|
2019-05-03 20:52:31 +08:00
|
|
|
if (HAL_DMA_DeInit(DMA_Handle) != HAL_OK)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
if (HAL_DMA_Init(DMA_Handle) != HAL_OK)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
if (flag == RT_DEVICE_FLAG_DMA_RX)
|
2018-11-29 17:00:22 +08:00
|
|
|
{
|
2019-05-03 20:52:31 +08:00
|
|
|
rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
|
|
|
|
/* Start DMA transfer */
|
|
|
|
if (HAL_UART_Receive_DMA(&(uart->handle), rx_fifo->buffer, serial->config.bufsz) != HAL_OK)
|
|
|
|
{
|
|
|
|
/* Transfer error in reception process */
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
CLEAR_BIT(uart->handle.Instance->CR3, USART_CR3_EIE);
|
|
|
|
__HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_IDLE);
|
|
|
|
}
|
2020-03-23 15:35:27 +08:00
|
|
|
|
|
|
|
/* DMA irq should set in DMA TX mode, or HAL_UART_TxCpltCallback function will not be called */
|
2019-05-03 20:52:31 +08:00
|
|
|
HAL_NVIC_SetPriority(dma_config->dma_irq, 0, 0);
|
|
|
|
HAL_NVIC_EnableIRQ(dma_config->dma_irq);
|
|
|
|
|
2018-12-26 10:43:16 +08:00
|
|
|
HAL_NVIC_SetPriority(uart->config->irq_type, 1, 0);
|
2018-11-29 17:00:22 +08:00
|
|
|
HAL_NVIC_EnableIRQ(uart->config->irq_type);
|
2019-05-03 20:52:31 +08:00
|
|
|
|
|
|
|
LOG_D("%s dma %s instance: %x", uart->config->name, flag == RT_DEVICE_FLAG_DMA_RX ? "RX" : "TX", DMA_Handle->Instance);
|
2018-11-29 17:00:22 +08:00
|
|
|
LOG_D("%s dma config done", uart->config->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief UART error callbacks
|
|
|
|
* @param huart: UART handle
|
|
|
|
* @note This example shows a simple way to report transfer error, and you can
|
|
|
|
* add your own implementation.
|
|
|
|
* @retval None
|
|
|
|
*/
|
|
|
|
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
|
|
|
{
|
|
|
|
RT_ASSERT(huart != NULL);
|
|
|
|
struct stm32_uart *uart = (struct stm32_uart *)huart;
|
|
|
|
LOG_D("%s: %s %d\n", __FUNCTION__, uart->config->name, huart->ErrorCode);
|
|
|
|
UNUSED(uart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Rx Transfer completed callback
|
|
|
|
* @param huart: UART handle
|
|
|
|
* @note This example shows a simple way to report end of DMA Rx transfer, and
|
|
|
|
* you can add your own implementation.
|
|
|
|
* @retval None
|
|
|
|
*/
|
|
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
RT_ASSERT(huart != NULL);
|
|
|
|
uart = (struct stm32_uart *)huart;
|
2019-05-03 18:16:16 +08:00
|
|
|
dma_isr(&uart->serial);
|
|
|
|
}
|
2018-11-29 17:00:22 +08:00
|
|
|
|
2019-05-16 12:37:25 +08:00
|
|
|
/**
|
|
|
|
* @brief Rx Half transfer completed callback
|
|
|
|
* @param huart: UART handle
|
2020-03-23 15:35:27 +08:00
|
|
|
* @note This example shows a simple way to report end of DMA Rx Half transfer,
|
2019-05-16 12:37:25 +08:00
|
|
|
* and you can add your own implementation.
|
|
|
|
* @retval None
|
|
|
|
*/
|
2019-05-03 18:16:16 +08:00
|
|
|
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
RT_ASSERT(huart != NULL);
|
|
|
|
uart = (struct stm32_uart *)huart;
|
|
|
|
dma_isr(&uart->serial);
|
2018-11-29 17:00:22 +08:00
|
|
|
}
|
2020-02-28 15:53:23 +08:00
|
|
|
|
|
|
|
static void _dma_tx_complete(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
rt_size_t trans_total_index;
|
|
|
|
rt_base_t level;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
|
|
|
|
2020-03-23 15:35:27 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
trans_total_index = __HAL_DMA_GET_COUNTER(&(uart->dma_tx.handle));
|
|
|
|
rt_hw_interrupt_enable(level);
|
2020-02-28 15:53:23 +08:00
|
|
|
|
2020-03-23 15:35:27 +08:00
|
|
|
if (trans_total_index == 0)
|
|
|
|
{
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DMADONE);
|
2020-02-28 15:53:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:35:27 +08:00
|
|
|
/**
|
|
|
|
* @brief HAL_UART_TxCpltCallback
|
|
|
|
* @param huart: UART handle
|
|
|
|
* @note This callback can be called by two functions, first in UART_EndTransmit_IT when
|
|
|
|
* UART Tx complete and second in UART_DMATransmitCplt function in DMA Circular mode.
|
|
|
|
* @retval None
|
|
|
|
*/
|
2019-05-03 20:52:31 +08:00
|
|
|
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
|
{
|
|
|
|
struct stm32_uart *uart;
|
|
|
|
RT_ASSERT(huart != NULL);
|
|
|
|
uart = (struct stm32_uart *)huart;
|
2020-02-28 15:53:23 +08:00
|
|
|
_dma_tx_complete(&uart->serial);
|
2019-05-03 20:52:31 +08:00
|
|
|
}
|
2018-12-26 10:43:16 +08:00
|
|
|
#endif /* RT_SERIAL_USING_DMA */
|
2018-11-29 17:00:22 +08:00
|
|
|
|
2020-03-23 15:35:27 +08:00
|
|
|
static const struct rt_uart_ops stm32_uart_ops =
|
2019-01-08 11:58:57 +08:00
|
|
|
{
|
2020-03-23 15:35:27 +08:00
|
|
|
.configure = stm32_configure,
|
|
|
|
.control = stm32_control,
|
|
|
|
.putc = stm32_putc,
|
|
|
|
.getc = stm32_getc,
|
|
|
|
.dma_transmit = stm32_dma_transmit
|
|
|
|
};
|
2019-01-08 11:58:57 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
int rt_hw_usart_init(void)
|
|
|
|
{
|
|
|
|
rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct stm32_uart);
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
|
rt_err_t result = 0;
|
2019-01-22 15:07:51 +08:00
|
|
|
|
2019-01-08 11:58:57 +08:00
|
|
|
stm32_uart_get_dma_config();
|
2019-05-03 20:52:31 +08:00
|
|
|
|
2018-11-29 17:00:22 +08:00
|
|
|
for (int i = 0; i < obj_num; i++)
|
|
|
|
{
|
2020-03-23 15:35:27 +08:00
|
|
|
/* init UART object */
|
2018-11-29 17:00:22 +08:00
|
|
|
uart_obj[i].config = &uart_config[i];
|
|
|
|
uart_obj[i].serial.ops = &stm32_uart_ops;
|
|
|
|
uart_obj[i].serial.config = config;
|
2020-03-23 15:35:27 +08:00
|
|
|
|
2019-05-03 20:52:31 +08:00
|
|
|
/* register UART device */
|
|
|
|
result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
|
|
|
|
RT_DEVICE_FLAG_RDWR
|
|
|
|
| RT_DEVICE_FLAG_INT_RX
|
|
|
|
| RT_DEVICE_FLAG_INT_TX
|
|
|
|
| uart_obj[i].uart_dma_flag
|
2019-09-21 13:51:04 +08:00
|
|
|
, NULL);
|
2018-11-29 17:00:22 +08:00
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* RT_USING_SERIAL */
|