cancel
Showing results for 
Search instead for 
Did you mean: 

I2C

MichaelR
Associate III

Hello 

I am new with STM32 boards. I have 2 STM32 boards, and I am trying to get I2C communication between them. The boards I use are: NUCLEO-G071RB as the master, and STM32F412G-DISCO as the slave.

I success to send a response from the slave to the master once, but at other recalls I could not, unless I disconnect the slave's power and restarts it.

How can I solve the issue?

8 REPLIES 8

@MichaelR wrote:

 I am new with STM32 boards.


Do you have experience with any other microcontroller(s)?

Do you have experience with I2C in general?

 


@MichaelR wrote:

I am trying to get I2C communication between them. ?


The classic mistake when doing this is to try to do both ends at once.

The trouble with that approach is that, when you have problems, you can't tell whether the issues are in the Master, or in the Slave - or both.

So I would strongly suggest that you concentrate on one end at a time:

https://www.avrfreaks.net/s/topic/a5C3l000000UYYaEAO/t146630?comment=P-1401100

 


@MichaelR wrote:

How can I solve the issue?


Impossible to tell as we can't see what you have done!

Sounds like your code is getting itself stuck in some bad state...

Again, per the link above, concentrate on getting one end of the link working at a time - in particular, provide good debug/instrumentation/diagnostics...

 

#BothEnds

I further debugged the code, and found out that the problem is that after transmitting the data, the slave handles an error callback with error code 4. Can you help me how do I handle this error that the slave will continue to get communication? 

Start by looking-up what error code 4 means.

But, again, you're left not knowing if that's due to a problem (or problems) in the Master, or in the Slave - or both.

 


@MichaelR wrote:

 Can you help me how do I handle this  


Again, don't try to do it all at once - get one end fully working, debugged, and understood before moving on to the other:

https://www.avrfreaks.net/s/topic/a5C3l000000UYYaEAO/t146630?comment=P-1401100 

 

MNapi
Senior III

Do you use pull up resistors ? that might be the reason

 

1.png

I don't use no pull up resistor. Should I use?

MichaelR_0-1714485768976.png

 


@MichaelR wrote:

I don't use no pull up resistor. Should I use?


That's really basic to the operation of I2C! Not specific to STM32.

Sounds like you need to do some reading-up on the fundamentals:

https://community.st.com/t5/stm32-mcus-products/i2c-interface-with-adxl345-using-stm32f446re/m-p/658323/highlight/true#M240123 

 

Note that The internal pull-ups are not suitable for use as I2C bus pull-ups - you need to use external pullups.

see examples in CubeMX

you have two boards configured for I2C communication and the pins are pulled up.

those board's pins wouldn't be connected through resistors to 3.3V

Alternatively you can attach 10 K resistors.

see this example from CubeMX

/* Configure SCL Pin as : Alternate function, High Speed, Open drain, Pull up */
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_0, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_0_7(GPIOC, LL_GPIO_PIN_0, LL_GPIO_AF_7);
LL_GPIO_SetPinSpeed(GPIOC, LL_GPIO_PIN_0, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOC, LL_GPIO_PIN_0, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_0, LL_GPIO_PULL_UP);

/* Configure SDA Pin as : Alternate function, High Speed, Open drain, Pull up */
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_1, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_0_7(GPIOC, LL_GPIO_PIN_1, LL_GPIO_AF_7);
LL_GPIO_SetPinSpeed(GPIOC, LL_GPIO_PIN_1, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOC, LL_GPIO_PIN_1, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_1, LL_GPIO_PULL_UP);

 

 

The internal pullups are not recommended for use as I2C bus pullups - they are too high resistance:

https://community.st.com/t5/stm32-mcus-products/i2c-channels-as-pull-up-in-stm32f103c8t6/m-p/668588/highlight/true#M242676

See this post for the effect of too-high and too-low pullups:

https://electronics.stackexchange.com/a/473799