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
Christopher Haster 2017-04-24 11:19:40 -05:00
parent 186f406c24
commit 58491aa244
2 changed files with 34 additions and 1 deletions

View File

@ -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();
}

View File

@ -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);