STM32F0 Discovery I2C

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
dflogeras
Posts: 232
Joined: Tue Sep 03, 2013 8:16 pm
Has thanked: 8 times
Been thanked: 22 times

STM32F0 Discovery I2C

Post by dflogeras »

Hi,

Chibi-3.0.3, and STM32F072 DISCO board. This is my first time dealing with the i2c-v2 driver, so I thought I'd give it a try.

I read the RM for my part and found the tables for I2C timings. Since my project has

Code: Select all

#define STM32_I2C1SW                        STM32_I2C1SW_HSI

The I2C freq should be 8MHz.

After looking that up in the table, and I wish to support 100kHz I2C mode, I ended up with:

Code: Select all

static const I2CConfig i2c_cfg_1 = {
  STM32_TIMINGR_PRESC(0x01U) |
  STM32_TIMINGR_SCLDEL(0x04U) | STM32_TIMINGR_SDADEL(0x02U) |
  STM32_TIMINGR_SCLH(0x0fU)  | STM32_TIMINGR_SCLL(0x13U),
  0,
  0
};



Sidenote, the example in testhal/STM32/STM32F37x/I2C that I have found using i2c-v2 driver is a bit confusing. The comment says one thing, then there is a I2C configuration which is commented out, and another replacing it, none of which seems to agree with the comment, or the manuals.

Anyway, I set up my two SDA/SCL pins as per the RM/Datasheet, and configure the device:

Code: Select all


  palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN );
  palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN );
  i2cInit();
  i2cStart( &I2CD1, &i2c_cfg_1 );


However, a subsequent call to i2cMasterTransmitTimeout() returns MSG_RESET. I'm not sure why, or how to proceed. I have all DBG defines enabled and nothing is causing an error/stack overflow that I can detect.

The I2C hardware works, as I began this project on a STM32L DISCOVERY, and am porting it to the F0. The only major differences are the I2C config/setup code. I can see on the scope that the SCL and SDA lines are attempting to communicate, but something must be different in the timing perhaps?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F0 Discovery I2C

Post by Giovanni »

Hi,

Those configuration fields should be taken from the reference manual, the comment probably is copied by the original demo for the F373 and is not correct.

MSG_RESET is documented as:

Code: Select all

 * @retval MSG_RESET    if one or more I2C errors occurred, the errors can
 *                      be retrieved using @p i2cGetErrors().


You need to verify the nature of the error.

Giovanni
dflogeras
Posts: 232
Joined: Tue Sep 03, 2013 8:16 pm
Has thanked: 8 times
Been thanked: 22 times

Re: STM32F0 Discovery I2C

Post by dflogeras »

Hmm, between re-testing with the STM32L discovery, adding the i2cGetErrors() calls, and hooking up my logic analyzer, the problem went away. I'm guessing I may have had a loose connection to the devkit. My code is in version control and I don't see anything that I am doing differently/changed since my first post.

All seems to be communicating just as well as the i2cv1 driver, the UV index in my office is 0.02 :)

Sorry for the noise, but sometimes if you don't post a question you spin your wheels.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F0 Discovery I2C

Post by Giovanni »

np :) glad it is solved.

Giovanni
dflogeras
Posts: 232
Joined: Tue Sep 03, 2013 8:16 pm
Has thanked: 8 times
Been thanked: 22 times

Re: STM32F0 Discovery I2C

Post by dflogeras »

So my next step was to port to my homebrew F030F4 board. Worked like a charm, just thought you might like to know in case no one has yet used I2C on this device.
Post Reply