Page 1 of 1

parallelism of i2c and SPI

Posted: Fri May 30, 2014 11:03 pm
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);

Re: parallelism of i2c and SPI

Posted: Sat May 31, 2014 7:23 am
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

Re: parallelism of i2c and SPI

Posted: Sat May 31, 2014 12:02 pm
by lesto
thank you,
and how about check if SPI connection was on error? how can i tell if returned data was valid?

Re: parallelism of i2c and SPI

Posted: Sat May 31, 2014 12:17 pm
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

Re: parallelism of i2c and SPI

Posted: Mon Jun 02, 2014 2:13 pm
by lesto
thank you very much