UART模块的结构图
主程序流程:
UART初始化→设置UART模式→设置数据格式→设置中断→发送UART数据
程序设计如下:
Config =XUartPs_LookupConfig(UART_DEVICE_ID);
if (NULL == Config) {
return XST_FAILURE;
}
Status= XUartPs_CfgInitialize(&Uart_PS, Config, Config-> Address);
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. Address))
{
XUartPs_WriteReg(InstancePtr->Config. Address,
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. Address,
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. Address,XUARTPS_FIFO_OFFSET);
ReceivedCount++;
CsrRegister= XUartPs_ReadReg(InstancePtr->Config. Address,
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;
}
继续阅读与本文标签相同的文章
4种事务的隔离级别,InnoDB如何巧妙实现?
Zynq中PS的MIO中断
-
超赞,InnoDB调试死锁的方法!
2026-05-21栏目: 教程
-
搜狗微信下线了怎么获取公众号文章?最新方式手把手教你
2026-05-21栏目: 教程
-
InnoDB,快照读,在RR和RC下有何差异?
2026-05-21栏目: 教程
-
Postman之通用断言( Assert )
2026-05-21栏目: 教程
-
在Intellij IDEA中使用Debug
2026-05-21栏目: 教程
