Merge pull request #4635 from deepikabhavnani/spi_acquire

_acquire() function added & removed duplication in format/freq calls
pull/4736/head
Anna Bridge 2017-07-07 12:41:34 +01:00 committed by GitHub
commit 28df3aeb87
2 changed files with 35 additions and 8 deletions

View File

@ -36,23 +36,35 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
// No lock needed in the constructor // No lock needed in the constructor
spi_init(&_spi, mosi, miso, sclk, ssel); spi_init(&_spi, mosi, miso, sclk, ssel);
aquire(); _acquire();
} }
void SPI::format(int bits, int mode) { void SPI::format(int bits, int mode) {
lock(); lock();
_bits = bits; _bits = bits;
_mode = mode; _mode = mode;
SPI::_owner = NULL; // Not that elegant, but works. rmeyer // If changing format while you are the owner than just
aquire(); // update format, but if owner is changed than even frequency should be
// updated which is done by acquire.
if (_owner == this) {
spi_format(&_spi, _bits, _mode, 0);
} else {
_acquire();
}
unlock(); unlock();
} }
void SPI::frequency(int hz) { void SPI::frequency(int hz) {
lock(); lock();
_hz = hz; _hz = hz;
SPI::_owner = NULL; // Not that elegant, but works. rmeyer // If changing format while you are the owner than just
aquire(); // update frequency, but if owner is changed than even frequency should be
// updated which is done by acquire.
if (_owner == this) {
spi_frequency(&_spi, _hz);
} else {
_acquire();
}
unlock(); unlock();
} }
@ -70,9 +82,18 @@ void SPI::aquire() {
unlock(); unlock();
} }
// Note: Private function with no locking
void SPI::_acquire() {
if (_owner != this) {
spi_format(&_spi, _bits, _mode, 0);
spi_frequency(&_spi, _hz);
_owner = this;
}
}
int SPI::write(int value) { int SPI::write(int value) {
lock(); lock();
aquire(); _acquire();
int ret = spi_master_write(&_spi, value); int ret = spi_master_write(&_spi, value);
unlock(); unlock();
return ret; return ret;
@ -80,7 +101,7 @@ int SPI::write(int value) {
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) { int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
lock(); lock();
aquire(); _acquire();
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length); int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
unlock(); unlock();
return ret; return ret;
@ -167,7 +188,7 @@ int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, i
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) 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)
{ {
aquire(); _acquire();
_callback = callback; _callback = callback;
_irq.callback(&SPI::irq_handler_asynch); _irq.callback(&SPI::irq_handler_asynch);
spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage); spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage);

View File

@ -271,6 +271,12 @@ protected:
int _bits; int _bits;
int _mode; int _mode;
int _hz; int _hz;
private:
/* Private acquire function without locking/unlocking
* Implemented in order to avoid duplicate locking and boost performance
*/
void _acquire(void);
}; };
} // namespace mbed } // namespace mbed