Skip to content

Commit 28df3ae

Browse files
authored
Merge pull request #4635 from deepikabhavnani/spi_acquire
_acquire() function added & removed duplication in format/freq calls
2 parents 292b408 + 56f3868 commit 28df3ae

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

drivers/SPI.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,35 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
3636
// No lock needed in the constructor
3737

3838
spi_init(&_spi, mosi, miso, sclk, ssel);
39-
aquire();
39+
_acquire();
4040
}
4141

4242
void SPI::format(int bits, int mode) {
4343
lock();
4444
_bits = bits;
4545
_mode = mode;
46-
SPI::_owner = NULL; // Not that elegant, but works. rmeyer
47-
aquire();
46+
// If changing format while you are the owner than just
47+
// update format, but if owner is changed than even frequency should be
48+
// updated which is done by acquire.
49+
if (_owner == this) {
50+
spi_format(&_spi, _bits, _mode, 0);
51+
} else {
52+
_acquire();
53+
}
4854
unlock();
4955
}
5056

5157
void SPI::frequency(int hz) {
5258
lock();
5359
_hz = hz;
54-
SPI::_owner = NULL; // Not that elegant, but works. rmeyer
55-
aquire();
60+
// If changing format while you are the owner than just
61+
// update frequency, but if owner is changed than even frequency should be
62+
// updated which is done by acquire.
63+
if (_owner == this) {
64+
spi_frequency(&_spi, _hz);
65+
} else {
66+
_acquire();
67+
}
5668
unlock();
5769
}
5870

@@ -70,17 +82,26 @@ void SPI::aquire() {
7082
unlock();
7183
}
7284

85+
// Note: Private function with no locking
86+
void SPI::_acquire() {
87+
if (_owner != this) {
88+
spi_format(&_spi, _bits, _mode, 0);
89+
spi_frequency(&_spi, _hz);
90+
_owner = this;
91+
}
92+
}
93+
7394
int SPI::write(int value) {
7495
lock();
75-
aquire();
96+
_acquire();
7697
int ret = spi_master_write(&_spi, value);
7798
unlock();
7899
return ret;
79100
}
80101

81102
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
82103
lock();
83-
aquire();
104+
_acquire();
84105
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
85106
unlock();
86107
return ret;
@@ -167,7 +188,7 @@ int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, i
167188

168189
void SPI::start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event)
169190
{
170-
aquire();
191+
_acquire();
171192
_callback = callback;
172193
_irq.callback(&SPI::irq_handler_asynch);
173194
spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage);

drivers/SPI.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ class SPI : private NonCopyable<SPI> {
271271
int _bits;
272272
int _mode;
273273
int _hz;
274+
275+
private:
276+
/* Private acquire function without locking/unlocking
277+
* Implemented in order to avoid duplicate locking and boost performance
278+
*/
279+
void _acquire(void);
274280
};
275281

276282
} // namespace mbed

0 commit comments

Comments
 (0)