kosmos/source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c

171 lines
4.6 KiB
C
Raw Permalink Normal View History

2016-08-17 08:06:18 +00:00
/*
* stm32f4_uart.c
*
* Created on: Jul 24, 2016
* Author: tkl
*/
#include <stddef.h>
#include <stdint.h>
#include "stm32f4xx.h"
#include "stm32f4xx_isr.h"
#include "stm32f4_uart.h"
#include "gpio.h"
#include "stm32f4_gpio.h"
struct stm32f4_uart_obj {
2016-08-17 11:29:05 +00:00
UART_HandleTypeDef *uart1_handle;
UART_HandleTypeDef *uart2_handle;
UART_HandleTypeDef *uart3_handle;
UART_HandleTypeDef *uart6_handle;
const void *uart1_callback; //!< Interrupt callback.
const void *uart1_parameter; //!< argument for the callback.
const void *uart2_callback; //!< Interrupt callback.
const void *uart2_parameter; //!< argument for the callback.
const void *uart3_callback; //!< Interrupt callback.
const void *uart3_parameter; //!< argument for the callback.
const void *uart6_callback; //!< Interrupt callback.
const void *uart6_parameter; //!< argument for the callback.
2016-08-17 08:06:18 +00:00
};
2016-08-17 11:29:05 +00:00
static volatile struct stm32f4_uart_obj uart_obj = {
.uart1_handle = NULL,
.uart1_callback = NULL,
.uart1_parameter = NULL,
2016-08-17 11:29:05 +00:00
.uart2_handle = NULL,
.uart2_callback = NULL,
.uart2_parameter = NULL,
2016-08-17 11:29:05 +00:00
.uart3_handle = NULL,
.uart3_callback = NULL,
.uart3_parameter = NULL,
2016-08-17 11:29:05 +00:00
.uart6_handle = NULL,
.uart6_callback = NULL,
.uart6_parameter = NULL,
2016-08-17 11:29:05 +00:00
};
2016-08-17 08:06:18 +00:00
int stm32f4_uart_open(const void *this)
{
if(NULL == this)
return -1;
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
2016-08-17 11:29:05 +00:00
IRQn_Type irq_type = USART1_IRQn;
2016-08-17 08:06:18 +00:00
/* init gpio */
stm32f4_gpio_open(uart->uart_gpio);
/* uart clock enable */
2016-08-17 11:29:05 +00:00
if(uart->uart_handle->Instance == USART1) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART1_CLK_ENABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART1_IRQn;
uart_obj.uart1_handle = uart->uart_handle;
}
else if(uart->uart_handle->Instance == USART2) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART2_CLK_ENABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART2_IRQn;
uart_obj.uart2_handle = uart->uart_handle;
}
else if(uart->uart_handle->Instance == USART3) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART3_CLK_ENABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART3_IRQn;
uart_obj.uart3_handle = uart->uart_handle;
}
else if(uart->uart_handle->Instance == USART6) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART6_CLK_ENABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART6_IRQn;
uart_obj.uart3_handle = uart->uart_handle;
}
2016-08-17 08:06:18 +00:00
2016-08-17 11:29:05 +00:00
HAL_UART_Init(uart->uart_handle);
HAL_NVIC_SetPriority(irq_type, 5, 1);
HAL_NVIC_EnableIRQ(USART1_IRQn);
__HAL_UART_ENABLE_IT(uart->uart_handle, UART_IT_RXNE);
__HAL_UART_DISABLE_IT(uart->uart_handle, UART_IT_TXE);
2016-08-17 08:06:18 +00:00
return (0);
}
int stm32f4_uart_close(const void *this)
{
if(NULL == this)
return -1;
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
2016-08-17 11:29:05 +00:00
IRQn_Type irq_type = USART1_IRQn;
2016-08-17 08:06:18 +00:00
HAL_UART_DeInit((UART_HandleTypeDef *)uart->uart_handle);
2016-08-17 11:29:05 +00:00
if(uart->uart_handle->Instance == USART1) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART1_CLK_DISABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART1_IRQn;
}
else if(uart->uart_handle->Instance == USART2) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART2_CLK_DISABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART2_IRQn;
}
else if(uart->uart_handle->Instance == USART3) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART3_CLK_DISABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART3_IRQn;
}
else if(uart->uart_handle->Instance == USART6) {
2016-08-17 08:06:18 +00:00
__HAL_RCC_USART6_CLK_DISABLE();
2016-08-17 11:29:05 +00:00
irq_type = USART6_IRQn;
}
HAL_NVIC_DisableIRQ(irq_type);
2016-08-17 08:06:18 +00:00
stm32f4_gpio_close(uart->uart_gpio);
return (0);
}
int stm32f4_uart_read(const void *this, char *buffer, int len)
{
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
*buffer = uart->uart_handle->Instance->DR;
2016-08-17 08:06:18 +00:00
return (1);
}
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->uart_handle, (uint8_t *)buffer, len, 1000);
2016-08-17 08:06:18 +00:00
return len;
}
int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param)
{
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
if(uart->uart_handle->Instance == USART1) {
uart_obj.uart1_callback = callback;
uart_obj.uart1_parameter = param;
}
else if(uart->uart_handle->Instance == USART2) {
uart_obj.uart2_callback = callback;
uart_obj.uart2_parameter = param;
}
else if(uart->uart_handle->Instance == USART3) {
uart_obj.uart3_callback = callback;
uart_obj.uart3_parameter = param;
}
else if(uart->uart_handle->Instance == USART6) {
uart_obj.uart6_callback = callback;
uart_obj.uart6_parameter = param;
}
2016-08-17 08:06:18 +00:00
return (0);
}
// this is the interrupt request handler (IRQ) for ALL USART1 interrupts
void USART1_IRQHandler(void)
{
enter_isr();
uint32_t tmp1 = 0U, tmp2 = 0U;
tmp1 = __HAL_UART_GET_FLAG(uart_obj.uart1_handle, UART_FLAG_RXNE);
tmp2 = __HAL_UART_GET_IT_SOURCE(uart_obj.uart1_handle, UART_IT_RXNE);
if((tmp1 != RESET) && (tmp2 != RESET)) {
if(uart_obj.uart1_callback) {
void (*cb)(const void *) = uart_obj.uart1_callback;
cb(uart_obj.uart1_parameter);
}
}
2016-08-17 08:06:18 +00:00
exit_isr();
}