From da27e37a4cb16af483e765beb7f55bcc58d981d8 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Tue, 29 Apr 2014 15:01:24 +0100 Subject: [PATCH 1/4] pinmap: Defining pindata structures * Logic for setting up required multiplexers Signed-off-by: Thomas Ingleby --- api/maa.h | 31 +++++++++++++++++++++++++++++++ src/maa.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/api/maa.h b/api/maa.h index fe4dd86..67f0248 100644 --- a/api/maa.h +++ b/api/maa.h @@ -49,6 +49,37 @@ typedef enum { MAA_ERROR_UNSPECIFIED = 99 } maa_result_t; +typedef unsigned int maa_boolean_t; + +typedef union { + struct { + maa_boolean_t valid:1; + maa_boolean_t gpio:1; + maa_boolean_t pwm:1; + maa_boolean_t fast_gpio:1; + maa_boolean_t spi:1; + maa_boolean_t i2c:1; + }; + int raw; +} maa_pincapabilities_t; + +typedef struct { + unsigned int pin; + unsigned int value; +} maa_mux_t; + +typedef struct { + char name[8];// do we need this + unsigned int pin; + int parent_id; + maa_pincapabilities_t capabilites; + maa_mux_t mux[4]; + unsigned int mux_total; +} maa_pininfo; + +unsigned int maa_check_gpio(int pin); +unsigned int maa_check_aio(int pin); + /** Get the version string of maa autogenerated from git tag * * The version returned may not be what is expected however it is a reliable diff --git a/src/maa.c b/src/maa.c index d2c0acd..8301017 100644 --- a/src/maa.c +++ b/src/maa.c @@ -22,11 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include "maa.h" +#include "gpio.h" #include "version.h" +static maa_pininfo* pindata; + const char * maa_get_version() { return gVERSION; } + +maa_result_t +maa_init() +{ + return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; +} + +unsigned int +maa_check_gpio(int pin){ + + if(pindata == NULL) { + return -1; + } + //Check in gpio bounds? + if(pindata[pin].mux_total > 0) { + int mi; + for(mi = 0; mi < pindata[pin].mux_total; mi++) { + //Do we want to keep the gpio object around + //I dont think so + maa_gpio_context* mux_i; + //TODO CHANGE TO RAW + mux_i = maa_gpio_init(pindata[pin].mux[mi].pin); + maa_gpio_dir(mux_i, "out"); + maa_gpio_write(mux_i, pindata[pin].mux[mi].value); + } + } + return pindata[pin].pin +} + From d4427ec35e259debfbb91351e64df48152736543 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Wed, 30 Apr 2014 11:13:36 +0100 Subject: [PATCH 2/4] pinmap: More work around abstracting pin information Signed-off-by: Thomas Ingleby --- api/maa.h | 1 + src/maa.c | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/api/maa.h b/api/maa.h index 67f0248..589209a 100644 --- a/api/maa.h +++ b/api/maa.h @@ -79,6 +79,7 @@ typedef struct { unsigned int maa_check_gpio(int pin); unsigned int maa_check_aio(int pin); +unsigned int maa_check_pwm(int pin); /** Get the version string of maa autogenerated from git tag * diff --git a/src/maa.c b/src/maa.c index 8301017..6b772be 100644 --- a/src/maa.c +++ b/src/maa.c @@ -42,6 +42,18 @@ maa_init() return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; } +static maa_result_t +maa_setup_mux_mapped(maa_pininfo meta) +{ + int mi; + for(mi = 0; mi < meta.mux_total; mi++) { + maa_gpio_context* mux_i; + mux_i = maa_gpio_init_raw(meta.mux[mi].pin); + maa_gpio_dir(mux_i, MAA_GPIO_OUT); + maa_gpio_write(mux_i, meta.mux[mi].value); + } +} + unsigned int maa_check_gpio(int pin){ @@ -49,18 +61,10 @@ maa_check_gpio(int pin){ return -1; } //Check in gpio bounds? - if(pindata[pin].mux_total > 0) { - int mi; - for(mi = 0; mi < pindata[pin].mux_total; mi++) { - //Do we want to keep the gpio object around - //I dont think so - maa_gpio_context* mux_i; - //TODO CHANGE TO RAW - mux_i = maa_gpio_init(pindata[pin].mux[mi].pin); - maa_gpio_dir(mux_i, "out"); - maa_gpio_write(mux_i, pindata[pin].mux[mi].value); - } - } - return pindata[pin].pin + if(pindata[pin].mux_total > 0) + if(maa_setup_mux_mapped(pindata[pin]) != MAA_SUCCESS) + return -1; + + return pindata[pin].pin; } From e96df16f75c0d0974938e8d8877f145350012303 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Thu, 1 May 2014 16:55:23 +0100 Subject: [PATCH 3/4] pinmap: GPIO pin map added. * maa_gpio_init can take the IO number read physically off the board. * maa_check_gpio will also set up mutiplexers if needed * Intel Galileo Rev D board data added Signed-off-by: Thomas Ingleby --- api/maa.h | 23 ++++-- include/intel_galileo_rev_d.h | 32 ++++++++ src/CMakeLists.txt | 2 + src/gpio/gpio.c | 10 ++- src/intel_galileo_rev_d.c | 144 ++++++++++++++++++++++++++++++++++ src/maa.c | 37 +++++---- 6 files changed, 224 insertions(+), 24 deletions(-) create mode 100644 include/intel_galileo_rev_d.h create mode 100644 src/intel_galileo_rev_d.c diff --git a/api/maa.h b/api/maa.h index 589209a..2e33754 100644 --- a/api/maa.h +++ b/api/maa.h @@ -45,23 +45,23 @@ typedef enum { MAA_ERROR_INVALID_RESOURCE = 7, MAA_ERROR_INVALID_QUEUE_TYPE = 8, MAA_ERROR_NO_DATA_AVAILABLE = 9, + MAA_ERROR_INVALID_PLATFORM = 10, + MAA_ERROR_PLATFORM_NOT_INITIALISED = 11, MAA_ERROR_UNSPECIFIED = 99 } maa_result_t; typedef unsigned int maa_boolean_t; -typedef union { - struct { +typedef struct { maa_boolean_t valid:1; maa_boolean_t gpio:1; maa_boolean_t pwm:1; maa_boolean_t fast_gpio:1; maa_boolean_t spi:1; maa_boolean_t i2c:1; - }; - int raw; -} maa_pincapabilities_t; + } + maa_pincapabilities_t; typedef struct { unsigned int pin; @@ -75,11 +75,18 @@ typedef struct { maa_pincapabilities_t capabilites; maa_mux_t mux[4]; unsigned int mux_total; -} maa_pininfo; +} maa_pininfo_t; + +typedef struct { + unsigned int gpio_count; + unsigned int aio_count; + unsigned int pwm_count; + maa_pininfo_t* pins; +} maa_board_t; unsigned int maa_check_gpio(int pin); -unsigned int maa_check_aio(int pin); -unsigned int maa_check_pwm(int pin); +//unsigned int maa_check_aio(int pin); +//unsigned int maa_check_pwm(int pin); /** Get the version string of maa autogenerated from git tag * diff --git a/include/intel_galileo_rev_d.h b/include/intel_galileo_rev_d.h new file mode 100644 index 0000000..cff0cea --- /dev/null +++ b/include/intel_galileo_rev_d.h @@ -0,0 +1,32 @@ +/* + * Author: Thomas Ingleby + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#include "maa.h" + +#define MAA_INTEL_GALILEO_REV_D_PINCOUNT 25 + +maa_board_t* +maa_intel_galileo_rev_d(); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 74051a3..d920374 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,6 +10,7 @@ set (maa_LIB_HEADERS ${PROJECT_SOURCE_DIR}/api/pwm.h ${PROJECT_SOURCE_DIR}/include/smbus.hpp ${PROJECT_SOURCE_DIR}/include/smbus.h + ${PROJECT_SOURCE_DIR}/include/intel_galileo_rev_d.h ) set (maa_LIB_KERNEL @@ -25,6 +26,7 @@ set (maa_LIB_SRCS ${PROJECT_SOURCE_DIR}/src/i2c/i2c.c ${PROJECT_SOURCE_DIR}/src/i2c/smbus.c ${PROJECT_SOURCE_DIR}/src/pwm/pwm.c + ${PROJECT_SOURCE_DIR}/src/intel_galileo_rev_d.c # autogenerated version file ${CMAKE_CURRENT_BINARY_DIR}/version.c ) diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index 1a94362..0561570 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -44,13 +44,17 @@ maa_gpio_get_valfp(maa_gpio_context *dev) maa_gpio_context* maa_gpio_init(int pin) { - //TODO - return maa_gpio_init_raw(pin); + int pinm = maa_check_gpio(pin); + if (pinm < 0) + return NULL; + return maa_gpio_init_raw(pinm); } maa_gpio_context* maa_gpio_init_raw(int pin) { + if(pin < 0) + return NULL; FILE *export_f; maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context)); @@ -105,6 +109,8 @@ maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode) maa_result_t maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir) { + if (dev == NULL) + return MAA_ERROR_INVALID_HANDLE; if (dev->value_fp != NULL) { dev->value_fp = NULL; } diff --git a/src/intel_galileo_rev_d.c b/src/intel_galileo_rev_d.c new file mode 100644 index 0000000..b0a61f8 --- /dev/null +++ b/src/intel_galileo_rev_d.c @@ -0,0 +1,144 @@ +/* + * Author: Thomas Ingleby + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include "maa.h" + +maa_board_t* +maa_intel_galileo_rev_d() +{ + maa_board_t* b = (maa_board_t*) malloc(sizeof(maa_board_t)); + if(b == NULL) + return NULL; + + b->gpio_count = 14; + b->aio_count = 6; + b->pwm_count = 5; + b->pins = (maa_pininfo_t*) malloc(sizeof(maa_pininfo_t)*25); + + //GPIO + strncpy(b->pins[0].name, "IO0", 8); + b->pins[0].pin = 50; + b->pins[0].parent_id = 0; + b->pins[0].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0}; + b->pins[0].mux_total = 1; + b->pins[0].mux[0].pin = 40; + b->pins[0].mux[0].value = 1; + + strncpy(b->pins[1].name, "IO1", 8); + b->pins[1].pin = 51; + b->pins[1].parent_id = 0; + b->pins[1].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0}; + b->pins[1].mux_total = 1; + b->pins[1].mux[0].pin = 41; + b->pins[1].mux[0].value = 1; + + strncpy(b->pins[2].name, "IO2", 8); + b->pins[2].pin = 32; + b->pins[2].parent_id = 0; + b->pins[2].capabilites = (maa_pincapabilities_t) {1,1,0,1,0,0}; + b->pins[2].mux_total = 1; + b->pins[2].mux[0].pin = 31; + b->pins[2].mux[0].value = 1; + + strncpy(b->pins[3].name, "IO3", 8); + b->pins[3].pin = 18; + b->pins[3].parent_id = 0; + b->pins[3].capabilites = (maa_pincapabilities_t) {1,1,1,1,0,0}; + b->pins[3].mux_total = 1; + b->pins[3].mux[0].pin = 30; + b->pins[3].mux[0].value = 1; + + strncpy(b->pins[4].name, "IO4", 8); + b->pins[4].pin = 28; + b->pins[4].parent_id = 0; + b->pins[4].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0}; + b->pins[4].mux_total = 0; + + strncpy(b->pins[5].name, "IO5", 8); + b->pins[5].pin = 17; + b->pins[5].parent_id = 0; + b->pins[5].capabilites = (maa_pincapabilities_t) {1,1,1,0,0,0}; + b->pins[5].mux_total = 0; + + strncpy(b->pins[6].name, "IO6", 8); + b->pins[6].pin = 24; + b->pins[6].parent_id = 0; + b->pins[6].capabilites = (maa_pincapabilities_t) {1,1,1,0,0,0}; + b->pins[6].mux_total = 0; + + strncpy(b->pins[7].name, "IO7", 8); + b->pins[7].pin = 27; + b->pins[7].parent_id = 0; + b->pins[7].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0}; + b->pins[7].mux_total = 0; + + strncpy(b->pins[8].name, "IO8", 8); + b->pins[8].pin = 26; + b->pins[8].parent_id = 0; + b->pins[8].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0}; + b->pins[8].mux_total = 0; + + strncpy(b->pins[9].name, "IO9", 8); + b->pins[9].pin = 19; + b->pins[9].parent_id = 0; + b->pins[9].capabilites = (maa_pincapabilities_t) {1,1,1,0,0,0}; + b->pins[9].mux_total = 0; + + strncpy(b->pins[10].name, "IO10", 8); + b->pins[10].pin = 16; + b->pins[10].parent_id = 0; + b->pins[10].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0}; + b->pins[10].mux_total = 1; + b->pins[10].mux[0].pin = 42; + b->pins[10].mux[0].value = 1; + + strncpy(b->pins[11].name, "IO11", 8); + b->pins[11].pin = 25; + b->pins[11].parent_id = 0; + b->pins[11].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0}; + b->pins[11].mux_total = 1; + b->pins[11].mux[0].pin = 43; + b->pins[11].mux[0].value = 1; + + strncpy(b->pins[12].name, "IO12", 8); + b->pins[12].pin = 38; + b->pins[12].parent_id = 0; + b->pins[12].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0}; + b->pins[12].mux_total = 1; + b->pins[12].mux[0].pin = 54; + b->pins[12].mux[0].value = 1; + + strncpy(b->pins[13].name, "IO13", 8); + b->pins[13].pin = 39; + b->pins[13].parent_id = 0; + b->pins[13].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0}; + b->pins[13].mux_total = 1; + b->pins[13].mux[0].pin = 55; + b->pins[13].mux[0].value = 1; + + return b; +} diff --git a/src/maa.c b/src/maa.c index 6b772be..93e2d1c 100644 --- a/src/maa.c +++ b/src/maa.c @@ -1,5 +1,6 @@ /* * Author: Brendan Le Foll + * Author: Thomas Ingleby * Copyright (c) 2014 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -25,10 +26,12 @@ #include #include "maa.h" +#include "intel_galileo_rev_d.h" #include "gpio.h" #include "version.h" -static maa_pininfo* pindata; +static maa_pininfo_t* pindata; +static maa_board_t* plat; const char * maa_get_version() @@ -39,32 +42,38 @@ maa_get_version() maa_result_t maa_init() { - return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; + plat = maa_intel_galileo_rev_d(); + return MAA_SUCCESS; } static maa_result_t -maa_setup_mux_mapped(maa_pininfo meta) +maa_setup_mux_mapped(maa_pininfo_t meta) { int mi; for(mi = 0; mi < meta.mux_total; mi++) { maa_gpio_context* mux_i; mux_i = maa_gpio_init_raw(meta.mux[mi].pin); - maa_gpio_dir(mux_i, MAA_GPIO_OUT); - maa_gpio_write(mux_i, meta.mux[mi].value); + if(mux_i == NULL) + return MAA_ERROR_INVALID_HANDLE; + if(maa_gpio_dir(mux_i, MAA_GPIO_OUT) != MAA_SUCCESS) + return MAA_ERROR_INVALID_RESOURCE; + if(maa_gpio_write(mux_i, meta.mux[mi].value) != MAA_SUCCESS) + return MAA_ERROR_INVALID_RESOURCE; } + return MAA_SUCCESS; } unsigned int -maa_check_gpio(int pin){ - - if(pindata == NULL) { +maa_check_gpio(int pin) +{ + if(plat == NULL) return -1; - } - //Check in gpio bounds? - if(pindata[pin].mux_total > 0) - if(maa_setup_mux_mapped(pindata[pin]) != MAA_SUCCESS) - return -1; - return pindata[pin].pin; + if(pin < 0 || pin > plat->gpio_count) + return -1; + if(plat->pins[pin].mux_total > 0) + if(maa_setup_mux_mapped(plat->pins[pin]) != MAA_SUCCESS) + return -1; + return plat->pins[pin].pin; } From ea81a0ba754a81526ab2f0f7d41295962105dac4 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Thu, 1 May 2014 17:08:54 +0100 Subject: [PATCH 4/4] examples: Update examples to use newer maa functions * Use maa_init() before any other maa functions are called * Use ioNN instead of gpioXX, Using mapping Signed-off-by: Thomas Ingleby --- examples/blink-io8.c | 3 ++- examples/cycle-pwm3.c | 1 + examples/i2c_HMC5883L.c | 1 + examples/javascript/example.js | 2 +- examples/python/blink-io8.py | 3 ++- examples/python/cycle-pwm3.py | 1 + examples/python/hello_gpio.py | 3 ++- examples/python/readi2c.py | 1 + 8 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/blink-io8.c b/examples/blink-io8.c index a26aba9..6c960a5 100644 --- a/examples/blink-io8.c +++ b/examples/blink-io8.c @@ -29,10 +29,11 @@ int main(int argc, char **argv) { + maa_init(); fprintf(stdout, "MAA Version: %d\nStarting Blinking on IO8\n", maa_get_version()); maa_gpio_context* gpio; - gpio = maa_gpio_init(26); + gpio = maa_gpio_init(8); maa_gpio_dir(gpio, MAA_GPIO_OUT); while (1) { diff --git a/examples/cycle-pwm3.c b/examples/cycle-pwm3.c index f716cf9..0d67a31 100644 --- a/examples/cycle-pwm3.c +++ b/examples/cycle-pwm3.c @@ -29,6 +29,7 @@ int main () { + maa_init(); maa_pwm_context* pwm; pwm = maa_pwm_init(0, 3); if (pwm == NULL) { diff --git a/examples/i2c_HMC5883L.c b/examples/i2c_HMC5883L.c index 653c550..54f450f 100644 --- a/examples/i2c_HMC5883L.c +++ b/examples/i2c_HMC5883L.c @@ -78,6 +78,7 @@ int main(int argc, char **argv) { + maa_init(); float direction = 0; int16_t x = 0, y = 0, z = 0; char rx_tx_buf[MAX_BUFFER_LENGTH]; diff --git a/examples/javascript/example.js b/examples/javascript/example.js index 3ca12a4..3270e02 100644 --- a/examples/javascript/example.js +++ b/examples/javascript/example.js @@ -23,7 +23,7 @@ */ var m = require("maajs") - +m.init(); console.log("maa version: " + m.get_version()); var r = new m.I2C(20, 21); diff --git a/examples/python/blink-io8.py b/examples/python/blink-io8.py index 26ebe3c..5b4f99b 100644 --- a/examples/python/blink-io8.py +++ b/examples/python/blink-io8.py @@ -25,8 +25,9 @@ import pymaa as maa import time +maa.maa_init() x = maa.gpio_t() -maa.gpio_init(x, 26) +maa.gpio_init(x, 8) maa.gpio_dir(x, "out") while True: diff --git a/examples/python/cycle-pwm3.py b/examples/python/cycle-pwm3.py index 290d16f..0adc4b6 100644 --- a/examples/python/cycle-pwm3.py +++ b/examples/python/cycle-pwm3.py @@ -25,6 +25,7 @@ import pymaa as maa import time +maa.maa_init() x = maa.PWM(0,3) x.enable(1); x.period_us(20) diff --git a/examples/python/hello_gpio.py b/examples/python/hello_gpio.py index a5913f9..473156f 100644 --- a/examples/python/hello_gpio.py +++ b/examples/python/hello_gpio.py @@ -24,7 +24,8 @@ import pymaa +pumaa.maa_init() x = pymaa.gpio_t() print(x.pin) -pymaa.gpio_init(x, 20) +pymaa.gpio_init(x, 8) print(x.pin) diff --git a/examples/python/readi2c.py b/examples/python/readi2c.py index 438e259..626cfb7 100644 --- a/examples/python/readi2c.py +++ b/examples/python/readi2c.py @@ -24,6 +24,7 @@ import pymaa +pumaa.maa_init() x = pymaa.I2CSlave(27,28) x.address(0x62) y= " "