📄 uart.c
字号:
/*-----------------------------------------------------------------------------
@@ (Summary) : LH7953x series UART Device Driver Source File
@@ (Comment) : Source codes of routines available for UART
@@ (RCS ID) :
@@
-----------------------------------------------------------------------------*/
#define APD_UART_C
/*+include files*************************************************************/
/* */
/* */
/**************************************************************************-*/
#include "uart.h"
/******************************************************************************
@@
@@ [Name] : apd_UARTInit
@@
@@ [Summary] : This function initializes the specified UART channel
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ uart_init: Specify the UART's initial setting type including
@@ - rate for baud rate;
@@ - format for data transfer format and
@@ - intmask for interrupt mask register setting
@@
@@ [Return] : None
@@
@@ [Desc] : Initializes the selected UART based on settings defined
@@ according to the argument (uart_init)
@@
@@ [Note] : APD_UART_CH2 is not used in LH79532 and LH79533.
@@
@@ [END]
******************************************************************************/
void apd_UARTInit (APD_UART_NUM_TYPE uart_num, APD_UART_INIT_TYPE uart_init)
{
*(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTINTMSK_OFST) = 0xFF;
*(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTINTMSK_OFST) &= ~uart_init.intmask ;
apd_UARTClrModemStatusChangeIntr(uart_num);
apd_UARTSetBaudRate(uart_num, uart_init.rate);
apd_UARTSetDataXferType(uart_num, uart_init.format);
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetBaudRate
@@
@@ [Summary] : This function sets the UART's baud rate
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ brate: Specify the baud rate
@@
@@ [Return] : None
@@
@@ [Desc] : Compute the baud rate divisor value from the user-specified
@@ baud rate and UART clock (UART clocks are defined in
@@ "apd_uart_sys_def.h" file). The computed result will be
@@ written into the UART H/M/L-BRLCR register.
@@
@@ [Note] : (1) UARTxHBRLCR register must be written first before UARTx-M/L-BRLCR
@@ registers are written.
@@
@@ [END]
******************************************************************************/
void apd_UARTSetBaudRate(APD_UART_NUM_TYPE uart_num, unsigned long brate)
{
unsigned long rdiv;
unsigned long ofst, hreg;
ofst = APD_UARTBASE + (uart_num*APD_UARTCH_OFST);
rdiv = APD_UARTCLKDIV[uart_num];
rdiv = ((rdiv /( 16 * brate)) -1);
*(volatile unsigned char *)(ofst + APD_UARTLBRLCR_OFST) = rdiv & 0xFF;
*(volatile unsigned char *)(ofst + APD_UARTMBRLCR_OFST) = rdiv >> 8;
hreg = *(volatile unsigned char *)(ofst + APD_UARTHBRLCR_OFST);
*(volatile unsigned char *)(ofst + APD_UARTHBRLCR_OFST) = hreg;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetDataXferType
@@
@@ [Summary] : This function sets the format of UART communications
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ dxtype : Specify the UART data transfer type.
@@ APD_UART_5W_NOXSTOP_NOPARITY for 5 words, 1 stop, no parity
@@ APD_UART_5W_1XSTOP_NOPARITY for 5 words, 2 stop, no parity
@@ APD_UART_5W_NOXSTOP_ODDPARITY for 5 words, 1 stop, odd parity
@@ APD_UART_5W_1XSTOP_ODDPARITY for 5 words, 2 stop, odd parity
@@ APD_UART_5W_NOXSTOP_EVENPARITY for 5 words, 1 stop, even parity
@@ APD_UART_5W_1XSTOP_EVENPARITY for 5 words, 2 stop, even parity
@@ APD_UART_6W_NOXSTOP_NOPARITY for 6 words, 1 stop, no parity
@@ APD_UART_6W_1XSTOP_NOPARITY for 6 words, 2 stop, no parity
@@ APD_UART_6W_NOXSTOP_ODDPARITY for 6 words, 1 stop, odd parity
@@ APD_UART_6W_1XSTOP_ODDPARITY for 6 words, 2 stop, odd parity
@@ APD_UART_6W_NOXSTOP_EVENPARITY for 6 words, 1 stop, even parity
@@ APD_UART_6W_1XSTOP_EVENPARITY for 6 words, 2 stop, even parity
@@ APD_UART_7W_NOXSTOP_NOPARITY for 7 words, 1 stop, no parity
@@ APD_UART_7W_1XSTOP_NOPARITY for 7 words, 2 stop, no parity
@@ APD_UART_7W_NOXSTOP_ODDPARITY for 7 words, 1 stop, odd parity
@@ APD_UART_7W_1XSTOP_ODDPARITY for 7 words, 2 stop, odd parity
@@ APD_UART_7W_NOXSTOP_EVENPARITY for 7 words, 1 stop, even parity
@@ APD_UART_7W_1XSTOP_EVENPARITY for 7 words, 2 stop, even parity
@@ APD_UART_8W_NOXSTOP_NOPARITY for 8 words, 1 stop, no parity
@@ APD_UART_8W_1XSTOP_NOPARITY for 8 words, 2 stop, no parity
@@ APD_UART_8W_NOXSTOP_ODDPARITY for 8 words, 1 stop, odd parity
@@ APD_UART_8W_1XSTOP_ODDPARITY for 8 words, 2 stop, odd parity
@@ APD_UART_8W_NOXSTOP_EVENPARITY for 8 words, 1 stop, even parity
@@ APD_UART_8W_1XSTOP_EVENPARITY for 8 words, 2 stop, even parity
@@
@@ [Return] : None
@@
@@ [Desc] : Sets the selected UART according to the argument setting. The
@@ word length, number of stop bits and parity check are set.
@@
@@ [END]
******************************************************************************/
void apd_UARTSetDataXferType(APD_UART_NUM_TYPE uart_num, APD_UART_DATAXFER_TYPE dxtype)
{
unsigned long hbrlcr;
hbrlcr = APD_UARTBASE + (uart_num * APD_UARTCH_OFST) + APD_UARTHBRLCR_OFST;
*(volatile APD_USHORT *)(hbrlcr) &= ~APD_UARTDATAXFER_MASK;
*(volatile APD_USHORT *)(hbrlcr) |= dxtype;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTIsBitSetInFlag
@@
@@ [Summary] : This function checks if a particular bit is set in the UART
@@ Flag register
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ bdata: Specify which bit to check
@@ For example, to check bit 2 set bdata to 0x04
@@
@@ [Return] : TRUE if the bit is set, FALSE if the bit is not set
@@
@@ [Desc] : Checks if a particluar bit is set in the UART Flag register
@@
@@ [END]
******************************************************************************/
APD_BOOLEAN apd_UARTIsBitSetInFlag(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
if ( ((*(volatile APD_USHORT *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTFLG_OFST)) & bdata) != 0)
{
return(TRUE);
}
else
{
return(FALSE);
}
}
/******************************************************************************
@@
@@ [Name] : apd_UARTIsBitSetInIntr
@@
@@ [Summary] : This function checks if a particular bit is set in the UART
@@ Interrupt register
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ bdata: Specify which bit to check
@2 For example, to check bit 2 set bdata to 0x04
@@
@@ [Return] : TRUE if the bit is set, FALSE if the bit is not set
@@
@@ [Desc] : Checks if a particluar bit is set in the UART Interrupt register
@@
@@ [END]
******************************************************************************/
APD_BOOLEAN apd_UARTIsBitSetInIntr(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
if ( ((*(volatile unsigned char *) (APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTINTR_OFST)) & bdata) != 0)
{
return(TRUE);
}
else
{
return(FALSE);
}
}
/******************************************************************************
@@
@@ [Name] : apd_UARTDisable
@@
@@ [Summary] : This function disables the specified UART channel
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : Disables the UART channel as specified in the argument
@@
@@ [END]
******************************************************************************/
void apd_UARTDisable(APD_UART_NUM_TYPE uart_num)
{
unsigned long control_reg;
control_reg = APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTCON_OFST;
*(volatile unsigned char *)control_reg &= ~APD_UARTCON_UARTEN;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTEnable
@@
@@ [Summary] : This function enables the specified UART channel
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : Enables the UART channel as specified in the argument
@@
@@ [END]
******************************************************************************/
void apd_UARTEnable(APD_UART_NUM_TYPE uart_num)
{
unsigned long control_reg;
control_reg = APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTCON_OFST;
*(volatile unsigned char *)control_reg |= APD_UARTCON_UARTEN;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTGetRxData
@@
@@ [Summary] : This function gets the data received by the UART
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : Data received by UART (APD_DR_DATA register)
@@
@@ [Desc] : Reads and returns the received data from the selected channel
@@
@@ [END]
******************************************************************************/
unsigned char apd_UARTGetRxData(APD_UART_NUM_TYPE uart_num)
{
return (*( volatile APD_USHORT *)(APD_UARTBASE + (uart_num * APD_UARTCH_OFST) + APD_UARTDR_OFST));
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetTxData
@@
@@ [Summary] : This function load the given data into UART for transmission
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@ APD_UART_CH2 for channel 2 (For LH79531 only)
@@
@@ txdata: data to transmit
@@
@@ [Return] : None
@@
@@ [Desc] : Loads the transmit data (txdata) into the selected UART
@@ (uart_num) for transmission
@@
@@ [END]
******************************************************************************/
void apd_UARTSetTxData(APD_UART_NUM_TYPE uart_num, unsigned char txdata)
{
*(volatile APD_USHORT *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTDR_OFST) = txdata;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTClrModemStatusChangeIntr
@@
@@ [Summary] : This function clears the MODEM status change interrupt
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : Clears the MODEM status change interrupt of the specified
@@ UART channel
@@
@@ [END]
******************************************************************************/
void apd_UARTClrModemStatusChangeIntr(APD_UART_NUM_TYPE uart_num)
{
*(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTMSEOI_OFST) = 0;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetHiLineCtrlReg
@@
@@ [Summary] : This function sets the bits in the H_UARTxBRLCR register to 1
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ bdata: Specify which bits to be set to 1
@@ For example, to set bit 0 and 2, bdata is 0x05
@@
@@ [Return] : None
@@
@@ [Desc] : Sets the given bits(bdata) in the selected UART(uart_num)
@@ H_UARTxBRLCR register to 1
@@
@@ [END]
******************************************************************************/
void apd_UARTSetHiLineCtrlReg(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
*(volatile APD_USHORT *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTHBRLCR_OFST) |= bdata;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTClrHiLineCtrlReg
@@
@@ [Summary] : This function clears the bits in the H_UARTxBRLCR register to 0
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ bdata: Specify which bits to be reset to 0
@@ For example, to reset bit 0 and 2, bdata is 0x05
@@
@@ [Return] : None
@@
@@ [Desc] : Sets the given bits(bdata) in the selected UART(uart_num)
@@ H_UARTxBRLCR register to 0.
@@
@@ [END]
******************************************************************************/
void apd_UARTClrHiLineCtrlReg(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
*(volatile APD_USHORT *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTHBRLCR_OFST) &= ~bdata;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetModemCtrlReg
@@
@@ [Summary] : This function sets the given bits in the UART's MODEM Control
@@ register to 1
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ bdata: Specify which bits to be set to 1
@@ For example, to set bit 0 and 2, bdata is 0x05
@@
@@ [Return] : None
@@
@@ [Desc] : Sets the selected UART MODEM Control register's bits to 1
@@
@@ [History] :
@@
@@ [END]
******************************************************************************/
void apd_UARTSetModemCtrlReg(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
*(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTMCR_OFST) |= bdata;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -