Quantcast
Channel: MSP low-power microcontroller forum - Recent Threads
Viewing all articles
Browse latest Browse all 21937

Why does I2C bus remain busy after first transaction?

$
0
0

Hello! I'm using MSP-EXP432P401R Launchpad with a BME280 sensor (breakout board). I'm trying to communicate using I2C, but after successfully performing a read or write, the I2C bus remains busy. The sensor board has 10kohm pull-ups mounted and I've set the clock at 400kHz. I'm using MSP432 DriverLib v3.21.00.05.

E.g., I've tried reading the chip ID two times consecutively, but my function hangs in while(I2C_isBusBusy(EUSCI_B1_BASE)) when reading the second time. If I remove this loop, then I2C_masterSendSingleByteWithTimeout() returns 0. The code I'm using:

int main(void)
{
	Clock_System_init();

	MAP_Interrupt_enableSleepOnIsrExit();
	MAP_Interrupt_enableMaster(); // Enabling MASTER interrupts

	Timer32_sleep(1000000);

	I2C_init();

	Timer32_sleep(1000000);

	char result;
	I2C_setslave(0x76); // BME280_SLAVE_ADDRESS
	if (!I2C_read8(0xD0, &result, 1000)) //BME280_REGISTER_CHIPID
		return false;
	if (result != 0x60) // Wrong chip ID
		return false;

	I2C_setslave(0x76); // BME280_SLAVE_ADDRESS
	if (!I2C_read8(0xD0, &result, 1000)) //BME280_REGISTER_CHIPID
		return false;
	if (result != 0x60) // Wrong chip ID
		return false;
        while(1) {};
}

const eUSCI_I2C_MasterConfig i2cConfig =
{
	EUSCI_B_I2C_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
	6000000,                                // SMCLK = 6MHz
	EUSCI_B_I2C_SET_DATA_RATE_400KBPS,      // Desired I2C Clock of 400khz
	0,                                      // No byte counter threshold
	EUSCI_B_I2C_NO_AUTO_STOP                // No Autostop
};

void I2C_init(void)
{
	/* Select I2C function for I2C_SCL(P6.5) & I2C_SDA(P6.4) */
	GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P6, GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);
	GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P6, GPIO_PIN4, GPIO_PRIMARY_MODULE_FUNCTION);

	/* Initialize USCI_B0 and I2C Master to communicate with slave devices*/
	I2C_initMaster(EUSCI_B1_BASE, &i2cConfig);

    /* Disable I2C module to make changes */
    I2C_disableModule(EUSCI_B1_BASE);

    /* Enable I2C Module to start operations */
    I2C_enableModule(EUSCI_B1_BASE);

    return;
}

/***************************************************************************//**
 * @brief  Reads data from the sensor
 * @param  pointer Address of register to read from
 * @return Register contents
 ******************************************************************************/

bool I2C_read8(unsigned char pointer, char * result, unsigned int timeout)
{
	while(I2C_isBusBusy(EUSCI_B1_BASE));

    /* Set master to transmit mode PL */
	I2C_setMode(EUSCI_B1_BASE,
        EUSCI_B_I2C_TRANSMIT_MODE);

    /* Clear any existing interrupt flag PL */
	I2C_clearInterruptFlag(EUSCI_B1_BASE,
        EUSCI_B_I2C_TRANSMIT_INTERRUPT0);

    /* Initiate start and send first character */
    if (!I2C_masterSendSingleByteWithTimeout(EUSCI_B1_BASE,
        pointer, timeout))
    	return 0;

    *result = I2C_masterReceiveSingleByte(EUSCI_B1_BASE);

    return 1;
}

I've attached my I2C .c and .h files hoping that someone could reproduce the problem.

I've tried using the example "BOOSTXL-SENSORS_SensorGUI_MSP432P401R", but without success, because the program hangs in the following code from readI2C():

while(ui8Status == eUSCI_BUSY)
	{
		if(MAP_I2C_getInterruptStatus(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0))
		{
			ui8Status = eUSCI_IDLE;
		}
	}

Does anybody know what is the problem?

(Please visit the site to view this file)(Please visit the site to view this file)


Viewing all articles
Browse latest Browse all 21937

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>