uart tx running
This commit is contained in:
62
source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h
Normal file
62
source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* stm32f4_uart.h
|
||||
*
|
||||
* Created on: Jul 24, 2016
|
||||
* Author: tkl
|
||||
*/
|
||||
|
||||
#ifndef SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_
|
||||
#define SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_
|
||||
|
||||
#include "driver.h"
|
||||
#include "uart.h"
|
||||
|
||||
//! \brief Stm32f4 uart device.
|
||||
#pragma pack(push)
|
||||
#pragma pack(1)
|
||||
struct stm32f4_uart {
|
||||
const struct stm32f4_gpio *uart_gpio;
|
||||
UART_HandleTypeDef *uart_handle;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
//! \brief Open an uart device.
|
||||
//! \param this The uart to open.
|
||||
//! \retval -1 in error case.
|
||||
int stm32f4_uart_open(const void *this);
|
||||
|
||||
//! \brief Close an uart device.
|
||||
//! \param this The uart to close.
|
||||
//! \retval -1 in error case.
|
||||
int stm32f4_uart_close(const void *this);
|
||||
|
||||
//! \brief Read from an uart device.
|
||||
//! \param this The uart to read from.
|
||||
//! \param buffer The buffer to read to.
|
||||
//! \param len The length of the buffer.
|
||||
//! \retval -1 in error case, otherwise number of read characters.
|
||||
int stm32f4_uart_read(const void *this, char *buffer, int len);
|
||||
|
||||
//! \brief Write to an uart device.
|
||||
//! \param this The uart to write to.
|
||||
//! \param buffer The buffer to write.
|
||||
//! \param len The number of characters to write.
|
||||
//! \retval -1 in error case, otherwise number of written characters.
|
||||
int stm32f4_uart_write(const void *this, const char *buffer, int len);
|
||||
|
||||
//! \brief Set a callback for interrupt handling of the uart.
|
||||
//! \param this The uart.
|
||||
//! \param callback The callback to execute in interrupt case.
|
||||
//! \param param The argument for the callback.
|
||||
//! \retval -1 in error case.
|
||||
int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param);
|
||||
|
||||
static const struct uart_fp stm32f4_uart_fp = {
|
||||
stm32f4_uart_open,
|
||||
stm32f4_uart_close,
|
||||
stm32f4_uart_read,
|
||||
stm32f4_uart_write,
|
||||
stm32f4_uart_set_cb
|
||||
};
|
||||
|
||||
#endif /* SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_ */
|
@@ -68,6 +68,9 @@ void SysTick_Handler(void)
|
||||
{
|
||||
enter_isr();
|
||||
|
||||
#if defined(USE_HAL_DRIVER)
|
||||
HAL_IncTick();
|
||||
#endif
|
||||
if(stm32_sys_tick_obj.sys_tick_cb != NULL) {
|
||||
stm32_sys_tick_cb_t cb = stm32_sys_tick_obj.sys_tick_cb;
|
||||
void *param = stm32_sys_tick_obj.sys_tick_cb_param;
|
||||
@@ -76,3 +79,8 @@ void SysTick_Handler(void)
|
||||
|
||||
exit_isr();
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
|
||||
{
|
||||
return HAL_OK;
|
||||
}
|
||||
|
@@ -83,6 +83,24 @@ int stm32f4_gpio_close(const void *gpio)
|
||||
return -1;
|
||||
struct stm32f4_gpio *this = (struct stm32f4_gpio *)gpio;
|
||||
HAL_GPIO_DeInit(this->port, this->pin->Pin);
|
||||
if(this->port == GPIOA)
|
||||
__HAL_RCC_GPIOA_CLK_DISABLE();
|
||||
else if(this->port == GPIOB)
|
||||
__HAL_RCC_GPIOB_CLK_DISABLE();
|
||||
else if(this->port == GPIOC)
|
||||
__HAL_RCC_GPIOC_CLK_DISABLE();
|
||||
else if(this->port == GPIOD)
|
||||
__HAL_RCC_GPIOD_CLK_DISABLE();
|
||||
else if(this->port == GPIOE)
|
||||
__HAL_RCC_GPIOE_CLK_DISABLE();
|
||||
else if(this->port == GPIOF)
|
||||
__HAL_RCC_GPIOF_CLK_DISABLE();
|
||||
else if(this->port == GPIOG)
|
||||
__HAL_RCC_GPIOG_CLK_DISABLE();
|
||||
else if(this->port == GPIOH)
|
||||
__HAL_RCC_GPIOH_CLK_DISABLE();
|
||||
else if(this->port == GPIOI)
|
||||
__HAL_RCC_GPIOI_CLK_DISABLE();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
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)
|
||||
{
|
||||
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
|
||||
uint8_t gpio_af = 0;
|
||||
uint32_t rcc_apb_uart = 0, rcc_apb_gpio = 0;
|
||||
|
||||
if(uart->usart_port == USART1) {
|
||||
gpio_af = GPIO_AF_USART1;
|
||||
rcc_apb_uart = RCC_APB2Periph_USART1;
|
||||
}
|
||||
if(uart->gpio_port == GPIOA) {
|
||||
rcc_apb_gpio = RCC_AHB1Periph_GPIOA;
|
||||
}
|
||||
else if(uart->gpio_port == GPIOB) {
|
||||
rcc_apb_gpio = RCC_AHB1Periph_GPIOB;
|
||||
}
|
||||
|
||||
RCC_APB2PeriphClockCmd(rcc_apb_uart, ENABLE);
|
||||
RCC_AHB1PeriphClockCmd(rcc_apb_gpio, ENABLE);
|
||||
|
||||
GPIO_Init(uart->gpio_port, (GPIO_InitTypeDef *)uart->gpio_init);
|
||||
|
||||
GPIO_PinAFConfig(uart->gpio_port, uart->pin_src_rx, gpio_af);
|
||||
GPIO_PinAFConfig(uart->gpio_port, uart->pin_src_tx, gpio_af);
|
||||
|
||||
USART_Init(uart->usart_port, (USART_InitTypeDef *)uart->usart_init);
|
||||
USART_ITConfig(uart->usart_port, uart->usart_it_select, ENABLE);
|
||||
NVIC_Init((NVIC_InitTypeDef *)uart->nvic_init);
|
||||
USART_Cmd(uart->usart_port, ENABLE);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int stm32f4_uart_close(const void *this)
|
||||
{
|
||||
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
|
||||
USART_Cmd(uart->usart_port, DISABLE);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int stm32f4_uart_read(const void *this, char *buffer, int len)
|
||||
{
|
||||
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
|
||||
*buffer = uart->usart_port->DR;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int stm32f4_uart_write(const void *this, const char *buffer, int len)
|
||||
{
|
||||
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
|
||||
int i;
|
||||
for(i = 0; i < len; i++) {
|
||||
// wait until data register is empty
|
||||
while(!(uart->usart_port->SR & 0x00000040));
|
||||
USART_SendData(uart->usart_port, buffer[i]);
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int stm32f4_uart_set_cb(const void *this, const void *callback, const void *param)
|
||||
{
|
||||
struct stm32f4_uart *uart = (struct stm32f4_uart *)this;
|
||||
if(uart->usart_port == USART1) {
|
||||
uart1_obj.callback = callback;
|
||||
uart1_obj.parameter = 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
|
||||
if(USART_GetITStatus(USART1, USART_IT_RXNE)) {
|
||||
if(uart1_obj.callback) {
|
||||
void (*cb)(const void *) = uart1_obj.callback;
|
||||
cb(uart1_obj.parameter);
|
||||
}
|
||||
}
|
||||
exit_isr();
|
||||
}
|
94
source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c
Normal file
94
source/firmware/arch/stm32f4xx/driver/stm32f4_uart.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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();
|
||||
}
|
Reference in New Issue
Block a user