// 串口驱动
// 杭州秒寻科技有限公司
// 周涛
// 2023-09-12

#ifndef DRIVERUART_H
#define DRIVERUART_H

#include "sysdef.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx.h"

#define DRIVER_UART_RXBUF_SIZE 512 // 接收缓存大小

// GUI串口
#define DRIVER_GUI_UART USART1
#define DRIVER_GUI_NVIC_IRQ_CHANNEL USART1_IRQn

#define DRIVER_GUI_UART_CLK RCC_APB2Periph_USART1
#define DRIVER_GUI_AF GPIO_AF_USART1
#define DRIVEr_GUI_AF_TX GPIO_PinSource9
#define DRIVEr_GUI_AF_RX GPIO_PinSource10

#define DRIVER_GUI_GPIO_CLK RCC_AHB1Periph_GPIOA

#define DRIVER_GUI_GPIO_TX_PORT GPIOA
#define DRIVER_GUI_GPIO_TX_PIN GPIO_Pin_9

#define DRIVER_GUI_GPIO_RX_PORT GPIOA
#define DRIVER_GUI_GPIO_RX_PIN GPIO_Pin_10

// WIFI串口
#define DRIVER_WIFI_UART USART2
#define DRIVER_WIFI_NVIC_IRQ_CHANNEL USART2_IRQn

#define DRIVER_WIFI_UART_CLK RCC_APB1Periph_USART2
#define DRIVER_WIFI_AF GPIO_AF_USART2
#define DRIVEr_WIFI_AF_TX GPIO_PinSource5
#define DRIVEr_WIFI_AF_RX GPIO_PinSource6

#define DRIVER_WIFI_GPIO_CLK RCC_AHB1Periph_GPIOD
#define DRIVER_WIFI_GPIO_TX_PORT GPIOD
#define DRIVER_WIFI_GPIO_TX_PIN GPIO_Pin_5

#define DRIVER_WIFI_GPIO_RX_PORT GPIOD
#define DRIVER_WIFI_GPIO_RX_PIN GPIO_Pin_6

// 串口接收结构
typedef struct
{
    uint32 dwGenerateIndex;                // 生产数据索引
    uint32 dwConsumeIndex;                 // 消费数据索引
    uint8 abyData[DRIVER_UART_RXBUF_SIZE]; // 数据缓存
} UART_RECEIVE_MANAGE_T;

// 供外部使用接口
void driver_uart_init();
uint8 driver_uart_send_data(uint8 byComNum, uint8 *pbyData, uint16 wLen);
uint32 driver_uart_receive_data(uint8 byComNum, uint8 *pbyData, uint32 dwLen);

// 内部接口
void driver_uart_init_gui();
void driver_uart_init_wifi();
USART_TypeDef *driver_uart_change_com_num(uint8 byComNum);
void driver_uart_send_byte(USART_TypeDef *pstUART, uint8 byData);

#endif
