parallelism of i2c and SPI

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

Moderator: RoccoMarco

Post Reply
lesto
Posts: 55
Joined: Thu May 22, 2014 11:27 am

parallelism of i2c and SPI

Post by lesto »

hello,

i'm reading the sensor form stm32f3discovery in a single thread program.
Let's imagine to move the code for every sensor (accelereter: i2c, magnetometer: i2c, gyroscope: SPI) into 3 thread, as far as i know my code is thread save for SAME KIND on bus; BUT as SPI and I2C are sharing DMA, will i2cAcquireBus and spiAcquireBus block each others? or i have to implement my own lock mechanism? (I'm assuming i've to use blocking function in 3 thread, and i can't run 3 ainc request togeder)

ps. i add how i'm doing EVERY read. Is it ok? it've seen around code doin *Start BEFORE *AcquareBus, but as far as i undertand from APIs mineis correct order as *AcquireBus is what prevent race condition.

Also, how can i know if a SPI reading/bus is on error state?
The api say spiExchange does not have as prerequisite to do a spiSelect, is it right? if yes, it is ok if i do it anyway?

Code: Select all

i2cAcquireBus(&I2CD1);
i2cStart(&I2CD1, &i2cconfig);
status = i2cMasterTransmitTimeout(&I2CD1, sensor_address_7_bit, &register_to_read, 1, buffer_rx, size_of_buffer_rx, time_out_clock);
i2cStop(&I2CD1);
i2cReleaseBus(&I2CD1);


Code: Select all

spiAcquireBus(&SPID1);
spiStart(&SPID1, &spi1cfg);
spiSelect(&SPID1);
spiExchange(&SPID1, 8, txbuf, rxbuf);
spiUnselect(&SPID1);
spiStop(&SPID1);
spiReleaseBus(&SPID1);
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: parallelism of i2c and SPI

Post by Giovanni »

No, they would not block each other because internally two separate mutexes would be used.

You need to create another mutex and use chMtxLock/chMtxUnlock instead of xxxAquire/xxxRelease.

Giovanni
lesto
Posts: 55
Joined: Thu May 22, 2014 11:27 am

Re: parallelism of i2c and SPI

Post by lesto »

thank you,
and how about check if SPI connection was on error? how can i tell if returned data was valid?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: parallelism of i2c and SPI

Post by Giovanni »

SPI does not have error correction/detection, it is up to the upper software layers to implement checks using checksums or CRCs.

Giovanni
lesto
Posts: 55
Joined: Thu May 22, 2014 11:27 am

Re: parallelism of i2c and SPI

Post by lesto »

thank you very much
Post Reply