⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xsstuart.c

📁 uart实验源码!希望你可以给你们带来帮助!谢谢可以跟大家分享!
💻 C
📖 第 1 页 / 共 2 页
字号:
* GLOBAL EFFECTS: none. 
* 
* ASSUMPTIONS: none. 
* 
******************************************************************************* 
*/ 

static 
void writeSTUart(UartContextT * ctxP, CHAR * txbufP, INT len) 
{ 
volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP; 
INT i; 

// Disable SIR receiver and enable the transmitter 
SelectIrOperationMode (Sir_Mode, IrShutdownPower); 
enableTransmIR(ctxP, enableSIR); 
SelectIrOperationMode (Sir_Mode, IrMaxPower); 

DM_WaitUs(100); 
for (i=0; i < len; i++) 
{ 
// Write data 
uartP->UDATA = *txbufP++; 

// Wait for UART to complete transmition 
while((uartP->LSR &amt; LSR_TEMT) == 0) 
{;} 
} 

// Disable SIR transmitter and enable the receiver 
enableTransmIR(ctxP, disableSIR); 
} 

/* 
******************************************************************************* 
* 
* FUNCTION: readSTUart 
* 
* DESCRIPTION: This function is used to receive data via UART in polled mode 
* operation 
* 
* INPUT PARAMETERS: ctxP is a pointer to UART context structure 
* rxbufP is a pointer to the buffer where received data is 
* going to be placed 
* len is a specified number of bytes to read. 
* 
* RETURNS: INT an actual number of bytes have been read 
* 
* GLOBAL EFFECTS: none. 
* 
* ASSUMPTIONS: none. 
* 
******************************************************************************* 
*/ 

static 
INT readSTUart(UartContextT * ctxP, CHAR * rxbufP, INT len) 
{ 
volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP; 
INT retry = RETRY; 
INT i; 

for (i=0; i < len; i++) 
{ 
// Wait for data to be available 
while (((uartP->LSR &amt; LSR_DR) == 0) &amt;&amt; (--retry > 0)) 
DM_WaitMs(1); 

// Read data 
if (retry > 0) 
*rxbufP++ = uartP->UDATA; 
else 
break; 
} 
return i; 
} 


/* 
******************************************************************************* 
* 
* FUNCTION: clearRxSTUart 
* 
* DESCRIPTION: This function is used to clear receive FIFO of the UART 
* 
* INPUT PARAMETERS: ctxP is a pointer to UART context structure 
* 
* RETURNS: INT total number of bytes read 
* 
* GLOBAL EFFECTS: none. 
* 
* ASSUMPTIONS: none. 
* 
******************************************************************************* 
*/ 

static 
INT clearRxSTUart(UartContextT * ctxP) 
{ 
volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP; 
UINT data; 
INT total = 0; 

// Clear the Rx FIFO 
uartP->FCR = ctxP->uartCfgP->maskFIFO | FCR_RESETRF; 

// Read data from FIFO until none is left 
while ((uartP->LSR &amt; LSR_DR) != 0) 
{ 
data = uartP->UDATA; 
total++; 
} 

return total; 
} 

/* 
******************************************************************************* 
* 
* FUNCTION: XsSTUartHWShutdown 
* 
* DESCRIPTION: This function is used to shutdown the UART 
* Tx and Rx FIFOs are cleared and and a clock is stopped. 
* 
* INPUT PARAMETERS: ctxP is a pointer to the UART's context structure 
* 
* RETURNS: INT a contence of the Interrupt Enable Register (IER) 
* 
* GLOBAL EFFECTS: none. 
* 
* ASSUMPTIONS: none. 
* 
******************************************************************************* 
*/ 

static 
INT XsSTUartHWShutdown (UartContextT * ctxP) 
{ 
volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP; 

// Disable the UART 
uartP->IER &amt;= ~IER_UUE; 

// Clear the peripheral clock bit for the UART 
xsCMDisableClock (CK_STUART); 

return uartP->IER; 
} 

/* 
******************************************************************************* 
* 
* FUNCTION: InterruptHandlerDmaTxSTUart 
* 
* DESCRIPTION: Interrupt handler for DMA Tx transfer. 
* This is callback function, called by the DMA service. 
* 
* INPUT PARAMETERS: PVOID param - pointer to the Uart's context structure 
* UINT32 triggeringDcsr - contains the interrupts status 
* 
* RETURNS: None 
* 
* GLOBAL EFFECTS: None 
* 
* ASSUMPTIONS: The DMA service clears the interrupts 
* 
* CALLS: 
* 
* CALLED BY: DMA interrupt service 
* 
* PROTOTYPE: VOID InterruptHandlerDmaTxSTUart (PVOID param, UINT32 triggeringDcsr); 
* 
******************************************************************************* 
*/ 

VOID InterruptHandlerDmaTxSTUart (PVOID param, UINT32 triggeringDcsr) 
{ 
UartContextT * ctxP = (UartContextT *)param; 

// Check for the bus error 
if (triggeringDcsr &amt; DCSR_BUSERRINTR) 
{ 
DM_CwDbgPrintf(DM_CW_UART_0, "Bus Error Interrupt \n"); 
// Increment bus errorcounter 
ctxP->dmaTxIntStatusP->busErrorIntCount++; 
} 

// Check for completion of the current transmition 
if (triggeringDcsr &amt; DCSR_ENDINTR) 
{ 
// Increment end interrupt counter 
ctxP->dmaTxIntStatusP->endIntCount++; 
if (ctxP->dmaTxIntStatusP->endIntCount >= ctxP->dmaTxCfgP->descNum) 
{ 
DM_CwDbgPrintf(DM_CW_UART_0, "End of the Tx transfer"); 
ctxP->xferTxComplete = TRUE; 
} 
} 

if (triggeringDcsr &amt; DCSR_STARTINTR) 
{ 
// Increment start interrupt counter 
ctxP->dmaTxIntStatusP->startIntCount++; 
} 

if (triggeringDcsr &amt; DCSR_STOPINTR) 
{ 
// Increment stop interrupt counter 
ctxP->dmaTxIntStatusP->stopIntCount++; 
DM_CwDbgPrintf(DM_CW_UART_0, "End of the Tx transfer"); 
ctxP->xferTxComplete = TRUE; 
} 
} 

/* 
******************************************************************************* 
* 
* FUNCTION: InterruptHandlerDmaRxSTUart 
* 
* DESCRIPTION: Interrupt handler for DMA Rx transfer. 
* This is callback function, called by the DMA service. 
* 
* INPUT PARAMETERS: PVOID param - pointer to the Uart's context structure 
* UINT32 triggeringDcsr - contains the interrupts status 
* 
* RETURNS: None 
* 
* GLOBAL EFFECTS: None 
* 
* ASSUMPTIONS: The DMA service clears the interrupts 
* 
* CALLS: 
* 
* CALLED BY: DMA interrupt service 
* 
* PROTOTYPE: VOID InterruptHandlerDmaRxSTUart (PVOID param, UINT32 triggeringDcsr); 
* 
******************************************************************************* 
*/ 

VOID InterruptHandlerDmaRxSTUart (PVOID param, UINT32 triggeringDcsr) 
{ 
UartContextT * ctxP = (UartContextT *)param; 

// Check for the bus error 
if (triggeringDcsr &amt; DCSR_BUSERRINTR) 
{ 
DM_CwDbgPrintf(DM_CW_UART_0, "Bus Error Interrupt \n"); 
// Increment bus errorcounter 
ctxP->dmaRxIntStatusP->busErrorIntCount++; 
} 

// Check for completion of the current transmition 
if (triggeringDcsr &amt; DCSR_ENDINTR) 
{ 
// Increment end interrupt counter 
ctxP->dmaRxIntStatusP->endIntCount++; 
if (ctxP->dmaRxIntStatusP->endIntCount >= ctxP->dmaRxCfgP->descNum) 
{ 
DM_CwDbgPrintf(DM_CW_UART_0, "End of the Rx transfer"); 
ctxP->xferRxComplete = TRUE; 
} 
} 

if (triggeringDcsr &amt; DCSR_STARTINTR) 
{ 
// Increment start interrupt counter 
ctxP->dmaRxIntStatusP->startIntCount++; 
} 

if (triggeringDcsr &amt; DCSR_STOPINTR) 
{ 
// Increment stop interrupt counter 
ctxP->dmaRxIntStatusP->stopIntCount++; 
DM_CwDbgPrintf(DM_CW_UART_0, "End of the Rx transfer"); 
ctxP->xferRxComplete = TRUE; 
} 
} 

/* 
******************************************************************************* 
* 
* FUNCTION: XsSTUartSWInit 
* 
* DESCRIPTION: This function is used to initialize STUART's context structure. 
* No hardware setup is done at this point. 
* 
* INPUT PARAMETERS: none. 
* 
* RETURNS: none. 
* 
* GLOBAL EFFECTS: none. 
* 
* ASSUMPTIONS: none. 
* 
******************************************************************************* 
*/ 

void XsSTUartSWInit (void) 
{ 
// Initialize the UART 
UartContextT * ctxP = &amt;STUart; 

// Initialize the UART's register base 
UartRegsT * regsP = (UartRegsT *)STUARTREG_BASE; 
ctxP->regsP = regsP; 

// Initialize the UART's setup configuration 
ctxP->uartCfgP = &amt;defaultSTUartCfg; 

// Initialize the UART's Dma configuration 
ctxP->dmaTxCfgP = &amt;defaultDmaTxCfg; 
ctxP->dmaRxCfgP = &amt;defaultDmaRxCfg; 

// Initialize the UART's pointer to the Dma Status structure 
ctxP->dmaTxIntStatusP = &amt;defaultDmaTxStatus; 
ctxP->dmaRxIntStatusP = &amt;defaultDmaRxStatus; 

// Set the context structures functions pointers 
ctxP->setupUartFnP = (UartSetupT)XsSTUartHWSetup; 
ctxP->loopbackUartFnP = (UartLoopbackT)loopbackSTUart; 
ctxP->writeUartFnP = (UartWriteT)writeSTUart; 
ctxP->readUartFnP = (UartReadT)readSTUart; 
ctxP->clearRxUartFnP = (UartClearRxT)clearRxSTUart; 
ctxP->shutdownUartFnP = (UartCleanupT)XsSTUartHWShutdown; 
ctxP->intHandlerDmaTxFnP = (UartDmaIntHandlerT)InterruptHandlerDmaTxSTUart; 
ctxP->intHandlerDmaRxFnP = (UartDmaIntHandlerT)InterruptHandlerDmaRxSTUart; 

// Load Uart ID 
ctxP->type = STUartType; 

ctxP->loggedError = 0; 
ctxP->dmaRxChannel = 0; 
ctxP->dmaTxChannel = 0; 
ctxP->xferTxComplete = 0; 
ctxP->xferRxComplete = 0; 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -