-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
drivers/periph/spi: added means to make SPI thread-safe #2290
drivers/periph/spi: added means to make SPI thread-safe #2290
Conversation
I think the memory bytes is a small price to pay for the thread safety of our device drivers. Errors caused by these kinds of race conditions will be extremely difficult to reproduce or locate when they bite you, adding a simple NOP somewhere completely unrelated could potentially make such a problem hide again. 👍 for the API, it is similar to what is being used in ChibiOS (which has been around for a very long time), so the general idea has already been tested thoroughly. I was thinking of whether to put |
I also think that the price is ok for the benefit we get. Should we maybe adapt the test? Or extend it? |
@gebart I wouldn't put the locking in a central file, as there might be platforms where the locking can be handled more efficient without mutexes or similar. Also the additional implementation effort is not too high... @PeterKietzmann I don't think we need to adapt the test, the Mutexes itself are well tested and I think the effort for a 'good' test are not worth it (as the locking implementation is quite straight forward...). |
Well, I would ACK this. Any other opinions? |
looks also good from my side |
@@ -123,6 +123,28 @@ int spi_init_slave(spi_t dev, spi_conf_t conf, char (*cb)(char data)); | |||
int spi_conf_pins(spi_t dev); | |||
|
|||
/** | |||
* @brief Get mutual access to the given SPI bus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mutual exclusive access?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
..."mutually exclusive"...
dc1ac66
to
2511e65
Compare
here is an update - the mutex initialization issue is fixed through static initialization - multiple calls to I decided against calling |
... and the new change even saved 24 bytes in ROM :-) |
Looks good to me. |
I am fine with this soultion and willing to ACK. But also I would like to hear @gebart 's optinion on it. |
@haukepetersen While it is still most likely a user error, I think it would be beneficial for developers to have the init function lock the mutex while initializing, to avoid any race conditions between two drivers, or between an application main thread and some data consumer thread. I don't really see why we should not lock the mutex for all of |
@gebart You are right, the overhead is limited and it could save some trouble. I was first reluctant against this idea since the major design goal was to keep the peripheral drivers as slim as possible - and every function call adds some more bytes to them... But I agree, the benefits for locking the init function are greater then the drawbacks of 2 function calls, so I will update this PR as soon as I have a minute. |
@haukepetersen I will update #2317 accordingly. |
@PeterKietzmann I think this is a good solution to the problem. The locking inside |
As the mutexes (mutices?) are statically initialised I thought usage would be spi_acquire(dev);
spi_init_master(dev, ...);
spi_release(dev); Wouldn't this result in a more consistent usage? |
@thomaseichinger It would make the API usage more consistent, yes. I am updating #2317 again. |
2511e65
to
3439247
Compare
I am completely for @thomaseichinger's solution! I think it is for (i) more consitent and it (ii) leaves it to the user to omit the locking for setups where I can sure that I am only using one SPI device for example... squashed and rebased - ready for merge? |
ACK |
ok, somehow Travis is complaining that the PR needs to be squashed, though the label was removed... Do we have a bug in our Travis script? |
Travis is happy |
finally. go then! |
drivers/periph/spi: added means to make SPI thread-safe
extended the SPI driver's API with locking/unlocking functions and implemented them exemplary for the
stm32f4
.These functions are needed for applications that share a SPI bus for more then one slave device, e.g. 2 sensors, while their drivers run in separate threads.
Usage:
The price to pay: