-
Notifications
You must be signed in to change notification settings - Fork 3k
Cleanup SPI constructor and destructor #8090
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
Conversation
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.
This constructor is a bit weird. Why is the constructor doing the _acquire()
at all? We're not doing any operation now, so why try to claim the bus to set the format?
And, actually, doing the _acquire
without the lock isn't safe - the comment is incorrect, as the lock is on a static thing, not just this object. This means construction of an SPI object isn't threadsafe versus an ongoing SPI operation.
So I think the _acquire()
should be removed from the constructor.
The constructor acquire was added in b09a87f, but that replaced some direct configuration which shouldn't have been happening in the first place.
The problem in #4969 can still happen whenever the first _acquire
occurs on write
or whatever, but that particular problem is more clearly addressed by logic in the destructor, as the original reporter suggested. However I think it should be conditional:
if (_owner == this) {
_owner = NULL;
}
That's the actual point at which _owner
becomes a stale pointer.
As I understand your comment even that direct configuration of format / frequency should not be done since we are not doing any operation.
Which should be
? |
I can add a destructor, but it will not be actually freeing SPI bus (till new SPI HAL is in place). |
cbb66e6
to
c9951f1
Compare
_acquire() is not required in constructor, since we are not performing any operation on SPI bus yet. Just initialize the pins/hw Destructor is required to clear _owner else SPI format/frequency will not be set if object is recreated. We do not free SPI bus, but init again in hardware may or may not change frequency/format. ``` { SPI spi1(...); spi1.transfer(...); } { SPI spi1(...); spi1.transfer(...); } ```
c9951f1
to
0a9ff20
Compare
Yes, your sequence for the b09a87f scenario looks correct to me. I think taking the acquire out of the constructor is also a Good Thing because it will help ensure (a little) that the other methods are actually doing their job and acquiring properly. |
GitHub comment/title should be updated with new commit message. |
This simple fix is now ready for CI cc @ithinuel (already requested for review). |
/morph build |
Build : SUCCESSBuild number : 3227 Triggering tests/morph test |
Test : SUCCESSBuild number : 3030 |
/morph export-build |
Exporter Build : FAILUREBuild number : 2813 |
/morph export-build |
Exporter Build : FAILUREBuild number : 2817 |
/morph export-build |
Exporter Build : SUCCESSBuild number : 2820 |
Description
The SPI class has a member "static SPI *_owner;" to indicate need to set SPI parameters or not. Clear the _owner in constructor to allow SPI to initialize everytime a object is constructed.
Resolves: #4969
Pull request type