joe 发表于 2022-4-8 10:28:00

microblaze访问LMB和AXI BRAM Controller

BD图如下:


#include "xparameters.h"
#include "xbram.h"
#include <stdio.h>

/************************** Constant Definitions *****************************/

/*
* The following constants map to the XPAR parameters created in the
* xparameters.h file. They are defined here such that a user can easily
* change all the needed parameters in one place.
*/
#define BRAM_DEVICE_ID                XPAR_BRAM_0_DEVICE_ID
#define LMB_DEVICE_ID                XPAR_LMB_BRAM_IF_CNTLR_0_DEVICE_ID

/************************** Function Prototypes ******************************/

int BramExample(u16 DeviceId);
static void InitializeECC(XBram_Config *ConfigPtr, u32 EffectiveAddr);


/************************** Variable Definitions *****************************/

/*
* The following are declared globally so they are zeroed and so they are
* easily accessible from a debugger
*/
XBram Bram;        /* The Instance of the BRAM Driver */
XBram lmbBram;

/****************************************************************************/
/**
*
* This function is the main function of the BRAM example.
*
* @param        None.
*
* @return
*                - XST_SUCCESS to indicate success.
*                - XST_FAILURE to indicate failure.
*
* @note                None.
*
*****************************************************************************/
#ifndef TESTAPP_GEN
int main(void)
{
        int Status;

        Status = BramExample(BRAM_DEVICE_ID);
        if (Status != XST_SUCCESS ) {
                xil_printf("Bram Example Failed\r\n");
                return XST_FAILURE;
        }

        xil_printf("Successfully ran Bram Example\r\n");
        return XST_SUCCESS;
}
#endif

/*****************************************************************************/
/**
*
* This is the entry point for the BRAM example.
*
* @param        DeviceId is the XPAR_<BRAM_instance>_DEVICE_ID value from
*                xparameters.h
*
* @return
*                - XST_SUCCESS to indicate success.
*                - XST_FAILURE to indicate failure.
*
* @note                None.
*
******************************************************************************/
int BramExample(u16 DeviceId)
{
        int Status;
        XBram_Config *ConfigPtr;

        /*
       * Initialize the BRAM driver. If an error occurs then exit
       */

        /*
       * Lookup configuration data in the device configuration table.
       * Use this configuration info down below when initializing this
       * driver.
       */
        ConfigPtr = XBram_LookupConfig(DeviceId);
        if (ConfigPtr == (XBram_Config *) NULL) {
                return XST_FAILURE;
        }

        Status = XBram_CfgInitialize(&Bram, ConfigPtr,
                                     ConfigPtr->CtrlBaseAddress);
        if (Status != XST_SUCCESS) {
                return XST_FAILURE;
        }


      InitializeECC(ConfigPtr, ConfigPtr->CtrlBaseAddress);


        /*
       * Execute the BRAM driver selftest.
       */
        Status = XBram_SelfTest(&Bram, 0);
        if (Status != XST_SUCCESS) {
                return XST_FAILURE;
        }
        /********joe code start****************/
        XBram_Out8(ConfigPtr->MemBaseAddress,(u8)0x36);
        volatile u8 data = XBram_In8(ConfigPtr->MemBaseAddress);
        XBram_Out8(ConfigPtr->MemBaseAddress,(u8)0x37);
        volatile u8 data2 = XBram_In8(ConfigPtr->MemBaseAddress);
//        if(data != data2)
//        {
//                return XST_SUCCESS;
//        }
//        else
//        {
//                return XST_FAILURE;
//        }
        XBram_Config *lmbConfigPtr;
        lmbConfigPtr = XBram_LookupConfig(LMB_DEVICE_ID);
                if (lmbConfigPtr == (XBram_Config *) NULL) {
                        return XST_FAILURE;
                }

                Status = XBram_CfgInitialize(&lmbBram, lmbConfigPtr,
                                lmbConfigPtr->CtrlBaseAddress);
                if (Status != XST_SUCCESS) {
                        return XST_FAILURE;
                }
                InitializeECC(lmbConfigPtr, lmbConfigPtr->CtrlBaseAddress);
//                XBram_Out8(lmbConfigPtr->MemBaseAddress,(u8)0x36);
                data2 = XBram_In8(lmbConfigPtr->MemBaseAddress);
                XBram_Out8(lmbConfigPtr->MemBaseAddress,(u8)0x37);
                data = XBram_In8(lmbConfigPtr->MemBaseAddress);
                if(data != data2)
                {
                        return XST_SUCCESS;
                }
                else
                {
                        return XST_FAILURE;
                }
        /********joe code end****************/
        return XST_SUCCESS;
}


/****************************************************************************/
/**
*
* This function ensures that ECC in the BRAM is initialized if no hardware
* initialization is available. The ECC bits are initialized by reading and
* writing data in the memory. This code is not optimized to only read data
* in initialized sections of the BRAM.
*
* @param        ConfigPtr is a reference to a structure containing information
*                about a specific BRAM device.
* @param         EffectiveAddr is the device base address in the virtual memory
*                address space.
*
* @return
*                None
*
* @note                None.
*
*****************************************************************************/
void InitializeECC(XBram_Config *ConfigPtr, u32 EffectiveAddr)
{
        u32 Addr;
        volatile u32 Data;

        if (ConfigPtr->EccPresent &&
          ConfigPtr->EccOnOffRegister &&
          ConfigPtr->EccOnOffResetValue == 0 &&
          ConfigPtr->WriteAccess != 0) {
                for (Addr = ConfigPtr->MemBaseAddress;
                     Addr < ConfigPtr->MemHighAddress; Addr+=4) {
                        Data = XBram_In32(Addr);
                        XBram_Out32(Addr, Data);
                }
                XBram_WriteReg(EffectiveAddr, XBRAM_ECC_ON_OFF_OFFSET, 1);
        }
}LMB和axi bram controller都可以访问到各自对应的BRAM中的数据,实验成功。
页: [1]
查看完整版本: microblaze访问LMB和AXI BRAM Controller