📄 uart.c
字号:
}
/******************************************************************************
@@
@@ [Name] : apd_UARTClrModemCtrlReg
@@
@@ [Summary] : This function clears the given bits in the UART's MODEM
@@ Control 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] : Clears the selected UART MODEM Control register's bits to 0
@@
@@ [END]
******************************************************************************/
void apd_UARTClrModemCtrlReg(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
*(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTMCR_OFST) &= ~bdata;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetIntrReg
@@
@@ [Summary] : This function sets the given bits in the UART Interrupt
@@ 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 Interrupt register's bits to 1
@@
@@ [END]
******************************************************************************/
void apd_UARTSetIntrReg(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
*(volatile unsigned char *)(APD_UARTBASE + (uart_num * APD_UARTCH_OFST) + APD_UARTINTMSK_OFST) |= bdata;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTClrIntrReg
@@
@@ [Summary] : This function clears the given bits in the UART Interrupt
@@ 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 set to 1
@@ For example, to set bit 0 and 2, bdata is 0x05
@@
@@ [Return] : None
@@
@@ [Desc] : Clears the selected UART Interrupt register's bits to 0
@@
@@ [END]
******************************************************************************/
void apd_UARTClrIntrReg(APD_UART_NUM_TYPE uart_num, unsigned char bdata)
{
*(volatile unsigned char *)(APD_UARTBASE + (uart_num * APD_UARTCH_OFST) + APD_UARTINTMSK_OFST) &= ~bdata;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTReadFLG
@@
@@ [Summary] : This function reads the contents of UART Flag register
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : Contents of UART Flag register
@@
@@ [Desc] : Reads and returns the contents of UART Flag register for the
@@ specified UART channel
@@
@@ [END]
******************************************************************************/
unsigned char apd_UARTReadFLG (APD_UART_NUM_TYPE uart_num)
{
return (*(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTFLG_OFST));
}
/******************************************************************************
@@
@@ [Name] : apd_UARTReadIntr
@@
@@ [Summary] : This function reads the contents of the UART Interrupt register
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : Contents of UART Interrupt register
@@
@@ [Desc] : Reads and returns the contents of UART Interrupt register for the
@@ specified UART channel
@@
@@ [END]
******************************************************************************/
unsigned char apd_UARTReadIntr (APD_UART_NUM_TYPE uart_num)
{
return(*(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTINTR_OFST));
}
/******************************************************************************
@@
@@ [Name] : apd_UARTReadRxStatus
@@
@@ [Summary] : This function reads the contents of the UART Received Status
@@ register
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : Contents of Receive Status register
@@
@@ [Desc] : Reads and returns the contents of UART Receive Status register
@@ for the specified UART channel
@@
@@ [END]
******************************************************************************/
unsigned char apd_UARTReadRxStatus(APD_UART_NUM_TYPE uart_num)
{
return ( *(volatile unsigned char *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTRXSTAT_OFST));
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSIRDisable
@@
@@ [Summary] : This function disables the specified SIR protocol
@@
@@ [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 SIR protocol as specified in the argument
@@
@@ [END]
******************************************************************************/
void apd_UARTSIRDisable(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_SIREN;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSIREnable
@@
@@ [Summary] : This function enables the specified SIR protocol
@@
@@ [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 SIR protocol as specified in the argument
@@
@@ [END]
******************************************************************************/
void apd_UARTSIREnable(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_SIREN;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetIRTXM
@@
@@ [Summary] : This function represent the zero bit as a pulse which is
@@ 3.5 times the period of the CLK2MHz input signal.
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : IrDA Tx mode bit. If '1', each zero bit is represented as
@@ a pulse which is 3.5 times the period of the CLK2MHz input signal.
@@
@@ [END]
******************************************************************************/
void apd_UARTSetIRTXM(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_IRTXM;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTClearIRTXM
@@
@@ [Summary] : This function represent the zero bit as a pulse of width
@@ 3/16th of the bit rate period
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : IrDA Tx mode bit. If '0', each zero bit transmitted is
@@ represented as a pulse of width 3/16th of the bit rate period
@@
@@ [END]
******************************************************************************/
void apd_UARTClearIRTXM(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_IRTXM;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTSetSIRPOL
@@
@@ [Summary] : This function set the polarity of SIR.
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : This function set the polarity of SIR. If '1', SIR receive
@@ signal polarity is positive
@@
@@ [END]
******************************************************************************/
void apd_UARTSetSIRPOL(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_SIRPOL;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTClearSIRPOL
@@
@@ [Summary] : This function clear the polarity of SIR.
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : This function clear the polarity of SIR.
@@
@@ [END]
******************************************************************************/
void apd_UARTClearSIRPOL(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_SIRPOL;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTERINTDisable
@@
@@ [Summary] : This function disables the error interrupt of the specified UART
@@
@@ [Argument] : uart_num: Specify the UART channel number
@@ APD_UART_CH0 for channel 0
@@ APD_UART_CH1 for channel 1
@@
@@ [Return] : None
@@
@@ [Desc] : This function disables the error interrupt of the specified UART
@@
@@ [END]
******************************************************************************/
void apd_UARTERINTDisable(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_ERINTEN;
}
/******************************************************************************
@@
@@ [Name] : apd_UARTERINTEnable
@@
@@ [Summary] : This function enables the error interrupt of the specified UART
@@
@@ [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 error interrupt as specified in the argument
@@
@@ [END]
******************************************************************************/
void apd_UARTERINTEnable(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_ERINTEN;
}
/******************************************************************************
Sio open
uart_num: APD_UART_CH0 for channel 0
APD_UART_CH1 for channel 1
int_src: APD_INTC_UART0 for UART channel 0 interrupt
APD_INTC_UART1 for UART channel 1 interrupt
******************************************************************************/
void apd_SIOOpen(APD_UART_NUM_TYPE uart_num,unsigned long interrupt_src)
{
apd_INTCEnableIRQ(interrupt_src);
apd_UARTEnable(uart_num);
apd_UARTEnableRxIntr(uart_num);
// apd_UARTEnableTxIntr(uart_num);
}
/******************************************************************************
Sio close
uart_num: APD_UART_CH0 for channel 0
APD_UART_CH1 for channel 1
int_src: APD_INTC_UART0 for UART channel 0 interrupt
APD_INTC_UART1 for UART channel 1 interrupt
******************************************************************************/
void apd_SIOClose(APD_UART_NUM_TYPE uart_num,unsigned long interrupt_src)
{
apd_UARTDisableRxIntr(uart_num);
apd_INTCDisableIRQ(interrupt_src);
apd_UARTDisable(uart_num);
}
/******************************************************************************
Sio read
uart_num: APD_UART_CH0 for channel 0
APD_UART_CH1 for channel 1
******************************************************************************/
unsigned char apd_SIORead(APD_UART_NUM_TYPE uart_num)
{
return (*( volatile APD_USHORT *)(APD_UARTBASE + (uart_num * APD_UARTCH_OFST) + APD_UARTDR_OFST));
}
/******************************************************************************
Sio write
uart_num: APD_UART_CH0 for channel 0
APD_UART_CH1 for channel 1
txdata: data to transmit
******************************************************************************/
void apd_SIOWrite(APD_UART_NUM_TYPE uart_num,unsigned char txdata)
{
*(volatile APD_USHORT *)(APD_UARTBASE + (uart_num*APD_UARTCH_OFST) + APD_UARTDR_OFST) = txdata;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -