diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c b/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c index cfcbca2..e2f8967 100644 --- a/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c +++ b/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c @@ -15,33 +15,54 @@ #include "stm32f4_gpio.h" struct stm32f4_uart_obj { - const void *callback; //!< Interrupt callback. - const void *parameter; //!< argument for the callback. + UART_HandleTypeDef *uart1_handle; + UART_HandleTypeDef *uart2_handle; + UART_HandleTypeDef *uart3_handle; + UART_HandleTypeDef *uart6_handle; }; -static volatile struct stm32f4_uart_obj uart1_obj; +static volatile struct stm32f4_uart_obj uart_obj = { + .uart1_handle = NULL, + .uart2_handle = NULL, + .uart3_handle = NULL, + .uart6_handle = NULL, +}; int stm32f4_uart_open(const void *this) { if(NULL == this) return -1; struct stm32f4_uart *uart = (struct stm32f4_uart *)this; + IRQn_Type irq_type = USART1_IRQn; /* init gpio */ stm32f4_gpio_open(uart->uart_gpio); /* uart clock enable */ - if(uart->uart_handle->Instance == USART1) + if(uart->uart_handle->Instance == USART1) { __HAL_RCC_USART1_CLK_ENABLE(); - else if(uart->uart_handle->Instance == USART2) + irq_type = USART1_IRQn; + uart_obj.uart1_handle = uart->uart_handle; + } + else if(uart->uart_handle->Instance == USART2) { __HAL_RCC_USART2_CLK_ENABLE(); - else if(uart->uart_handle->Instance == USART3) + irq_type = USART2_IRQn; + uart_obj.uart2_handle = uart->uart_handle; + } + else if(uart->uart_handle->Instance == USART3) { __HAL_RCC_USART3_CLK_ENABLE(); - else if(uart->uart_handle->Instance == USART6) + irq_type = USART3_IRQn; + uart_obj.uart3_handle = uart->uart_handle; + } + else if(uart->uart_handle->Instance == USART6) { __HAL_RCC_USART6_CLK_ENABLE(); + irq_type = USART6_IRQn; + uart_obj.uart3_handle = uart->uart_handle; + } - /* init uart */ - HAL_UART_Init((UART_HandleTypeDef *)uart->uart_handle); + HAL_UART_Init(uart->uart_handle); + HAL_NVIC_SetPriority(irq_type, 5, 1); + HAL_NVIC_EnableIRQ(USART1_IRQn); return (0); } @@ -51,16 +72,26 @@ int stm32f4_uart_close(const void *this) if(NULL == this) return -1; struct stm32f4_uart *uart = (struct stm32f4_uart *)this; + IRQn_Type irq_type = USART1_IRQn; HAL_UART_DeInit((UART_HandleTypeDef *)uart->uart_handle); - if(uart->uart_handle->Instance == USART1) + if(uart->uart_handle->Instance == USART1) { __HAL_RCC_USART1_CLK_DISABLE(); - else if(uart->uart_handle->Instance == USART2) + irq_type = USART1_IRQn; + } + else if(uart->uart_handle->Instance == USART2) { __HAL_RCC_USART2_CLK_DISABLE(); - else if(uart->uart_handle->Instance == USART3) + irq_type = USART2_IRQn; + } + else if(uart->uart_handle->Instance == USART3) { __HAL_RCC_USART3_CLK_DISABLE(); - else if(uart->uart_handle->Instance == USART6) + irq_type = USART3_IRQn; + } + else if(uart->uart_handle->Instance == USART6) { __HAL_RCC_USART6_CLK_DISABLE(); + irq_type = USART6_IRQn; + } + HAL_NVIC_DisableIRQ(irq_type); stm32f4_gpio_close(uart->uart_gpio); return (0); @@ -76,7 +107,7 @@ int stm32f4_uart_write(const void *this, const char *buffer, int len) if(NULL == this) return -1; struct stm32f4_uart *uart = (struct stm32f4_uart *)this; - HAL_UART_Transmit((UART_HandleTypeDef *)uart->uart_handle, (uint8_t *)buffer, len, 1000); + HAL_UART_Transmit_IT(uart->uart_handle, (uint8_t *)buffer, len); return len; } @@ -89,6 +120,6 @@ int stm32f4_uart_set_cb(const void *this, const void *callback, const void *para void USART1_IRQHandler(void) { enter_isr(); - // check if the USART1 receive interrupt flag was set + HAL_UART_IRQHandler(uart_obj.uart1_handle); exit_isr(); }