From 58491aa244d236a1ee41d222a8b6d90c3b33ec31 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 24 Apr 2017 11:19:40 -0500 Subject: [PATCH] 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. --- drivers/SPI.cpp | 17 +++++++++++++++++ drivers/SPI.h | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/drivers/SPI.cpp b/drivers/SPI.cpp index a3b66eee5b..945f8be52f 100644 --- a/drivers/SPI.cpp +++ b/drivers/SPI.cpp @@ -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(); } diff --git a/drivers/SPI.h b/drivers/SPI.h index 6eede8a797..ee177cecb2 100644 --- a/drivers/SPI.h +++ b/drivers/SPI.h @@ -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);