- disable usb vport
- uart drv for stm32f4
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
include firmware/arch/stm32f4xx/driver/gpio/gpio.mk
|
||||
include firmware/arch/stm32f4xx/driver/timer/timer.mk
|
||||
include firmware/arch/stm32f4xx/driver/usb/usb.mk
|
||||
include firmware/arch/stm32f4xx/driver/usb_vport/usb_vport.mk
|
||||
include firmware/arch/stm32f4xx/driver/uart/uart.mk
|
||||
#include firmware/arch/stm32f4xx/driver/usb/usb.mk
|
||||
#include firmware/arch/stm32f4xx/driver/usb_vport/usb_vport.mk
|
||||
|
102
source/firmware/arch/stm32f4xx/driver/uart/stm32f4_uart.c
Normal file
102
source/firmware/arch/stm32f4xx/driver/uart/stm32f4_uart.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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();
|
||||
}
|
64
source/firmware/arch/stm32f4xx/driver/uart/stm32f4_uart.h
Normal file
64
source/firmware/arch/stm32f4xx/driver/uart/stm32f4_uart.h
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* stm32_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 "uart.h"
|
||||
|
||||
//! \brief Stm32f4 uart device.
|
||||
struct stm32f4_uart {
|
||||
const GPIO_InitTypeDef *gpio_init;
|
||||
GPIO_TypeDef *gpio_port;
|
||||
uint8_t pin_src_rx;
|
||||
uint8_t pin_src_tx;
|
||||
const USART_InitTypeDef *usart_init;
|
||||
USART_TypeDef *usart_port;
|
||||
uint16_t usart_it_select;
|
||||
const NVIC_InitTypeDef *nvic_init;
|
||||
};
|
||||
|
||||
//! \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_ */
|
4
source/firmware/arch/stm32f4xx/driver/uart/uart.mk
Executable file
4
source/firmware/arch/stm32f4xx/driver/uart/uart.mk
Executable file
@@ -0,0 +1,4 @@
|
||||
CHECK_FOLDER += firmware/arch/stm32f4xx/driver/uart
|
||||
SUB_FOLDER += firmware/arch/stm32f4xx/driver/uart
|
||||
INCLUDES += firmware/arch/stm32f4xx/driver/uart
|
||||
DOC_SRC += firmware/arch/stm32f4xx/driver/uart
|
Reference in New Issue
Block a user