cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Wrong - STM32F0

Nickname925_O
Associate II
Posted on December 20, 2013 at 08:56

Hello everybody,

I have a problem with the I2C protocol used by the STM32F0.

In fact, after setting up all the necessary stuff if I use this code:

I2C_GenerateSTART(I2C1);

I get the waveform I2C_ONE

http://www.coocox.org/forum/images/ckeditorimages/20131219181541_I2C_ONE.png

The first channel is SDA and the second one is SCL.

Otherwise if I use this code:

I2C1->CR2=0xAA;

I2C_GenerateSTART(I2C1);

I get the waveform I2C_TWO.

http://www.coocox.org/forum/images/ckeditorimages/20131219181602_I2C_TWO.png

In any case, if I use I2C_SendData(....

I am not able to get any data on the I2C bus.

Can you try to use the I2C protocol with the STM32F0 and give me a feedback?

Thanks a lot,

Peppe

stm32f0-i2c-start

23 REPLIES 23
hotek
Associate II
Posted on December 20, 2013 at 12:30

Hi.

This is a working test code for 7 bits addressing. Use init, and next send address your slave and length of data.

void I2C_PCF_init()

 {

     RCC->AHBENR|=RCC_AHBENR_GPIOBEN;

     GPIOB->MODER|= GPIO_MODER_MODER6_1|GPIO_MODER_MODER7_1;  

     GPIOB->OSPEEDR|=GPIO_OSPEEDER_OSPEEDR6|GPIO_OSPEEDER_OSPEEDR7;//predkosc 50MHZ

     GPIOB->AFR[0]|=0x11000000;    //AF1

 

     RCC->APB1ENR|=RCC_APB1ENR_I2C1EN ;        //enable clock for I2C1

 

     I2C1->CR1|=I2C_CR1_PE;                    //set PE

     I2C1->CR1&=~I2C_CR1_PE;                    //reset PE

     while(I2C1->CR1&I2C_CR1_PE);            //while PE ==1

 

     I2C1->TIMINGR|=(PRESC << 28)|(SCLL<<0)|(SCLH<<8)|(SCLDEL<<20)|(SDADEL<<16); //configure for 48Mhz clock

 

     I2C1->CR1|=I2C_CR1_PE;                    //set PE

 }

void I2C_PCF_send(int address, int length)

 {

     while ((I2C1->ISR & I2C_ISR_TXE)==0);    //while TXE ==0, buffer is full

     I2C1->CR2|=(address<<0)|(length<<16)| I2C_CR2_AUTOEND ;    //address SLAVE 7bits

     I2C1->CR2 &=~ I2C_CR2_RD_WRN;                        //write

     I2C1->CR2 |= I2C_CR2_START;

     while ((I2C1->ISR & I2C_ISR_TXE)==0);    //while TXE ==0, buffer is full

     I2C1->TXDR=0xff;//example data

 }

Nickname925_O
Associate II
Posted on January 07, 2014 at 16:38

Hi h.n,

I'll use it soon and I'll inform you 🙂

Thanks a lot in advance for your help.

Ciao 🙂

Nickname925_O
Associate II
Posted on January 08, 2014 at 15:18

Hi hn,n

thanks a lot for your code. It's working... finally!

Now, pease, can you help me on set up the I2C2 on STM32F051R8 uC?

I am able to set up the I2C1 but not able to set up the I2C2... it seems it has a different clock activation...

Nickname925_O
Associate II
Posted on January 14, 2014 at 18:56

Hi,

here my update:

if I try to communicate with IC2 of STM32F4 discovery it acks the address while, if I send the I2C2 address of the stm32F0 discovery, while if I try to send an address rom I2C1 of stm32f0 I am not able to see any ack.

How can I set the slave of I2C2 of STM32F0?

chen
Associate II
Posted on January 15, 2014 at 12:16

Hi

It is not clear what you are trying to do :

''here my update:

if I try to communicate with IC2 of STM32F4 discovery it acks the address''

'' while, if I send the I2C2 address of the stm32F0 discovery, ''

So you have set up the I2C 2 - who is sending the I2C 2 address?

''while if I try to send an address rom I2C1 of stm32f0 I am not able to see any ack.''

Who is sending the I2C 1 address

''How can I set the slave of I2C2 of STM32F0?''

The I2C peripheral must be configured as a I2C slave. The example code probably configures the I2C peripheral as a master.

What is connected to what?

Are all 3 on the I2C bus?

What are you using for he pull ups?

Nickname925_O
Associate II
Posted on January 15, 2014 at 14:28

Ok,

now I try to be clearer and I try to semplificate all.

Here what I am doing:

 

STM32F0 discovery: I use I2C1 module like I2C master.

STM32F0 discovery: I use I2C2 module like I2C slave.

Pull-up resistor 4k7

I2C1 sends the slave address (I2C2 address) to I2C2 module.

I am not able to see any ack during the 9th clk.

Do you know how to set up the I2C2 module as slave?

PS: I tried to do the same thing uging I2C1 of stm32F0discovery and I2C2 module of STM32F4discovery but no good resuts at all...

frankmeyer9
Associate II
Posted on January 15, 2014 at 14:40

Have you considered that you might have set the slave address incorrectly ?

Could it be you ''overdrive'' your slave, i.e. clocking too fast ?

Did you measure those waveforms on the pins of your I2C slave ?

chen
Associate II
Posted on January 15, 2014 at 16:05

Hi

Very basic question -

Have you connected I2C1 pins (SDA & SCL) and I2C2 pins together on the same I2C Bus?

Nickname925_O
Associate II
Posted on January 16, 2014 at 23:41

Hi,

I connected

SDA of IC1 with SDA of IC2

SCL of IC1 with SCL of IC2

I am sure I did set correctly the slave address of IC2 because I set it in init() function...