I2C implementation for STM32

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
matis
Posts: 53
Joined: Fri Jul 01, 2011 1:46 pm

Re: I2C implementation for STM32

Post by matis »

Guys,

Currently I'm developing an application that uses hot-pluggable I2C nodes. To determine if a node is connected, I use a loop to poll all the addresses where nodes could possible live.

When read-through-write to a address that's not available my application breaks and waits forever in the _unhandled_exception(). When I logic analyse the data, it looks like this:
Image
(click for large)

I'm trying to make the i2c calls as synchronous as possible. Therefore my mcuconf.h looks like this:

Code: Select all

/*
 * I2C driver system settings.
 */
#define STM32_I2C_USE_I2C1                  TRUE
#define STM32_I2C_USE_I2C2                  TRUE
#define I2C_SUPPORTS_CALLBACKS              FALSE
#define STM32_I2C_I2C1_IRQ_PRIORITY         10
#define STM32_I2C_I2C2_IRQ_PRIORITY         10
#define STM32_I2C_I2C1_DMA_PRIORITY         4
#define STM32_I2C_I2C2_DMA_PRIORITY         4
#define STM32_I2C_I2C1_DMA_ERROR_HOOK()     chSysHalt()
#define STM32_I2C_I2C2_DMA_ERROR_HOOK()     chSysHalt()
/* I2C1 */
//#define STM32_I2C_I2C1_USE_GPT_TIM          GPTD1
#define STM32_I2C_I2C1_USE_POLLING_WAIT     TRUE
/* I2C2 */
//#define STM32_I2C_I2C2_USE_GPT_TIM          GPTD2
#define STM32_I2C_I2C2_USE_POLLING_WAIT     TRUE

Hence the commented out GPT!

The error reproduces on I2C1 as well I2C2.

To fix this problem, I disabled the I2C_CR2_ITERREN for both the I2C busses

Code: Select all

#define  I2C_CR2_ITERREN                     ((uint16_t)0x0000)            /*!< Error Interrupt Enable */


But when I run the application and try to read from a nonexisting address, it keeps looping in the

Code: Select all

void i2c_lld_master_transmit(I2CDriver *i2cp, uint16_t slave_addr,
    uint8_t *txbuf, size_t txbytes, uint8_t *rxbuf, size_t rxbytes) {
...
  while (!(i2cp->id_i2c->SR1 & I2C_SR1_ADDR))
    ;
...


On the logic analyser, it looks like this
Image

Is there a way to let the application time out after a number of milliseconds?

I also found the I2C_POLLING_TIMEOUT (0xFFFF default) lowered it to 0x00FF, but that doesn't seem to affect the hung.
Can anybody help me out on this one?

I'm currently at 2.3.4 rev 3451. Is it worth updating?

Thanks in advance!

matis
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 implementation for STM32

Post by Giovanni »

The timeout should be implemented inside the driver the application has no control over those loops. The current I2C code contains a lot of workarounds for the STM32 I2C limitations (the only thing I don't like in STM32), this would be yet another workaround.

Probably this kind of situations would be best served by a software I2C driver, my idea is to provide a portable bit banged I2C driver. Probably after the 2.3.4 release (this weekend).

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: I2C implementation for STM32

Post by barthess »

Hi, Giovanni.
I am trying to port I2C to stm32f407 and observe strange behavior. BUSY bit is always set like SDA or SCL line is low (but they are not). Peripheral is properly functional (perhaps SWRST bit temporally clears BUSY bit). When I manually shorted lines to ground I see zeroes instead of ones in GPIOB_IDR register. Any suggestions? May be I configured pins in wrong way?

Code: Select all

  palSetPadMode(GPIOB, 10,  PAL_STM32_PUDR_FLOATING | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_ALTERNATE(4));
  palSetPadMode(GPIOB, 11,  PAL_STM32_PUDR_FLOATING | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_ALTERNATE(4));
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 implementation for STM32

Post by Giovanni »

Hi,

You need to specify PAL_MODE_ALTERNATE(4) instead of PAL_STM32_ALTERNATE(4), this macro also includes PAL_STM32_MODE_ALTERNATE.

Code: Select all

#define PAL_MODE_ALTERNATE(n)           (PAL_STM32_MODE_ALTERNATE |         \
                                         PAL_STM32_ALTERNATE(n))


In your code you specified which alternate function the pin would select if programmed as alternate but it is not actually made alternate.

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: I2C implementation for STM32

Post by barthess »

Thanks, that was the problem. I have tested driver with tmp75 and bmp085 - works almost without changes. In my plans to rewrite communication part using DMA.
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 implementation for STM32

Post by Giovanni »

I would try to make it thinner, it is a very large driver, I am worried about maintenance because of its size. Of course DMA operations would be nice too.

What do you think about making it purely synchronous with DMA? probably this would cut a lot of code and make the API simpler.

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: I2C implementation for STM32

Post by barthess »

Using DMA allow to cut out the must ugly code but make impossible to one byte at a transaction, only two or more (stm32 i2c specific, sic!). This will be the lesser evil.
Giovanni wrote:What do you think about making it purely synchronous with DMA?

I think it will lead to a loss of flexibility and prefer to make async driver with optional synchronization using standard OS mechanisms.
Giovanni wrote:make the API simpler.

What do you mean?

I2C users, join the discussion.
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 implementation for STM32

Post by Giovanni »

barthess wrote:
Giovanni wrote:make the API simpler.

What do you mean?


I mean that removing the asynchronous part would make the API easier to use. BTW I am considering a synchronous API for the USB driver too, endpoints would be accessed as streams instead of having callbacks.

In my opinion a good driver model is one that balances usability, features and complexity. A complex API that requires a complex implementation that is only required by 2% of users is usually not a good idea.

All the other drivers are done following this idea: keep it simple, the goal is not to make it good for 100% of users and support 100% features of the underlying hardware. The design goal is to make is good enough for most users, elegant, and support the underlying hardware where this does not conflict with the previous two points.

Anyway, let's collect opinions.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: I2C implementation for STM32

Post by Badger »

I do use the i2c callbacks, but only to wake a thread. My usage pattern involves issuing a read transaction on two different devices simultaneously (on I2C1 and I2C2), and sleeping until one of the callbacks wakes the thread (only one callback ever tries to wake it). So as long as there is a possibility to issue two simultaneous read commands from within one thread, I can live without the callbacks.

Great news on getting it working on the F4! I'll have a play with getting the accelerometer working on the F4-Discovery (we could have the board do the same demo with the 4 LEDs that comes pre-installed)
Jacon
Posts: 143
Joined: Wed Dec 08, 2010 7:52 am
Has thanked: 50 times
Been thanked: 5 times

Re: I2C implementation for STM32

Post by Jacon »

Badger wrote:Great news on getting it working on the F4! I'll have a play with getting the accelerometer working on the F4-Discovery (we could have the board do the same demo with the 4 LEDs that comes pre-installed)


I'm sorry, Badger, but F4-Discovery uses SPI to connect with its accelerometer... ;)
Your own build board may use I2C, because LIS302DL has both interfaces.
Post Reply