123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- /*
- * File : drv_usart.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2015, RT-Thread Development Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date Author Notes
- * 2017-08-25 LongfeiMa the first version for stm32h7xx
- */
- #include "stm32h7xx.h"
- #include "drv_usart.h"
- #include "board.h"
- #include <rtdevice.h>
- /* Definition for USART1 clock resources */
- #define USART1_CLK_ENABLE() __USART1_CLK_ENABLE()
- #define USART1_RX_GPIO_CLK_ENABLE() __GPIOB_CLK_ENABLE()
- #define USART1_TX_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
- #define USART1_FORCE_RESET() __USART1_FORCE_RESET()
- #define USART1_RELEASE_RESET() __USART1_RELEASE_RESET()
- /* Definition for USARTx Pins */
- #define USART1_TX_PIN GPIO_PIN_9
- #define USART1_TX_GPIO_PORT GPIOA
- #define USART1_TX_AF GPIO_AF7_USART1
- #define USART1_RX_PIN GPIO_PIN_7
- #define USART1_RX_GPIO_PORT GPIOB
- #define USART1_RX_AF GPIO_AF7_USART1
- /* STM32 uart driver */
- struct stm32_uart
- {
- UART_HandleTypeDef UartHandle;
- IRQn_Type irq;
- };
- 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);
- uart = (struct stm32_uart *)serial->parent.user_data;
- uart->UartHandle.Init.BaudRate = cfg->baud_rate;
- uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
- uart->UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- switch (cfg->data_bits)
- {
- case DATA_BITS_7:
- uart->UartHandle.Init.WordLength = UART_WORDLENGTH_7B;
- break;
- 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_DeInit(&uart->UartHandle) != HAL_OK)
- {
- return RT_ERROR;
- }
- if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
- {
- return RT_ERROR;
- }
- return RT_EOK;
- }
- static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
- {
- struct stm32_uart *uart;
- RT_ASSERT(serial != RT_NULL);
- uart = (struct stm32_uart *)serial->parent.user_data;
- switch (cmd)
- {
- case RT_DEVICE_CTRL_CLR_INT:
- /* disable rx irq */
- UART_DISABLE_IRQ(uart->irq);
- /* disable interrupt */
- __HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
- break;
- case RT_DEVICE_CTRL_SET_INT:
- /* enable rx irq */
- UART_ENABLE_IRQ(uart->irq);
- /* enable interrupt */
- __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
- break;
- }
- return RT_EOK;
- }
- static int stm32_putc(struct rt_serial_device *serial, char c)
- {
- struct stm32_uart *uart;
- RT_ASSERT(serial != RT_NULL);
- uart = (struct stm32_uart *)serial->parent.user_data;
- while (!(uart->UartHandle.Instance->ISR & UART_FLAG_TXE));
- uart->UartHandle.Instance->TDR = c;
- return 1;
- }
- static int stm32_getc(struct rt_serial_device *serial)
- {
- int ch;
- struct stm32_uart *uart;
- RT_ASSERT(serial != RT_NULL);
- uart = (struct stm32_uart *)serial->parent.user_data;
- ch = -1;
- if (uart->UartHandle.Instance->ISR & UART_FLAG_RXNE)
- {
- ch = uart->UartHandle.Instance->RDR & 0xff;
- }
- return ch;
- }
- static const struct rt_uart_ops stm32_uart_ops =
- {
- stm32_configure,
- stm32_control,
- stm32_putc,
- stm32_getc,
- };
- #if defined(RT_USING_UART1)
- /* UART1 device driver structure */
- static struct stm32_uart uart1;
- struct rt_serial_device serial1;
- void USART1_IRQHandler(void)
- {
- struct stm32_uart *uart;
- uart = &uart1;
- /* enter interrupt */
- rt_interrupt_enter();
- /* UART in mode Receiver ---------------------------------------------------*/
- if ((__HAL_UART_GET_IT(&uart->UartHandle, UART_IT_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_SEND_REQ(&uart->UartHandle, UART_RXDATA_FLUSH_REQUEST);
- }
- /* leave interrupt */
- rt_interrupt_leave();
- }
- #endif /* RT_USING_UART1 */
- /**
- * @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 *huart)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if (huart->Instance == USART1)
- {
- /* Enable GPIO TX/RX clock */
- USART1_TX_GPIO_CLK_ENABLE();
- USART1_RX_GPIO_CLK_ENABLE();
- /* Enable USARTx clock */
- USART1_CLK_ENABLE();
- /* UART TX GPIO pin configuration */
- GPIO_InitStruct.Pin = USART1_TX_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = USART1_TX_AF;
- HAL_GPIO_Init(USART1_TX_GPIO_PORT, &GPIO_InitStruct);
- /* UART RX GPIO pin configuration */
- GPIO_InitStruct.Pin = USART1_RX_PIN;
- GPIO_InitStruct.Alternate = USART1_RX_AF;
- HAL_GPIO_Init(USART1_RX_GPIO_PORT, &GPIO_InitStruct);
- /* NVIC for USART */
- HAL_NVIC_SetPriority(USART1_IRQn, 0, 1);
- HAL_NVIC_EnableIRQ(USART1_IRQn);
- }
- }
- /**
- * @brief UART MSP De-Initialization
- * This function frees the hardware resources used in this example:
- * - Disable the Peripheral's clock
- * - Revert GPIO and NVIC configuration to their default state
- * @param huart: UART handle pointer
- * @retval None
- */
- void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
- {
- if (huart->Instance == USART1)
- {
- /* Reset peripherals */
- USART1_FORCE_RESET();
- USART1_RELEASE_RESET();
- /* Disable peripherals and GPIO Clocks */
- /* Configure UART Tx as alternate function */
- HAL_GPIO_DeInit(USART1_TX_GPIO_PORT, USART1_TX_PIN);
- /* Configure UART Rx as alternate function */
- HAL_GPIO_DeInit(USART1_RX_GPIO_PORT, USART1_RX_PIN);
- /* Disable the NVIC for UART */
- HAL_NVIC_DisableIRQ(USART1_IRQn);
- }
- }
- int stm32_hw_usart_init(void)
- {
- struct stm32_uart *uart;
- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
- #ifdef RT_USING_UART1
- uart = &uart1;
- uart->UartHandle.Instance = USART1;
- serial1.ops = &stm32_uart_ops;
- serial1.config = config;
- /* register UART1 device */
- rt_hw_serial_register(&serial1, "uart1",
- RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
- uart);
- #endif /* RT_USING_UART1 */
- return 0;
- }
- INIT_BOARD_EXPORT(stm32_hw_usart_init);
|