Skip to main content
JChen.43
Associate
March 31, 2023
Question

Stm32F4 i2c as master trasmit HAL_OK but not working

  • March 31, 2023
  • 5 replies
  • 5089 views

I’m using the board, motordriver IIC, to control motor through I2C bus.

The chip on the board is PCA9685.

The board is originally a extension for arduino UNO, and I’m using STM32F446RE which also support arduino UNO expansion connector.

The official give the arduino code, here is github link

github.com/YFROBOT-TM/Yfrobot-Motor-Driver-Library

and I translate it into stm32 code.

Now I want to drive a motor connent to M1 on the board with PWM 100%.

The following is the my stm32 code in main function:

0693W00000bhIjYQAU.pngAnd this is the ioc setting(including clock configuration):

[I2C]

standard mode with 100kHz

PB8 for SCL and PB9 for SDA, both pin set as open drain and pull-up.

[RCC]

set HSE to Crystal/Ceramic Resonator

[SYS]

set Debug to Serial Wire

0693W00000bhInFQAU.png 

I use the logic analyzer, and the SDA line shows what I trasmit is correct

0693W00000bhIrbQAE.png0693W00000bhIsPQAU.png0693W00000bhIikQAE.png 

***Worth mentionong, I found that at the SCL line, some duty cycle are 50%, and some are40%, don’t know if it make the i2c not working…

0693W00000bhIupQAE.png 

100kHz = 10us, refer to the form, the duty cycle should be between 40%~53% (?)

0693W00000bhJ0iQAE.png

This topic has been closed for replies.

5 replies

Bob S
Super User
March 31, 2023

Why do you use "M1IN1 << 1" as the register address in buffer 1, and likewise shifting by 1 in buffer 2 and 3? Presuming LED0_ON_L is defined as 6, then you are sending 12 (0x0c) as the register address (as shown in the logic analyzer data). That is LED1_OFF_L, probably not what you intend.

JChen.43
JChen.43Author
Associate
April 1, 2023

@Bob S​ Thanks for answering the question.

I shift the register address left one bit is beacause I thought it will show the right address in the logic analyzer.

I'm curious about how to read the logic analyzer data.

I read SDA 0 or 1 when SCL in evert HIGH pulse. so the result as below, 1~7bit is the data, the 8th bit (red one) for write/read, and the 9th bit (green one) for ACK/NACK.

Because during the 9th high pulse of SCL, SDA is LOW, so it's ACK.

And, 0x06 = 6 in decimal = 110 in binary

Is that right?

0693W00000bhKV8QAM.png

AScha.3
Super User
April 1, 2023

>I'm curious about how to read the logic analyzer data.

:)

only first byte, = address , has 8. bit (0 write, 1 read ) ; all following bytes are 8 bits data and stay on r/w mode from first byte; so to change "write" to "read" mode, need send address-byte again, called: repeated start; always needed, when read from any chip or eeprom, because first access is write to select address or register in target chip, but to read then this value, need switching from write to read mode.

and 9. bit always ack/nack.

in HAL this is : HAL_I2C_Mem_Read() , or HAL_I2C_Mem_Write() for write to ...

"If you feel a post has answered your question, please click ""Accept as Solution""."
JChen.43
JChen.43Author
Associate
April 1, 2023

I try the example code with arduino, and it works.

@Bob S​ you're right. I don't need to shift one bit left for control register.

another mistake for me is that the write value for 4096, sould be 4096 fiirst, and then 4096>>8u.

I fix the two mistakes, and comapre the logic analyzer result with STM32 and arduino. Here is the different.

use HAL_I2C_Master_Transmit() in STM32 v.s. arduino

​SDA line got some other HIGH signal between every byte0693W00000bhKbQQAU.png

use HAL_I2C_Mem_Write() (recommend from @AScha.3​  ) in STM32 v.s. arduino

SDA line data looks ok

but the problem is SCL line, there is no gap between every 9bit

0693W00000bhKbkQAE.png

Bob S
Super User
April 7, 2023

There doesn't NEED to be a gap between each byte. A gap appears only then the master is not quite ready to send the next byte after the ACK cycle. As long as the data line is valid on the rising clock edge, all is fine.

JChen.43
JChen.43Author
Associate
April 11, 2023

but it still doesn't work... :( Is there anything I missed checking?

HGanc.1
Associate
May 23, 2023

Do you find the solution? Because i have the same problem, i use a PCA9685 with the adress 0x7f, and the i2c connection don't work. I test my connection with the HAL status.

JChen.43
JChen.43Author
Associate
June 2, 2023

Sadly, I haven't found the solution...