前言
工具: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中,有相关的几个例程可以进行参考。
直接选择这个工程进行导入,查看例程。
随后,对下述代码进行简单的分析。
将该代码贴上,并且把不必要的注释进行删除。
#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..中,设置复位。
终端显示