SPI callback with parameters possible?
Posted: Sat Feb 08, 2014 10:47 pm
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
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