From 31bf0d5099d9c3b9538bc9f0c830fe344fd60f35 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 24 Apr 2017 19:38:59 -0500 Subject: [PATCH] spi: Added block-level SPI writes at the HAL level Poking the block-level SPI writes through the HAL level gives more freedom to targets to improve SPI transaction speed. --- drivers/SPI.cpp | 13 ++----------- hal/spi_api.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/drivers/SPI.cpp b/drivers/SPI.cpp index 945f8be52f..6b0c66bc78 100644 --- a/drivers/SPI.cpp +++ b/drivers/SPI.cpp @@ -79,20 +79,11 @@ int SPI::write(int value) { } 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; - } - } + int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length); unlock(); - - return total; + return ret; } void SPI::lock() { diff --git a/hal/spi_api.h b/hal/spi_api.h index 7b53373370..368e7b0e69 100644 --- a/hal/spi_api.h +++ b/hal/spi_api.h @@ -116,6 +116,23 @@ void spi_frequency(spi_t *obj, int hz); */ int spi_master_write(spi_t *obj, int value); +/** Write a block out in master mode and receive a value + * + * 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[in] obj The SPI peripheral to use for sending + * @param[in] tx_buffer Pointer to the byte-array of data to write to the device + * @param[in] tx_length Number of bytes to write, may be zero + * @param[in] rx_buffer Pointer to the byte-array of data to read from the device + * @param[in] 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. + */ +int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length); + /** Check if a value is available to read * * @param[in] obj The SPI peripheral to check