Page 1 of 1

SPI callback with parameters possible?

Posted: Sat Feb 08, 2014 10:47 pm
by goeck
Hey everyone,

At the moment I do SPI communication stuff with a LDC1000 sensor and the STM32F0-Discovery. First off I implemented the protocol mentioned in the datasheet using synchronous data transfers, all works.
Now I switched to asynchronous data transfer because I then can do other stuff during SPI communication, which actually also works great with one exception. So, the idea is, I start the data Exchange and lay the calling thread asleep. I then can make use of a callback function in case the communication finished successfully. Since I have a bunch of function adressing several needs (init SPI slave , write single register of SPI slave, read single register of SPI slave, read chunk of data from SPI slave,...) I have to have callbacks forch each function to do processing of gathered data, which may be different from function to function. I am not able to write a unified callback and from there call different handling routines (e.g. in a switch statement) by using a parameter that is handed over to the callback in any way. Is that true? I understood the SPI driver this way.
So my workaround is:
- have a calllback for each data exchanging starter function
- have the callback pointer in the SPI config variable set on the fly accordingly prior to restarting the SPI driver each time a data exchange needs to be started
- have global variables for use between callbacks and invoking function, that only one of all the callbacks need to access (several buffers and so on)
I can supply code examples of this if that's of interest.

So my intention was, why not pass a pointer or a ID from the function that invokes the SPI driver to the callback? Using a pointer the would enable the callback to do calculations on the SPI input buffer and then write strait into a meaningful global variable, that for instance keeps the normalized X component of measured acceleration data gathering data from an SPI bound accelerometer.
Using a integer value (used as ID) as parameter would allow at least the usage of one single SPI callback but knowing "from where" the callback was invoked and what the callback has to do with the gathered data stored in a global buffer variable.

How do you guys handle such cases right now?
Thanks for your thoughts.

Keep it up!
Stefan

Re: SPI callback with parameters possible?

Posted: Sun Feb 09, 2014 9:03 am
by Giovanni
If you have a different reaction to a callback depending on the system state then you are basically implementing a state machine. I would create a global "state" variable and design my states and arcs. A simple implementation: "state" would be the index of an array of function pointers.

Alternatively you may extend your SPIDriver structure using the SPI_DRIVER_EXT_FIELDS macro.

Giovanni

Re: SPI callback with parameters possible?

Posted: Mon Feb 10, 2014 9:28 am
by Giovanni
If your application is multi-threaded then the asynchronous mode is not mandatory at all, during DMA transfer the CPU is free to execute lower priority tasks.

All drivers are designed to not monopolize the CPU during slow operations unless you read "Polled" in the function name. The synchronous API is designed to be multi-threading friendly.

Giovanni

Re: SPI callback with parameters possible?

Posted: Mon Feb 10, 2014 9:37 am
by goeck
Uff,.. you where blazingly fast this morning, I should have known this actually... ;)

I deleted the post only minutes after posting, because I had another look into the spi.c/spi.h and compared both approaches. I saw that the synchronous function all use the _spi_wait_s() macro which led me to the insight that this way is actually the same as my asynchronous imlementation without a specific callback. For my needs, this is not a must. I do it this way now (e.g.):

Code: Select all

void ldc1000_read_data(uint16_t* prox, uint32_t* freq) {
   static uint8_t  spiBufferTx[6] = {LDC1000_CMD_PROXLSB | 0x80, 0,0,0,0,0};
               spiBufferRx[6];

   ldc1000_acquire_bus();
   spiExchange(&SPID1, 6, spiBufferTx, spiBufferRx);
   *prox = (uint16_t)(spiBufferRx[2] <<  8) | spiBufferRx[1];
   *freq = (uint32_t)(spiBufferRx[5] << 16) | (uint32_t)(spiBufferRx[4] << 8) | spiBufferRx[3];
   ldc1000_release_bus();
}

I guess that's alright. If I use callbacks here I would have more overhead than this solution is unelegant.

Sorry, I didn't mean to bug anyone with my thoughts.

Thanks for the help anyways, i really appreciate the support.
Kind regards
Stefan