📄 uart.c
字号:
//
//cTxMode,bTxPolling
bool TxString(unsigned char ch, char *str, int bytesize, void (*func_start)(void), bool (*func_test)(unsigned char )) // The last character of 'str' should be NULL
{
int iTxMode;
bool bTxPolling;
int i;
bool bret = 1;
//global
TxStringSet(ch, str);
//local
iTxMode= g_pUartConTx->cTxMode & 3;
bTxPolling = g_pUartConTx->bTxPolling;
printf("rUCON: %x\n",g_pUartRegsTx->rUCon);
printf("rUFCON: %x\n",g_pUartRegsTx->rUfCon);
printf("rUMCON: %x\n",g_pUartRegsTx->rUmCon);
printf("rUMSTAT: %x\n",g_pUartRegsTx->rUmStat);
if(iTxMode==0)
{
TxStringStartINT(ch);//prevent isr if not used
return 0;//ASSERT : in case of TX disabled
}
SetResStopwatch(1000); // us order, interval 1000us = 1ms
StartStopwatchExtendSet(0);
//set
if(iTxMode == 2 || iTxMode==3) TxStringSetDMA(ch, str, iTxMode);//DMA mode
//start
if(func_start==0)
{
if(iTxMode == 1 && bTxPolling==1) StartStopwatchExtendStart(0);
else if(iTxMode == 1 && bTxPolling ==0) TxStringStartINT(ch);
else TxStringStartDMA(iTxMode);
}
else
func_start();//custom fn.
printf("sending...\n");
//test & finish
if(func_test==0)
{
if(iTxMode == 1 && bTxPolling ==1) TxStringTestPolling(ch, bytesize);
else if(iTxMode == 1 && bTxPolling ==0) TxStringTestINT(ch);
else TxStringTestDMA(ch, iTxMode);
}
else
bret = func_test(ch);//custom fn.
i=EndStopwatchExtend(0);
printf("time took : %d us",i);
return bret;
}
char* RxString(unsigned char ch, char *str, bool * success, void (*func_start)(void), bool (*func_test)(unsigned char)) // The last character of input string should be '\r'. simple test code
{
int iRxMode;
bool bRxPolling;
bool bret = 1;
//global
RxStringSet(ch, str);
//local
iRxMode= g_pUartConRx->cRxMode & 3;
bRxPolling = g_pUartConRx->bRxPolling;
if(g_pUartConRx->bRXTimeOuten) printf("rx timeout interrupt enabled\n");
printf("rUCON: %x\n",g_pUartRegsRx->rUCon);
printf("rUFCON: %x\n",g_pUartRegsRx->rUfCon);
printf("rUMCON: %x\n",g_pUartRegsRx->rUmCon);
if(iRxMode==0) //ASSERT : in case of RX disabled
{
RxStringStartINT(ch);//to prevent isr if rx is disabled
return 0;
}
//set
if(iRxMode == 2 || iRxMode==3) RxStringSetDMA(ch, str);//DMA mode
//start
if(func_start==0)
{
if(iRxMode == 1 && bRxPolling==1) ;//StartStopwatchExtendStart(0);
else if(iRxMode == 1 && bRxPolling ==0) RxStringStartINT(ch);
else RxStringStartDMA(iRxMode);
}
else
func_start();//custom fn.
printf("receiving...\n");
//test & finish
if(func_test==0)
{
if(iRxMode == 1 && bRxPolling ==1) RxStringTestPolling(ch);
else if(iRxMode == 1 && bRxPolling ==0) RxStringTestINT(ch);
else RxStringTestDMA(ch, iRxMode);
}
else
bret = func_test(ch);//custom fn.
*success = bret;
pUartRxStr[ch] = ( char *)str;
return pUartRxStr[ch];
}
bool TxRxString(unsigned char Txch, char *Txstr,
unsigned char Rxch, char *Rxstr,
void (*func_start)(void), bool (*func_test)(unsigned char, unsigned char ))
{
int iTxMode;
bool bTxPolling;
int iRxMode;
bool bRxPolling;
int i;
bool bret = 1;
//global
TxStringSet(Txch, Txstr);
RxStringSet(Rxch, Rxstr);
//local
iTxMode= g_pUartConTx->cTxMode & 3;
bTxPolling = g_pUartConTx->bTxPolling;
iRxMode= g_pUartConRx->cRxMode & 3;
bRxPolling = g_pUartConRx->bRxPolling;
if(iTxMode==0) return 0;//ASSERT : in case of TX disabled
if(iRxMode==0) return 0;//ASSERT : in case of RX disabled
SetResStopwatch(1000); // us order, interval 1000us = 1ms
StartStopwatchExtendSet(0);
//set
if(iTxMode == 2 || iTxMode==3) TxStringSetDMA(Txch, Txstr, iTxMode);//DMA mode
if(iRxMode == 2 || iRxMode==3) RxStringSetDMA(Rxch, Rxstr);//DMA mode
//start
if(func_start==0)
{
if(iTxMode == 1 && bTxPolling==1) StartStopwatchExtendStart(0);
else if(iTxMode == 1 && bTxPolling ==0) TxStringStartINT(Txch);
else TxStringStartDMA(iTxMode);
if(iRxMode == 1 && bRxPolling==1) ;
else if(iRxMode == 1 && bRxPolling ==0)
{
//for(i=0; i<3; i++) RxStringStartINT(i); //clear
RxStringStartINT(Rxch);
}
else RxStringStartDMA(iRxMode);
}
else
func_start();//custom fn.
//test & finish
if(func_test==0)
{
if(iTxMode == 1 && bTxPolling==1 && iRxMode == 1 && bRxPolling==1) TxRxStringTestPolling(Txch, Rxch);
else if(iTxMode == 1 && bTxPolling ==0 && iRxMode == 1 && bRxPolling ==0) TxRxStringTestINT(Txch, Rxch);
else ;
}
else
bret = func_test(Txch, Rxch);//custom fn.
i=EndStopwatchExtend(0);
printf("time took : %d us",i);
return bret;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// TX / RX Sub routine Core for Polliing/ISR
//
//////////////////////////////// RX ///////////////////////////////////
__inline void RxFifoSub(unsigned char ch)
{
int k,fifocnt;
fifocnt = g_pUartRegsRx->rUfStat & 0x3f;
//rx fifo read when rx fifo data is equal or more then the rx fifo trigger level.
//while ( g_pUartRegsRx->rUfStat & 0x3f )//until rx fifo count 0
for(k=0; k< fifocnt; k++) *pUartRxStr[ch]++ = (unsigned char)(g_pUartRegsRx->rUrxh) ;
if( (pUartRxStr[ch]-g_pRxString) >=g_UART_RXdatasize) isRxDone[ch] = 1;
}
__inline void RxFifoSubISR(unsigned char ch)
{
int k,fifocnt;
fifocnt = g_pUartRegsRx->rUfStat & 0x3f;
//rx fifo read when rx fifo data is equal or more then the rx fifo trigger level.
//while ( g_pUartRegsRx->rUfStat & 0x3f )//until rx fifo count 0
for(k=0; k< fifocnt; k++) *pUartRxStr[ch]++ = (unsigned char)(g_pUartRegsRx->rUrxh) ;
if( (pUartRxStr[ch]-g_pRxString) >=g_UART_RXdatasize)
{
rINTSUBMSK |= (BIT_UART_RXD<<SUBINTOFFSETDUPLEX1); //rx
isRxDone[ch] = 1;
}
}
__inline void RxNonFifoSub(unsigned char ch)
{
if(g_pUartRegsRx->rUtrStat&0x1)
{
*pUartRxStr[ch]++ = (unsigned char)(g_pUartRegsRx->rUrxh) ;
if( (pUartRxStr[ch]-g_pRxString) ==g_UART_RXdatasize) isRxDone[ch] = 1;
}
}
__inline void RxNonFifoSubISR(unsigned char ch)
{
*pUartRxStr[ch]++ = (unsigned char)(g_pUartRegsRx->rUrxh);
if( (pUartRxStr[ch]-g_pRxString) ==g_UART_RXdatasize)
{
rINTSUBMSK |= (BIT_UART_RXD<<SUBINTOFFSETDUPLEX1); //rx
isRxDone[ch] = 1;
}
}
//////////////////////////////// TX ///////////////////////////////////
__inline void TxFifoSub(unsigned char ch)
{
while ( !( (g_pUartRegsTx->rUfStat >>14) & 0x1 ) )//until tx fifo full or end of string
{
g_pUartRegsTx->rUtxh = *pUartTxStr[ch]++;
if( (pUartTxStr[ch]-g_pTxString) ==g_UART_TXdatasize) isTxDone[ch] = 1;
}
}
__inline void TxFifoSubISR(unsigned char ch)
{
int k, fifocnt;
fifocnt = 64- ( (g_pUartRegsTx->rUfStat>>8) & 0x3f );
//while ( !( (g_pUartRegsTx->rUfStat >>14) & 0x1 ) )//until tx fifo full or end of string
for(k=0; k< fifocnt; k++)
{
g_pUartRegsTx->rUtxh = *pUartTxStr[ch]++;
if( (pUartTxStr[ch]-g_pTxString) ==g_UART_TXdatasize) break;
}
if( (pUartTxStr[ch]-g_pTxString) ==g_UART_TXdatasize)
{
rINTSUBMSK |= (BIT_UART_TXD<<SUBINTOFFSETDUPLEX0);
isTxDone[ch] = 1;
}
}
__inline void TxNonFifoSub(unsigned char ch)
{
//if(g_pUartRegsTx->rUtrStat &0x4) {//buffer & shifer
if(g_pUartRegsTx->rUtrStat &0x2) //buffer(recommand)
{
g_pUartRegsTx->rUtxh = *pUartTxStr[ch]++;
if( (pUartTxStr[ch]-g_pTxString) ==g_UART_TXdatasize) isTxDone[ch] = 1;
}
}
__inline void TxNonFifoSubISR(unsigned char ch)
{
g_pUartRegsTx->rUtxh = *pUartTxStr[ch]++;
if( (pUartTxStr[ch]-g_pTxString) ==g_UART_TXdatasize)
{
rINTSUBMSK |= (BIT_UART_TXD<<SUBINTOFFSETDUPLEX0);
isTxDone[ch] = 1;
}
}
__inline void ErrorSubISR(unsigned char ch, unsigned int ErrorStatus)
{
switch(ErrorStatus)//to clear and check the status of register bits
{
case 1: printf("Overrun error!\n"); break;
case 2: printf("Parity error!\n"); break;
case 4: printf("Frame error!\n"); break;
case 6: printf("Parity, Frame error!\n"); break;
case 8: printf("Breake detect\n"); break;
case 0xa: printf("Parity error & Break detect!\n"); break;
case 0xc: printf("Frame error & Breake detect\n"); break;
case 0xe: printf("Parity, Frame error & Break detect!\n"); break;
default : printf("Unknown error : 0x%x\n", ErrorStatus); break;
}
rINTSUBMSK |= (BIT_UART_ERROR<<SUBINTOFFSETDUPLEX1);//rx
isRxDone[ch] = 1;//stop at error
}
#if 1
//SUBINTOFFSETDUPLEX0, SUBINTOFFSETDUPLEX0 should be fixed before test
//even one side is not used.
void UartSubIsr(unsigned char ch)
{
unsigned int uSubSrcPnd= rSUBSRCPND;
bool duplex0pend=0;//tx
bool duplex1pend=0;//rx
// receive Errors ISR
if (uSubSrcPnd & (BIT_UART_ERROR<< SUBINTOFFSETDUPLEX1))
{
unsigned int ErrorStatus = g_pUartRegsRx->rUerStat;
if( !g_bLeveltypeRx ) rSUBSRCPND = (BIT_UART_ERROR<<SUBINTOFFSETDUPLEX1) ;
ErrorSubISR(ch, ErrorStatus);
if( g_bLeveltypeRx ) rSUBSRCPND = (BIT_UART_ERROR<<SUBINTOFFSETDUPLEX1) ;
duplex1pend=1;
}
// Rx ISR
if (uSubSrcPnd & (BIT_UART_RXD<<SUBINTOFFSETDUPLEX1))
{
if( !g_bLeveltypeRx ) rSUBSRCPND = (BIT_UART_RXD<<SUBINTOFFSETDUPLEX1);//pulse type
// if dma mode, Rx timeout
// guide to use non fifo mode,
// if fifo mode(not spec), should be multifle of trigger lever size
if (g_FifoEnableRx) RxFifoSubISR(ch);// 1 : fifo enable (fifo mode) -inline
else RxNonFifoSubISR(ch);// 0 : fifo disable(non fifo mode) - inline
if( g_bLeveltypeRx ) rSUBSRCPND = (BIT_UART_RXD<<SUBINTOFFSETDUPLEX1);//level type
duplex1pend=1;
}
// Tx ISR
if (uSubSrcPnd & (BIT_UART_TXD<<SUBINTOFFSETDUPLEX0))
{
if( !g_bLeveltypeTx ) rSUBSRCPND = (BIT_UART_TXD<<SUBINTOFFSETDUPLEX0);//pulse type
if (g_FifoEnableTx) TxFifoSubISR(ch);// 1 : fifo enable (fifo mode) -inline
else TxNonFifoSubISR(ch);// 0 : fifo disable (non fifo mode) - inline
if( g_bLeveltypeTx ) rSUBSRCPND = (BIT_UART_TXD<<SUBINTOFFSETDUPLEX0);//level type
duplex0pend=1;
}
if(duplex0pend)ClearPending(BIT_UART_DUPLEX0);
if(duplex1pend)ClearPending(BIT_UART_DUPLEX1);
}
#else
//2443
void UartSubIsr(unsigned char ch)
{
unsigned int ErrorStatus = 0;
char ReadChar;
unsigned int uSubSrcPnd;
volatile UART_REGS *pUartRegs;
uSubSrcPnd = rSUBSRCPND;
pUartRegs = ( UART_REGS *)(UART_REG_BASE+UART_REG_OFFSET*ch);
if(ch == 1) { SUBINTOFFSET = 3; SRCPNDOFFSET = 23; }
else if(ch == 2) { SUBINTOFFSET = 6; SRCPNDOFFSET = 15; }
else if(ch == 3) { SUBINTOFFSET = 24; SRCPNDOFFSET = 18; }
else { SUBINTOFFSET = 0; SRCPNDOFFSET = 28; }// default : CH 0
if(!g_bIsLevelInt) rSUBSRCPND = (BIT_UART_ERROR<<SUBINTOFFSET)|(BIT_UART_TXD<<SUBINTOFFSET)|(BIT_UART_RXD<<SUBINTOFFSET);
// Check Errors
if (uSubSrcPnd & (BIT_UART_ERROR<< SUBINTOFFSET))
{
ErrorStatus = pUartRegs->rUerStat;
switch(ErrorStatus)//to clear and check the status of register bits
{
case 1: printf("Overrun error!\n"); break;
case 2: printf("Parity error!\n"); break;
case 4: printf("Frame error!\n"); break;
case 6: printf("Parity, Frame error!\n"); break;
case 8: printf("Breake detect\n"); break;
case 0xa: printf("Parity error & Break detect!\n"); break;
case 0xc: printf("Frame error & Breake detect\n"); break;
case 0xe: printf("Parity, Frame error & Break detect!\n"); break;
default : printf("Unknown error : 0x%x\n", ErrorStatus); break;
}
isRxDone[ch] = 1;
rINTSUBMSK |= (BIT_UART_RXD<<SUBINTOFFSET);
}
// Tx ISR
if (uSubSrcPnd & (BIT_UART_TXD<<SUBINTOFFSET))
{
if (pUartRegs->rUfCon & 1) // 1 : fifo enable
{
while (!(pUartRegs->rUfStat & (1<<14)) && (*pUartTxStr[ch] != TX_END_CHAR)) //until tx fifo full or end of string
pUartRegs->rUtxh = *pUartTxStr[ch]++;
if( !( *pUartTxStr[ch])) // if there is no character in the pUartTxStr[ch], Tx is done
{ rINTSUBMSK |= (BIT_UART_TXD<<SUBINTOFFSET); isTxDone[ch] = 1; }
}
else // 0 : fifo disable
{
if (*pUartTxStr[ch]) pUartRegs->rUtxh = *pUartTxStr[ch]++; // if there is no character in the pUartTxStr[ch], Tx is done
else
{ rINTSUBMSK |= (BIT_UART_TXD<<SUBINTOFFSET); isTxDone[ch] = 1; }
}
}
// Rx ISR
if (uSubSrcPnd & (BIT_UART_RXD<<SUBINTOFFSET))
{
if (pUartRegs->rUfCon & 1) // 1 : fifo enable
{
while (pUartRegs->rUfStat & 0x3f) //until rx fifo count 0
*pUartRxStr[ch]++ = (unsigned char)(pUartRegs->rUrxh);
if(*(pUartRxStr[ch]-1) == RX_END_CHAR)
{
*pUartRxStr[ch] = NULL; isRxDone[ch] = 1;
rINTSUBMSK |= (BIT_UART_RXD<<SUBINTOFFSET);
}
}
else // 0 : fifo disable
{
ReadChar = (unsigned char)(pUartRegs->rUrxh);
if (ReadChar != RX_END_CHAR) *pUartRxStr[ch]++ = ReadChar;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -