EXT initiated SPI

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
king2
Posts: 98
Joined: Mon May 28, 2012 1:57 pm
Been thanked: 2 times

EXT initiated SPI

Postby king2 » Fri Jun 09, 2017 11:19 pm

Hi!
I working with device that makes interrupt when measurement is ready by rising its INT pin (connected to stm32f411's EXTI pin).
When device has finished measurement and set its INT pin, I need to get new data via SPI in ~300us, accurately, ~2000 times per second, without missing any data.

I set up EXTI with EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART and callback:

Code: Select all

void exti_callback(EXTDriver *extp, expchannel_t channel) {
    spiSelectI(&SPID1);
    spiStartExchangeI(&SPID1, 17, &mems_read_cmd, &mems_data);
}

void spi_callback(SPIDriver *spip) {
    spiUnselectI(spip);
}

But it not works as expected :(

As I need to configure it synchronously, I start SPI without callback, configure it register by register with spiSend (turning on interrupts, and getting INT pin high immediately). After this I re-initialize SPI with callback, initialize EXT (that missed rising edge).
Finally, I get data from device, after this INT pin goes low, and everything works... till I'm trying to enter to endless main loop. At this point it finishes to work with SV#8.

If I add two chThdSleepMilliseconds(3000) just before loop, one after another, it works while first chThdSleepMilliseconds will end, then SV#8 again.

Can you help me with this?
Maybe there is a more suitable way to get data via SPI by external interrupt?
Should I surround spiSelectI/spiStartExchangeI in exti callback with lock/unlock? If yes, which function (exactly) should I use?
I have tried chSysLockFromISR() with no luck..

Thank you in advance!

p.s. After returning priorities of EXTI and SPI interrupt back to default levels, it not raises SV#8. Just finishes exchange when main loop starts to work (I have only sleep inside it). After number of data exchanges, INT goes high, but EXTI callback not raised, at least I cannot see CS lowered.
Then I see ~8ms delay, then SPI exchange (INT goes low, but CS do not goes high - spi_callback was not called). After this all exchange locks, ChibiOS just repeatedly calls chThdSleepMilliseconds in main loop. Maybe this is a time when idle thread was created?..
I need not just to collect data, but also to write data to SD card, without any data losses... and I afraid that SDC will make same problem with callback delaying.

Is there a way to make exti and spi callbacks to be run with absolutely highest priority?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: EXT initiated SPI

Postby Giovanni » Sat Jun 10, 2017 7:33 am

Hi,

You need to put chSysLockFromISR()/chSysUnlockFromISR() around calls to I-class functions. See "kernel concepts" in the RT reference manual.

Giovanni

king2
Posts: 98
Joined: Mon May 28, 2012 1:57 pm
Been thanked: 2 times

Re: EXT initiated SPI

Postby king2 » Sun Jun 11, 2017 1:52 am

I have tried this with no luck.
Everything started to work after moving sensor initialization to thread instead of 'before main loop' section.


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: Bing [Bot] and 8 guests