前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Zynq中UART

Zynq中UART

作者头像
瓜大三哥
发布2019-11-05 12:35:32
2.2K0
发布2019-11-05 12:35:32
举报
文章被收录于专栏:瓜大三哥

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;

}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 瓜大三哥 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档