cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot read I2C on F303K8T6 when TIM3 is in PWM generation on all its 4 channels

ECitt.648
Associate II

after generating the code with cubeMX in order to have 490Hz output PWM for all TIM3 4 channels inside the main if I call (using HAL)

Hal_Tim_Base_Start(&htim3);

Hal_Tim_Pwm_start(&htim3, TIM_CHANNEL_1);

Hal_Tim_Pwm_start(&htim3, TIM_CHANNEL_2);

Hal_Tim_Pwm_start(&htim3, TIM_CHANNEL_3);

Hal_Tim_Pwm_start(&htim3, TIM_CHANNEL_4);

i2c not works correctly and I cannot read the mpu6050.

I2C is configured in fastMode (400KHz).

If I comment the 4 pwm_start line above I can read correctly the sensor when TIM3 is active and PWM is not enabled.

if I change TIM3 with TIM1 I can read correctly the mpu6050 and I can generate the PWM.

So the question is: why i2c not work when I use TIM3 and works correct when I use TIM1 ?

thanks

4 REPLIES 4
berendi
Principal

What are the GPIOB->MODER and GPIOB->AFRL values values for pin 7, in the working and non-working cases?

ECitt.648
Associate II

working config

GPIOB->MODER = 00000000000000001010010101000000

GPIOB->AFR   = 01001000000000000000010000100000

not working config

GPIOB->MODER = 00000000000000001010010101001010 

GPIOB->AFR   = 01001000000000000000010000100000

berendi
Principal

Separated the bitfields for readability,

working config
                                 B7 B6 B5 B4 B3 B2 B1 B0
GPIOB->MODER = 0000000000000000  10 10 01 01 01 00 00 00
 
               PB7  PB6  PB5  PB4  PB3  PB2  PB1  PB0
GPIOB->AFR   = 0100 1000 0000 0000 0000 0100 0010 0000
 
not working config
                                 B7 B6 B5 B4 B3 B2 B1 B0
GPIOB->MODER = 0000000000000000  10 10 01 01 01 00 10 10
 
               PB7  PB6  PB5  PB4  PB3  PB2  PB1  PB0
GPIOB->AFR   = 0100 1000 0000 0000 0000 0100 0010 0000

Looking at the port configuration bits for PB7 (SDA) and PB6 (SCL), MODER bits are 10 for both, they are in alternate function mode, good.

Alternate function number for PB7 is 4, this is the AF number for I2C1, good.

Alternate function number for PB6 is 8, this is wrong.

See Table 14. Alternate functions in the datasheet. AF8 is not defined for PB6, so anything might happen there, it might even accidentally work.

Find out what code puts 8 in bits 24-27 of AFR[0], and correct it. The AF value for PB0 (AF0) is undefined too, and there is no reason to set PB2 to AF8 when it's not configured to alternate function mode, something is writing junk all over the place. I'd suggest going over all GPIO registers, and checking their values against the datasheet and the reference manual.

0690X00000ArPRwQAN.png

Thanks for your answer,

so If I understood correctly the wrong value AF8 should be replaced with AF4 according to the picture.

Thank you