From e68a420f0a57cf7add88312a7084ea7228b85cb2 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 2 May 2014 13:53:20 +0100 Subject: [PATCH 1/5] i2c: removed gpio from context Signed-off-by: Thomas Ingleby --- api/i2c.h | 1 - src/maa.i | 1 - 2 files changed, 2 deletions(-) diff --git a/api/i2c.h b/api/i2c.h index 89c86c1..843c047 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -49,7 +49,6 @@ typedef struct { int hz; /**< frequency of communication */ int fh; /**< the file handle to the /dev/i2c-* device */ int addr; /**< the address of the i2c slave */ - maa_gpio_context gpio; /*@}*/ } maa_i2c_context; diff --git a/src/maa.i b/src/maa.i index 8100937..6390e30 100644 --- a/src/maa.i +++ b/src/maa.i @@ -60,7 +60,6 @@ typedef struct { int hz; /**< frequency of communication */ int fh; /**< the file handle to the /dev/i2c-* device */ int addr; /**< the address of the i2c slave */ - maa_gpio_context gpio; /*@}*/ } maa_i2c_context; From a8661e599cfdaea0999e658782c746ddecfa78f3 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 2 May 2014 14:24:08 +0100 Subject: [PATCH 2/5] i2c: generalising api * Added raw init function (maa_i2c_init_raw) for not using pinmap Signed-off-by: Thomas Ingleby --- api/i2c.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/i2c.h b/api/i2c.h index 843c047..fb1e391 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -52,8 +52,19 @@ typedef struct { /*@}*/ } maa_i2c_context; +/** Initialise i2c context, using board defintions + * + * @return maa_i2c_context i2c context ready for other calls. + */ maa_i2c_context* maa_i2c_init(); +/** Initialise i2c context, passing in spi bus to use. + * + * @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2" + * @return maa_i2c_context i2c context ready for other calls. + */ +maa_i2c_context* maa_i2c_init_raw(unsigned int bus); + /** Sets the frequency of the i2c context * * @param dev the i2c context From 82d3615f6c88839571c6626f12c50f982032acc5 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 2 May 2014 14:45:24 +0100 Subject: [PATCH 3/5] pinmap: Added spi and i2c to board definition. Signed-off-by: Thomas Ingleby --- api/maa.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/maa.h b/api/maa.h index 7a5d1be..6d6f2cc 100644 --- a/api/maa.h +++ b/api/maa.h @@ -107,6 +107,10 @@ typedef struct { unsigned int gpio_count; /**< GPIO Count */ unsigned int aio_count; /**< Analog In Count */ unsigned int pwm_count; /**< PWM Count */ + unsigned int i2c_bus_count; /**< Usable i2c Count */ + unsigned int i2c_bus[8]; /**< Array of i2c */ + unsigned int spi_bus_count; /**< Usable spi Count */ + double spi_bus[8]; /**< Array of spi */ maa_pininfo_t* pins; /**< Pointer to pin array */ /*@}*/ } maa_board_t; From 1005df165213964130acefc29acff9ba4eaa1d0e Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 2 May 2014 14:57:55 +0100 Subject: [PATCH 4/5] intel_galileo_rev_d: Update to new board definition * Added SPI and I2C information Signed-off-by: Thomas Ingleby --- src/intel_galileo_rev_d.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/intel_galileo_rev_d.c b/src/intel_galileo_rev_d.c index ae64f45..1def06b 100644 --- a/src/intel_galileo_rev_d.c +++ b/src/intel_galileo_rev_d.c @@ -37,6 +37,13 @@ maa_intel_galileo_rev_d() b->gpio_count = 14; b->aio_count = 6; b->pwm_count = 5; + + b->i2c_bus_count = 1; + b->i2c_bus[0] = 0; + + b->spi_bus_count = 1; + b->spi_bus[0] = 1.0; + b->pins = (maa_pininfo_t*) malloc(sizeof(maa_pininfo_t)*25); //GPIO From 64d3c78ca9c5fcf5de2eabd4cada86a4ea09bf5f Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 2 May 2014 16:07:18 +0100 Subject: [PATCH 5/5] i2c: Work started on using pinmap with i2c * Update examples and swig interface. Signed-off-by: Thomas Ingleby --- api/i2c.h | 3 ++- api/maa.h | 6 +++++- examples/i2c_HMC5883L.c | 2 +- src/i2c/i2c.c | 35 ++++++++++++++++++++++++++++++----- src/intel_galileo_rev_d.c | 22 ++++++++++++++++++++++ src/maa.c | 19 +++++++++++++++++++ src/maa.i | 2 +- 7 files changed, 80 insertions(+), 9 deletions(-) diff --git a/api/i2c.h b/api/i2c.h index fb1e391..2f0e650 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -54,9 +54,10 @@ typedef struct { /** Initialise i2c context, using board defintions * + * @param bus i2c bus to use * @return maa_i2c_context i2c context ready for other calls. */ -maa_i2c_context* maa_i2c_init(); +maa_i2c_context* maa_i2c_init(int bus); /** Initialise i2c context, passing in spi bus to use. * diff --git a/api/maa.h b/api/maa.h index 6d6f2cc..b6f89b7 100644 --- a/api/maa.h +++ b/api/maa.h @@ -138,7 +138,11 @@ unsigned int maa_check_gpio(int pin); */ unsigned int maa_check_aio(int pin); -//unsigned int maa_check_pwm(int pin); +/** Check i2c interface, sets up multiplexer on device. + * + * @return unsigned int if using /dev/i2c-2 returned would be 2 + */ +unsigned int maa_check_i2c(); /** Get the version string of maa autogenerated from git tag * diff --git a/examples/i2c_HMC5883L.c b/examples/i2c_HMC5883L.c index 54f450f..7e65c51 100644 --- a/examples/i2c_HMC5883L.c +++ b/examples/i2c_HMC5883L.c @@ -84,7 +84,7 @@ main(int argc, char **argv) char rx_tx_buf[MAX_BUFFER_LENGTH]; maa_i2c_context *i2c; - i2c = maa_i2c_init(); + i2c = maa_i2c_init(0); maa_i2c_address(i2c, HMC5883L_I2C_ADDR); rx_tx_buf[0] = HMC5883L_CONF_REG_B; diff --git a/src/i2c/i2c.c b/src/i2c/i2c.c index 01726d2..1fb601e 100644 --- a/src/i2c/i2c.c +++ b/src/i2c/i2c.c @@ -26,16 +26,41 @@ #include "smbus.h" maa_i2c_context* -maa_i2c_init() +maa_i2c_init(int bus) { + unsigned int checked_pin = maa_check_i2c(bus); + if (checked_pin < 0) { + switch(checked_pin) { + case -1: + fprintf(stderr, "No i2c on board\n"); + return NULL; + case -2: + fprintf(stderr, "Failed to set-up i2c multiplexer!\n"); + return NULL; + case -3: + fprintf(stderr, "Platform Not Initialised"); + return NULL; + default: return NULL; + } + } + return maa_i2c_init_raw(checked_pin); +} + +maa_i2c_context* +maa_i2c_init_raw(unsigned int bus) +{ + if (bus < 0) { + fprintf(stderr, "Bus -%u- bellow zero\n", bus); + return NULL; + } maa_i2c_context* dev = (maa_i2c_context*) malloc(sizeof(maa_i2c_context)); if (dev == NULL) return NULL; - // Galileo only has one I2C master which should be /dev/i2c-0 - // reliability is a fickle friend! - if ((dev->fh = open("/dev/i2c-0", O_RDWR)) < 1) { - fprintf(stderr, "Failed to open requested i2c port"); + char filepath[32]; + snprintf(filepath, 32, "/dev/i2c-%u", bus); + if ((dev->fh = open(filepath, O_RDWR)) < 1) { + fprintf(stderr, "Failed to open requested i2c port %s", filepath); } return dev; } diff --git a/src/intel_galileo_rev_d.c b/src/intel_galileo_rev_d.c index 1def06b..215bafe 100644 --- a/src/intel_galileo_rev_d.c +++ b/src/intel_galileo_rev_d.c @@ -200,5 +200,27 @@ maa_intel_galileo_rev_d() b->pins[19].mux[1].pin = 20; b->pins[19].mux[1].value = 0; + strncpy(b->pins[20].name, "I2C", 8); + b->pins[20].pin = 0; + b->pins[20].parent_id = 0; + b->pins[20].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,1,1}; + b->pins[20].mux_total = 1; + b->pins[20].mux[0].pin = 29; + b->pins[20].mux[0].value = 0; + + strncpy(b->pins[21].name, "SPI-1.0", 8); + b->pins[21].pin = 1; + b->pins[21].parent_id = 0; + b->pins[21].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,1,1}; + b->pins[21].mux_total = 4; + b->pins[21].mux[0].pin = 42; + b->pins[21].mux[0].value = 0; + b->pins[21].mux[1].pin = 43; + b->pins[21].mux[1].value = 0; + b->pins[21].mux[2].pin = 54; + b->pins[21].mux[2].value = 0; + b->pins[21].mux[3].pin = 55; + b->pins[21].mux[3].value = 0; + return b; } diff --git a/src/maa.c b/src/maa.c index d54756d..97f7619 100644 --- a/src/maa.c +++ b/src/maa.c @@ -97,3 +97,22 @@ maa_check_aio(int aio) return plat->pins[pin].pin; } + +unsigned int +maa_check_i2c(int bus) +{ + if (plat == NULL) + return -3; + + if (plat->i2c_bus_count >! 0) { + fprintf(stderr, "No i2c buses defined in platform"); + return -1; + } + int pin = (plat->gpio_count + plat->aio_count) + bus; + + if (plat->pins[pin].mux_total > 0) + if (maa_setup_mux_mapped(plat->pins[pin]) != MAA_SUCCESS) + return -2; + + return plat->pins[pin].pin; +} diff --git a/src/maa.i b/src/maa.i index 6390e30..a0ee5df 100644 --- a/src/maa.i +++ b/src/maa.i @@ -67,7 +67,7 @@ typedef struct { %extend maa_i2c_context { maa_i2c_context() { - return maa_i2c_init(); + return maa_i2c_init(0); } ~maa_i2c_context() {