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.
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: I2C implementation for STM32

Post by Badger »

I'm building a C++ class around the i2c drivers (i2cDevice), and I need some way of accessing the data stored within the classes from the static i2c callback functions. What complicates matters is that I am using several sub-classes of my main i2c class, so I can't just treat everything as an i2cDevice, instead I added pointers to internal structures (one of which is a thread pointer, which is sometimes NULL). I guess I can get around the const problem by adding a const pointer to a non const structure.

Looking back now, I think perhaps my main issue before was that I was extending the slave config structure in the wrong place, so the memory was being allocated in the class header file before it was extended, and I ended up with a horrible overflow :oops: moving the hook definition into chconf.h sorted that one out :mrgreen:.
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 »

Badger,
If I right understand, I must set const qualifier back to satisfy both ChibiOS traditions and your desires?
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: I2C implementation for STM32

Post by Badger »

barthess wrote:If I right understand, I must set const qualifier back to satisfy both ChibiOS traditions and your desires?

Yes, I think that Giovanni's suggestion of having a const pointer to a non-const struct should do the trick
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 »

Done at rev. 3310
drgnz
Posts: 5
Joined: Wed Sep 28, 2011 1:00 am

Re: I2C implementation for STM32

Post by drgnz »

Howdy? What's the status of the I2C driver? Is it working? Is there an example?
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 »

Driver successfully works on my stm32f103. Examples in .\testhal\STM32F1xx\I2C
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,
For a moment driver uses hardware timers or software wait loops to capture start or stop conditions on the bus. I have try to use trick with EXTI driver like in viewtopic.php?f=3&t=165 topic. It does not work because port must be configured in input mode (I2C bus ports must be configured in open drain mode). So, this software hack needs hardware hack -- connecting SDA line to some port of MCU configured in input mode. Now I am developing new board to replace my Olimex demo board and easily can realize that trick. Is it too dirty to be part of the ChibiOS?

Second thing, I have found very abstract software realization of I2C but not tested it. http://www.levap.ru/2010/03/rabotaem-s- ... i2c-smbus/
Is it possible to use it in ChibiOS due to licensing limitations?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: I2C implementation for STM32

Post by Giovanni »

Hi,

It could be added as an option, the problem is that the driver is already very complicated (because the HW limitations).

About a software implementation, the author does not specify a license, I guess it could be used but probably a clean sheet implementation with timings done using a GPT driver would be better.

The other problem with a software implementation is that we would have to make the API synchronous (and may be, leave the asynchronous callbacks optional). A software driver would only implement the synchronous part. Currently it is the other way, the default is asynchronous with optional synchronous calls.

Giovanni
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: I2C implementation for STM32

Post by DrunkenDonkey »

Code is very pretty I must admit, lacks the ability to send/received more than a single byte, but that can easily be added. It was a long time since I learned russian at school, but there is no restriction of any kind in the message either, so can freely be used. (oh, and long live synchronous communication and threads :D )
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 »

One more way to simplify driver is using DMA. But it is not always possible, my current hardware has no free DMA requests for I2C at all. On STM32F1x they are hardwired (sic!) to peripherals and can not be reassigned.
Post Reply