cancel
Showing results for 
Search instead for 
Did you mean: 

MPU6050 I2C communication using ll driver

PToma.14
Associate II

I am using STM32F0 microcontroller for my project with MPU6050. The HAL driver i2c code is working fine for mpu6050 but LL driver code doens't work. I want to use LL driver for i2c. Following is my LL driver code for reading and writing.

uint8_t motionSensor_read(uint8_t slave_address, uint8_t start_address)
{
	LL_I2C_SetSlaveAddr(I2C2, 0xD0); /// Prepare Address to send
	LL_I2C_SetMasterAddressingMode(I2C2, LL_I2C_ADDRSLAVE_7BIT);
	LL_I2C_SetTransferRequest(I2C2, LL_I2C_REQUEST_WRITE); // Request write
	LL_I2C_DisableAutoEndMode(I2C2); // Disable automatic STOP condition generation
	LL_I2C_SetTransferSize(I2C2, 1); // Set transfer size register
 
 
	while(LL_I2C_IsActiveFlag_BUSY(I2C2)){} // check I2C busy
	LL_I2C_GenerateStartCondition(I2C2); // generate I2C Start address and send address
 
	while (!LL_I2C_IsActiveFlag_TXIS(I2C2)){
	}
 
	LL_I2C_TransmitData8(I2C2, start_address);
	while (!LL_I2C_IsActiveFlag_TXIS(I2C2)){
	}
 
 
	LL_I2C_SetSlaveAddr(I2C2, 0xD0); // Prepare Address to send
	LL_I2C_SetMasterAddressingMode(I2C2, LL_I2C_ADDRSLAVE_7BIT);
	LL_I2C_SetTransferRequest(I2C2, LL_I2C_REQUEST_READ); // Request read
	LL_I2C_DisableAutoEndMode(I2C2); // Enable autoend mode
	LL_I2C_SetTransferSize(I2C2, 1); // Set transfer size register
	LL_I2C_GenerateStartCondition(I2C2); // generate I2C Start address and send address
 
	LL_I2C_AcknowledgeNextData(I2C2, LL_I2C_ACK);
	while (!LL_I2C_IsActiveFlag_RXNE(I2C2)){
	}
	dat_m = LL_I2C_ReceiveData8(I2C2);
	LL_I2C_AcknowledgeNextData(I2C2, LL_I2C_NACK);
 
	LL_I2C_GenerateStopCondition(I2C2);
 
	LL_I2C_ClearFlag_TXE(I2C2);
	LL_I2C_ClearFlag_OVR(I2C2);
	LL_I2C_ClearFlag_NACK(I2C2);
	LL_I2C_ClearFlag_STOP(I2C2);
 
	return dat_m;
}
 
int motionSensor_write(uint8_t slave_address, uint8_t start_address,
		uint8_t data) {
 
 
	LL_I2C_Disable(I2C2);
	LL_I2C_ClearFlag_STOP(I2C2);
	LL_I2C_Enable(I2C2);
	LL_I2C_EnableIT_TX(I2C2);
 
	LL_I2C_HandleTransfer(I2C2, 0xD0, LL_I2C_ADDRSLAVE_7BIT, 2, LL_I2C_MODE_AUTOEND,    LL_I2C_GENERATE_START_WRITE);
 
	LL_I2C_TransmitData8(I2C2, start_address);
	while (!LL_I2C_IsActiveFlag_TXIS(I2C2)) {
	}
 
	//Send data
	LL_I2C_TransmitData8(I2C2, data);
	while (!LL_I2C_IsActiveFlag_TXIS(I2C2)) {
	}
 
	LL_I2C_GenerateStopCondition(I2C2);
 
	return 0;
}

I first try to write to a particular register of mpu6050 but the code is always struck while checking for TXIS interrupt flag in motionSensor_write. Where I am making mistake? How to resolve my problem.

1 REPLY 1
TurbotEMS
Associate

@PToma.14​ did you figure this out? I have the exact same issue!