Files
kunlun/rom/riscv/inc/sUart.h
2024-09-28 14:24:04 +08:00

135 lines
3.2 KiB
C

/****************************************************************************
*
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
*
* This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
* be copied by any method or incorporated into another program without
* the express written consent of Aerospace C.Power. This Information or any portion
* thereof remains the property of Aerospace C.Power. The Information contained herein
* is believed to be accurate and Aerospace C.Power assumes no responsibility or
* liability for its use in any way and conveys no license or title under
* any patent or copyright and makes no representation or warranty that this
* Information is free from patent or copyright infringement.
*
* ****************************************************************************/
#ifndef _S_UART_H_
#define _S_UART_H_
char sUartGetChar();
void sUartClearFifo(void);
/* For config */
#define UART0 0x44001000
#define UART_FULL_THRESH 120
#define UART_CLOCK DEFAULT_PLL_FRQ
#define UART_CLOCK_SYSTICK 1000000 /* 1M */
#define REG_UARTDR (0x00 + UART0)
#define REG_STATUS (0x24 + UART0)
#define REG_CLKDIV (0x1C + UART0)
#define REG_CONFIG0 (0x14 + UART0)
#define REG_INT_ENA (0x04 + UART0)
#define REG_INT_CLR (0x08 + UART0)
#define UART_TXFIFO_CNT ((REG32(REG_STATUS)>>16)&0xFF)
#define UART_RXFIFO_CNT (REG32(REG_STATUS)&0xFF)
#define uGET_CHAR() sUartGetChar()
#define uPUT_CHAR(c) (REG32(REG_UARTDR) = (0xFF)&((UINT32)(c)))
#define uRX_RAEDY() (UART_RXFIFO_CNT > 0 ? 1 : 0)
#define uTX_BUSY() (UART_TXFIFO_CNT > UART_FULL_THRESH ? 1 : 0)
#define CLEAR_FIFO() sUartClearFifo()
/* ----------------------- Uart ----------------------- */
#define ASC_CTRL_T 0x14
#define aDELAY(m) do{\
int T = m;\
while(T--) __asm volatile("nop\n");\
}while(0)
enum
{
xmSOH = 0x01,
xmSTX = 0x02,
xmEOT = 0x04,
xmACK = 0x06,
xmNAK = 0x15,
xmCAN = 0x18,
xmCRC = 0x43, /* ASCII of 'C' */
};
enum
{
xmRT_Finish = 0,
xmRT_Error = -1,
};
/* --------------------- XMODEM --------------------------- */
#define xmSTOP_NOW() uPUT_CHAR(xmCAN)
#define xmERROR_HANDER() do{xmSTOP_NOW(); return xmRT_Error;}while(0)
#define xm1ST_RETRY_TIME 200
#define xmFRMTYPE_128 128
#define xmFRMTYPE_1K 1024
#define xmRCV_BUF_SIZE xmFRMTYPE_1K
#define xmCHECK_CHKSUM 0x01
#define xmCHECK_CRC16 0x02
#define xmTRY_CHECKSUM_MODE(t, l, m, c) do\
{\
l = 0x400000;\
CLEAR_FIFO();\
uPUT_CHAR(m);\
while((--l)&&(!uRX_RAEDY()));\
if(l)\
{\
c = uGET_CHAR();\
if((c ==xmSOH) || (c ==xmSTX))\
{\
break;\
}\
}\
}while(--t)
#define xmTRY_CHECK_FRAME_START(t, l, m, c) do\
{\
l = 0x400000;\
while((--l)&&(!uRX_RAEDY()));\
if(l)\
{\
c = uGET_CHAR();\
if((c ==xmSOH) || (c ==xmSTX) || (c ==xmEOT))\
{\
break;\
}\
}\
}while(--t)
#define RECIVE_CHAR(b, l) do{\
l = 0x4000;\
while((--l)&&(!uRX_RAEDY())) __asm volatile("nop\n");\
if(l) (b) = uGET_CHAR();\
}while(0)
uint32_t sUartTxFifoCnt();
uint32_t sUartRxFifoCnt();
void sUartPortInit(int bRate, int UseRefTick);
void sUartPutString(char *pStr);
UINT32 xModemReciveframe(char *const pBuf, char frNO, int newFile);
#endif