欢迎来到羊村

microblaze通过串口读写FPGA内部axi4总线上的寄存器

上一篇 / 下一篇  2017-12-18 18:34:27 / 2018注册送彩金分类:FPGA_Verilog

对于一个几乎没有用过xilinx器件的,一个毕业后就没怎么写过C的人来说,做到能用就行了(反正也只是辅助调试,反正我以后还可以再优化(估计这辈子都不会再看这个代码)),其它的也不敢要求更多。

vivado的易用性实在是……,想死的心都有了

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xuartlite_i.h"
#include "xuartlite_l.h"
#include "xuartlite.h"
#include "xparameters.h"

#define UART_RCV_BUF_SIZE 20
#define UARTLITE_BASEADDR XPAR_AXI_UARTLITE_0_BASEADDR
u8 rcv_buf[UART_RCV_BUF_SIZE];

void log_print();
u8 AsciiToHex(u8 InAscii);
int HexToASCII(u8 *HexBuf, int NumBytes, u8 *SendBuf);

int main()
{
    init_platform();

    log_print();

    int i ;
    u32 addr;
    u32 data;
    xil_printf("write operation format\n\r");
    xil_printf("w 0001 12345678\n\r");
    xil_printf("read operation format\n\r");
    xil_printf("r 0001\n\r");
    xil_printf("********************************************************************************\n\r");

    while (1) {
    if (!XUartLite_IsReceiveEmpty(UARTLITE_BASEADDR)) {
    for (i = 0; i< UART_RCV_BUF_SIZE; i++) {
    rcv_buf[i] = XUartLite_RecvByte(UARTLITE_BASEADDR);
    if (rcv_buf[i] == 13) {
    break;
    }
    XUartLite_SendByte(UARTLITE_BASEADDR,rcv_buf[i]);
    }
    xil_printf("\n\r");
    }

addr =  (AsciiToHex(rcv_buf[2]) << 12) + (AsciiToHex(rcv_buf[3]) << 8) +
(AsciiToHex(rcv_buf[4]) << 4) + (AsciiToHex(rcv_buf[5]) << 0);
    if ((rcv_buf[0] == 'w') | (rcv_buf[0] == 'W')) {
    data =  (AsciiToHex(rcv_buf[7]) << 28) + (AsciiToHex(rcv_buf[8]) << 24) +
(AsciiToHex(rcv_buf[9]) << 20) + (AsciiToHex(rcv_buf[10]) << 16) +
(AsciiToHex(rcv_buf[11]) << 12) + (AsciiToHex(rcv_buf[12]) << 8) +
(AsciiToHex(rcv_buf[13]) << 4) + (AsciiToHex(rcv_buf[14]) << 0);
//    xil_printf("write data = %x\n\r",data);
//    xil_printf("write address = %x\n\r",addr);
    Xil_Out32(XPAR_AXI4_CPU_REG_IP_0_S00_AXI_BASEADDR + (addr<<2), data);
    xil_printf("%08x\n\r",Xil_In32((XPAR_AXI4_CPU_REG_IP_0_S00_AXI_BASEADDR) + (addr<<2)));
    for (i=0;i<UART_RCV_BUF_SIZE;i++)
    rcv_buf[0] = '0';
    }
    else if ((rcv_buf[0] == 'r') | (rcv_buf[0] == 'R')) {
//    xil_printf("read fpga reg\n\r");
//    xil_printf("addr = %x\n\r",addr);
    xil_printf("%08x\n\r",Xil_In32((XPAR_AXI4_CPU_REG_IP_0_S00_AXI_BASEADDR) + (addr<<2)));
    for (i=0;i<UART_RCV_BUF_SIZE;i++)
    rcv_buf[0] = '0';
    }
    }

    cleanup_platform();
    return 0;
}

void log_print()
{
xil_printf("********************************************************************************\n\r");
xil_printf("********************************************************************************\n\r");
xil_printf("**************************Welcome to Microblaze System**************************\n\r");
xil_printf("********************************************************************************\n\r");
xil_printf("********************************************************************************\n\r");
xil_printf("\n\n\n\r");
}

u8 AsciiToHex(u8 InAscii)
{
u8 HexVal;
if (((InAscii >= 0x41) & (InAscii <= 0x46)) | ((InAscii >= 0x61) & (InAscii <= 0x66)))
{
HexVal = (InAscii & 0x0F) + 0x9;
} else if ((InAscii >= 0x30) | (InAscii <= 0x39)) {
HexVal = InAscii - 0x30;
} else {
return XST_FAILURE;
}
//xil_printf("ASCII To HEX: %0x - %0x\n", InAscii, HexVal);
return HexVal;
}

int HexToASCII(u8 *HexBuf, int NumBytes, u8 *SendBuf)
{
int Index=0, localIndex=0;
u8 Value, intVal;

for (Index = 0; Index < NumBytes; Index++)
{
//xil_printf("HexToASCII: Byte = %0x\n", HexBuf[Index]);
localIndex = 2 * Index;
Value = HexBuf[Index];
//- Handle upper nibble
intVal = Value >> 4;
if (intVal > 9)
SendBuf[localIndex] = intVal + 0x37;
else
SendBuf[localIndex] = intVal + 0x30;
//- Handle lower nibble
intVal = Value & 0x0F;
if (intVal > 9)
SendBuf[localIndex + 1] = intVal + 0x37;
else
SendBuf[localIndex + 1] = intVal + 0x30;
//xil_printf("HexToASCII: upper nibble = %0x, Lower nibble = %0x\n", SendBuf[localIndex], SendBuf[localIndex+1]);
}
return (localIndex + 2);
}


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar
博聚网