Skip to content

Commit 0a9ff20

Browse files
author
deepikabhavnani
committed
Cleanup SPI constructor and add destructor
_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(...); } ```
1 parent 920db63 commit 0a9ff20

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

drivers/SPI.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
4141
_write_fill(SPI_FILL_CHAR)
4242
{
4343
// No lock needed in the constructor
44-
4544
spi_init(&_spi, mosi, miso, sclk, ssel);
46-
_acquire();
45+
}
46+
47+
SPI::~SPI()
48+
{
49+
if (_owner == this) {
50+
_owner = NULL;
51+
}
4752
}
4853

4954
void SPI::format(int bits, int mode)

drivers/SPI.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class SPI : private NonCopyable<SPI> {
8787
* @param ssel SPI chip select pin
8888
*/
8989
SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC);
90+
virtual ~SPI();
9091

9192
/** Configure the data transmission format
9293
*
@@ -272,11 +273,6 @@ class SPI : private NonCopyable<SPI> {
272273

273274
#endif
274275

275-
public:
276-
virtual ~SPI()
277-
{
278-
}
279-
280276
protected:
281277
spi_t _spi;
282278

0 commit comments

Comments
 (0)