1

STM32F030F4 does not start I2C1 correctly. this is my code:

//Clocks------------------------------------------------
    RCC->AHBENR  = 0x200000;
    RCC->APB1ENR = 0x4040 ; 
//------------------------------------------------------    

//GPIO-------------------------------------------------- 
//Configure Alternate Fucntion GPIO to I2C1
    GPIOA->AFR[1]  = 0x440  //(0b0100)<<4 | (0b0100)<<8 //AF4;
//Set GPIOA Pin 9-10 as Alternate Function
    GPIOA->MODER   = 0x280000;
//Set GPIOA pin 9-10 as speed as High
    GPIOA->OSPEEDR = 0x3c0000; 
//Set GPIOA pin 9-10 as open drain
    GPIOA->OTYPER  = 0x600;
//Set GPIOA pin 9-10 as no Pull-up Pull-down
    GPIOA->PUPDR   = 0x0;
//------------------------------------------------------

//I2C Resgisters
    I2C1->TIMINGR = 0x00201D2D;
    I2C1->CR1     = 0x1;
    I2C1->CR2           = 0x307001c;
    I2C1->TXDR      = 0x111;        
    //Start I2C 
    I2C1->CR2 |= (0b1) << 13;

But output is not true. It is like this: enter image description here

SDA port is not working.

2 Answers2

0

This line seems to be problematic.

GPIOA->AFR[1]  = GPIO_Aternate_Function_I2C1;

You should set AFSEL9 and AFSEL10 value if I2C pins are 9 and 10. But you set AFSEL8 (which is the lowest in AFR[1]), and clear others.

This code should do the thing:

GPIOA->AFR[1]  = GPIO_Aternate_Function_I2C1 * 0x00000110;
Vovanium
  • 3,798
  • 17
  • 23
  • Thanks for your paticipant. But AFR[1] means AFR_H that mentioned in reference manual,not a 1st pin on AFR. – peyman khalili Feb 09 '19 at 09:16
  • I know, AFR[1] refer to pins 8 through 15. You set bits 0..3 of AFR[1] which correspond to AFSEL8, but you should set bits 4..7 for AFSEL9 and 8..11 for AFSEL10. – Vovanium Feb 09 '19 at 09:31
  • I forgot to replace GPIO_Aternate_Function_I2C1 from #defines. I means your conditions was satisfied but not working.. – peyman khalili Feb 09 '19 at 15:20
0

Problem Solved..

Clocks are problematic: changes:(

//Enable PORTA clocks
RCC->AHBENR  |= 0x20000;

//Enable I2C1 clocks
RCC->APB1ENR |= 0x200000 ;

)