BRAM
C代码功能:
先接收串口的数据,之后将接收到的数据写到 BRAM里边,写完之后再读出来。







/* * main.c * * Created on: 2022年3月6日 * Author: lht */ #include "xil_printf.h" #include "stdio.h" #include "xbram_hw.h" #include "pl_bram_sw.h" #include "xparameters.h" #define PL_BRAM_START PL_BRAM_SW_S00_AXI_SLV_REG0_OFFSET #define PL_BRAM_START_ADDR PL_BRAM_SW_S00_AXI_SLV_REG1_OFFSET #define PL_BRAM_LEN PL_BRAM_SW_S00_AXI_SLV_REG2_OFFSET #define PL_BRAM_BASE XPAR_PL_BRAM_SW_0_S00_AXI_BASEADDR #define START_ADDR 0 #define BRAM_DATA_BYTE 4 //BRAM单个数据4个字节 char ch_data[1024]; int ch_data_len; int main() { printf("please1 input data to read and write bram\n"); while(1) { int i=0; int wr_cnt=0; int read_data; printf("please input data to read and write bram\n"); scanf("%s",ch_data); ch_data_len = strlen(ch_data); for(i=START_ADDR*BRAM_DATA_BYTE;i<(START_ADDR+ch_data_len)*BRAM_DATA_BYTE;i+=BRAM_DATA_BYTE) { XBram_WriteReg(XPAR_BRAM_0_BASEADDR,i,ch_data[wr_cnt]); wr_cnt++; } //配置PLbram起始地址 PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_START_ADDR,START_ADDR*BRAM_DATA_BYTE); //配置PL_BRAM长度 PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_LEN,ch_data_len*BRAM_DATA_BYTE); //配置PL_BRAM_RD开始读信号 PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_START,1); PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_START,1); for(i=START_ADDR*BRAM_DATA_BYTE;i<(START_ADDR+ch_data_len)*BRAM_DATA_BYTE;i+=BRAM_DATA_BYTE) { read_data = XBram_ReadReg(XPAR_BRAM_0_BASEADDR,i); printf("BRAM address is %d\t,read data is %c\n",i/BRAM_DATA_BYTE,read_data); } } return 0; }


往BRAM里面写数据,写操作

读数据,读操作
对BRAM的读写操作就靠这两个函数
REGOFFSET :相对于BASEADDRESS的偏移量,即朝BRAM的哪个地址去写数据
DATA :真正写入的数据
自定义IP核.h里边 关注的 XBRAM_WRITEREG() XBRAM_READREG() 函数

strlen() 函数,可计算字符数量


朝BRAM里边写数据

读数据
/* * main.c * * Created on: 2022年3月7日 * Author: lht */ /* * main.c * * Created on: 2022年3月6日 * Author: lht */ #include "xil_printf.h" #include "stdio.h" #include "xbram_hw.h" #include "pl_bram_sw.h" #include "xparameters.h" #include "xuartps.h" #include "xscugic.h" #include <stdlib.h> #include"sleep.h" #define INTC_DEVICE_ID XPAR_PS7_SCUGIC_0_DEVICE_ID #define INTC_DEVICE_INT_ID 0x0E #define DIST_BASEADDR XPAR_PS7_SCUGIC_0_DIST_BASEADDR #define uart_id XPAR_PS7_UART_1_DEVICE_ID #define baud_rate XUARTPS_DFT_BAUDRATE #define normal_mode XUARTPS_OPER_MODE_NORMAL #define uart1_intr XPAR_PS7_UART_1_INTR #define BUFFER_SIZE 1024 #define PL_BRAM_START PL_BRAM_SW_S00_AXI_SLV_REG0_OFFSET #define PL_BRAM_START_ADDR PL_BRAM_SW_S00_AXI_SLV_REG1_OFFSET #define PL_BRAM_LEN PL_BRAM_SW_S00_AXI_SLV_REG2_OFFSET #define PL_BRAM_BASE XPAR_PL_BRAM_SW_0_S00_AXI_BASEADDR #define START_ADDR 0 #define BRAM_DATA_BYTE 4 //BRAM单个数据4个字节 XScuGic ScuGic; static XScuGic_Config *GicConfig; XUartPs uart; XUartPs_Config *uart_cfg; char ch_data[1024]; int ch_data_len; u32 RecvCnt; u32 codecnt; int sig; static u8 RecvBuffer[BUFFER_SIZE]; u16 *recvbuf_and=NULL; int code; u8 sum_check; u8 xor_check; u16 data_len; void uart1handler(void *CallBackRef, u32 Event,u32 EventData); void intr_init(); void init_uart(); int main() { intr_init(); init_uart(); printf("please1 input data to read and write bram\n"); while(1) { int i=0; int wr_cnt=0; int read_data; sig=1; printf("please input data to read and write bram\n"); while(sig){} ch_data_len = codecnt; for(i=START_ADDR*BRAM_DATA_BYTE;i<(START_ADDR+ch_data_len)*BRAM_DATA_BYTE;i+=BRAM_DATA_BYTE) { XBram_WriteReg(XPAR_BRAM_0_BASEADDR,i,recvbuf_and[wr_cnt]); wr_cnt++; } //配置PLbram起始地址 PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_START_ADDR,START_ADDR*BRAM_DATA_BYTE); //配置PL_BRAM长度 PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_LEN,ch_data_len*BRAM_DATA_BYTE); //配置PL_BRAM_RD开始读信号 PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_START,1); PL_BRAM_SW_mWriteReg(PL_BRAM_BASE,PL_BRAM_START,0); for(i=START_ADDR*BRAM_DATA_BYTE;i<(START_ADDR+ch_data_len)*BRAM_DATA_BYTE;i+=BRAM_DATA_BYTE) { read_data = XBram_ReadReg(XPAR_BRAM_0_BASEADDR,i); printf("BRAM address is %d\t,read data is %d\n",i/BRAM_DATA_BYTE,read_data); } sleep(1); } return 0; } void intr_init() { GicConfig = XScuGic_LookupConfig(INTC_DEVICE_ID); XScuGic_CfgInitialize(&ScuGic, GicConfig,GicConfig->CpuBaseAddress); Xil_ExceptionInit(); Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, &ScuGic); Xil_ExceptionEnable(); /* XScuGic_Connect(&ScuGic, F2P_INTR0_ID, (Xil_ExceptionHandler)f2pintr0handler, (void *)&ScuGic); XScuGic_SetPriTrigTypeByDistAddr(DIST_BASEADDR,F2P_INTR0_ID,0x20,0x03); //GIC地址,中断号,中断优先级,中断类型上升沿3; XScuGic_Enable(&ScuGic, F2P_INTR0_ID); XScuGic_InterruptMaptoCpu(&ScuGic,0,F2P_INTR0_ID); */ XScuGic_Connect(&ScuGic,uart1_intr,(Xil_ExceptionHandler)XUartPs_InterruptHandler,&uart); XScuGic_Enable(&ScuGic,uart1_intr); } void init_uart(){ u32 intrmask=0; uart_cfg=XUartPs_LookupConfig(uart_id); XUartPs_CfgInitialize(&uart,uart_cfg,uart_cfg->BaseAddress); XUartPs_SetBaudRate(&uart,baud_rate); XUartPs_SetHandler(&uart,(XUartPs_Handler)uart1handler,&uart); intrmask=XUARTPS_IXR_TOUT|XUARTPS_IXR_RXOVR; XUartPs_SetInterruptMask(&uart,intrmask); XUartPs_SetOperMode(&uart,normal_mode); XUartPs_SetFifoThreshold(&uart,32); XUartPs_SetRecvTimeout(&uart,8); //XUartPs_Recv(&uart,recvbuf,64);// } void uart1handler(void *CallBackRef, u32 Event,u32 EventData){ //u32 recvcnt; if(Event==XUARTPS_EVENT_RECV_DATA){ /* 清除中断标志 */ XUartPs_WriteReg(uart_cfg->BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_RXOVR) ; XUartPs_Recv(&uart, RecvBuffer, 500) ; //设置为最大,这样有多少就会接收多少 //RecvCnt += EventData ; //RecvBufferPtr += EventData; xil_printf("1\r\n"); } else if(Event==XUARTPS_EVENT_RECV_TOUT){ int i; int j; int right=0; recvbuf_and = (u16 *) malloc(codecnt * sizeof(u16)); /* 清除中断标志 */ XUartPs_WriteReg(uart_cfg->BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_TOUT) ; XUartPs_Recv(&uart, RecvBuffer, 500) ; //RecvCnt += EventData ; if((RecvBuffer[0]==0x55) && (RecvBuffer[1]==0x55)){ data_len=(((RecvBuffer[2])&0x00ff)<<8|(RecvBuffer[3]&0x00ff)); for(j=2;j<(data_len+4);j++){ sum_check+=RecvBuffer[j]; xor_check^=RecvBuffer[j]; //printf("RecvBuffer[j]=%d\n\r",RecvBuffer[j]); //printf("j=%d\n\r",j); } /* printf("RecvBuffer[1]=%d\n\r",RecvBuffer[1]); printf("RecvBuffer[2]=%d\n\r",RecvBuffer[2]); printf("RecvBuffer[3]=%d\n\r",RecvBuffer[3]); printf("DATA_LEN=%d\n\r",data_len); */ printf("SUM_CHECK=%d\n\r",sum_check); printf("XOR_CHECK=%d\n\r",xor_check); /* printf("RecvBuffer[4+data_len]=%d\n\r",RecvBuffer[4+data_len]); printf("RecvBuffer[5+data_len]=%d\n\r",RecvBuffer[5+data_len]); printf("RecvBuffer[6+data_len]=%d\n\r",RecvBuffer[6+data_len]); printf("RecvBuffer[7+data_len]=%d\n\r",RecvBuffer[7+data_len]); */ if((sum_check==RecvBuffer[4+data_len]) && (xor_check==RecvBuffer[5+data_len])){ right=1; printf("right=%d\n\r",right); right=0; if((RecvBuffer[6+data_len]==0x66) && (RecvBuffer[7+data_len]==0x66) ){ codecnt=data_len/2; for(i=2;i<codecnt+2;i++){ recvbuf_and[i-2]=(((RecvBuffer[2*i])&0x00ff)<<8|(RecvBuffer[2*i+1]&0x00ff)); printf("recvbuf_and[i]=%d\n\r",recvbuf_and[i-2]); printf("i=%d\n\r",i-2); } } else{ printf("codecnt=%d\n\r",codecnt); } } else{ printf("right=%d\n\r",right); } } else { for(i=0;i<BUFFER_SIZE;i++){ RecvBuffer[i]=0; } } xil_printf("2:%d\r\n",data_len); sum_check=0; xor_check=0; data_len=0; sig=0; xil_printf("sig:%d\r\n",sig); //recvcnt=EventData; // if(recvcnt==8 && recvbuf[0]==0x55 && recvbuf[1]==0x55){ // printf("recved frame1\n\r"); //} } }
浙公网安备 33010602011771号