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

95 lines
2.2 KiB
C
Raw 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 {
const void *callback; //!< Interrupt callback.
const void *parameter; //!< argument for the callback.
};
static volatile struct stm32f4_uart_obj uart1_obj;
int stm32f4_uart_open(const void *this)
{
if(NULL == this)
return -1;
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
/* init gpio */
stm32f4_gpio_open(uart->uart_gpio);
/* uart clock enable */
if(uart->uart_handle->Instance == USART1)
__HAL_RCC_USART1_CLK_ENABLE();
else if(uart->uart_handle->Instance == USART2)
__HAL_RCC_USART2_CLK_ENABLE();
else if(uart->uart_handle->Instance == USART3)
__HAL_RCC_USART3_CLK_ENABLE();
else if(uart->uart_handle->Instance == USART6)
__HAL_RCC_USART6_CLK_ENABLE();
/* init uart */
HAL_UART_Init((UART_HandleTypeDef *)uart->uart_handle);
return (0);
}
int stm32f4_uart_close(const void *this)
{
if(NULL == this)
return -1;
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
HAL_UART_DeInit((UART_HandleTypeDef *)uart->uart_handle);
if(uart->uart_handle->Instance == USART1)
__HAL_RCC_USART1_CLK_DISABLE();
else if(uart->uart_handle->Instance == USART2)
__HAL_RCC_USART2_CLK_DISABLE();
else if(uart->uart_handle->Instance == USART3)
__HAL_RCC_USART3_CLK_DISABLE();
else if(uart->uart_handle->Instance == USART6)
__HAL_RCC_USART6_CLK_DISABLE();
stm32f4_gpio_close(uart->uart_gpio);
return (0);
}
int stm32f4_uart_read(const void *this, char *buffer, int len)
{
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_HandleTypeDef *)uart->uart_handle, (uint8_t *)buffer, len, 1000);
return len;
}
int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param)
{
return (0);
}
// this is the interrupt request handler (IRQ) for ALL USART1 interrupts
void USART1_IRQHandler(void)
{
enter_isr();
// check if the USART1 receive interrupt flag was set
exit_isr();
}