Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: STM32L452_Nucleo_ticker
Fork of mbed-dev by
Diff: drivers/SPI.cpp
- Revision:
- 169:e3b6fe271b81
- Parent:
- 167:e84263d55307
- Child:
- 170:19eb464bc2be
--- a/drivers/SPI.cpp Thu Jul 06 15:42:05 2017 +0100
+++ b/drivers/SPI.cpp Wed Jul 19 17:31:21 2017 +0100
@@ -36,23 +36,35 @@
// 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();
}
@@ -70,9 +82,18 @@
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;
@@ -80,7 +101,7 @@
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;
@@ -167,7 +188,7 @@
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);
