From a8d7473a5b291e05f8eb85bfb3bd580705490195 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Tue, 20 May 2014 16:55:27 +0100 Subject: [PATCH] spi: completed implementation. * Added more functions for transaction settings. Signed-off-by: Thomas Ingleby Signed-off-by: Brendan Le Foll --- api/spi.h | 25 +++++++++++++++++++++++-- src/spi/spi.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/api/spi.h b/api/spi.h index 12d7426..0ac13f7 100644 --- a/api/spi.h +++ b/api/spi.h @@ -44,12 +44,13 @@ typedef struct _spi* maa_spi_context; /** Initialise SPI_context, uses board mapping. Sets the muxes * + * @param bus to use, as listed in platform definition. Normally 0 * @return maa_spi_context The returned initialised SPI context */ -maa_spi_context maa_spi_init(); +maa_spi_context maa_spi_init(int bus); /** Set the SPI device mode. see spidev - * + * 0-3. * @param spi the spi device context * @param mode the mode. See Linux spidev * @@ -85,6 +86,26 @@ uint8_t maa_spi_write(maa_spi_context dev, uint8_t data); */ uint8_t* maa_spi_write_buf(maa_spi_context dev, uint8_t* data, int length); +/** + * + * @param dev spi context + * @param lsb. Use least significant bit transmission. 0 for msbi + * + * @return maa result of operation + */ +maa_result_t +maa_spi_lsbmode(maa_spi_context dev, maa_boolean_t lsb) + +/** Set bits per mode on transaction + * Defaults at 8. + * + * @param dev spi context + * @param bits bits per word + * + * @return Result of operation + */ +maa_result_t +maa_spi_bit_per_word(maa_spi_context dev, unsigned int bits) /** De-inits an maa_spi_context device * diff --git a/src/spi/spi.c b/src/spi/spi.c index d73b819..f1b6255 100644 --- a/src/spi/spi.c +++ b/src/spi/spi.c @@ -40,6 +40,10 @@ struct _spi { /*@{*/ int devfd; /**< File descriptor to SPI Device */ + int mode; /**< Spi mode see spidev.h */ + int clock; /**< clock to run transactions at */ + maa_boolean_t lsb; /**< least significant bit mode */ + unsigned int bpw; /**< Bits per word */ /*@}*/ }; @@ -63,6 +67,10 @@ maa_spi_init(int bus) free(dev); return NULL; } + dev->bpw = 8; + dev->clock = 4000000; + dev->lsb = 0; + dev->mode = 0; return dev; } @@ -70,13 +78,37 @@ maa_spi_init(int bus) maa_result_t maa_spi_mode(maa_spi_context dev, unsigned short mode) { - return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; + dev->mode = mode; + return MAA_SUCCESS; } maa_result_t maa_spi_frequency(maa_spi_context dev, int hz) { - return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; + dev->clock = hz; + return MAA_SUCCESS; +} + +maa_result_t +maa_spi_lsbmode(maa_spi_context dev, maa_boolean_t lsb) +{ + uint8_t lsb_mode = 0; + if (lsb == 1) { + lsb_mode = 1; + } + if (ioctl (dev->devfd, SPI_IOC_WR_LSB_FIRST, &lsb_mode) < 0) { + fprintf(stderr, "Failed to set bit order\n"); + return MAA_ERROR_INVALID_RESOURCE; + } + dev->lsb = lsb; + return MAA_SUCCESS; +} + +maa_result_t +maa_spi_bit_per_word(maa_spi_context dev, unsigned int bits) +{ + dev->bpw = bits; + return MAA_SUCCESS; } uint8_t @@ -90,8 +122,8 @@ maa_spi_write(maa_spi_context dev, uint8_t data) uint8_t recv = 0; msg.tx_buf = (unsigned long) &data; msg.rx_buf = (unsigned long) &recv; - msg.speed_hz = 100000; - msg.bits_per_word = 8; + 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) { @@ -111,8 +143,8 @@ maa_spi_write_buf(maa_spi_context dev, uint8_t* data, int length) msg.tx_buf = (unsigned long) data; msg.rx_buf = (unsigned long) recv; - msg.speed_hz = 100000; - msg.bits_per_word = 8; + 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) { @@ -125,5 +157,6 @@ maa_spi_write_buf(maa_spi_context dev, uint8_t* data, int length) maa_result_t maa_spi_stop(maa_spi_context dev) { - return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; + close(dev->devfd); + return MAA_SUCCESS; }