cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Started with I2C

george23
Associate II
Posted on August 17, 2009 at 23:46

Getting Started with I2C

5 REPLIES 5
george23
Associate II
Posted on May 17, 2011 at 13:20

Hi, I am writing my own I2C class and I am having trouble getting started. Does anyone have code that shows how to initialize the I2C1? I am trying to configure it as a master on standard mode (100kHz). This is my code as of the moment:

GPIOB_CRL = 0xFF222222; //sets pin 6 & 7 as Alt_Func and Open Drain with //max speed of 50mhz.

RCC_APB1ENR = 0x200000; //Enables clock for I2C1 (1<

I2C1_CR2 = 0x2;

I2C1_TRISE = 0x3;

I2C1_CR1= 0x701;

I2C1_CCR = 0x0;

Are their any obvious errors?

Thanks,

George

design5
Associate II
Posted on May 17, 2011 at 13:20

Hi George:

I am using I2C1 for external EE device. Also, I am using the ST Firmware Library.

The code in the attache file works for me. Hope it helps you. To find out what happens with the Firmware Library function calls, you can look into the ''stm32f10x_i2c.c'' file and see what the Init function call does with the registers, and how it stores the elements I have intialized in the structure.

Best wishes,

Garry.

design5
Associate II
Posted on May 17, 2011 at 13:20

Hi George:

Don't forget Garry's Rule #6: When an I/O pin is not behaving as expected, make sure that the GPIO function has been set correctly for that pin, and also that the Clock to the Peripheral has been enabled.

Good luck,

Garry.

st3
Associate II
Posted on May 17, 2011 at 13:20

Quote:

Don't forget Garry's Rule #6:

And what are rules 1-5...?

design5
Associate II
Posted on May 17, 2011 at 13:20

Garry's Rules for correct STM32 development and testing:

1. Check that you have turned on the power and verify power is reaching the CPU (volt meter or scope).

2. Check that you have connected all the cables to the equipment you are using to verify CPU operation.

3. Check that the Reset Pin is not holding the CPU in Reset, or is not pulsing (20 us) with watchdog resets (you will need a good scope for this one).

4. Check that the oscillator (external) is working, either by scoping the external crystal, or monitoring the GPIO Port pin you have programmed to toggle every time the System Tick interrupt occurs.

5. Check that you have actually programmed the CPU with the correct code and not some previous version which did not work correctly.

6. When an I/O pin is not behaving as expected, make sure that the GPIO function has been set correctly for that pin, and also that the Clock to the Peripheral has been enabled.

7. When a peripheral is not behaving as expected, check the Errata for the particular CPU in question.

More rules will be published as they are formulated.

Garry.