mirror of https://github.com/ARMmbed/mbed-os.git
spi: Added block-level SPI writes to the C++ api
virtual int write(const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length);
The main benefit of block-level SPI writes is the performance
improvement from not acquiring a mutex lock between each byte sent on
the SPI bus. The block write may also be poked through the hal level for
additional speed improvements.
pull/4207/head
parent
186f406c24
commit
58491aa244
|
|
@ -78,6 +78,23 @@ int SPI::write(int value) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
|
||||
int total = (tx_length > rx_length) ? tx_length : rx_length;
|
||||
|
||||
lock();
|
||||
aquire();
|
||||
for (int i = 0; i < total; i++) {
|
||||
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
|
||||
char in = spi_master_write(&_spi, out);
|
||||
if (i < rx_length) {
|
||||
rx_buffer[i] = in;
|
||||
}
|
||||
}
|
||||
unlock();
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
void SPI::lock() {
|
||||
_mutex->lock();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,9 +115,25 @@ public:
|
|||
*
|
||||
* @returns
|
||||
* Response from the SPI slave
|
||||
*/
|
||||
*/
|
||||
virtual int write(int value);
|
||||
|
||||
/** Write to the SPI Slave and obtain the response
|
||||
*
|
||||
* The total number of bytes sent and recieved will be the maximum of
|
||||
* tx_length and rx_length. The bytes written will be padded with the
|
||||
* value 0xff.
|
||||
*
|
||||
* @param tx_buffer Pointer to the byte-array of data to write to the device
|
||||
* @param tx_length Number of bytes to write, may be zero
|
||||
* @param rx_buffer Pointer to the byte-array of data to read from the device
|
||||
* @param rx_length Number of bytes to read, may be zero
|
||||
* @returns
|
||||
* The number of bytes written and read from the device. This is
|
||||
* maximum of tx_length and rx_length.
|
||||
*/
|
||||
virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
|
||||
|
||||
/** Acquire exclusive access to this SPI bus
|
||||
*/
|
||||
virtual void lock(void);
|
||||
|
|
|
|||
Loading…
Reference in New Issue