【FPGA-MicroBlaze】串口收发以及相关函数讲解

前言

工具:Vivado2018.3及其所对应的SDK版本

目前网上有许多MicroBlaze 的入门教程,比如下面的这个参考文章,用串口打印一个hello world。

【FPGA】Xilinx MicroBlaze软核使用第一节:Hello World!_fpga软核microblaze-CSDN博客

个人感觉这些文章的重合度极高,看多了也没有什么参考价值,且单单就串口打印而言,这个学会了也无法对我们实际工程产生多大的帮助。

个人工程

Vivado部分

在我的工程里,时钟的复位我用的是低电平复位,并且我没有将复位信号进行引出,而是用了一个常数进行代替,该常数固定输出1,不对整个模块进行复位。将模块建立完成之后,Creat HDL Wrapper,然后生成比特流文件,导出SDK即可。

关于XDC文件可以根据自己板卡的引脚进行约定即可,我的XDC文件是这样的

复制代码
set_property IOSTANDARD LVCMOS33 [get_ports clk_in]
set_property PACKAGE_PIN W19 [get_ports clk_in]

set_property IOSTANDARD LVCMOS33 [get_ports uart_rxd]
set_property PACKAGE_PIN N2 [get_ports uart_rxd]
set_property IOSTANDARD LVCMOS33 [get_ports uart_txd]
set_property PACKAGE_PIN N5 [get_ports uart_txd]

SDK部分

像大部分教程一样,建立一个Hello World工程,可以看到在system.mss中,有相关的几个例程可以进行参考。

直接选择这个工程进行导入,查看例程。

随后,对下述代码进行简单的分析。

将该代码贴上,并且把不必要的注释进行删除。

cpp 复制代码
#include "xparameters.h"
#include "xstatus.h"
#include "xuartlite.h"
#include "xil_printf.h"

#define TEST_BUFFER_SIZE 16    /* 定义TEST_BUFFER_SIZE 的值为16 */

int UartLitePolledExample(u16 DeviceId);    /* 定义函数 */

XUartLite UartLite;		/* Instance of the UartLite Device */

u8 SendBuffer[TEST_BUFFER_SIZE];	/* Buffer for Transmitting Data 发送数据的一个数组Buffer */
u8 RecvBuffer[TEST_BUFFER_SIZE];	/* Buffer for Receiving Data 接收数据的一个数组Buffer*/

/* 主函数的作用:将UartLitePolledExample(UARTLITE_DEVICE_ID) 的值返回给Status
 * 如果Status != XST_SUCCESS,则串口打印"Uartlite polled Example Failed\r\n"
 * 如果Status == XST_SUCCESS,则串口打印"Successfully ran Uartlite polled Example\r\n"
 */
int main(void)
{
	int Status;

	/*
	 * Run the UartLite polled example, specify the Device ID that is
	 * generated in xparameters.h
	 */
	Status = UartLitePolledExample(UARTLITE_DEVICE_ID);
	if (Status != XST_SUCCESS) {
		xil_printf("Uartlite polled Example Failed\r\n");
		return XST_FAILURE;
	}

	xil_printf("Successfully ran Uartlite polled Example\r\n");
	return XST_SUCCESS;

}


/* UartLitePolledExample函数的作用 */

int UartLitePolledExample(u16 DeviceId)
{
	int Status;
	unsigned int SentCount;    /* 定义SentCount为一个无符号数 */
	unsigned int ReceivedCount = 0;    /* 定义ReceivedCount为一个无符号数,且初值为0 */
	int Index;

	/*初始化串口,若失败则返回XST_FAILURE,若成功则函数继续向下进行判断
	 * Initialize the UartLite driver so that it is ready to use.
	 */
	Status = XUartLite_Initialize(&UartLite, DeviceId);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*确定硬件平台已经成功建立
	 * Perform a self-test to ensure that the hardware was built correctly.
	 */
	Status = XUartLite_SelfTest(&UartLite);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*    for循环,SendBuffer[0] = 0,SendBuffer[1] = 1,...,SendBuffer[15] = 15;
     *        SendBuffer[0] = 0,SendBuffer[1] = 0,...,SendBuffer[15] = 0;
	 * Initialize the send buffer bytes with a pattern to send and the
	 * the receive buffer bytes to zero.
	 */
	for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
		SendBuffer[Index] = Index;
		RecvBuffer[Index] = 0;
	}

	/*    用XUartLite_Send函数,将SendBuffer中的16个数据发送出去
	 * Send the buffer through the UartLite waiting til the data can be sent
	 * (block), if the specified number of bytes was not sent successfully,
	 * then an error occurred.
	 */
	SentCount = XUartLite_Send(&UartLite, SendBuffer, TEST_BUFFER_SIZE);
	if (SentCount != TEST_BUFFER_SIZE) {
		return XST_FAILURE;
	}

	/*    while(1)进行判断,判断接收到的数据个数是否等于XUartLite_Send函数所发送的数据个数
	 * Receive the number of bytes which is transfered.
	 * Data may be received in fifo with some delay hence we continuously
	 * check the receive fifo for valid data and update the receive buffer
	 * accordingly.
	 */
	while (1) {
		ReceivedCount += XUartLite_Recv(&UartLite,
					   RecvBuffer + ReceivedCount,
					   TEST_BUFFER_SIZE - ReceivedCount);
		if (ReceivedCount == TEST_BUFFER_SIZE) {
			break;
		}
	}

	/*    for循环,判断接收到的数据是否等于发送的数据若失败则返回XST_FAILURE
	 * Check the receive buffer data against the send buffer and verify the
	 * data was correctly received.
	 */
	for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
		if (SendBuffer[Index] != RecvBuffer[Index]) {
			return XST_FAILURE;
		}
	}

	return XST_SUCCESS;
}

在上述SDK的软件程序正,用到了许多函数,如XUartLite_Initialize、XUartLite_SelfTest、 XUartLite_Send、XUartLite_Recv,这些函数都可以在SDK中按住ctrl键,查看其功能。

比如进入XUartLite.h头文件中,就可以看到串口收发相关的函数调用了,还可以按住ctrl进一步查看其.c文件中的定义。

运行程序上板验证

右键工程,Run As--Run Configurations..中,设置复位。

终端显示

相关推荐
Saniffer_SH15 小时前
NAND技术(二):从 Channel、Die/LUN、P/E Cycle 到 LDPC,一次讲透 NAND 里那些最容易误解的概念
人工智能·驱动开发·嵌入式硬件·测试工具·fpga开发·计算机外设·压力测试
千寻xun15 小时前
二、实战篇-NVME SSD控制之ZYNQ实现(四)
fpga开发·nvme·nvme ssd
ALINX技术博客16 小时前
【黑金云课堂】FPGA技术教程Linux开发:系统进阶-PS DMA
linux·fpga开发
喵喵苗19 小时前
FPGA Verilog 入门避坑:寄存器与锁存器的本质区别 & 为什么时序逻辑缺 else 不会生成锁存器
fpga开发
Thinker36120 小时前
笔记本外接创新5.1声卡实战:M.2转PCIe软排线方案 vs 雷电扩展卡方案对比
fpga开发·声卡·笔记本外接pcie·显卡坞
传感器与混合集成电路1 天前
136通道采集模块SPI接口设计:协议结构、时序要求与多模块级联扩展方案
fpga开发
I'm a winner2 天前
基于Xilinx FPGA的LVDS高速串行通信系统(四)--数据测试【文末源码】
fpga开发
ALINX技术博客2 天前
AMD MoP 封装策略解读 | HBM 大热,为何 AMD Versal 系列反选 LPDDR5X?
fpga开发·fpga·amd·versal
zlinear数据采集卡2 天前
从万用表的6步调零到硅片级微秒自校准:硬核拆解LHAMP188的宽压轨到轨与零漂移实战
arm开发·stm32·单片机·嵌入式硬件·fpga开发
Rambo.xia2 天前
AXI-Stream反压与背靠背传输——TREADY反压丢帧、TDEST路由错误、反压死锁,流式数据一反压就出事
fpga开发