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,

I'm currently encountering problems when reading more than 3 bytes (16 in my case) from an I2C node.
I'm having the I2C_SUPPORTS_CALLBACKS set to false, therefore my code uses the i2c_lld_master_receive on line 985 of i2c_lld.c

After reading 15 bytes, it gets stuck at the

Code: Select all

while (i2cp->id_i2c->CR1 & I2C_CR1_STOP)
      ;

loop.

And it never never never exits that loop. When I logic analyse the data, it looks like this:
Image

The statemachine at that moment looks like this

Code: Select all

i2cp   0x20001950   
   id_state   I2C_ACTIVE_TRANSMIT   
   id_mutex   {...}   
   id_config   0x080096d0   
   id_slave_config   0x080096e0   
   txbytes   0   
   rxbytes   1   
   rxbuf   0x200013e8 ==> "EMBRIONIX      "
   txbuf   0x200013f8   
   rxbuff_p   0x00000000   
   txbuff_p   0x00000000   
   errors   0   
   flags   0   
   slave_addr   80   
   slave_addr1   161   
   slave_addr2   0   
   sevent   {...}   
   id_i2c   0x40005800


I use the I2C2 with STM32_I2C_I2C2_USE_POLLING_WAIT set to true.

Is this a bug or am I using the implementation wrong?
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: I2C implementation for STM32

Post by Badger »

How are you issuing the read command? I successfully read 6 bytes at a time using:

Code: Select all

i2cMasterTransmit(i2cp, &slave_cfg, address, tbuf, 1, outBuffer, 6);
matis
Posts: 53
Joined: Fri Jul 01, 2011 1:46 pm

Re: I2C implementation for STM32

Post by matis »

Badger,

We use it as read through write as you can see in de image from the logic analyser. The call looks like this:

Code: Select all

#define I2C_RX_DEPTH 16
#define I2C_TX_DEPTH 32
static i2cblock_t i2c_rx_data[I2C_RX_DEPTH];
static i2cblock_t i2c_tx_data[I2C_TX_DEPTH];

int i2c_smbus_read_i2c_block_data(int file, uint8_t command, uint8_t length, uint8_t *values)
{
    uint8_t n = 0;
    int32_t result = 0;
    i2c_tx_data[0] = command;
    i2cMasterTransmit(i2cBusses[file].i2drv, &i2cSlaveCfg, i2cBusses[file].addr, i2c_tx_data, 1, i2c_rx_data, length);
    for (n = 0; n < length; n++)
    {
        values[n] = i2c_rx_data[n];
    }
    return result;
}


txbytes is fixed at 1 and rxbytes (length) is dynamic from 0 to I2C_RX_DEPTH bytes. And is in this case 16 bytes. The i2c_tx_data[0] is 20.
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 »

I do not know where is problem can be. Try to set I2C_SUPPORTS_CALLBACKS to TRUE and create stubs for callbacks. With callbacks driver tested much better by me.
matis
Posts: 53
Joined: Fri Jul 01, 2011 1:46 pm

Re: I2C implementation for STM32

Post by matis »

barthess wrote:I do not know where is problem can be. Try to set I2C_SUPPORTS_CALLBACKS to TRUE and create stubs for callbacks. With callbacks driver tested much better by me.

Thanks for your reply, I followed your suggestion and it works like a charm now. I find it a bit dissapointing that the synchronous version doesn't work the way the async version does, but if it works it works.

Question;
Do I need to synchronise the function call / return statement with the

Code: Select all

while (i2cBusses[file].i2drv->id_state != I2C_READY)
    {
        chThdSleepMilliseconds(1);
    }

construction?

I have STM32_I2C_I2C1_USE_POLLING_WAIT set to true.
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: I2C implementation for STM32

Post by DrunkenDonkey »

If my opinion counts for anything (I STILL DONT HAVE THE DAMN BOARD!!!), threading is so easy and cheap in chibi (after all, that's one of the main advantages), that I'd rather have the sync api that I can run on a thread and very easily create request-response complex command queues, so every command uses the data from the previous in order to create the next request and logically the whole function/code block stays as one. I do remember the days of state-machine code, where running anything resembling a thread would kill the stuff, the massive amounts of flags and variables to hold the current progress of the logic with callbacks allover the place from the main loop, but I think what we have now allows us to get away from this model and write more software, than firmware oriented code. Or at least I hope so.
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 »

I have take a look at code of my current project. Most of I2C routines use sync model, others can be easily rewrite to it. So sync API would be a nice start point.

Giovanni,
I need some explanation of DMA configuring on STM32F4. I am trying to add DMA support like in UART driver.

from hal_lld.h:

Code: Select all

#define STM32_USART2_RX_DMA_CHN 0x00400000
#define STM32_USART2_TX_DMA_CHN 0x04000000

How to interpret that magic hex constants? Can I found DMA manual somewhere?
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,

That constants map DMA streams with channesl, there is a table in the DMA section of the Reference Manual. For example:

If the constant 0x00400007 is associated to peripheral XXX then it means that streams 5 and 0 (nibbles positions) can be used with peripheral XXX by mapping on channels 4 and 7 (nibbles values).

The constants are used by the driver in order to make the mapping automatically and to verify if selected streams are valid for the peripheral.

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 »

The most serious problem is lack of STOP interrupt in master mode. Lets brainstorming:
1. MCU have EXTI controller
2. EXTI controller can generate interrupt when software write bit in EXTI_SWIER register.

So, is it elegant way to "map" bit from one memory address to another?
Or any other way to generate interrupt on bit change?
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 think this problem does not have an elegant solution, I really don't like this I2C peripheral, it is so in contrast with the rest of the SOC which is generally well thought. For example the ADC and SPI peripherals are so easy to work with.

The only way would be an external flip flop connected to SDI, SDC and an EXTI line, that could generate an interrupt when a stop condition happens, of course this is not an acceptable general solution.

It is mainly because of this that I am suggesting a synchronous API, it would put less constraints on the underlying HW, a synchronous API would also allow an error to be returned when the bus is stuck (RDY_RESET for example) or simply implement standard timeouts. The driver could be done intrinsically more robust.

I just regret asking for an asynchronous API when you started working on this, entirely my fault, I should have known better...

Giovanni
Post Reply