-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add a safeguard against declaring multiple variables for the same SPI device #473
Comments
Honestly, I am starting to have doubts on the whole |
@stevstrong you are right that was introduced a while back for the setModule stuff. About changing the pointers, another solution could be to declare static variables for the pointer, like I did for spi_this, have an spi1_onRx, spi1_onTx, spi1_onTRX, and another set for spi2 and spi3, and take them out of _currentSetting. And when the spi port2 ISR is called, it will call myCallBack function, no matter how may objects there are using SPI2, and no matter if the object that was used for setting the callback has later been changed to control a different port. |
We could add the callback pointer (to function) to the Settings class so can save that as part of the currentSettings, so that when you change settings it changes the callback function (if its not null) |
@rogerclarkmelbourne currently it's like that, and that's the cause of the problem.
myCallBack will not be called, because when the DMA finishes and call the SPI callback function, that function will read the callback from the currentSettings, but at this point currentSettings has changed to use the port 1, and the callback (myCallBack) has not been set in those settings, but in the ones for port 2. |
setModule() in actual form has the issue with bus sharing (several devices on the same bus). |
I think I will merge the version from my repo which has already implemented ISR pointers assigned for each port in part, which would solve this issue. |
Hello,
My original issue was that my onReceive callback was not called. I've finally realized it's because I've declared a variable SPIClass spi(1), whereas there was one inside the library already: SPIClass SPI(1). The pointer to instance (_spi1_this) was rewritten, causing bugs when I called SPI functions through my variable, but interrupt handler used library variable.
Maybe the variable declared in the library is worth removing? Or, if it breaks existing code, add a safeguard to fail early when user declares another variable for using SPI 1?
I'm leaving the original text for historical purposes. Here it goes:
The text was updated successfully, but these errors were encountered: