From 5af11978caedd2b2028da92eb7590a3a2777b9c6 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Tue, 13 Jan 2015 17:20:54 +0000 Subject: [PATCH] spi: add mraa_spi_init_raw This lets you access a spidev device directly without any checking in the style of mraa_i2c_init_raw Signed-off-by: Brendan Le Foll --- api/mraa/spi.h | 9 +++++++++ src/spi/spi.c | 26 ++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/api/mraa/spi.h b/api/mraa/spi.h index 4a69371..0b7a993 100644 --- a/api/mraa/spi.h +++ b/api/mraa/spi.h @@ -71,6 +71,15 @@ typedef struct _spi* mraa_spi_context; mraa_spi_context mraa_spi_init(int bus); /** + * Initialise SPI_context without any board configuration, selects a bus and a mux. + * + * @param bus Bus to use as listed by spidev + * @param cs Chip select to use as listed in spidev + * @return Spi context or NULL + */ +mraa_spi_context mraa_spi_init_raw(unsigned int bus, unsigned int cs); + +/** * Set the SPI device mode. see spidev 0-3. * * @param dev The Spi context diff --git a/src/spi/spi.c b/src/spi/spi.c index 7739ce2..e402143 100644 --- a/src/spi/spi.c +++ b/src/spi/spi.c @@ -103,7 +103,22 @@ mraa_spi_init(int bus) return NULL; } } + mraa_spi_context dev = mraa_spi_init_raw(plat->spi_bus[bus].bus_id, plat->spi_bus[bus].slave_s); + if (advance_func->spi_init_post != NULL) { + mraa_result_t ret = advance_func->spi_init_post(dev); + if (ret != MRAA_SUCCESS) { + free(dev); + return NULL; + } + } + + return dev; +} + +mraa_spi_context +mraa_spi_init_raw(unsigned int bus, unsigned int cs) +{ mraa_spi_context dev = (mraa_spi_context) malloc(sizeof(struct _spi)); if (dev == NULL) { syslog(LOG_CRIT, "spi: Failed to allocate memory for context"); @@ -112,8 +127,7 @@ mraa_spi_init(int bus) memset(dev, 0, sizeof(struct _spi)); char path[MAX_SIZE]; - sprintf(path, "/dev/spidev%u.%u", - plat->spi_bus[bus].bus_id, plat->spi_bus[bus].slave_s); + sprintf(path, "/dev/spidev%u.%u", bus, cs); dev->devfd = open(path, O_RDWR); if (dev->devfd < 0) { @@ -126,14 +140,6 @@ mraa_spi_init(int bus) dev->lsb = 0; dev->mode = 0; - if (advance_func->spi_init_post != NULL) { - mraa_result_t ret = advance_func->spi_init_post(dev); - if (ret != MRAA_SUCCESS) { - free(dev); - return NULL; - } - } - return dev; } -- 2.7.4