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.

MSP430F5359: Handling repeated start condition with driverlib: strange stop condition

Part Number: MSP430F5359

Hello,

I have built two i2c_write and i2c_read polling function on top of the driverlib.

I need to do a I2C read that contains a repeated start condition.
Since my device responded wrongly, I plugged a logical analyzer on my I2C bus and I noticed that a stop occurs before the repeated start while the driverlib functions I use should'nt trigger stop condition.
My target device works well when it comes to do a stop between two commands, as well as others devices connected on the bus, but I can't manage to handle Repeated start gracefully.


The logical analyzer output:




The code I have written

bool i2c_read(uint8_t device_address, uint8_t register_address, uint8_t *data, uint8_t len)
{
	bool result = false;
	USCI_B_I2C_initMaster(USCI_B2_BASE, device_addressS);
	USCI_B_I2C_setMode(USCI_B2_BASE, USCI_B_I2C_TRANSMIT_MODE);
	USCI_B_I2C_enable(USCI_B2_BASE);
	_disable_interrupts();

    /* Sending the register address */
	if (!USCI_B_I2C_masterSendMultiByteStartWithTimeout(USCI_B2_BASE, register_address, 400))
 		goto end;
	
    /* Reading the result */
    USCI_B_I2C_setMode(USCI_B2_BASE, USCI_B_I2C_RECEIVE_MODE);
	USCI_B_I2C_enable(USCI_B2_BASE);
        
	if (len == 1) {
		if (!USCI_B_I2C_masterReceiveSingleStartWithTimeout(USCI_B2_BASE, TIMEOUT))
			goto end;
		*data = USCI_B_I2C_masterReceiveSingle(USCI_B2_BASE);
	} else {
		USCI_B_I2C_masterReceiveMultiByteStart(I2C_BASE);
		
		while (len--) {
			if (len == 1) {	
				if (!USCI_B_I2C_masterReceiveMultiByteFinishWithTimeout(I2C_BASE, data++, 400))
					goto end;
			} else {
				msleep(1);
				*data++ = USCI_B_I2C_masterReceiveMultiByteNext(I2C_BASE);
			}
		}
	}

	result = true;
end: 
	_enable_interrupts();
	USCI_B_I2C_disable(I2C_BASE);
	return result;
}

Why do I observe this stop condition  ?

What am I doing wrong ?

  • Hello,

    Thanks for your detailed post. Unfortunately, I2C issues are not my specialty, but I'll do my best to help out. First, can you narrow down which DriverLib function is causing the stop condition to occur? In the meantime, you may find the following threads to be helpful regarding repeated start conditions. I tried to find a code example with repeated start, but it seems that they only exist for our MSP432 devices (not sure if referencing them will help or not).

    In the MSP430x5xx User's Guide, I found where setting UCTXSTT again in Master Transmitter Mode triggers a repeated start condition. In the Master Receive Mode section (page 1007), I found a NOTE that the UCTXSTT bit must be set before the last data byte is received; that is, immediately after the UCRXIFG is set and the UCRXBUF with the second to last byte is read, the UCTXSTT bit should be set.

    Regards,

    James

    MSP Customer Applications

  • There do not appear to be any driverlib functions that would allow to wait for TXIFG before issuing the restart:

    USCI_B_I2C_masterSendMultiByteStart(..., baseAddress);
    
    //Poll for transmit interrupt flag.
    while(!(HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG))
    {
        ;
    }
    HWREG8(baseAddress + OFS_UCBxIFG) &= ~UCTXIFG;
    
    USCI_B_I2C_masterReceiveSingleStart(...);
    
    data = USCI_B_I2C_masterReceiveSingle(...);

    (untested)

  • Hi and thanks for your response.
    The function that triggers the stop condition seems to be USCI_B_I2C_masterSendMultiByteStartWithTimeout but reading its code I don't see anything that could be the culprit.

    I also found the note in the user's guide but no success for now. I set again UCTXSTT bit but anyway the stop condition is generated.
    If you can give me some code sample for a repeated start, even for MSP432, that would be nice.
  • Hi and thanks for your response. I have tested this code sample since you have posted it on another I2C thread, but I couldn'nt get it to work.
  • I found a MSP432 thread discussing repeated start, and I also found the following comment in the code that may be helpful.

    /* If we reached the transmit interrupt, it means we are at index 1 of
    
    * the transmit buffer. When doing a repeated start, before we reach the
    
    * last byte we will need to change the mode to receive mode, set the start
    
    * condition send bit, and then load the final byte into the TXBUF.

    Regards,

    James

    MSP Customer Applications

  • I found the MSP432 code example that contains the comment in my previous reply. Keep in mind that these DriverLib functions for MSP432 may be different than what's done for MSP430 DriverLib functions. I thought that it may help you as a reference. The repeated start condition seems to be handled in the ISR.

    i2c_master_rw_repeated_start-master_code

    /*
     * -------------------------------------------
     *    MSP432 DriverLib - v3_21_00_05 
     * -------------------------------------------
     *
     * --COPYRIGHT--,BSD,BSD
     * Copyright (c) 2016, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * --/COPYRIGHT--*/
    /******************************************************************************
     *  MSP432 I2C - EUSCI_B0_BASE I2C Master TX  bytes to MSP432 Slave - Repeated Start
     *
     *  Description: This demo connects two MSP432 's via the I2C bus. The master
     *  transmits to the slave. This is the MASTER CODE. It continuously
     *  transmits an array of data and demonstrates how to implement an I2C
     *  master transmitter sending multiple bytes followed by a repeated start,
     *  followed by a read of multiple bytes.  This is a common operation for
     *  reading register values from I2C slave devices such as sensors. The
     *  transaction for the I2C that is written looks as follows:
     *
     *  ________________________________________________________________
     *  |  Start   |      |      |  Start   |                   |       |
     *  | 0x48Addr | 0x04 | 0x00 | 0x48Addr |  <10 Byte Read>   | Stop  |
     *  |    W     |      |      |    R     |                   |       |
     *  |__________|______|______|__________|___________________|_______|
     *
     *  ACLK = n/a, MCLK = HSMCLK = SMCLK = BRCLK = default DCO = ~3.0MHz
     *
     *                                /|\  /|\
     *                MSP432P401      10k  10k      MSP432P401
     *                   slave         |    |         master
     *             -----------------   |    |   -----------------
     *            |     P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA     |
     *            |                 |  |       |                 |
     *            |                 |  |       |                 |
     *            |     P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL     |
     *            |                 |          |                 |
     *
     * Author: Timothy Logan
     *****************************************************************************/
    /* DriverLib Defines */
    #include "driverlib.h"
    
    /* Standard Defines */
    #include <stdint.h>
    
    #include <stdbool.h>
    #include <string.h>
    
    /* Slave Address for I2C Slave */
    #define SLAVE_ADDRESS       0x48
    #define NUM_OF_REC_BYTES    10
    
    /* Variables */
    const uint8_t TXData[2] = {0x04, 0x00};
    static uint8_t RXData[NUM_OF_REC_BYTES];
    static volatile uint32_t xferIndex;
    static volatile bool stopSent;
    
    /* I2C Master Configuration Parameter */
    const eUSCI_I2C_MasterConfig i2cConfig =
    {
            EUSCI_B_I2C_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
            3000000,                                // SMCLK = 3MHz
            EUSCI_B_I2C_SET_DATA_RATE_100KBPS,      // Desired I2C Clock of 100khz
            0,                                      // No byte counter threshold
            EUSCI_B_I2C_NO_AUTO_STOP                // No Autostop
    };
    
    int main(void)
    {
        volatile uint32_t ii;
    
        /* Disabling the Watchdog  */
        MAP_WDT_A_holdTimer();
    
        /* Select Port 1 for I2C - Set Pin 6, 7 to input Primary Module Function,
         *   (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
         */
        MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
                GPIO_PIN6 + GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
        stopSent = false;
        memset(RXData, 0x00, NUM_OF_REC_BYTES);
    
        /* Initializing I2C Master to SMCLK at 100khz with no autostop */
        MAP_I2C_initMaster(EUSCI_B0_BASE, &i2cConfig);
    
        /* Specify slave address */
        MAP_I2C_setSlaveAddress(EUSCI_B0_BASE, SLAVE_ADDRESS);
    
        /* Set Master in transmit mode */
        MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
    
        /* Enable I2C Module to start operations */
        MAP_I2C_enableModule(EUSCI_B0_BASE);
    
        /* Enable and clear the interrupt flag */
        MAP_I2C_clearInterruptFlag(EUSCI_B0_BASE,
                EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_RECEIVE_INTERRUPT0);
        //Enable master Receive interrupt
        MAP_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
        MAP_Interrupt_enableSleepOnIsrExit();
        MAP_Interrupt_enableInterrupt(INT_EUSCIB0);
    
        while (1)
        {
            /* Making sure the last transaction has been completely sent out */
            while (MAP_I2C_masterIsStopSent(EUSCI_B0_BASE) == EUSCI_B_I2C_SENDING_STOP);
    
            /* Send start and the first byte of the transmit buffer. We have to send
             * two bytes to clean out whatever is in the buffer from a previous
             * send  */
            MAP_I2C_masterSendMultiByteStart(EUSCI_B0_BASE, TXData[0]);
            MAP_I2C_masterSendMultiByteNext(EUSCI_B0_BASE, TXData[0]);
    
    
            /* Enabling transfer interrupt after stop has been sent */
            MAP_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
    
            /* While the stop condition hasn't been sent out... */
            while(!stopSent)
            {
                MAP_PCM_gotoLPM0InterruptSafe();
            }
    
            stopSent = false;
        }
    }
    
    /*******************************************************************************
     * eUSCIB0 ISR. The repeated start and transmit/receive operations happen
     * within this ISR.
     *******************************************************************************/
    void EUSCIB0_IRQHandler(void)
    {
        uint_fast16_t status;
    
        status = MAP_I2C_getEnabledInterruptStatus(EUSCI_B0_BASE);
        MAP_I2C_clearInterruptFlag(EUSCI_B0_BASE, status);
    
        /* If we reached the transmit interrupt, it means we are at index 1 of
         * the transmit buffer. When doing a repeated start, before we reach the
         * last byte we will need to change the mode to receive mode, set the start
         * condition send bit, and then load the final byte into the TXBUF.
         */
        if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT0)
        {
            MAP_I2C_masterSendMultiByteNext(EUSCI_B0_BASE, TXData[1]);
            MAP_I2C_disableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
            MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
            xferIndex = 0;
            MAP_I2C_masterReceiveStart(EUSCI_B0_BASE);
            MAP_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0);
    
        }
    
        /* Receives bytes into the receive buffer. If we have received all bytes,
         * send a STOP condition */
        if (status & EUSCI_B_I2C_RECEIVE_INTERRUPT0)
        {
            if(xferIndex == NUM_OF_REC_BYTES - 2)
            {
                MAP_I2C_masterReceiveMultiByteStop(EUSCI_B0_BASE);
                RXData[xferIndex++] = MAP_I2C_masterReceiveMultiByteNext(EUSCI_B0_BASE);
            }
            else if(xferIndex == NUM_OF_REC_BYTES - 1)
            {
                RXData[xferIndex++] = MAP_I2C_masterReceiveMultiByteNext(EUSCI_B0_BASE);
                MAP_I2C_disableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0);
                MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
                xferIndex = 0;
                stopSent = true;
                MAP_Interrupt_disableSleepOnIsrExit();
            }
            else
            {
                RXData[xferIndex++] = MAP_I2C_masterReceiveMultiByteNext(EUSCI_B0_BASE);
            }
    
        }
    }
       

    Regards,

    James

    MSP Customer Applications

  • Hi all and thanks for your responses. I have done a lot of researches about the issue for the last weeks and I found I was simply misuing my logicial analyzer. I have set a higher sampling rate (5 MHz) and I don't observe the strange stop condition anymore.

    On a hard side, everything seems OK and I managed to make my software implementation working. However, the same code does or does not work depending on the final clock of the I2C block...

    I use the driverlib to configure my i2c block and if I set the dataRate to 100000 in the config struct, all is working.
    If I put 10000 in the data rate, it does not work anymore.

    I noticed that in driverlib there is only two available values for dataRate (as macros): 100000 and 400000. But I also noticed in the core that this parameter is only used to divide the i2cClk parameter in order to obtain the preScalarValue that will be written to OFS_UCBxBRW.

    I use SMCLK with a 1 MHz frequency as input clock fro my usci block.

    Is 10000 a valid value for dataRate in my case ?

    Thanks for your responses.

  • The issue could be related to the timeout. At a higher I2C frequency of 100kHz, the timeout most likely won't get triggered but could be at the lower (slower) frequency of 10kHz. I would recommend using 100kHz.

    Regards,

    James

    MSP Customer Applications

**Attention** This is a public forum