From e7db61ec04c0440196abf2c034b9945b06c5a7a0 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 24 Apr 2017 11:19:40 -0500 Subject: [PATCH] Added block-level spi writes /** Write to the SPI Slave and obtain the response * * The total number of bytes sent and recieved will be the maximum of * outlength and inlength. The bytes written will be padded with the * value 0xff. * * @param outdata Pointer to the byte-array of data to write to the device * @param outlength Number of bytes to write * @param indata Pointer to the byte-array of data to read from the device * @param inlength Number of bytes to read from the device * @returns * The number of bytes written and read from the device. This is * maximum of outlength and inlength. */ virtual int write(const char *outdata, int outlength, char *indata, int inlength); --- 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 a3b66eee5b7..580feeffa61 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 *outdata, int outlength, char *indata, int inlength) { + int total = (outlength > inlength) ? outlength : inlength; + + lock(); + aquire(); + for (int i = 0; i < total; i++) { + char out = (i < outlength) ? outdata[i] : 0xff; + char in = spi_master_write(&_spi, out); + if (i < inlength) { + indata[i] = in; + } + } + unlock(); + + return total; +} + void SPI::lock() { _mutex->lock(); } diff --git a/drivers/SPI.h b/drivers/SPI.h index 866531e68bd..b17cc1a48b1 100644 --- a/drivers/SPI.h +++ b/drivers/SPI.h @@ -115,9 +115,25 @@ class SPI { * * @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 + * outlength and inlength. The bytes written will be padded with the + * value 0xff. + * + * @param outdata Pointer to the byte-array of data to write to the device + * @param outlength Number of bytes to write + * @param indata Pointer to the byte-array of data to read from the device + * @param inlength Number of bytes to read from the device + * @returns + * The number of bytes written and read from the device. This is + * maximum of outlength and inlength. + */ + virtual int write(const char *outdata, int outlength, char *indata, int inlength); + /** Acquire exclusive access to this SPI bus */ virtual void lock(void);