diff --git a/api/maa.h b/api/maa.h index 902f779..28ca177 100644 --- a/api/maa.h +++ b/api/maa.h @@ -48,10 +48,49 @@ typedef enum { MAA_ERROR_INVALID_RESOURCE = 7, /**< Resource invalid */ MAA_ERROR_INVALID_QUEUE_TYPE = 8, /**< Queue type incorrect */ MAA_ERROR_NO_DATA_AVAILABLE = 9, /**< No data available */ + MAA_ERROR_INVALID_PLATFORM = 10, /**< Platform not recognised */ + MAA_ERROR_PLATFORM_NOT_INITIALISED = 11, /**< Board information not initialised */ MAA_ERROR_UNSPECIFIED = 99 /**< Unknown Error */ } maa_result_t; +typedef unsigned int maa_boolean_t; + +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; + } + 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_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); + /** 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/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 1ed0d4a..c8fcafc 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(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= " " 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 4f2b6c1..faa5c4b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,7 @@ set (maa_LIB_HEADERS ${PROJECT_SOURCE_DIR}/api/aio.h ${PROJECT_SOURCE_DIR}/include/smbus.h ${PROJECT_SOURCE_DIR}/include/version.h + ${PROJECT_SOURCE_DIR}/include/intel_galileo_rev_d.h ) set (maa_LIB_KERNEL @@ -29,6 +30,7 @@ set (maa_LIB_SRCS ${PROJECT_SOURCE_DIR}/src/pwm/pwm.c ${PROJECT_SOURCE_DIR}/src/spi/spi.c ${PROJECT_SOURCE_DIR}/src/aio/aio.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 d2c0acd..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 @@ -22,11 +23,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include "maa.h" +#include "intel_galileo_rev_d.h" +#include "gpio.h" #include "version.h" +static maa_pininfo_t* pindata; +static maa_board_t* plat; + const char * maa_get_version() { return gVERSION; } + +maa_result_t +maa_init() +{ + plat = maa_intel_galileo_rev_d(); + return MAA_SUCCESS; +} + +static maa_result_t +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); + 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(plat == NULL) + return -1; + + 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; +} +