Zynq中UART
- 2019 年 11 月 5 日
- 筆記
UART模块的结构图

主程序流程:
UART初始化→设置UART模式→设置数据格式→设置中断→发送UART数据
程序设计如下:
Config =XUartPs_LookupConfig(UART_DEVICE_ID);
if (NULL == Config) {
return XST_FAILURE;
}
Status= XUartPs_CfgInitialize(&Uart_PS, Config, Config->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/* 配置UART模式
XUartPs_SetOperMode(&Uart_PS,XUARTPS_OPER_MODE_NORMAL);
/* 配置UART数据格式波特率:115200, 数据:8bits, 无校验, 1个停止位*/
XUartPs_SetDataFormat(&Uart_PS,&UartFormat) ;
/*设置中断
XUartPs_SetFifoThreshold(&Uart_PS,1);
/*使能接收FIFO触发电平和FIFO空中断
XUartPs_SetInterruptMask(&Uart_PS,XUARTPS_IXR_RXOVR|XUARTPS_IXR_RXEMPTY);
SetupInterruptSystem(&IntcInstPtr,&Uart_PS, UART_INT_IRQ_ID) ;
//发送数据
SendBufferPtr =TxString ;
SendByteNum= sizeof(TxString) ;
UartPsSend(&Uart_PS,SendBufferPtr, SendByteNum);
/* Block receiving the buffer. */
ReceivedCount= 0;
while (ReceivedCount < TEST_BUFFER_SIZE) {
ReceivedCount+=
XUartPs_Recv(&Uart_PS,&RecvBuffer[ReceivedCount],
(TEST_BUFFER_SIZE – ReceivedCount));
}

附录:
//UART发送数据
int UartPsSend(XUartPs *InstancePtr, u8 *BufferPtr, u32 NumBytes)
{
u32 SentCount = 0U;
/* Setup the buffer parameters */
InstancePtr->SendBuffer.RequestedBytes = NumBytes;
InstancePtr->SendBuffer.RemainingBytes = NumBytes;
InstancePtr->SendBuffer.NextBytePtr = BufferPtr;
while (InstancePtr->SendBuffer.RemainingBytes > SentCount)
{
/* Fill the FIFO from the buffer */
if (!XUartPs_IsTransmitFull(InstancePtr->Config.BaseAddress))
{
XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
XUARTPS_FIFO_OFFSET,
((u32)InstancePtr->SendBuffer.
NextBytePtr[SentCount]));
/* Increment the send count. */
SentCount++;
}
}
/* Update the buffer to reflect thebytes that were sent from it */
InstancePtr->SendBuffer.NextBytePtr += SentCount;
InstancePtr->SendBuffer.RemainingBytes -= SentCount;
return SentCount;
}
//UART接收数据
int UartPsRev(XUartPs *InstancePtr, u8 *BufferPtr, u32 NumBytes)
{
u32 ReceivedCount = 0;
u32 CsrRegister;
/* Setup the buffer parameters */
InstancePtr->ReceiveBuffer.RequestedBytes = NumBytes;
InstancePtr->ReceiveBuffer.RemainingBytes = NumBytes;
InstancePtr->ReceiveBuffer.NextBytePtr = BufferPtr;
/*
* Read the Channel Status Register todetermine if there is any data in
* the RX FIFO
*/
CsrRegister= XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
XUARTPS_SR_OFFSET);
/*
* Loop until there is no more data in RX FIFOor the specified
* number of bytes has been received
*/
while((ReceivedCount < InstancePtr->ReceiveBuffer.RemainingBytes)&&
(((CsrRegister& XUARTPS_SR_RXEMPTY) == (u32)0)))
{
InstancePtr->ReceiveBuffer.NextBytePtr[ReceivedCount] =
XUartPs_ReadReg(InstancePtr->Config.BaseAddress,XUARTPS_FIFO_OFFSET);
ReceivedCount++;
CsrRegister= XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
XUARTPS_SR_OFFSET);
}
InstancePtr->is_rxbs_error = 0;
/*
* Update the receive buffer to reflect thenumber of bytes just
* received
*/
if(InstancePtr->ReceiveBuffer.NextBytePtr != NULL){
InstancePtr->ReceiveBuffer.NextBytePtr += ReceivedCount;
}
InstancePtr->ReceiveBuffer.RemainingBytes -= ReceivedCount;
return ReceivedCount;
}