rt-thread/bsp/stm32f7-disco/drivers/drv_usart.c

504 lines
15 KiB
C
Raw Normal View History

2015-08-07 13:30:13 +08:00
/*
2018-10-16 13:00:37 +08:00
* Copyright (c) 2006-2018, RT-Thread Development Team
2015-08-07 13:30:13 +08:00
*
2018-10-16 13:00:37 +08:00
* SPDX-License-Identifier: Apache-2.0
2015-08-07 13:30:13 +08:00
*
* Change Logs:
* Date Author Notes
* 2018-05-17 ZYH first implementation
2015-08-07 13:30:13 +08:00
*/
#include "drv_usart.h"
#include "board.h"
#include <rtdevice.h>
#include <rthw.h>
#include <rtthread.h>
2015-08-07 13:30:13 +08:00
/* STM32 uart driver */
struct drv_uart
2015-08-07 13:30:13 +08:00
{
UART_HandleTypeDef UartHandle;
IRQn_Type irq;
};
static rt_err_t drv_configure(struct rt_serial_device *serial,
struct serial_configure *cfg)
2015-08-07 13:30:13 +08:00
{
struct drv_uart *uart;
2015-08-07 13:30:13 +08:00
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
2015-08-09 09:17:28 +08:00
uart->UartHandle.Init.BaudRate = cfg->baud_rate;
uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
uart->UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
2015-08-09 09:17:28 +08:00
switch (cfg->data_bits)
{
case DATA_BITS_8:
uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
break;
case DATA_BITS_9:
uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
break;
default:
uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
break;
}
switch (cfg->stop_bits)
{
case STOP_BITS_1:
uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
break;
case STOP_BITS_2:
uart->UartHandle.Init.StopBits = UART_STOPBITS_2;
break;
default:
uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
break;
}
switch (cfg->parity)
{
case PARITY_NONE:
uart->UartHandle.Init.Parity = UART_PARITY_NONE;
break;
case PARITY_ODD:
uart->UartHandle.Init.Parity = UART_PARITY_ODD;
break;
case PARITY_EVEN:
uart->UartHandle.Init.Parity = UART_PARITY_EVEN;
break;
default:
uart->UartHandle.Init.Parity = UART_PARITY_NONE;
break;
}
if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
{
return RT_ERROR;
}
2015-08-07 13:30:13 +08:00
return RT_EOK;
}
static rt_err_t drv_control(struct rt_serial_device *serial,
int cmd, void *arg)
2015-08-07 13:30:13 +08:00
{
struct drv_uart *uart;
2015-08-07 13:30:13 +08:00
RT_ASSERT(serial != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
2015-08-07 13:30:13 +08:00
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */
HAL_NVIC_DisableIRQ(uart->irq);
2015-08-07 13:30:13 +08:00
/* disable interrupt */
2015-08-09 09:17:28 +08:00
__HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
2015-08-07 13:30:13 +08:00
break;
case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */
HAL_NVIC_EnableIRQ(uart->irq);
HAL_NVIC_SetPriority(uart->irq, 5, 0);
2015-08-07 13:30:13 +08:00
/* enable interrupt */
__HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
break;
}
return RT_EOK;
}
static int drv_putc(struct rt_serial_device *serial, char c)
2015-08-07 13:30:13 +08:00
{
struct drv_uart *uart;
2015-08-07 13:30:13 +08:00
RT_ASSERT(serial != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
while ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET));
2015-08-07 13:30:13 +08:00
uart->UartHandle.Instance->TDR = c;
return 1;
}
static int drv_getc(struct rt_serial_device *serial)
2015-08-07 13:30:13 +08:00
{
int ch;
struct drv_uart *uart;
2015-08-07 13:30:13 +08:00
RT_ASSERT(serial != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
2015-08-07 13:30:13 +08:00
ch = -1;
if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
2015-08-07 13:30:13 +08:00
ch = uart->UartHandle.Instance->RDR & 0xff;
return ch;
}
static const struct rt_uart_ops drv_uart_ops =
2015-08-07 13:30:13 +08:00
{
drv_configure,
drv_control,
drv_putc,
drv_getc,
2015-08-07 13:30:13 +08:00
};
#if defined(BSP_USING_UART1)
2015-08-07 13:30:13 +08:00
/* UART1 device driver structure */
static struct drv_uart uart1;
2015-08-07 13:30:13 +08:00
struct rt_serial_device serial1;
void USART1_IRQHandler(void)
{
struct drv_uart *uart;
2015-08-07 13:30:13 +08:00
uart = &uart1;
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
{
rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
/* Clear RXNE interrupt flag */
__HAL_UART_CLEAR_IT(&uart->UartHandle, UART_FLAG_RXNE);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART1 */
2015-08-07 13:30:13 +08:00
#if defined(BSP_USING_UART4)
/* UART4 device driver structure */
static struct drv_uart uart4;
struct rt_serial_device serial4;
void UART4_IRQHandler(void)
{
struct drv_uart *uart;
uart = &uart4;
2015-08-07 13:30:13 +08:00
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
{
rt_hw_serial_isr(&serial4, RT_SERIAL_EVENT_RX_IND);
/* Clear RXNE interrupt flag */
__HAL_UART_CLEAR_IT(&uart->UartHandle, UART_FLAG_RXNE);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART4 */
2015-08-07 13:30:13 +08:00
#if defined(BSP_USING_UART5)
/* UART5 device driver structure */
static struct drv_uart uart5;
struct rt_serial_device serial5;
void UART5_IRQHandler(void)
{
struct drv_uart *uart;
uart = &uart5;
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
2015-08-09 09:17:28 +08:00
{
rt_hw_serial_isr(&serial5, RT_SERIAL_EVENT_RX_IND);
2015-08-09 09:17:28 +08:00
/* Clear RXNE interrupt flag */
__HAL_UART_CLEAR_IT(&uart->UartHandle, UART_FLAG_RXNE);
2015-08-09 09:17:28 +08:00
}
2015-08-07 13:30:13 +08:00
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART5 */
2015-08-07 13:30:13 +08:00
#if defined(BSP_USING_UART6)
/* UART6 device driver structure */
static struct drv_uart uart6;
struct rt_serial_device serial6;
void USART6_IRQHandler(void)
2015-08-09 09:17:28 +08:00
{
struct drv_uart *uart;
uart = &uart6;
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
2015-08-09 09:17:28 +08:00
{
rt_hw_serial_isr(&serial6, RT_SERIAL_EVENT_RX_IND);
/* Clear RXNE interrupt flag */
__HAL_UART_CLEAR_IT(&uart->UartHandle, UART_FLAG_RXNE);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART6 */
2015-08-09 09:17:28 +08:00
#if defined(BSP_USING_UART7)
/* UART7 device driver structure */
static struct drv_uart uart7;
struct rt_serial_device serial7;
void UART7_IRQHandler(void)
{
struct drv_uart *uart;
uart = &uart7;
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
{
rt_hw_serial_isr(&serial7, RT_SERIAL_EVENT_RX_IND);
/* Clear RXNE interrupt flag */
__HAL_UART_CLEAR_IT(&uart->UartHandle, UART_FLAG_RXNE);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART7 */
2015-08-09 09:17:28 +08:00
/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example:
* - Peripheral's clock enable
* - Peripheral's GPIO Configuration
* - NVIC configuration for UART interrupt request enable
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
{
2015-08-09 09:17:28 +08:00
GPIO_InitTypeDef GPIO_InitStruct;
if (uartHandle->Instance == UART4)
{
/* UART4 clock enable */
__HAL_RCC_UART4_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**UART4 GPIO Configuration
PC11 ------> UART4_RX
PA0/WKUP ------> UART4_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* UART4 interrupt Init */
HAL_NVIC_SetPriority(UART4_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(UART4_IRQn);
}
else if (uartHandle->Instance == UART5)
{
/* UART5 clock enable */
__HAL_RCC_UART5_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/**UART5 GPIO Configuration
PB8 ------> UART5_RX
PC12 ------> UART5_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_UART5;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* UART5 interrupt Init */
HAL_NVIC_SetPriority(UART5_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(UART5_IRQn);
}
else if (uartHandle->Instance == UART7)
{
/* UART7 clock enable */
__HAL_RCC_UART7_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**UART7 GPIO Configuration
PA15 ------> UART7_TX
PA8 ------> UART7_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_15 | GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_UART7;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* UART7 interrupt Init */
HAL_NVIC_SetPriority(UART7_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(UART7_IRQn);
}
else if (uartHandle->Instance == USART1)
{
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA10 ------> USART1_RX
PA9 ------> USART1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
2015-08-09 09:17:28 +08:00
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
else if (uartHandle->Instance == USART6)
{
/* USART6 clock enable */
__HAL_RCC_USART6_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/**USART6 GPIO Configuration
PC7 ------> USART6_RX
PC6 ------> USART6_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
2015-08-07 13:30:13 +08:00
}
void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
2015-08-07 13:30:13 +08:00
{
if (uartHandle->Instance == UART4)
2015-08-09 09:17:28 +08:00
{
/* Peripheral clock disable */
__HAL_RCC_UART4_CLK_DISABLE();
/**UART4 GPIO Configuration
PC11 ------> UART4_RX
PA0/WKUP ------> UART4_TX
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_11);
2015-08-09 09:17:28 +08:00
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
}
else if (uartHandle->Instance == UART5)
{
/* Peripheral clock disable */
__HAL_RCC_UART5_CLK_DISABLE();
2015-08-09 09:17:28 +08:00
/**UART5 GPIO Configuration
PB8 ------> UART5_RX
PC12 ------> UART5_TX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_12);
}
else if (uartHandle->Instance == UART7)
{
/* Peripheral clock disable */
__HAL_RCC_UART7_CLK_DISABLE();
/**UART7 GPIO Configuration
PA15 ------> UART7_TX
PA8 ------> UART7_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_15 | GPIO_PIN_8);
}
else if (uartHandle->Instance == USART1)
{
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PB7 ------> USART1_RX
PA9 ------> USART1_TX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9);
}
else if (uartHandle->Instance == USART6)
{
/* Peripheral clock disable */
__HAL_RCC_USART6_CLK_DISABLE();
/**USART6 GPIO Configuration
PC7 ------> USART6_RX
PC6 ------> USART6_TX
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_7 | GPIO_PIN_6);
2015-08-09 09:17:28 +08:00
}
2015-08-07 13:30:13 +08:00
}
int hw_usart_init(void)
2015-08-07 13:30:13 +08:00
{
struct drv_uart *uart;
2015-08-07 13:30:13 +08:00
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
#ifdef BSP_USING_UART1
2015-08-07 13:30:13 +08:00
uart = &uart1;
uart->UartHandle.Instance = USART1;
uart->irq = USART1_IRQn;
serial1.ops = &drv_uart_ops;
2015-08-07 13:30:13 +08:00
serial1.config = config;
/* register UART1 device */
rt_hw_serial_register(&serial1, "uart1",
2015-08-07 13:30:13 +08:00
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* BSP_USING_UART1 */
#ifdef BSP_USING_UART4
uart = &uart4;
uart->UartHandle.Instance = UART4;
uart->irq = UART4_IRQn;
serial4.ops = &drv_uart_ops;
serial4.config = config;
/* register UART4 device */
rt_hw_serial_register(&serial4, "uart4",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* BSP_USING_UART4 */
#ifdef BSP_USING_UART5
uart = &uart5;
uart->UartHandle.Instance = UART5;
uart->irq = UART5_IRQn;
serial5.ops = &drv_uart_ops;
serial5.config = config;
/* register UART5 device */
rt_hw_serial_register(&serial5, "uart5",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* BSP_USING_UART5 */
#ifdef BSP_USING_UART6
uart = &uart6;
uart->UartHandle.Instance = USART6;
uart->irq = USART6_IRQn;
serial6.ops = &drv_uart_ops;
serial6.config = config;
/* register UART6 device */
rt_hw_serial_register(&serial6, "uart6",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* BSP_USING_UART6 */
#ifdef BSP_USING_UART7
uart = &uart7;
uart->UartHandle.Instance = UART7;
uart->irq = UART7_IRQn;
serial7.ops = &drv_uart_ops;
serial7.config = config;
/* register UART7 device */
rt_hw_serial_register(&serial7, "uart7",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* BSP_USING_UART7 */
2015-08-07 13:30:13 +08:00
return 0;
}
INIT_BOARD_EXPORT(hw_usart_init);