119 lines
3.5 KiB
C
119 lines
3.5 KiB
C
/** ##########################################################################
|
|
** Filename : uart.h
|
|
** Project :
|
|
** Module :
|
|
** Processor :
|
|
** Version : 1.0
|
|
** Compiler :
|
|
** Date/Time :
|
|
** Abstract :
|
|
** Contents :
|
|
** Note : uart底层
|
|
**
|
|
** (c) Copyright dmdz Co.,Ltd
|
|
** --------------------------------------------------------------------------
|
|
** R E V I S I O N H I S T O R Y
|
|
** --------------------------------------------------------------------------
|
|
** Date Ver Author Description
|
|
|
|
** -20230602- --V1.0-- --mingyea--- --修改--
|
|
|
|
** #########################################################################*/
|
|
|
|
#ifndef UART_H__
|
|
#define UART_H__
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
* I N C L U D E F I L E S
|
|
----------------------------------------------------------------------------*/
|
|
#include "common_types.h"
|
|
#include "uart_cfg.h"
|
|
|
|
/*---------------------------------------------------------------------------
|
|
* D E F I N E S / M A C R O S
|
|
----------------------------------------------------------------------------*/
|
|
#define UART_STATE_IDLE 0u
|
|
#define UART_STATE_TX 1u
|
|
#define UART_STATE_RX 2u
|
|
|
|
#define UART_RESULT_INIT 0u
|
|
//#define UART_RESULT_OK 1u
|
|
#define UART_RESULT_CHECK_ERROR 2u
|
|
#define UART_RESULT_TIMEOUT 3u
|
|
#define UART_RESULT_OVER_LEN 4u
|
|
#define UART_RESULT_TX_OK 5u
|
|
#define UART_RESULT_RX_OK 6u
|
|
|
|
#define UART_OP_RESULT_OK 0u
|
|
#define UART_OP_RESULT_BUSY 1u
|
|
#define UART_OP_RESULT_FAILED 2u
|
|
|
|
/****************和协议相关的内容********************/
|
|
#define USART_HEAD0_VALUE 0x55u
|
|
#define USART_HEAD1_VALUE 0xaau
|
|
|
|
#define USART_PID 0xa1
|
|
|
|
#define USART_CMD_READ_DIFF 0x01
|
|
#define USART_CMD_READ_VERSION 0x02
|
|
|
|
#define USART_LEN_READ_VERSION 0x05
|
|
|
|
#define USART_SLEEP_STATE_WAKE 0x00
|
|
#define USART_SLEEP_STATE_SLEEP 0x01
|
|
|
|
#define UART_DRV_GOTO_IDLE_TIME_OUT_CNT 5 //2000
|
|
/*---------------------------------------------------------------------------
|
|
* T Y P E D E F I N I T I O N S
|
|
----------------------------------------------------------------------------*/
|
|
typedef struct
|
|
{
|
|
u8 state; //见 UART_STATE_IDLE
|
|
u8 sub_state; //见 UART_STATE_IDLE
|
|
u8 cmd; //指令
|
|
u8 byte_count; //字节计数
|
|
u8 tx_byte_total; //要发送的总字节数
|
|
u8 rx_byte_total; //要接收的总字节数
|
|
u8 result;
|
|
u8 timecount;
|
|
u8 tx_data[UART_TX_BUFF_LENGTH];
|
|
u8 rx_data[UART_RX_BUFF_LENGTH];
|
|
|
|
#ifdef UART_DEBUG_EN
|
|
u16 rx_count;
|
|
u16 rx_count_timeout;
|
|
u16 tx_count;
|
|
u16 tx_count_timeout;
|
|
#endif
|
|
}uart_s;
|
|
/*---------------------------------------------------------------------------
|
|
* G L O B A L V A R I A B L E S
|
|
* only configuration table allowed here,variables are not allowed!
|
|
----------------------------------------------------------------------------*/
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
* C O N S T A N T S
|
|
----------------------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------
|
|
* F U N C T I O N P R O T O T Y P E
|
|
----------------------------------------------------------------------------*/
|
|
|
|
|
|
void uart_init(void);
|
|
void uart_task(void);
|
|
|
|
u8 uart_tx_start(UART_Type *device, u8 *pdata, u8 length);
|
|
void uart_tx_isr_task(UART_Type *device);
|
|
void uart_rx_isr_task(UART_Type *device,u8 data);
|
|
void uart_timeout_task(UART_Type *device);
|
|
|
|
u8 uart_get_result(void);
|
|
u8 uart_get_rx_data(u8 *pdata,u8 len);
|
|
|
|
void Uartx_Init(UART_Type *device, uint32_t baud_rate);
|
|
|
|
#endif
|