Skip to content

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

Merged
merged 1 commit into from
Oct 5, 2018

Conversation

deepikabhavnani
Copy link

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

[X] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Breaking change

Copy link
Contributor

@kjbracey kjbracey left a 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.

@deepikabhavnani
Copy link
Author

The constructor acquire was added in b09a87f, but that replaced some direct configuration which shouldn't have been happening in the first place.

As I understand your comment even that direct configuration of format / frequency should not be done since we are not doing any operation.
As per the commit message of b09a87f scenario was

SPI spi1(...);      // _owner is NULL, peripheral config is 1
spi1.transfer(...); // _owner is 1, config is 1
SPI spi2(...);      // _owner is 1, config is 2
spi1.transfer(...)  // 1 thinks it still owns peripheral, doesn't reconfigure

Which should be

SPI spi1(...);      // _owner is NULL, peripheral config is NULL
spi1.transfer(...); // _owner is 1, config is 1
SPI spi2(...);      // _owner is 1, config is 1 (Dont change config in constructor)
spi1.transfer(...)  // 1 thinks it still owns peripheral, doesn't reconfigure (No need)

?

@deepikabhavnani
Copy link
Author

deepikabhavnani commented Sep 12, 2018

The problem in #4969 can still happen whenever the first _acquire occurs on write or whatever

I can add a destructor, but it will not be actually freeing SPI bus (till new SPI HAL is in place).

_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(...);
}
```
@kjbracey
Copy link
Contributor

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.

@kjbracey
Copy link
Contributor

GitHub comment/title should be updated with new commit message.

@deepikabhavnani deepikabhavnani changed the title Clean owner variable before init Cleanup SPI constructor and destructor Sep 13, 2018
@0xc0170 0xc0170 requested a review from a team September 24, 2018 12:02
@0xc0170
Copy link
Contributor

0xc0170 commented Oct 1, 2018

This simple fix is now ready for CI

cc @ithinuel (already requested for review).

@NirSonnenschein
Copy link
Contributor

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 3, 2018

Build : SUCCESS

Build number : 3227
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/8090/

Triggering tests

/morph test
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 4, 2018

@NirSonnenschein
Copy link
Contributor

/morph export-build

@mbed-ci
Copy link

mbed-ci commented Oct 4, 2018

@NirSonnenschein
Copy link
Contributor

/morph export-build

@mbed-ci
Copy link

mbed-ci commented Oct 4, 2018

@deepikabhavnani
Copy link
Author

/morph export-build

@mbed-ci
Copy link

mbed-ci commented Oct 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants