Skip to content
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

_acquire() function added & removed duplication in format/freq calls #4635

Merged
merged 3 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions drivers/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,35 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
// No lock needed in the constructor

spi_init(&_spi, mosi, miso, sclk, ssel);
aquire();
_acquire();
}

void SPI::format(int bits, int mode) {
lock();
_bits = bits;
_mode = mode;
SPI::_owner = NULL; // Not that elegant, but works. rmeyer
aquire();
// If changing format while you are the owner than just
// 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();
}

void SPI::frequency(int hz) {
lock();
_hz = hz;
SPI::_owner = NULL; // Not that elegant, but works. rmeyer
aquire();
// If changing format while you are the owner than just
// 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();
}

Expand All @@ -70,17 +82,26 @@ void SPI::aquire() {
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) {
lock();
aquire();
_acquire();
int ret = spi_master_write(&_spi, value);
unlock();
return ret;
}

int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
lock();
aquire();
_acquire();
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
unlock();
return ret;
Expand Down Expand Up @@ -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)
{
aquire();
_acquire();
_callback = callback;
_irq.callback(&SPI::irq_handler_asynch);
spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage);
Expand Down
6 changes: 6 additions & 0 deletions drivers/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ class SPI {
int _bits;
int _mode;
int _hz;

private:
/* Private acquire fucntion without locking/unlocking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo fucntion

* Implemented in order to avoid duplicate locking and boost performance
*/
void _acquire(void);
};

} // namespace mbed
Expand Down