optical/NxBase/serial.h
2025-09-04 09:45:08 +08:00

116 lines
3.0 KiB
C

#ifndef __SERIAL_H__
#define __SERIAL_H__
//---------------------------------------------------------------------------
#include "config.h"
//---------------------------------------------------------------------------
typedef enum
{
B4800 = 4800,
B9600 = 9600,
B14400 = 14400,
B19200 = 19200,
B38400 = 38400,
B57600 = 57600,
B115200 = 115200,
B230400 = 230400,
B460800 = 460800,
B921600 = 921600,
B1000000 = 1000000
}BAUD_TypeDef;
//-----------------------------------------------------------------------
typedef void (*ResvExProc)(u8 dat);
//-----------------------------------------------------------------------
#define USART_FUNC_DEF(usart) \
\
/* 初始化USART */ \
int InitUsart##usart(BAUD_TypeDef baud, char dat, char parity, char stop); \
/* 发送数据 */ \
int Usart##usart##SendData(u8 * pDatBuf, int len); \
/* 接收数据 */ \
int Usart##usart##GetData(u8 * pDat, int expectLen); \
/* 清空接收缓冲区 */ \
int Usart##usart##CleanRsBuf(void); \
/* 得到已接收数据的长度 */ \
int Usart##usart##GetRsLen(void); \
/* 清空发送缓冲区 */ \
int Usart##usart##CleanSdBuf(void); \
/* 得到发送缓冲区空闲长度 */ \
int Usart##usart##GetSdFreeLen(void); \
/* 发送缓冲区是否为空 */ \
int IsUsart##usart##SendBufEmpty(void); \
/* 发送是否完成 */ \
int IsUsart##usart##SendOver(void); \
/* 中断处理过程 */ \
void USART##usart##IntProc(void); \
/* 注册接收数据外部处理函数 */ \
void RegUsart##usart##ResvExFunc(ResvExProc proc); \
/* 同步发送数据 */ \
int Usart##usart##SendDataSync(u8 * pDatBuf, int len); \
/* 同步接收数据 */ \
int Usart##usart##GetDataSync(u8 * pDat, int expectLen); \
//-----------
//-----------------------------------------------------------------------
#ifdef COMM_USART1
USART_FUNC_DEF(1);
#endif
//-----------------------------------------------------------------------
#ifdef COMM_USART2
USART_FUNC_DEF(2);
#endif
//-----------------------------------------------------------------------
#ifdef COMM_USART3
USART_FUNC_DEF(3);
#endif
//-----------------------------------------------------------------------
#ifdef COMM_USART4
USART_FUNC_DEF(4);
#endif
//-----------------------------------------------------------------------
#ifdef COMM_USART5
USART_FUNC_DEF(5);
#endif
//-----------------------------------------------------------------------
#ifdef COMM_USART6
USART_FUNC_DEF(6);
#endif
//-----------------------------------------------------------------------
typedef int (*InitUsart)(BAUD_TypeDef baud, char dat, char parity, char stop); // 初始化
typedef int (*UsartSendData)(u8 * pDatBuf, int len); // 发送数据
typedef int (*UsartGetData)(u8 * pDat, int expectLen); // 接收数据
typedef int (*UsartCleanRsBuf)(void); // 清空接收缓冲区
typedef int (*UsartGetRsLen)(void); // 得到已接收数据的长度
typedef int (*UsartGetSdFreeLen)(void); // 得到发送缓冲区空闲长度
typedef int (*UsartCleanSdBuf)(void); // 清空发送缓冲区
typedef int (*IsUsartSendOver)(void); // 发送缓冲区是否为空
//-----------------------------------------------------------------------
#endif