Skip to content

Commit de89be3

Browse files
author
Deepika
committed
_acquire() function added & no duplication in format/freq calls
1. Private _acquire() function is added to avoid multiple locking/unlocking 2. format and frequency functions updated to use appropriate function calls instead of a aquire()
1 parent cbfb234 commit de89be3

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

drivers/SPI.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ 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+
spi_format(&_spi, _bits, _mode, 0);
47+
_owner = this;
4848
unlock();
4949
}
5050

5151
void SPI::frequency(int hz) {
5252
lock();
5353
_hz = hz;
54-
SPI::_owner = NULL; // Not that elegant, but works. rmeyer
55-
aquire();
54+
spi_frequency(&_spi, _hz);
55+
_owner = this;
5656
unlock();
5757
}
5858

@@ -70,17 +70,26 @@ void SPI::aquire() {
7070
unlock();
7171
}
7272

73+
// Note: Private function with no locking
74+
void SPI::_acquire() {
75+
if (_owner != this) {
76+
spi_format(&_spi, _bits, _mode, 0);
77+
spi_frequency(&_spi, _hz);
78+
_owner = this;
79+
}
80+
}
81+
7382
int SPI::write(int value) {
7483
lock();
75-
aquire();
84+
_acquire();
7685
int ret = spi_master_write(&_spi, value);
7786
unlock();
7887
return ret;
7988
}
8089

8190
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
8291
lock();
83-
aquire();
92+
_acquire();
8493
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
8594
unlock();
8695
return ret;
@@ -167,7 +176,7 @@ int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, i
167176

168177
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)
169178
{
170-
aquire();
179+
_acquire();
171180
_callback = callback;
172181
_irq.callback(&SPI::irq_handler_asynch);
173182
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
@@ -270,6 +270,12 @@ class SPI {
270270
int _bits;
271271
int _mode;
272272
int _hz;
273+
274+
private:
275+
/* Private acquire fucntion without locking/unlocking
276+
* Implemented in order to avoid duplicate locking and boost performance
277+
*/
278+
void _acquire(void);
273279
};
274280

275281
} // namespace mbed

0 commit comments

Comments
 (0)