diff --git a/api/mraa/spi.h b/api/mraa/spi.h index b2ca81c..ed64db0 100644 --- a/api/mraa/spi.h +++ b/api/mraa/spi.h @@ -96,7 +96,7 @@ mraa_result_t mraa_spi_frequency(mraa_spi_context dev, int hz); uint8_t mraa_spi_write(mraa_spi_context dev, uint8_t data); /** Write Buffer of bytes to the SPI device. The pointer return has to be - * free'd by the caller. + * free'd by the caller. It will return a NULL pointer in cases of error. * * @param dev The Spi context * @param data to send diff --git a/api/mraa/spi.hpp b/api/mraa/spi.hpp index 941f3e8..520698e 100644 --- a/api/mraa/spi.hpp +++ b/api/mraa/spi.hpp @@ -85,7 +85,8 @@ class Spi { } /** * Write buffer of bytes to SPI device The pointer return has to be - * free'd by the caller. + * free'd by the caller. It will return a NULL pointer in cases of + * error * * @param data buffer to send * @param length size of buffer to send diff --git a/src/spi/spi.c b/src/spi/spi.c index 6589eaf..7350261 100644 --- a/src/spi/spi.c +++ b/src/spi/spi.c @@ -172,27 +172,6 @@ mraa_spi_write(mraa_spi_context dev, uint8_t data) return recv; } -uint8_t* -mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length) -{ - struct spi_ioc_transfer msg; - memset(&msg, 0, sizeof(msg)); - - uint8_t* recv = malloc(sizeof(uint8_t) * length); - - msg.tx_buf = (unsigned long) data; - msg.rx_buf = (unsigned long) recv; - msg.speed_hz = dev->clock; - msg.bits_per_word = dev->bpw; - msg.delay_usecs = 0; - msg.len = length; - if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) { - syslog(LOG_ERR, "Failed to perform dev transfer"); - return NULL; - } - return recv; -} - mraa_result_t mraa_spi_transfer_buf(mraa_spi_context dev, uint8_t* data, uint8_t* rxbuf, int length) { @@ -212,6 +191,18 @@ mraa_spi_transfer_buf(mraa_spi_context dev, uint8_t* data, uint8_t* rxbuf, int l return MRAA_SUCCESS; } +uint8_t* +mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length) +{ + uint8_t* recv = malloc(sizeof(uint8_t) * length); + + if (mraa_spi_transfer_buf(dev, data, recv, length) != MRAA_SUCCESS) { + free(recv); + return NULL; + } + return recv; +} + mraa_result_t mraa_spi_stop(mraa_spi_context dev) {