不含stm32 底层的代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MyStm32Code/bsp/Inc/bsp_uart.h

191 lines
5.4 KiB

2 years ago
#ifndef __BSP_UART_H
#define __BSP_UART_H
#include "stm32f4xx.h"
/**
* 使 232 485 max3160
* GPIO操作
*
*/
/* 485 3160 启用一个, 两个接口都有 MAX3160_ENABLE */
#define MAX485_ENABLE 0
#define MAX3160_ENABLE 1
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include "main.h"
2 years ago
typedef enum
{
Uart_Interface_Default = 0x00U,
Uart_Interface_485 = 0x01U,
Uart_Interface_Max3160_232 = 0x02U,
Uart_Interface_Max3160_485 = 0x03U,
} Uart_Interface_Type_Typedef;
2 years ago
2 years ago
typedef enum
{
Uart_Trans_Polling = 0x00U,
Uart_Trans_IT = 0x01U,
Uart_Trans_DMA = 0x02U,
} Uart_Transmode_TypeDef;
typedef enum
{
Uart_IDLE_IT_DISABLE = 0x00U,
Uart_IDLE_IT_ENABLE = 0x01U,
} Uart_IDLE_Enable_TypeDef;
typedef struct
{
uint32_t size;
uint8_t *buf;
}Record_Trans_Rcv_TypeDef;
2 years ago
/* ----------------------- variables ---------------------------------*/
2 years ago
// extern UART_HandleTypeDef huart3;
// UART_HandleTypeDef *puarthelper = &huart3;
2 years ago
2 years ago
typedef void ( *enable_func_gpio) (void);
2 years ago
typedef int ( *callback_fun) (void *obj, void *buf, uint16_t size);
typedef struct
{
UART_HandleTypeDef *huart;
2 years ago
Uart_Interface_Type_Typedef interface_type; /* 0: default(TTL 232), 1: 485 ,2:3160-232 3: 3160-485*/
Uart_IDLE_Enable_TypeDef enable_idle_it;
Uart_Transmode_TypeDef trans_mode; /* 0 :polling, 1: IT 2: DMA*/
Uart_Transmode_TypeDef rcv_mode;
Record_Trans_Rcv_TypeDef *trans_record;
// Record_Trans_Rcv_TypeDef *rcv_record;
// uint8_t *trans_buf;
// uint16_t trans_size;
uint8_t *rcv_buf;
uint16_t rcv_size;
volatile uint8_t send_flag;
volatile uint8_t send_tc_flag; /* 0 开始发送, 1 发送完成*/
volatile uint8_t transferring;
// uint32_t timebase;
enable_func_gpio enable_trans_gpio;
enable_func_gpio enable_trans_cplt_gpio;
void * obj;
callback_fun callback;
// 这个是供外部调用的; 中断是统一管理的, 中断会找到这里的接收回调,然后返回回去
2 years ago
// #if MAX485_ENABLE
// GPIO_TypeDef *de485_gpio;
// uint16_t de485_pin;
// uint8_t de485;
// #endif
// #if MAX3160_ENABLE
// GPIO_TypeDef *de485_gpio;
// uint16_t de485_pin;
// GPIO_TypeDef *sel_gpio;
// GPIO_TypeDef *dplx_gpio;
// uint16_t sel_pin;
// uint16_t dplx_pin;
// uint8_t sel;
// uint8_t dplx;
// #endif
// uint8_t dplx;
// uint8_t sel;
// uint8_t de485;
2 years ago
}UART_HELPER_TypeDef;
2 years ago
2 years ago
extern UART_HELPER_TypeDef *uarthelper;
2 years ago
2 years ago
UART_HELPER_TypeDef * UART_HELPER_Init( );
void UART_HELPER_Set_Huart(UART_HELPER_TypeDef * uarthelper, UART_HandleTypeDef *huart);
int UART_HELPER_Set_Interface_Type(UART_HELPER_TypeDef * uarthelper, uint8_t interface_type);
2 years ago
2 years ago
int UART_HELPER_Setup_Trans_mode( UART_HELPER_TypeDef * uarthelper, uint8_t trans_mode );
int UART_HELPER_Setup_Rcv_mode( UART_HELPER_TypeDef * uarthelper, uint8_t rcv_mode );
2 years ago
2 years ago
int UART_HELPER_Set_trans_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_gpio_func );
int UART_HELPER_Set_trans_cplt_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_cplt_gpio_func );
int UART_HELPER_Set_enable_idle( UART_HELPER_TypeDef * uarthelper, uint8_t enable_idle);
2 years ago
2 years ago
int UART_HELPER_Set_Rcv_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size);
int UART_HELPER_Set_Trans_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size);
2 years ago
2 years ago
int UART_HELPER_Set_Callback( UART_HELPER_TypeDef * uarthelper, void *obj, callback_fun func);
2 years ago
2 years ago
int UART_HELPER_Trans(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size);
// int __UART_HELPER_Trans(UART_HELPER_TypeDef * uarthelper , uint8_t *buf, uint16_t size);
int UART_HELPER_Trans_TxCplt_Callback( UART_HELPER_TypeDef * uarthelper );
/* 接收方式 也有三种类型 */
int UART_HELPER_Start_Rcv(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size);
2 years ago
2 years ago
int UART_HELPER_RCV_Cplt_Callback( UART_HELPER_TypeDef * uarthelper );
int UART_HELPER_RCV_IDLE_Callback( UART_HELPER_TypeDef * uarthelper, uint16_t size );
2 years ago
2 years ago
#endif
2 years ago
2 years ago
/*
2 years ago
2 years ago
GPIO
GPIO
2 years ago
2 years ago
IDLE RXCplt
2 years ago
485
enable rcv
2 years ago
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 0);
2 years ago
enable send
2 years ago
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 1);
2 years ago
3160 485 -- (232 - )
enable rcv
2 years ago
HAL_GPIO_WritePin(uarthelper->dplx_gpio, uarthelper->dplx_gpio , 1);
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 0);
2 years ago
enable send
2 years ago
HAL_GPIO_WritePin(uarthelper->dplx_gpio, uarthelper->dplx_gpio , 0);
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 1);
uarthelper = UART_HELPER_Init();
UART_HELPER_Set_Huart( uarthelper, phuart),
UART_HELPER_Setup_Transmod( uarthelper, 2 );
UART_HELPER_Set_enable_snd_func( uarthelper, NULL );
UART_HELPER_Set_enable_rcv_func( uarthelper, NULL);
UART_HELPER_Set_enable_idle( uarthelper, enable_idle);
UART_HELPER_Set_enable_rcv_func( uarthelper, obj, (callback_fun) func);
2 years ago
2 years ago
// #if MAX3160_ENABLE
// #define DUPLEX_HALF 1
// #define DUPLEX_FULL 0
// #define SEL_485 1
// #define SEL_232 0
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
// #if MAX485_ENABLE
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
2 years ago
*/