ZYNQ使用PL部分IIC收发数测试

一个人想着一个人 提交于 2020-08-05 11:23:19

1、背景介绍
ZYNQ在PS部分有两路I2C,但有时候存在不够用的情况,这时就需要使用PL部分的I2C IP核(以下简称AXI I2C)。关于该IP核的信息可以参考XILINX官方的datasheet,不过在使用AXI I2C时需要进行修改才能够正确进行收发数。

2、硬件配置
如下图所示,需要在PL部分添加AXI I2C,注意这里直接把中断信号连入ZYNQ PS部分的IRQ

在ZYNQ PS中启用PL-PS中断

由于AXI I2C中断只有一种类型,所以这里启用的中断ID为61,即61号中断,后面会用到。

3、master端代码
在xilinx sdk中提供的axi i2c master示例代码使用了PL部分的INTC IP核,这与硬件设计不符,所以需要对示例代码进行改动才能正常工作。

首先是修改中断部分,修改为使用PS部分的GIC中断,如下图

IIC_INT_VEC_ID这里指定的是FPGA0_INT_ID,其实就是61,如下图,这样就把中断引脚配置了。

然后就需要对中断进行初始化,具体参考master端发送代码。这里是基于xiic_repeated_start_example这个例子改动的。

/******************************************************************************
*
* Copyright (C) 2006 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*****************************************************************************/
/**
* @file xiic_repeated_start_example.c
*
* This file consists of a interrupt mode design example to demonstrate the use
* of repeated start using the XIic driver.
*
* The XIic_MasterSend() API is used to transmit the data and XIic_MasterRecv()
* API is used to receive the data.
*
* The IIC devices that are present on the Xilinx boards donot support the
* repeated start option. These examples have been tested with an IIC
* device external to the boards.
*
* This code assumes that no Operating System is being used.
*
* @note
*
* None.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who  Date     Changes
* ----- ---- -------- -----------------------------------------------
* 1.00a mta  02/20/06 Created.
* 2.00a sdm  09/22/09 Updated to use the HAL APIs, replaced call to
*              XIic_Initialize API with XIic_LookupConfig and
*              XIic_CfgInitialize. Updated the example with a
*                  fix for CR539763 where XIic_Start was being called
*                  instead of XIic_Stop. Added code for setting up the
*                  StatusHandler callback.
*
* </pre>
*
******************************************************************************/
 
/***************************** Include Files *********************************/
 
#include "xparameters.h"
#include "xiic.h"
#include "xintc.h"
#include "xil_exception.h"
#include "xscugic.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 IIC_DEVICE_ID        XPAR_IIC_0_DEVICE_ID
//#define INTC_DEVICE_ID        XPAR_INTC_0_DEVICE_ID
//#define IIC_INTR_ID        XPAR_INTC_0_IIC_0_VEC_ID
 
#define INTC_DEVICE_ID        XPAR_SCUGIC_SINGLE_DEVICE_ID
#define IIC_INT_VEC_ID        XPS_FPGA0_INT_ID
 
/*
 * The following constant defines the address of the IIC
 * device on the IIC bus. Note that since the address is only 7 bits, this
 * constant is the address divided by 2.
 */
#define SLAVE_ADDRESS    0x50    /* 0xE0 as an 8 bit number. */
 
#define SEND_COUNT    25
#define RECEIVE_COUNT   25
 
/**************************** Type Definitions *******************************/
 
/***************** Macros (Inline Functions) Definitions *********************/
 
/************************** Function Prototypes ******************************/
 
int IicRepeatedStartExample();
static int WriteData(u16 ByteCount);
static int ReadData(u8 *BufferPtr, u16 ByteCount);
static int SetupInterruptSystem(XIic *IicInstPtr);
static void SendHandler(XIic *InstancePtr);
static void ReceiveHandler(XIic *InstancePtr);
static void StatusHandler(XIic *InstancePtr, int Event);
 
/************************** Variable Definitions *****************************/
 
XIic IicInstance;
XScuGic InterruptController;
 
u8 WriteBuffer[SEND_COUNT];    /* Write buffer for writing a page. */
u8 ReadBuffer[RECEIVE_COUNT];    /* Read buffer for reading a page. */
 
volatile u8 TransmitComplete;
volatile u8 ReceiveComplete;
 
/************************** Function Definitions *****************************/
 
/*****************************************************************************/
/**
* Main function to call the Repeated Start example.
*
* @param    None.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
int main(void)
{
    int Status;
 
    /*
     * Run the Repeated Start example.
     */
    Status = IicRepeatedStartExample();
    if (Status != XST_SUCCESS) {
 
        return XST_FAILURE;
    }
 
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
* This function writes and reads the data to the IIC Slave.
*
* @param    None.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
int IicRepeatedStartExample(void)
{
    u8 Index;
    int Status;
    XIic_Config *ConfigPtr;    /* Pointer to configuration data */
 
    /*
     * Initialize the data to write and the read buffer.
     */
    for (Index = 0; Index < SEND_COUNT; Index++) {
        WriteBuffer[Index] = Index;
        ReadBuffer[Index] = 0;
    }
 
    /*
     * Initialize the IIC driver so that it is ready to use.
     */
    ConfigPtr = XIic_LookupConfig(XPAR_IIC_0_DEVICE_ID);
    if (ConfigPtr == NULL) {
        return XST_FAILURE;
    }
 
    Status = XIic_CfgInitialize(&IicInstance, ConfigPtr,
                    ConfigPtr->BaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Setup the Interrupt System.
     */
    Status = SetupInterruptSystem(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Set the Transmit, Receive and Status handlers.
     */
    XIic_SetSendHandler(&IicInstance, &IicInstance,
                (XIic_Handler) SendHandler);
    XIic_SetRecvHandler(&IicInstance, &IicInstance,
                (XIic_Handler) ReceiveHandler);
    XIic_SetStatusHandler(&IicInstance, &IicInstance,
                  (XIic_StatusHandler) StatusHandler);
 
    /*
     * Set the Address of the Slave.
     */
    Status = XIic_SetAddress(&IicInstance, XII_ADDR_TO_SEND_TYPE,
                 SLAVE_ADDRESS);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Write to the IIC Slave.
     */
    while(1)
    {
        Status = WriteData(SEND_COUNT);
        delay_SL(0x11111111);
    }
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Read from the IIC Slave.
     */
    Status = ReadData(ReadBuffer, RECEIVE_COUNT);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    return XST_SUCCESS;
}
 
void delay_SL(u32 delayCount) {
    do {
        __asm__("nop");
        delayCount--;
    } while (delayCount > 0);
}
 
/*****************************************************************************/
/**
* This function writes a buffer of data to IIC Slave.
*
* @param    ByteCount contains the number of bytes in the buffer to be
*        written.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
static int WriteData(u16 ByteCount)
{
    int Status;
    int BusBusy;
 
    /*
     * Set the defaults.
     */
    TransmitComplete = 1;
 
    /*
     * Start the IIC device.
     */
    Status = XIic_Start(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Set the Repeated Start option.
     */
    IicInstance.Options = XII_REPEATED_START_OPTION;
 
    /*
     * Send the data.
     */
    Status = XIic_MasterSend(&IicInstance, WriteBuffer, ByteCount);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    xil_printf("i2c master send begin \r\n");
 
//    delay_SL(0x11111111);
    /*
     * Wait till data is transmitted.
     */
    while (TransmitComplete) {
 
    }
 
    xil_printf("i2c master TransmitComplete end \r\n");
 
    /*
     * This is for verification that Bus is not released and still Busy.
     */
    BusBusy = XIic_IsIicBusy(&IicInstance);
 
    TransmitComplete = 1;
    IicInstance.Options = 0x0;
 
    /*
     * Send the Data.
     */
    Status = XIic_MasterSend(&IicInstance, WriteBuffer, ByteCount);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    xil_printf("i2c master second send begin \r\n");
 
//    delay_SL(0x11111111);
    /*
     * Wait till data is transmitted.
     */
    while ((TransmitComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
 
    }
 
    xil_printf("i2c master second TransmitComplete end \r\n");
    /*
     * Stop the IIC device.
     */
    Status = XIic_Stop(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
* This function reads a data from the IIC Slave into a specified buffer.
*
* @param    BufferPtr contains the address of the data buffer to be filled.
* @param    ByteCount contains the number of bytes to be read.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
static int ReadData(u8 *BufferPtr, u16 ByteCount)
{
    int Status;
    int BusBusy;
 
    /*
     * Set the defaults.
     */
    ReceiveComplete = 1;
 
    /*
     * Start the IIC device.
     */
    Status = XIic_Start(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Set the Repeated Start option.
     */
    IicInstance.Options = XII_REPEATED_START_OPTION;
 
    /*
     * Receive the data.
     */
    Status = XIic_MasterRecv(&IicInstance, BufferPtr, ByteCount);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Wait till all the data is received.
     */
    while (ReceiveComplete) {
 
    }
 
    /*
     * This is for verification that Bus is not released and still Busy.
     */
    BusBusy = XIic_IsIicBusy(&IicInstance);
 
    ReceiveComplete = 1;
    IicInstance.Options = 0x0;
 
    /*
     * Receive the Data.
     */
    Status = XIic_MasterRecv(&IicInstance, BufferPtr, ByteCount);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Wait till all the data is received.
     */
    while ((ReceiveComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
 
    }
 
    /*
     * Stop the IIC device.
     */
    Status = XIic_Stop(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
* This function setups the interrupt system so interrupts can occur for the
* IIC. The function is application-specific since the actual system may or
* may not have an interrupt controller. The IIC device could be directly
* connected to a processor without an interrupt controller. The user should
* modify this function to fit the application.
*
* @param    IicInstPtr contains a pointer to the instance of the IIC  which
*        is going to be connected to the interrupt controller.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
static int SetupInterruptSystem(XIic *IicInstPtr)
{
 
#if 0
    int Status;
 
    if (InterruptController.IsStarted == XIL_COMPONENT_IS_STARTED) {
        return XST_SUCCESS;
    }
 
    /*
     * Initialize the interrupt controller driver so that it's ready to use.
     */
    Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Connect the device driver handler that will be called when an
     * interrupt for the device occurs, the handler defined above performs
     *  the specific interrupt processing for the device.
     */
    Status = XIntc_Connect(&InterruptController, IIC_INTR_ID,
                   (XInterruptHandler) XIic_InterruptHandler,
                   IicInstPtr);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Start the interrupt controller so interrupts are enabled for all
     * devices that cause interrupts.
     */
    Status = XIntc_Start(&InterruptController, XIN_REAL_MODE);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Enable the interrupts for the IIC device.
     */
    XIntc_Enable(&InterruptController, IIC_INTR_ID);
 
    /*
     * Initialize the exception table.
     */
    Xil_ExceptionInit();
 
    /*
     * Register the interrupt controller handler with the exception table.
     */
    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
                 (Xil_ExceptionHandler) XIntc_InterruptHandler,
                 &InterruptController);
 
    /*
     * Enable non-critical exceptions.
     */
    Xil_ExceptionEnable();
 
    return XST_SUCCESS;
#endif
 
    int Status;
    XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
 
    Xil_ExceptionInit();
 
    /*
     * Initialize the interrupt controller driver so that it is ready to
     * use.
     */
    IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
    if (NULL == IntcConfig) {
        return XST_FAILURE;
    }
 
    Status = XScuGic_CfgInitialize(&InterruptController, IntcConfig,
                    IntcConfig->CpuBaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
 
    /*
     * Connect the interrupt controller interrupt handler to the hardware
     * interrupt handling logic in the processor.
     */
    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
                (Xil_ExceptionHandler)XScuGic_InterruptHandler,
                &InterruptController);
 
    /*
     * Connect the device driver handler that will be called when an
     * interrupt for the device occurs, the handler defined above performs
     * the specific interrupt processing for the device.
     */
    Status = XScuGic_Connect(&InterruptController, IIC_INT_VEC_ID,
            (Xil_InterruptHandler)XIic_InterruptHandler,
            (void *)IicInstPtr);
    if (Status != XST_SUCCESS) {
        return Status;
    }
 
    /*
     * Enable the interrupt for the Iic device.
     */
    XScuGic_Enable(&InterruptController, IIC_INT_VEC_ID);
 
 
    /*
     * Enable interrupts in the Processor.
     */
    Xil_ExceptionEnable();
 
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
* This Send handler is called asynchronously from an interrupt context and
* indicates that data in the specified buffer has been sent.
*
* @param    InstancePtr is a pointer to the IIC driver instance for which
*         the handler is being called for.
*
* @return    None.
*
* @note        None.
*
******************************************************************************/
static void SendHandler(XIic *InstancePtr)
{
    TransmitComplete = 0;
}
 
/*****************************************************************************/
/**
* This Receive handler is called asynchronously from an interrupt context and
* indicates that data in the specified buffer has been Received.
*
* @param    InstancePtr is a pointer to the IIC driver instance for which
*         the handler is being called for.
*
* @return    None.
*
* @note        None.
*
******************************************************************************/
static void ReceiveHandler(XIic *InstancePtr)
{
    ReceiveComplete = 0;
}
 
/*****************************************************************************/
/**
* This Status handler is called asynchronously from an interrupt
* context and indicates the events that have occurred.
*
* @param    InstancePtr is a pointer to the IIC driver instance for which
*        the handler is being called for.
* @param    Event indicates the condition that has occurred.
*
* @return    None.
*
* @note        None.
*
******************************************************************************/
static void StatusHandler(XIic *InstancePtr, int Event)
{
    TransmitComplete = 0;
}
注意master在发送时产生中断后进入的回调函数只有StatusHandler,send和receive handler其实都无法响应。

4、slave端代码
slave端和master类似,也需要修改中断处理那部分代码,其中中断处理回调函数如下:

slave端的源码如下,这里基于xiic_slave_example修改

/******************************************************************************
*
* Copyright (C) 2006 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*****************************************************************************/
/**
* @file xiic_slave_example.c
*
* This file consists of a Interrupt mode design example which uses the Xilinx
* IIC device and XIic driver to exercise the slave functionality of the IIC
* device.
*
* The XIic_SlaveSend() API is used to transmit the data and
* XIic_SlaveRecv() API is used to receive the data.
*
* The example is tested on ML300/ML310/ML403/ML501 Xilinx boards.
*
* The IIC devices that are present on the Xilinx boards donot support the Master
* functionality. This example has been tested with an off board external IIC
* Master device and the IIC device configured as a Slave.
*
* This code assumes that no Operating System is being used.
*
* @note
*
* None.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who  Date     Changes
* ----- ---- -------- -----------------------------------------------
* 1.00a mta  03/01/06 Created.
* 2.00a ktn  11/17/09 Updated to use the HAL APIs and replaced call to
*              XIic_Initialize API with XIic_LookupConfig and
*              XIic_CfgInitialize. Some of the macros have been
*              renamed in the IIC driver and some renamed macros are
*              used in this example.
* </pre>
*
******************************************************************************/
 
/***************************** Include Files *********************************/
 
#include "xparameters.h"
#include "xiic.h"
#include "xintc.h"
#include "xil_exception.h"
#include "xscugic.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 IIC_DEVICE_ID        XPAR_IIC_0_DEVICE_ID
//#define INTC_DEVICE_ID        XPAR_INTC_0_DEVICE_ID
//#define IIC_INTR_ID        XPAR_INTC_0_IIC_0_VEC_ID
 
#define INTC_DEVICE_ID        XPAR_SCUGIC_SINGLE_DEVICE_ID
#define IIC_INT_VEC_ID        XPS_FPGA0_INT_ID
 
/*
 * The following constant defines the address of the IIC device on the IIC bus.
 * Since the address is only 7 bits, this constant is the address divided by 2.
 */
#define SLAVE_ADDRESS        0x50    /* 0xE0 as an 8 bit number. */
 
#define RECEIVE_COUNT        30
#define SEND_COUNT        30
 
 
/**************************** Type Definitions *******************************/
 
/***************** Macros (Inline Functions) Definitions *********************/
 
/************************** Function Prototypes ******************************/
 
int IicSlaveExample();
int SlaveWriteData(u16 ByteCount);
int SlaveReadData(u8 *BufferPtr, u16 ByteCount);
static int SetupInterruptSystem(XIic * IicInstPtr);
static void StatusHandler(XIic *InstancePtr, int Event);
static void SendHandler(XIic *InstancePtr);
static void ReceiveHandler(XIic *InstancePtr);
 
/************************** Variable Definitions *****************************/
 
XIic IicInstance;        /* The instance of the IIC device. */
XScuGic InterruptController;    /* The instance of the Interrupt Controller */
 
 
u8 WriteBuffer[SEND_COUNT];    /* Write buffer for writing a page. */
u8 ReadBuffer[RECEIVE_COUNT];    /* Read buffer for reading a page. */
 
volatile u8 TransmitComplete;
volatile u8 ReceiveComplete;
 
volatile u8 SlaveRead;
volatile u8 SlaveWrite;
 
/************************** Function Definitions *****************************/
 
/*****************************************************************************/
/**
* Main function to call the IIC Slave example.
*
* @param    None.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
int main(void)
{
    int Status;
 
    /*
     * Run the IIC Slave example.
     */
    xil_printf("i2c slave example begin\n");
    Status = IicSlaveExample();
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    return XST_SUCCESS;
}
 
void delay_SL(u32 delayCount) {
    do {
        __asm__("nop");
        delayCount--;
    } while (delayCount > 0);
}
/*****************************************************************************/
/**
* This function writes and reads the data as a slave. The IIC master on the bus
* initiates the transfers.
*
* @param    None.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
int IicSlaveExample()
{
    int Status;
    u8 Index;
    XIic_Config *ConfigPtr;    /* Pointer to configuration data */
 
 
    /*
     * Initialize the IIC driver so that it is ready to use.
     */
    ConfigPtr = XIic_LookupConfig(IIC_DEVICE_ID);
    if (ConfigPtr == NULL) {
        return XST_FAILURE;
    }
 
    Status = XIic_CfgInitialize(&IicInstance, ConfigPtr,
                    ConfigPtr->BaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Setup the Interrupt System.
     */
    Status = SetupInterruptSystem(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    xil_printf("i2c slave example begin test1\r\n");
    /*
     * Include the Slave functions.
     */
    XIic_SlaveInclude();
 
    /*
     * Set the Transmit, Receive and Status Handlers.
     */
    XIic_SetStatusHandler(&IicInstance, &IicInstance,
                  (XIic_StatusHandler) StatusHandler);
    XIic_SetSendHandler(&IicInstance, &IicInstance,
                (XIic_Handler) SendHandler);
    XIic_SetRecvHandler(&IicInstance, &IicInstance,
                (XIic_Handler) ReceiveHandler);
 
//    xil_printf("i2c slave example begin test2\r\n");
    /*
     * Set the Address as a RESPOND type.
     */
    Status = XIic_SetAddress(&IicInstance, XII_ADDR_TO_RESPOND_TYPE,
                 SLAVE_ADDRESS);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
//    xil_printf("i2c slave example begin test3\r\n");
    /*
     * The IIC Master on this bus should initiate the transfer
     * and write data to the slave at this instance.
     */
    while(1)
    {
        xil_printf("i2c slave example recv data\r\n");
        SlaveReadData(ReadBuffer, RECEIVE_COUNT);
 
 
        delay_SL(0x1fffff);
 
    }
        for (Index = 0; Index < SEND_COUNT; Index++) {
            WriteBuffer[Index] = Index;
        }
 
        xil_printf("i2c slave example write data to master\r\n");
        /*
         * The IIC Master on this bus should initiate the transfer
         * and read data from the slave.
         */
        SlaveWriteData(SEND_COUNT);
 
 
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
* This function reads a buffer of bytes  when the IIC Master on the bus writes
* data to the slave device.
*
* @param    BufferPtr contains the address of the data buffer to be filled.
* @param    ByteCount contains the number of bytes in the buffer to be read.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None
*
******************************************************************************/
int SlaveReadData(u8 *BufferPtr, u16 ByteCount)
{
    int Status;
    int i=0;
 
    /*
     * Set the defaults.
     */
    ReceiveComplete = 1;
 
    /*
     * Start the IIC device.
     */
    //xil_printf("i2c slave example begin test4\r\n");
    Status = XIic_Start(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Set the Global Interrupt Enable.
     */
    XIic_IntrGlobalEnable(IicInstance.BaseAddress);
 
    //xil_printf("i2c slave example begin test5\r\n");
 
//    while(1)
//    {
//        XIic_SlaveRecv(&IicInstance, ReadBuffer, RECEIVE_COUNT);
 
        //delay_SL(0x1fffff);
 
//    }
    /*
     * Wait for AAS interrupt and completion of data reception.
     */
    while ((ReceiveComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
        if (SlaveRead) {
            XIic_SlaveRecv(&IicInstance, ReadBuffer, RECEIVE_COUNT);
            SlaveRead = 0;
        }
    }
    xil_printf("\r\n slave readbuffer is \r\n");
    for(i=0;i<25;i+=6)
    {
        xil_printf("[%d] 0x%x [%d] 0x%x [%d] 0x%x [%d] 0x%x [%d] 0x%x [%d] 0x%x\r\n",i,ReadBuffer[i],i+1,ReadBuffer[i+1],i+2,ReadBuffer[i+2],i+3,ReadBuffer[i+3],i+4,ReadBuffer[i+4],i+5,ReadBuffer[i+5]);
    }
//    xil_printf("i2c slave example begin test6\r\n");
    /*
     * Disable the Global Interrupt Enable.
     */
    XIic_IntrGlobalDisable(IicInstance.BaseAddress);
 
//    xil_printf("i2c slave example begin test7\r\n");
    /*
     * Stop the IIC device.
     */
    Status = XIic_Stop(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
* This function writes a buffer of bytes to the IIC bus when the IIC master
* initiates a read operation.
*
* @param    ByteCount contains the number of bytes in the buffer to be
*        written.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
******************************************************************************/
int SlaveWriteData(u16 ByteCount)
{
    int Status;
 
    /*
     * Set the defaults.
     */
    TransmitComplete = 1;
 
    /*
     * Start the IIC device.
     */
    Status = XIic_Start(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Set the Global Interrupt Enable.
     */
    XIic_IntrGlobalEnable(IicInstance.BaseAddress);
 
    /*
     * Wait for AAS interrupt and transmission to complete.
     */
    while ((TransmitComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
        if (SlaveWrite) {
            XIic_SlaveSend(&IicInstance, WriteBuffer, SEND_COUNT);
            SlaveWrite = 0;
        }
    }
 
    /*
     * Disable the Global Interrupt Enable bit.
     */
    XIic_IntrGlobalDisable(IicInstance.BaseAddress);
 
    /*
     * Stop the IIC device.
     */
    Status = XIic_Stop(&IicInstance);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    return XST_SUCCESS;
}
 
/****************************************************************************/
/**
* This Status handler is called asynchronously from an interrupt context and
* indicates the events that have occurred.
*
* @param    InstancePtr is not used, but contains a pointer to the IIC
*         device driver instance which the handler is being called for.
* @param    Event indicates whether it is a request for a write or read.
*
* @return    None.
*
* @note        None.
*
****************************************************************************/
static void StatusHandler(XIic *InstancePtr, int Event)
{
    xil_printf("\r\n zz debug slave int occured\r\n");
    /*
     * Check whether the Event is to write or read the data from the slave.
     */
    if (Event == XII_MASTER_WRITE_EVENT) {
        /*
         * Its a Write request from Master.
         */
        SlaveRead = 1;
        ReceiveComplete=0;
        xil_printf("\r\n zz debug  a Write request from Master\r\n");
    } else {
        /*
         * Its a Read request from the master.
         */
        SlaveWrite = 1;
        TransmitComplete=0;
    }
}
 
/****************************************************************************/
/**
* This Send handler is called asynchronously from an interrupt
* context and indicates that data in the specified buffer has been sent.
*
* @param    InstancePtr is a pointer to the IIC driver instance for which
*        the handler is being called for.
*
* @return    None.
*
* @note        None.
*
****************************************************************************/
static void SendHandler(XIic *InstancePtr)
{
    TransmitComplete = 0;
}
 
/****************************************************************************/
/**
* This Receive handler is called asynchronously from an interrupt
* context and indicates that data in the specified buffer has been Received.
*
* @param    InstancePtr is a pointer to the IIC driver instance for which
*         the handler is being called for.
*
* @return    None.
*
* @note        None.
*
****************************************************************************/
static void ReceiveHandler(XIic *InstancePtr)
{
    ReceiveComplete = 0;
}
 
/****************************************************************************/
/**
* This function setups the interrupt system so interrupts can occur for the
* IIC. The function is application-specific since the actual system may or
* may not have an interrupt controller. The IIC device could be directly
* connected to a processor without an interrupt controller. The user should
* modify this function to fit the application.
*
* @param    IicInstPtr contains a pointer to the instance of the IIC  which
*        is going to be connected to the interrupt controller.
*
* @return    XST_SUCCESS if successful else XST_FAILURE.
*
* @note        None.
*
****************************************************************************/
static int SetupInterruptSystem(XIic * IicInstPtr)
{
#if 0
    int Status;
 
    if (InterruptController.IsStarted == XIL_COMPONENT_IS_STARTED) {
        return XST_SUCCESS;
    }
 
    /*
     * Initialize the interrupt controller driver so that it's ready to use.
     */
    Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Connect the device driver handler that will be called when an
     * interrupt for the device occurs, the handler defined above
     * performs the specific interrupt processing for the device.
     */
    Status = XIntc_Connect(&InterruptController, IIC_INTR_ID,
                   (XInterruptHandler) XIic_InterruptHandler,
                   IicInstPtr);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Start the interrupt controller so interrupts are enabled for all
     * devices that cause interrupts.
     */
    Status = XIntc_Start(&InterruptController, XIN_REAL_MODE);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Enable the interrupts for the IIC device.
     */
    XIntc_Enable(&InterruptController, IIC_INTR_ID);
 
    /*
     * Initialize the exception table.
     */
    Xil_ExceptionInit();
 
    /*
     * Register the interrupt controller handler with the exception table.
     */
    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
                 (Xil_ExceptionHandler) XIntc_InterruptHandler,
                 &InterruptController);
 
    /*
     * Enable non-critical exceptions.
     */
    Xil_ExceptionEnable();
 
 
    return XST_SUCCESS;
#endif
 
    int Status;
    XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
 
    Xil_ExceptionInit();
 
    /*
     * Initialize the interrupt controller driver so that it is ready to
     * use.
     */
    IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
    if (NULL == IntcConfig) {
        return XST_FAILURE;
    }
 
    Status = XScuGic_CfgInitialize(&InterruptController, IntcConfig,
                    IntcConfig->CpuBaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
 
    /*
     * Connect the interrupt controller interrupt handler to the hardware
     * interrupt handling logic in the processor.
     */
    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
                (Xil_ExceptionHandler)XScuGic_InterruptHandler,
                &InterruptController);
 
    /*
     * Connect the device driver handler that will be called when an
     * interrupt for the device occurs, the handler defined above performs
     * the specific interrupt processing for the device.
     */
    Status = XScuGic_Connect(&InterruptController, IIC_INT_VEC_ID,
            (Xil_InterruptHandler)XIic_InterruptHandler,
            (void *)IicInstPtr);
    if (Status != XST_SUCCESS) {
        return Status;
    }
 
    /*
     * Enable the interrupt for the Iic device.
     */
    XScuGic_Enable(&InterruptController, IIC_INT_VEC_ID);
 
 
    /*
     * Enable interrupts in the Processor.
     */
    Xil_ExceptionEnable();
 
    return XST_SUCCESS;
}
5、测试
最后看一下效果,两块板子,一块做master,一块做slave,slave能够正确收到递增数了。


————————————————
版权声明:本文为CSDN博主「Felven」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jj12345jj198999/java/article/details/102527417

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!