134 lines
3.8 KiB
C
134 lines
3.8 KiB
C
#ifndef _UART_H_
|
||
#define _UART_H_
|
||
|
||
#include "apt32f102.h"
|
||
#include "apt32f102_uart.h"
|
||
|
||
#define Recv_2400_TimeOut 10 //ms
|
||
#define Recv_9600_TimeOut 5 //ms
|
||
#define Recv_115200_TimeOut 3 //ms
|
||
|
||
|
||
#define USART_BUFFER_SIZE 300
|
||
|
||
#define UART_SEND_BUFFER_NUM 10
|
||
#define UART_SEND_BUFFER_SIZE 20
|
||
|
||
#define READ_RXLEVEL_STATE GPIO_Read_Status(GPIOB0,5) //485总线RX引脚
|
||
|
||
#define WRITE_HIGH_DR GPIO_Write_High(GPIOB0,3) //485 DR
|
||
#define WRITE_LOW_DR GPIO_Write_Low(GPIOB0,3) //485 DR
|
||
#define REVERISE_DR GPIO_Reverse(GPIOB0,3) //485 DR
|
||
|
||
#define UART_BUSBUSY 0x01 //总线繁忙
|
||
#define UART_BUSIDLE 0x00 //总线空闲
|
||
|
||
/*调试信息相关定义*/
|
||
#ifndef DBG_LOG_EN
|
||
#define DBG_LOG_EN 0 //DEBUG LOG 输出总开关
|
||
#endif
|
||
|
||
/*调试信息初始状态*/
|
||
#define DBG_OPT_Debug_STATUS 0 //临时调试信息打印开关
|
||
#define DBG_OPT_DEVICE_STATUS 0 //设备驱动层打印调试信息打印开关
|
||
#define DBG_OPT_SYS_STATUS 0 //系统调试信息打印开关
|
||
|
||
/*调试信息输出控制位*/
|
||
#define DBG_BIT_Debug_STATUS 2
|
||
#define DBG_BIT_DEVICE_STATUS 1
|
||
#define DBG_BIT_SYS_STATUS 0
|
||
|
||
#if DBG_LOG_EN
|
||
#define DBG_SendByte(data) UARTTxByte(UART2,data)
|
||
#define DBG_Printf(data,len) UARTTransmit(UART2,data,len)
|
||
|
||
#else
|
||
#define DBG_SendByte(data)
|
||
#define DBG_Printf
|
||
#endif
|
||
|
||
typedef U8_T (*Uart_prt)(U8_T *,U16_T);
|
||
|
||
typedef enum
|
||
{
|
||
UART_0,
|
||
UART_1,
|
||
UART_2,
|
||
UART_3,
|
||
UART_MAX,
|
||
}UART_IDX;
|
||
|
||
typedef enum
|
||
{
|
||
BUSSEND_SUCC = 0x00, //发送成功
|
||
BUSSEND_WAIT, //等待发送机会
|
||
DATA_END, //数据有效期结束
|
||
RETRY_END, //重发结束
|
||
LEN_ERR, //长度错误
|
||
}BUSSEND_REV;
|
||
|
||
typedef struct{
|
||
|
||
U8_T RecvBuffer[USART_BUFFER_SIZE];
|
||
U8_T DealBuffer[USART_BUFFER_SIZE];
|
||
U8_T Receiving;
|
||
U16_T RecvLen;
|
||
U16_T DealLen;
|
||
|
||
U32_T RecvTimeout;
|
||
U32_T RecvIdleTiming;
|
||
|
||
Uart_prt processing_cf; //处理函数指针
|
||
} UART_t;
|
||
|
||
typedef struct{ //总线繁忙判断
|
||
U8_T SendBuffer[USART_BUFFER_SIZE]; //发送缓冲
|
||
|
||
U8_T BusState_Flag; //总线繁忙标记位, 0x01:总线繁忙,0x00:总线空闲
|
||
U8_T HighBit_Flag; //串口RX高电平标记位,默认是高电平0x01.
|
||
U8_T BUSBUSY_LOCK; //锁定总线繁忙状态
|
||
|
||
U8_T ResendCnt; //当前发送次数
|
||
U8_T ASend_Flag; //主动上报发送标记
|
||
U8_T TotalCnt; //发送总次数
|
||
U8_T SetBaudFlag; //设置波特率
|
||
|
||
U16_T SendLen; //发送缓冲区数据长度
|
||
|
||
U32_T SetBaud;
|
||
U32_T Bus_DelayTime; //总线繁忙转换到空闲状态的随机延时时间
|
||
U32_T DataWait_Time; //上报数据间隔
|
||
U32_T DataValid_Time; //上报数据有效期
|
||
|
||
U32_T BusState_Tick; //总线繁忙状态判断时间戳
|
||
U32_T ASend_Tick; //主动上报发送间隔判断时间戳
|
||
U32_T BusbusyTimeout; //上报数据有效期判断时间戳
|
||
}MULIT_t;
|
||
|
||
extern U32_T Dbg_Switch;
|
||
extern volatile int RS485_Comm_Flag,RS485_Comm_Start,RS485_Comm_End,RS485_Comming;
|
||
|
||
void UARTx_Init(UART_IDX uart_id, Uart_prt prt_cf);
|
||
U16_T Get_Uart_BaudCnt(U32_T baud);
|
||
U8_T UARTx_ChangeBaud(uint8_t uart_id,uint32_t baud);
|
||
|
||
void UART2_RecvINT_Processing(char data);
|
||
void UART2_TASK(void);
|
||
void MCU485_SendData(U8_T *buff,U16_T len);
|
||
|
||
U8_T BUS485_Send(U8_T *buff,U16_T len);
|
||
U8_T MultSend_Task(U8_T *buff,U16_T len,U8_T DatSd);
|
||
void Set_GroupSend(U8_T *data,U16_T sled,U8_T SCnt,U32_T indate,U32_T tim_val);
|
||
//void Clear_SendFlag(void);
|
||
void BUS485_SetBaud(U32_T baud);
|
||
void BUS485Send_Task(void);
|
||
void BusIdle_Task(void);
|
||
void BusBusy_Task(void);
|
||
|
||
void Dbg_Print(int DbgOptBit, const char *cmd, ...);
|
||
void Dbg_Println(int DbgOptBit, const char *cmd, ...);
|
||
void Dbg_Print_Buff(int DbgOptBit, const char *cmd, U8_T *buff,U16_T len);
|
||
|
||
|
||
#endif
|