I2C using on STM32L15x

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Raig
Posts: 10
Joined: Tue Apr 26, 2011 4:00 pm

Re: I2C using on STM32L15x

Post by Raig »

What can I do for work around?

Raig
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: I2C using on STM32L15x

Post by mabl »

Without ever having used the I2C module - couldn't you just use:

palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C using on STM32L15x

Post by Giovanni »

Right, try declaring both clock and data as opendrain, do you have external pull-up resistors right?

Giovanni
Raig
Posts: 10
Joined: Tue Apr 26, 2011 4:00 pm

Re: I2C using on STM32L15x

Post by Raig »

Hello again,

the initialization of the I2C pins works fine with the OR operation posted by mabl. My sensor delivers stable values now, in moment I got 1004.8hPa here in Berlin. :D

Raig
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Re: I2C using on STM32L15x

Post by genosensor »

I'm also having trouble getting I2C to function on an STM32L-discovery board.

The working example user Raig had posted in February is no longer available at:
http://rapose.de/Raig/ChibiOS_2.4.0_I2C ... m_Raig.zip

Does anyone else have a copy of that zip file?

Has anyone managed to get I2C working on the STM32L?
If so, could you please provide an working example?

For me, the I2C unit refuses to drive the bus because it reports the bus as BUSY (bit set in SR2)
Most likely, this means that the functional unit is not attached to the I2C pads properly.

Here's how I'm doing that:

Code: Select all

  palSetGroupMode(GPIOB, 3, 8, PAL_MODE_ALTERNATE(4) |
    PAL_MODE_OUTPUT_OPENDRAIN | PAL_STM32_OSPEED_MID1);

I have verified that PB8 and PB9 can drive the I2C bus when configured as open drain outputs.
(I can bang the bits)
Other masters on the bus are not affected, so I know neither SDA or SCL are being pulled low.
I'm not absolutely sure about PAL_MODE_ALTERNATE(4), but that's what the reference manual
and examples for other STM32 variants seem to imply.

I'm working with ChiBiOS 2.6.1

Thanks in Advance for any help!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C using on STM32L15x

Post by Giovanni »

Do you have external pull up resistors? the alternate modes are listed in the Datasheet not in the Reference Manual. You also could give a look to the guide Barthess wrote in the community section of the wiki.

Giovanni
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Re: I2C using on STM32L15x

Post by genosensor »

I can bang the bits when they are configured as open drain but not mapped to alternative_fuction(4).
The other I2C masters on the same bus can access slaves. That proves that the bus and associated pull ups are working.

You are right about the datasheet vs. the reference. My mistake. Nonetheless, it looks like AFIO4 is the correct selection.

Yes, I have looked at the wiki. But, the pad set up for the STM32L is different.

I've attached the little test program in the hopes that another set of eyes will catch my stupid mistake.
Attachments
i2ctest.zip
simple i2c master test. SB status is stuck active.
(1009 Bytes) Downloaded 547 times
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C using on STM32L15x

Post by Giovanni »

I don't see obvious errors, try increasing the pad speed to mid2 or high.

Giovanni
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Re: I2C using on STM32L15x

Post by genosensor »

[SOLVED]
It was a pin configuration issue after all.
This is a proper initialization for the STM32L1xx:

Code: Select all

/* I2C1 */
static const I2CConfig i2cfg1 = {
    OPMODE_I2C,
    100000,
    STD_DUTY_CYCLE
};

int main(void) {
  halInit();
  chSysInit();
  /* I2C1 SCL on PB8, SDA on PB9 */
  palSetGroupMode(GPIOB,3,8, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);
  i2cStart(&I2CD1, &i2cfg1);


It seems that we all got confused by the need to declare the alternate function pin as OPENDRAIN.
These alternate function pads are *NOT* a type of output pad!!

As a result, this was causing palSetGroupMode to quietly generate an invalid moder mask:

Code: Select all

  palSetGroupMode(GPIOB,3,8, PAL_MODE_ALTERNATE(4) | PAL_OUTPUT_OPENDRAIN);


This sort of thing is so insidious that it got me thinking about a way to flag the error at compile time.
What's needed is a check to ensure that the result of combining attributes with the logical OR operator
equals that of combining them with XOR. I just can't think of how to coax the 'C' preprocessor to do this.

Any Ideas?
Post Reply