cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U5G9 - I2C1 not working

ttnickb
Associate III

# Overview

I'm trying to add I2C1 in "controller mode" to an existing STM32U5G9 project, but am struggling to get it working.

I am setting up the peripheral using HAL/LL calls and not relying on CubeMX setup.


# What I'm Seeing

The goal is to have PG13 = SDA and PG14 = SCL with an I2C clock rate of 10kHz.  On those pins, I see no activity even though my firmware should attempt an I2C transaction after initializing the peripheral.  The two I2C pins just stay HIGH all the time.  (I have 4.7kOhm external pullups.)

If I switch SCL to PB8, I do see some activity but it looks wrong (not enough clock pulses for SCL):

 

image (6).png

For the ~2 clock pulses on SCL here ^, the timing is correct (~10kHz), but I'm not sure why it stops and doesn't do a full 8 clock pulses...

 

# I2C1 Initialization Code for PG13/PG14
My project is using STM32U5xx HAL `v1.5.0`.  The code snippet below is how I2C1 is initialized:

 
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOG);
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

GPIO_InitStruct.Pin = LL_GPIO_PIN_14;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
GPIO_InitStruct.Alternate = LL_GPIO_AF_4;
LL_GPIO_Init(GPIOG, &GPIO_InitStruct);

GPIO_InitStruct.Pin = LL_GPIO_PIN_13;
LL_GPIO_Init(GPIOG, &GPIO_InitStruct);

// Set I2C clock to HSI (16MHz)
LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);

// Peripheral clock enable
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);

// I2C Init
I2C_MasterInitStruct.Instance = I2C1;
I2C_MasterInitStruct.Init.Timing = 0x3042c3c7; // 10kHz according to RM
I2C_MasterInitStruct.Init.OwnAddress1 = 0x00;
I2C_MasterInitStruct.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2C_MasterInitStruct.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2C_MasterInitStruct.Init.OwnAddress2 = 0;
I2C_MasterInitStruct.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
I2C_MasterInitStruct.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2C_MasterInitStruct.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&I2C_MasterInitStruct) != HAL_OK)
{
 printf("An error occurred during I2C init");
}

if (HAL_I2CEx_ConfigAnalogFilter(&I2C_MasterInitStruct, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
 printf("An error occurred during I2C analog filter config");
}


# Conclusion

Any tips on using I2C1 on the STM32U5G9 would be appreciated.  I feel like this is something specific to I2C1 and/or the U5G9, since the I2C4 peripheral works OK in the same project with basically the same code above (only the relevant constants changed for I2C4).

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> PG13 = SDA and PG14 = SCL

Port G needs to be enabled:

HAL_PWREx_EnableVddIO2();

 

How are you using the I2C? I see initialization but not usage. Do the functions return HAL_OK?

Does the TIMING value match up with what CubeMX generates with the same settings?

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

View solution in original post

3 REPLIES 3
TDK
Guru

> PG13 = SDA and PG14 = SCL

Port G needs to be enabled:

HAL_PWREx_EnableVddIO2();

 

How are you using the I2C? I see initialization but not usage. Do the functions return HAL_OK?

Does the TIMING value match up with what CubeMX generates with the same settings?

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

Wow, thanks TDK.  Seems like it was just that 1 missing HAL_PWREx_EnableVddIO2() function call to get it working on port G pins:

ttnickb_1-1722869771996.png

^ This is via a call to HAL_I2C_Mem_Read() after initialization now.  Still need to figure out why the target device isn't responding, but at least the STM32 is doing the correct thing.

 

Note that 7-bit slave addresses should be shifted left by one bit. What are you trying to interface to?

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