cancel
Showing results for 
Search instead for 
Did you mean: 

I2C clock not working

Tendo
Associate II

Hi everybody !! i have an issue on I2C with a STM32 issue. i thought this was a code issue but in the main i also make LEDs blinking and it works, only the I2C doesn't work but... i noticed that my lines both rise to 3.3V but never fall down it just stay high. i guess it might be because i have a clock issue. Im using internal clock for some reasons, no external crystal. The HAL-delay function work with the SYSCLK but my I2C clock doesn't. Thx for your future answers. Best regards.

22 REPLIES 22
TDK
Guru

Do you call HAL_I2C_* functions? Show your usage of them. Do they return HAL_OK or something else?

If you feel a post has answered your question, please click "Accept as Solution".
Imen.D
ST Employee

Hello @Tendo,

Please share more details to help you solve your issue.

Have a look at this FAQ which describes few tips related to I2C: STM32 I2C does not work - STMicroelectronics Community

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

please see the Posting Tips for how to properly post source code - not as screenshots:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 

Tendo
Associate II

Thank for your answer. I will show you what i did : 

#include "STM332_IMU.h"


#define IMU_addr_R  0xD0
#define IMU_addr_W  0xD1

#define IMU_REG_SMPLRT_DIV  0x19
#define IMU_REG_CONFIG  0x1A
#define IMU_REG_GYRO_CONFIG  0x1B
#define IMU_REG_ACCEL_CONFIG  0x1C
#define IMU_REG_PWR_MNGMT 0x6B

#define IMU_TEMP_H  0x41
#define IMU_TEMP_L  0x42
#define IMU_GYRO_X_H  0x43
#define IMU_GYRO_X_L  0x44
#define IMU_GYRO_Y_H  0x45
#define IMU_GYRO_Y_L  0x46
#define IMU_GYRO_Z_H  0x47
#define IMU_GYRO_Z_L  0x48

I2C_HandleTypeDef hi2c;

uint8_t tx_buffer,rx_buffer,temp_H,temp_L;
uint16_t temperature;
uint16_t temp_degree;
uint8_t Rx_gyro_values[6];


int16_t Gyro_X_data = 0;
int16_t Gyro_Y_data = 0;
int16_t Gyro_Z_data = 0;


void Read_IMU_temp ()
{
	HAL_I2C_Mem_Read(&hi2c, IMU_addr_R, IMU_TEMP_H, 1, temp_H, 1, 1000);

	HAL_I2C_Mem_Read(&hi2c, IMU_addr_R, IMU_TEMP_H, 1, temp_L, 1, 1000);

	temperature = ((temp_H << 8) + temp_L);
	temp_degree = ((temperature/340) + 36.53);
}


void Read_IMU_Gyro ()
{
	HAL_I2C_Mem_Read(&hi2c, IMU_addr_R, IMU_GYRO_X_H ,1, Rx_gyro_values, 6, 1000);
    Gyro_X_data = (int16_t)(Rx_gyro_values[0] << 8 | Rx_gyro_values [1]);
    Gyro_Y_data = (int16_t)(Rx_gyro_values[2] << 8 | Rx_gyro_values [3]);
    Gyro_Z_data = (int16_t)(Rx_gyro_values[4] << 8 | Rx_gyro_values [5]);
}


void init_IMU_MPU6050 ()
{

	if	(HAL_I2C_IsDeviceReady(&hi2c, IMU_addr_R, 1, 1000))

		{
			tx_buffer = 0x07;
			HAL_I2C_Mem_Write(&hi2c, IMU_addr_W, IMU_REG_SMPLRT_DIV, 1, tx_buffer, 1, 1000);
			tx_buffer = 0xE0;
			HAL_I2C_Mem_Write(&hi2c, IMU_addr_W, IMU_REG_GYRO_CONFIG, 1, tx_buffer, 1, 1000);
			tx_buffer = 0x00;
			HAL_I2C_Mem_Write(&hi2c, IMU_addr_W, IMU_REG_PWR_MNGMT, 1, tx_buffer, 1, 1000);
			tx_buffer = 0x00;
			HAL_I2C_Mem_Write(&hi2c, IMU_addr_W, IMU_REG_ACCEL_CONFIG, 1, tx_buffer, 1, 1000);
		}

}

As @TDK suggested, check the return values from all your HAL_I2C_... function calls

 

I think this is wrong:

	if	(HAL_I2C_IsDeviceReady(&hi2c, IMU_addr_R, 1, 1000))
		{
			tx_buffer = 0x07;

If the device is ready, then HAL_I2C_IsDeviceReady() will return HAL_OK, which is zero - ie, false !

So it should be:

	if	(HAL_OK == HAL_I2C_IsDeviceReady(&hi2c, IMU_addr_R, 1, 1000))
		{
			tx_buffer = 0x07;

 

TDK
Guru

> I2C_HandleTypeDef hi2c;

Where do you initialize this? Probably nowhere. Certainly not in the included code.

Probably you should be using the handle that is declared and initialized in main.c or i2c.c. Something like:

extern I2C_HandleTypeDef hi2c1;

 

Debug your code, step through functions to understand where and why they don't work as expected. Don't treat them as a black box.

If you feel a post has answered your question, please click "Accept as Solution".

i tried but nothing happens.

Tendo
Associate II

this doesn't compile with because it isn't declared i don't know why. teh thing is , i knwo there might have some issues in my code but :

First, the code compiles properly

Second, nothing block the program, my LED keep blinking and the loop do the job

AND finally i don't have anything on SCL line, even with a bad code, if the I2C is activated we should see the clock however i just see the line going to 3.3V

> AND finally i don't have anything on SCL line, even with a bad code, if the I2C is activated we should see the clock however i just see the line going to 3.3V

Why do you expect specific functionality even with bad code? That is madness.

 

Do you initialize I2C anywhere? If so, where and how? Really hard to debug through a keyhole. Might have to put in some work yourself to properly illustrate what you having going on in the code.

If you feel a post has answered your question, please click "Accept as Solution".