This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

I2C Multi Byte Operation

Hi,

I am using LM3S9B96 Dev board to interface with my device through I2C. I need to use 8 bit address and 16 bit data. Its working fine but at some instance it's missing some data while write process. My code is

Write Function:

void Write(uint8_t Reg_Addr, usint16_t data) {

I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);         /* I2C Start */
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, Slave_Adress, I2C_SEND );            /* Slave Address Transfer */
I2CMasterDataPut( I2C1_MASTER_BASE, Reg_Addr);                                 /* Register Address Transfer */
while( I2CMasterBusy(I2C1_MASTER_BASE));                                    /* Waiting for BusIdle */
I2CMasterDataPut( I2C1_MASTER_BASE, (data >> 8) & 0xFF );                     /* Hr Byte data transfer */
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
while( I2CMasterBusy(I2C1_MASTER_BASE));                                     /* Waiting for BusIdle */
I2CMasterDataPut( I2C1_MASTER_BASE, (data & 0xFF) );                         /* Lr Byte data transfer */
while( I2CMasterBusy(I2C1_MASTER_BASE));                                     /* Waiting for BusIdle */
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH );       /* I2C Stop */

}

Read Function:

void Read(uint8_t Reg_Addr) {

I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);            /* I2C Start */
    I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, Slave_Adress, I2C_SEND );            /* Slave Address Transfer Enable*/
    I2CMasterDataPut( I2C1_MASTER_BASE, Reg_Addr);                                /* Register Address Transfer */
    while( I2CMasterBusy(I2C1_MASTER_BASE));                                    /* Waiting for BusIdle */
   
    I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, Slave_Adress, I2C_RECEIVE );        /* I2C Data Receive Enable */
    I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);        /* I2C Restart */
    while( I2CMasterBusy(I2C1_MASTER_BASE));                                    /* Waiting for BusIdle */
    rec[0] = I2CMasterDataGet(I2C1_MASTER_BASE);                                /* Hr Byte data Read */
    I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH);        /* I2C Stop */
    while( I2CMasterBusy(I2C1_MASTER_BASE));                                    /* Waiting for BusIdle */
    rec[1] = I2CMasterDataGet(I2C1_MASTER_BASE);                                /* Lr Byte data Read */

}

Thanks in advance...

  • Hi,

        What is the value of I2C_SEND?

    -kel

  • Hi kel,

    #define I2C_SEND false
    #define I2C_RECEIVE true

    #define High_Speed true
    #define Low_Speed  false

    These are the things i have defined. But the problem is solved now. That's because of my slave device.

    But the external interrupt is not working now. The code is

    Initialization:

       SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

        PinoutSet();

        IntMasterEnable();

        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
        GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_7);
        GPIOPortIntRegister(GPIO_PORTJ_BASE,Proximity_I);                    
        GPIOIntTypeSet(GPIO_PORTJ_BASE, GPIO_PIN_7,    GPIO_RISING_EDGE );
        IntEnable(INT_GPIOJ);

        GPIOPinIntEnable(GPIO_PORTJ_BASE,GPIO_PIN_7);

    Interrupt Handler:

    void intGPIOJ() {

        GPIOPinIntClear(GPIO_PORTJ_BASE,GPIO_PIN_7);                                                                
        IntMasterDisable();

        //

        // My code

       //

      IntMasterEnable();

    }

    Startup Code:

            AREA    RESET, CODE, READONLY
            THUMB

            EXTERN  Timer0IntHandler
            EXTERN  IntGPIOJ
    ;******************************************************************************
    ;
    ; The vector table.
    ;
    ;******************************************************************************
            EXPORT  __Vectors
    __Vectors
            DCD     StackMem + Stack            ; Top of Stack
            DCD     Reset_Handler               ; Reset Handler
            DCD     NmiSR                       ; NMI Handler
            DCD     FaultISR                    ; Hard Fault Handler
            DCD     IntDefaultHandler           ; The MPU fault handler
            DCD     IntDefaultHandler           ; The bus fault handler
            DCD     IntDefaultHandler           ; The usage fault handler
            DCD     0                           ; Reserved
            DCD     0                           ; Reserved
            DCD     0                           ; Reserved
            DCD     0                           ; Reserved
            DCD     IntDefaultHandler           ; SVCall handler
            DCD     IntDefaultHandler           ; Debug monitor handler
            DCD     0                           ; Reserved
            DCD     IntDefaultHandler           ; The PendSV handler
            DCD     IntDefaultHandler           ; The SysTick handler
            DCD     IntDefaultHandler           ; GPIO Port A
            DCD     IntDefaultHandler           ; GPIO Port B
            DCD     IntDefaultHandler           ; GPIO Port C
            DCD     IntDefaultHandler           ; GPIO Port D
            DCD     IntDefaultHandler           ; GPIO Port E
            DCD     IntDefaultHandler           ; UART0 Rx and Tx
            DCD     IntDefaultHandler           ; UART1 Rx and Tx
            DCD     IntDefaultHandler           ; SSI0 Rx and Tx
            DCD     IntDefaultHandler           ; I2C0 Master and Slave
            DCD     IntDefaultHandler           ; PWM Fault
            DCD     IntDefaultHandler           ; PWM Generator 0
            DCD     IntDefaultHandler           ; PWM Generator 1
            DCD     IntDefaultHandler           ; PWM Generator 2
            DCD     IntDefaultHandler           ; Quadrature Encoder 0
            DCD     IntDefaultHandler           ; ADC Sequence 0
            DCD     IntDefaultHandler           ; ADC Sequence 1
            DCD     IntDefaultHandler           ; ADC Sequence 2
            DCD     IntDefaultHandler           ; ADC Sequence 3
            DCD     IntDefaultHandler           ; Watchdog timer
            DCD     Timer0IntHandler            ; Timer 0 subtimer A
            DCD     IntDefaultHandler           ; Timer 0 subtimer B
            DCD     IntDefaultHandler           ; Timer 1 subtimer A
            DCD     IntDefaultHandler           ; Timer 1 subtimer B
            DCD     IntDefaultHandler           ; Timer 2 subtimer A
            DCD     IntDefaultHandler           ; Timer 2 subtimer B
            DCD     IntDefaultHandler           ; Analog Comparator 0
            DCD     IntDefaultHandler           ; Analog Comparator 1
            DCD     IntDefaultHandler           ; Analog Comparator 2
            DCD     IntDefaultHandler           ; System Control (PLL, OSC, BO)
            DCD     IntDefaultHandler           ; FLASH Control
            DCD     IntDefaultHandler           ; GPIO Port F
            DCD     IntDefaultHandler           ; GPIO Port G
            DCD     IntDefaultHandler           ; GPIO Port H
            DCD     IntDefaultHandler           ; UART2 Rx and Tx
            DCD     IntDefaultHandler           ; SSI1 Rx and Tx
            DCD     IntDefaultHandler           ; Timer 3 subtimer A
            DCD     IntDefaultHandler           ; Timer 3 subtimer B
            DCD     IntDefaultHandler           ; I2C1 Master and Slave
            DCD     IntDefaultHandler           ; Quadrature Encoder 1
            DCD     IntDefaultHandler           ; CAN0
            DCD     IntDefaultHandler           ; CAN1
            DCD     IntDefaultHandler           ; CAN2
            DCD     IntDefaultHandler           ; Ethernet
            DCD     IntDefaultHandler           ; Hibernate
            DCD     IntDefaultHandler           ; USB0
            DCD     IntDefaultHandler           ; PWM Generator 3
            DCD     IntDefaultHandler           ; uDMA Software Transfer
            DCD     IntDefaultHandler           ; uDMA Error
            DCD     IntDefaultHandler           ; ADC1 Sequence 0
            DCD     IntDefaultHandler           ; ADC1 Sequence 1
            DCD     IntDefaultHandler           ; ADC1 Sequence 2
            DCD     IntDefaultHandler           ; ADC1 Sequence 3
            DCD     IntDefaultHandler           ; I2S0
            DCD     IntDefaultHandler           ; External Bus Interface 0
            DCD     IntGPIOJ           ; GPIO Port J

    The interrupt is generated but it's not reaching it's handler, instead it goes to the default handler loop.

  • Hi,

    Prakash N said:
    GPIOPortIntRegister(GPIO_PORTJ_BASE,Proximity_I); 

        Try, commenting this out, and see if the interrupt work. You, setup IntGPIOJ() at your startup file as the interrupt handler for PORTJ not Proximity_I(). Are you triggering that interrupt using a switch?

    -kel

  • Hi,

    I have changed the handler name and commented that line, but still the error exists. I am using a sensor to trigger the interrupt.

    I have some more doubts.

    I2C:

    - Whether the internal pull ups are enough or we have to connect any external pull up resistor. I have enabled the week pull up through the code but it is not working. It's working fine when i connect an external pull up(10K).

    Interrupt:

    - While initialization of the Interrupt i am using GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_7); line to configure the pin as push pull one. Here i am using the active high interrupt and the voltage level is 1.8V only. Is it enough to trigger the interrupt.  When the interrupt is enabled it is remaining in the high state. Can i use any external Pull down resistors to avoid this issue.

    Thanks in advance

    Prakash

  • Hi,

       I believe those pull-up resistors is necessary for I2C SDA and SCL lines. You, can search about that in the internet.

       What is that sensor that your are connecting to PJ7?

    -kel

  • Hi,

    I am using accelerometer which has the interrupt pin in push pull state. I am configuring the interrupt pin as active high.

    Prakash.