UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
42 Views
Registered: ‎12-05-2018

Microblaze GPIO Interrupt with accelemeter sensor (Neso Artix-7)

Hi,

I am using GPIO interrupt with interrupt source from INT in LIS331HH accelemeter sensor. I use the xgpio_intr_tapp_example in library of sdk for gpio. But the problem is this code not work. The program initialize the sensor is work but the GPIO_Intr1() function don't work.

This is the first post for me , I'm newbie and I hope finding helpful replys

thanks!

this is my code:


#include "xparameters.h"    /* XPAR parameters */
#include "xspi.h"        /* SPI device driver */
#include "xspi_l.h"
#include "xil_printf.h"
#include "xil_exception.h"
#include "math.h"
#include <stdio.h>
#include "xgpio.h"

//#ifdef XPAR_INTC_0_DEVICE_ID
 #include "xintc.h"
 #include <stdio.h>
//#else
// #include "xscugic.h"
// #include "xil_printf.h"
//#endif

//#include "LIS331HH.h"
//#include "xintc.h"        /* Interrupt controller device driver */

/************************** Constant Definitions *****************************/
 */
#define SPI_DEVICE_ID        XPAR_SPI_0_DEVICE_ID
#define GPIO_DEVICE_ID        XPAR_GPIO_2_DEVICE_ID
#define INTC_DEVICE_ID        XPAR_INTC_0_DEVICE_ID

//#ifdef XPAR_INTC_0_DEVICE_ID
 #define INTC_DEVICE_ID    XPAR_INTC_0_DEVICE_ID
 #define INTC            XIntc
 #define INTC_HANDLER    XIntc_InterruptHandler
//#else
// #define INTC_DEVICE_ID    XPAR_SCUGIC_SINGLE_DEVICE_ID
// #define INTC            XScuGic
// #define INTC_HANDLER    XScuGic_InterruptHandler
//#endif
/*
 *  This is the size of the buffer to be transmitted/received in this example.
 */
#define BUFFER_SIZE            22
#define DATA_BUFFER_SIZE    12
#define DATA_LEGTH            16
/*
 *  Proxy register address
 */
#define TINT 0x1
#define GFK  0x1
#define GFID 0x1

#define COMMAND_BYTE_1_OFFSET 0
#define COMMAND_BYTE_2_OFFSET 1
#define COMMAND_BYTE_3_OFFSET 2
#define COMMAND_BYTE_4_OFFSET 3
#define COMMAND_BYTE_5_OFFSET 4
#define COMMAND_BYTE_6_OFFSET 5
#define COMMAND_BYTE_7_OFFSET 6
#define COMMAND_BYTE_8_OFFSET 7
#define COMMAND_BYTE_9_OFFSET 8
#define COMMAND_BYTE_10_OFFSET 9


#define SCALE 0.0007324; // approximate scale factor for full range (+/-24g)
// scale factor: +/-24g = 48G range. 2^16 bits. 48/65536 = 0.0007324

// global acceleration values
double xAcc, yAcc, zAcc;
double roll, pitch, roll_raw, pitch_raw;
int xVal, yVal, zVal, xL, xH, yL, yH, zL, zH;
double Angle[3][2] =
{
    { 0, 0},  //row 1
    { 0, 0}, //row 2
    { 0, 0}  //row 3
};
double Angle_filter[3][2] =
{
    { 0, 0},  //row 1
    { 0, 0},  //row 2
    { 0, 0}   //row 3
};
int m = 1;
int d = 0;

//for (i = 0; i < 3; i++)
//    for(j = 0; j < 2; j++)
//    {
//        Angle[i][j]        = 0;
//        Angle_filter[i][j] = 0;
//    }

//#define SPI_DEVICE_ID        XPAR_SPI_0_DEVICE_ID
//#define INTC_DEVICE_ID        XPAR_INTC_0_DEVICE_ID
//#define SPI_INTR_ID        XPAR_INTC_0_SPI_0_VEC_ID


/**************************** Type Definitions *******************************/
/*
 * The following data type is used to send and receive data on the SPI
 * interface.
 */
typedef u8 DataBuffer[BUFFER_SIZE];
/************************** Function Prototypes ******************************/

int SpiMasterExample(    XSpi *SpiInstancePtr,
                        u16 SpiDeviceId);
//static int SetupInterruptSystem(XSpi *SpiPtr);
void SpiHandler(    void *CallBackRef,
                    u32 StatusEvent,
                    unsigned int ByteCount);
void Measure_angle();
int GPIO_Intr1(    INTC *IntcInstancePtr,
                XGpio *InstancePtr,
                u16 DeviceId,
                u16 IntrId,
                u16 IntrMask);
int GpioSetupIntrSystem(INTC *IntcInstancePtr,
                        XGpio *InstancePtr,
                        u16 DeviceId,
                        u16 IntrId,
                        u16 IntrMask);
void GpioDisableIntr(INTC *IntcInstancePtr,
                    XGpio *InstancePtr,
                    u16 IntrId,
                    u16 IntrMask);
void GpioHandler();
/************************** Variable Definitions *****************************/
/*
 * The instances to support the device drivers are global such that the
 * are initialized to zero each time the program runs.
 */
static XSpi  SpiInstance;     /* The instance of the SPI device */

XGpio Gpio; /* The Instance of the GPIO Driver */

INTC Intc; /* The Instance of the Interrupt Controller Driver */

static u16 GlobalIntrMask;

static volatile u32 IntrFlag; /* Interrupt Handler Flag */
/*
 * The following variables are used to read and write to the  Spi device, they
 * are global to avoid having large buffers on the stack.
 */
//u8 ReadBuffer[BUFFER_SIZE];
u8 WriteBuffer[BUFFER_SIZE];
u8 ReadBuffer[DATA_BUFFER_SIZE];
/*****************************************************************************/

******************************************************************************/
int main(void){
    int Status;
//    double roll, pitch;
    /*
     * Run the Spi Polled example.
     */
    Status = SpiMasterExample(&SpiInstance, SPI_DEVICE_ID);
    if (Status != XST_SUCCESS) {
        xil_printf("Spi polled Example Failed\r\n");
        return XST_FAILURE;
    }
    // GPIO Interrupt on INT1_port in LIS331HH:
    Status = GPIO_Intr1(&Intc,
                        &Gpio,
                             GPIO_DEVICE_ID,
                       XPAR_INTC_0_GPIO_2_VEC_ID,
                       XPAR_AXI_GPIO_2_IP2INTC_IRPT_MASK);

    while(1)
    {}
    return XST_SUCCESS;
}

/*****************************************************************************/
/**
*
* This function does a minimal test on the Spi device and driver as a
* design example. The purpose of this function is to illustrate how to use
* the XSpi component using the polled mode.
*
* This function sends data and expects to receive the same data.
*
*
* @param    SpiInstancePtr is a pointer to the instance of Spi component.
* @param    SpiDeviceId is the Device ID of the Spi Device and is the
*        XPAR_<SPI_instance>_DEVICE_ID value from xparameters.h.
*
* @return    XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note
*
* This function contains an infinite loop such that if the Spi device is not
* working it may never return.
*
******************************************************************************/
int SpiMasterExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
{
    int Status;
    //u32 Count;
//    u32 Test1, Test2, Test3, Test4, Test5, Test6;
    XSpi_Config *ConfigPtr;    /* Pointer to Configuration data */
    /*
     * Initialize the SPI driver so that it is  ready to use.
     */
    ConfigPtr = XSpi_LookupConfig(SpiDeviceId);
    if (ConfigPtr == NULL) {
        return XST_DEVICE_NOT_FOUND;
    }

    Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
                  ConfigPtr->BaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
    /*
    * Set the SPI device as a master and in manual slave select mode such
    * that the slave select signal does not toggle for every byte of a
    * transfer, this must be done before the slave select is set.
    */
    Status = XSpi_SetOptions(&SpiInstance, XSP_MASTER_OPTION
                                            | XSP_CLK_ACTIVE_LOW_OPTION
                                            | XSP_CLK_PHASE_1_OPTION);
    if(Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    /*
     * Start the SPI driver so that the device is enabled.
     */
    XSpi_Start(SpiInstancePtr);
    /*
     * Disable Global interrupt
     */
    XSpi_IntrGlobalDisable(SpiInstancePtr);

    u8 WriteBuffer[BUFFER_SIZE] = {0x3F,0x20,0x00,0x21,0x02,0x22,0x30,0x23,0x00,0x24,0x00,0x30,0x00,0x32,0x00,0x33,0x2A,0x34,0x05,0x36,0x00,0x37};

//     * Select the slave on the SPI bus so that the device can be
//     * accessed using the SPI bus.
//     */
    Status = XSpi_SetSlaveSelect(&SpiInstance, 0x01);
    if(Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

////     * Transmit the data.

    XSpi_Transfer(SpiInstancePtr, WriteBuffer, NULL, BUFFER_SIZE);

    Status = XSpi_SetSlaveSelect(&SpiInstance, 0x00);
    if(Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    return XST_SUCCESS;
}

/******************************************************************************/
/**
* Initialize the GPIO driver and setup Interrupt System
*
* @param    IntcInstancePtr is a reference to the Interrupt Controller
*        driver Instance
* @param    InstancePtr is a reference to the GPIO driver Instance
* @param    DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from
*        xparameters.h
* @param    IntrId is XPAR_INTC_0_GPIO_2_VEC_ID
*        value from xparameters.h
* @param    IntrMask is the GPIO channel mask: XPAR_AXI_GPIO_2_IP2INTC_IRPT_MASK in xparameter.h
*
* @return
*        - XST_SUCCESS if the Test is successful
*        - XST_FAILURE if the test is not successful
*
* @note        None.
*
******************************************************************************/

int GPIO_Intr1(    INTC *IntcInstancePtr, XGpio *InstancePtr, u16 DeviceId, u16 IntrId, u16 IntrMask)
{
    int Status;
    /* Initialize the GPIO driver. If an error occurs then exit */
    Status = XGpio_Initialize(InstancePtr, DeviceId);
        if (Status != XST_SUCCESS) {
            return XST_FAILURE;
        }

    XGpio_SelfTest(&Gpio);

    Status = GpioSetupIntrSystem(IntcInstancePtr, InstancePtr, DeviceId,
                            IntrId, IntrMask);
            if (Status != XST_SUCCESS) {
                return XST_FAILURE;
            }


//    GpioDisableIntr(IntcInstancePtr, InstancePtr, IntrId, IntrMask);
    return Status;
}

/******************************************************************************/
/**
* @param    IntcInstancePtr is a reference to the Interrupt Controller
*        driver Instance
* @param    InstancePtr is a reference to the GPIO driver Instance
* @param    DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from
*        xparameters.h
* @param    IntrId is XPAR_INTC_0_GPIO_2_VEC_ID
*        value from xparameters.h
* @param    IntrMask is the GPIO channel mask: XPAR_AXI_GPIO_2_IP2INTC_IRPT_MASK in xparameter.h
*
* @return
*        - XST_SUCCESS if the Test is successful
*        - XST_FAILURE if the test is not successful
*
* @note        None.
*
******************************************************************************/

int GpioSetupIntrSystem(INTC *IntcInstancePtr, XGpio *InstancePtr,    u16 DeviceId, u16 IntrId, u16 IntrMask)
{
    int Result;
    GlobalIntrMask = IntrMask;
/*
 * Initialize the interrupt controller driver so that it's ready to use.
 * specify the device ID that was generated in xparameters.h
 */
    Result = XIntc_Initialize(IntcInstancePtr, INTC_DEVICE_ID);
    if (Result != XST_SUCCESS) {
        return Result;
    }

/* Hook up interrupt service routine */
    XIntc_Connect(IntcInstancePtr, IntrId, (Xil_ExceptionHandler) GpioHandler, InstancePtr);

/* Enable the interrupt vector at the interrupt controller */
    XIntc_Enable(IntcInstancePtr, IntrId);
/*
 * Start the interrupt controller such that interrupts are recognized
 * and handled by the processor
 */
    Result = XIntc_Start(IntcInstancePtr, XIN_REAL_MODE);
    if (Result != XST_SUCCESS) {
        return Result;
    }
/*
 * Enable the GPIO channel interrupts so that push button can be
 * detected and enable interrupts for the GPIO device
 */
    XGpio_InterruptEnable(InstancePtr, XGPIO_IR_CH1_MASK);
    XGpio_InterruptGlobalEnable(InstancePtr);

/*
 * Initialize the exception table and register the interrupt
 * controller handler with the exception table
 */
    Xil_ExceptionInit();

    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
             (Xil_ExceptionHandler)INTC_HANDLER, IntcInstancePtr);

/* Enable non-critical exceptions */
    Xil_ExceptionEnable();

    return XST_SUCCESS;
}

void GpioHandler()
{
//    XGpio *GpioPtr = (XGpio *)CallbackRef;
    d++;
}


/******************************************************************************/
/**
*
* This function disables the interrupts for the GPIO
*
* @param    IntcInstancePtr is a pointer to the Interrupt Controller
*        driver Instance
* @param    InstancePtr is a pointer to the GPIO driver Instance
* @param    IntrId is XPAR_INTC_0_GPIO_2_VEC_ID
*        value from xparameters.h
* @param    IntrMask is the GPIO channel mask: XPAR_AXI_GPIO_2_IP2INTC_IRPT_MASK in xparameter.h
*
* @return    None
*
* @note        None.
*
******************************************************************************/

void GpioDisableIntr(INTC *IntcInstancePtr, XGpio *InstancePtr,
            u16 IntrId, u16 IntrMask)
{
    XGpio_InterruptDisable(InstancePtr, IntrMask);
#ifdef XPAR_INTC_0_DEVICE_ID
    XIntc_Disable(IntcInstancePtr, IntrId);
#else
    /* Disconnect the interrupt */
    XScuGic_Disable(IntcInstancePtr, IntrId);
    XScuGic_Disconnect(IntcInstancePtr, IntrId);
#endif
    return;
}

0 Kudos