/*
*Copyright (C),2023 , NANOCHAP
*File name:   ENS1_UART.H          
*Author:      
*Version:     V1.0 
*Date:        2023-11-                  
*Description:  
*Function List: 
1	void UART_Init(CMSDK_UART_TypeDef *CMSDK_UART, UART_InitStructure* uart_paraX);
2	void UART_ITConfig(CMSDK_UART_TypeDef *CMSDK_UART, UART_ITStructure* uart_paraX);
3	unsigned char UartPutc(CMSDK_UART_TypeDef *CMSDK_UART ,unsigned char my_ch);

History: 
1.V1.0
Date:          
Author: 
Modification: ����
*/
#ifndef _ENS1_UART_H_
#define _ENS1_UART_H_

#include "MY_HEADER.h"
#include "CMSDK_CM0.h"
#include "ENS1_CLOCK.h"
#include "math.h"

#define UART0_RX   GPIO_2
#define UART0_TX   GPIO_3
#define UART0_RTS  GPIO_4
#define UART0_CTS  GPIO_5
#define UART1_RX  GPIO_12
#define UART1_TX  GPIO_13 
#define UART1_RTS  GPIO_14
#define UART1_CTS  GPIO_15 

#define InterruptDisable __disable_irq
#define InterruptEnable __enable_irq
typedef enum uartRcvfifoTrigger{byte_1=0 , bytes_4=1,bytes_8=2,bytes_14=3}uartRcvTrigger;
typedef enum {OverSamp_16 = 0,OverSamp_13}UART_OverSamp;



typedef enum {
StopLen_1 = 0,
StopLen_Other
}UART_StopLen;

typedef enum {
WordLen_5,
WordLen_6,
WordLen_7,
WordLen_8
}UART_WordLen;


typedef enum {
Stick_RESET = 0,
Stick_SET
}UART_Stick_EN;

typedef enum {
Odd_SET = 0,
Even_SET
}UART_Even_EN;

typedef enum {
Parity_RESET = 0,
Parity_SET
}UART_Parity_EN;

/*uart�������ýṹ��*/

typedef struct uart_fifo{
	uartRcvTrigger  level;
	uint8_t DMA_Enable  ;
	uint8_t FIFO_Enable ;	
}UART_FifoStructrue;

typedef struct uart_para{
	 uint8_t  UART_OverSampMode;
	 uint32_t UART_BaudRate;	
	 uint8_t UART_HardwareFlowControl;		    //Ӳ�������Ƿ���
	 UART_FifoStructrue*  FifoSetting ;
	 UART_StopLen stop_len;						//停止位长度
	 UART_WordLen Word_len;						//数据位长度
	 UART_Stick_EN Stick_EN;						//固定奇偶校验使能
	 UART_Even_EN Even_EN;						//奇偶校验选择
	 UART_Parity_EN Parity_EN;					//奇偶校验使能
}UART_InitStructure;

extern UART_FifoStructrue UART0_Fifo;
extern UART_InitStructure UART0_Init;
extern UART_FifoStructrue UART1_Fifo;
extern UART_InitStructure UART1_Init;

/*
�ж���Դ˵����
1��THREINT THR���ͼĴ�������FIFO��
2��RDAINT  ���ռĴ������ݿ��� ��FIFOģʽ�´���������ֵ
3��RTOINT  FIFOģʽ�½��ճ�ʱ��������ĸ��ֽ�ʱ����û���ֽ��Ƴ��������룬�����ʱ����������һ���ֽ��ڽ���FIFO��
4��RLSINT  ������·״̬���������������У�����֡��������ж�
5��MSIINT  ���ƽ����״̬��CTS����״̬������autoflow����DSR/RI/DCD����״̬
*/
/*�ж�ʹ��*/
#define  MSI_EN   (uint8_t)0X8    
#define  RLSI_EN  (uint8_t)0X4  
#define  THRE_EN  (uint8_t)0X2 
#define  RDAI_EN  (uint8_t)0X1
/*�ж���Ϣ*/
#define INT_MODEM_STATUS  (uint8_t)0X0
#define INT_THR_EMPTY     (uint8_t)0X1
#define INT_RCV_DATA_AVAILABLE   (uint8_t)0X2
#define INT_RCV_LINE_STATUS    (uint8_t)0X3
#define INT_CHAR_TIMEOUT_INDICATION  (uint8_t)0X6

typedef struct uart_it_para{
	uint8_t UartIntModel ;   //ѡ���жϵ�ģʽ
	uint8_t UartDMAEnable;   //ѡ���Ƿ���DMA
}UART_ITStructure;

extern UART_ITStructure UART0_ITSet;
extern UART_ITStructure UART1_ITSet;

#define UART_WordLength_5b                  ((uint8_t)0x00)
#define UART_WordLength_6b                  ((uint8_t)0x01)
#define UART_WordLength_7b                  ((uint8_t)0x02)
#define UART_WordLength_8b                  ((uint8_t)0x03)

#define UART_NO_PARITY         ((uint8_t)0x00 & 0xff)
#define UART_LOGIC_1_ODD_NUM   ((uint8_t)0x08 & 0xff)   //�߼�1������
#define UART_LOGIC_1_EVEN_NUM  ((uint8_t)0x18 & 0xff)   //�߼�1ż����
#define UART_STICK_PARITY_AS1  ((uint8_t)0x28 & 0xff)   //�̶���żУ��Ϊ1
#define UART_STICK_PARITY_AS0  ((uint8_t)0x38 & 0xff)


/*�ж���Ϣ��ȡ*/
/*�жϹ���*/
uint8_t UART_INT_PEND(CMSDK_UART_TypeDef* UARTx);  //Ϊ0ʱ��UART���жϹ��� 
/*�ж�״̬�ж�*/
uint8_t UART_INT_TYPE(CMSDK_UART_TypeDef* UARTx);
/*fifo ʹ��ָʾ��*/
uint8_t UART_FIFO_USE(CMSDK_UART_TypeDef* UARTx);  //0:��fifoģʽ  1��fifo ʹ��
/*�������FIFO*/
void UART_TXCLR(CMSDK_UART_TypeDef* UARTx);
/*�������FIFO*/
void UART_RXCLR(CMSDK_UART_TypeDef* UARTx);
/*FIFOʹ��*/
void UART_FIFOEnable(CMSDK_UART_TypeDef* UARTx);
/*FIFO�ر�*/
void UART_FIFODisable(CMSDK_UART_TypeDef* UARTx);

/*��������*/
uint8_t READ_UART_RCVBuff(CMSDK_UART_TypeDef* UARTx );
/*��������*/
void WRITE_UART_THRBuff(CMSDK_UART_TypeDef* UARTx ,uint8_t data);

/*FIFO״̬��ȡ*/
uint8_t UART_RX_FIFO_LEN(CMSDK_UART_TypeDef* UARTx) ;
uint8_t UART_TX_FIFO_LEN(CMSDK_UART_TypeDef* UARTx)  ;
uint8_t UART_RX_FIFO_FULL(CMSDK_UART_TypeDef* UARTx) ;	
uint8_t UART_RX_FIFO_EMPTY(CMSDK_UART_TypeDef* UARTx) ;
uint8_t UART_TX_FIFO_FULL(CMSDK_UART_TypeDef* UARTx)  ;
uint8_t UART_TX_FIFO_EMPTY(CMSDK_UART_TypeDef* UARTx) ;

/*�շ���״̬��ȡ*/
uint8_t UARTLine_RCVError(CMSDK_UART_TypeDef* UARTx);

/*�жϷ��ͻ����Ƿ�Ϊ��*/
uint8_t UARTLine_TRANSEmpty(CMSDK_UART_TypeDef* UARTx);

/*�ж�THR�Ƿ�Ϊ��*/
uint8_t UARTLine_THREmpty(CMSDK_UART_TypeDef* UARTx);

/*UART��ʼ��*/
void UART_Init(CMSDK_UART_TypeDef *CMSDK_UART, UART_InitStructure* uart_paraX);
void UART_ITConfig(CMSDK_UART_TypeDef *CMSDK_UART, UART_ITStructure* uart_paraX);
unsigned char UartPutc(CMSDK_UART_TypeDef *CMSDK_UART ,unsigned char my_ch);
void Uart_Send(CMSDK_UART_TypeDef *CMSDK_UART ,uint8_t *data, uint8_t len);

	


#endif

