Merge branch 'pinmap-aio'
authorThomas Ingleby <thomas.c.ingleby@intel.com>
Fri, 2 May 2014 10:56:02 +0000 (11:56 +0100)
committerThomas Ingleby <thomas.c.ingleby@intel.com>
Fri, 2 May 2014 10:56:02 +0000 (11:56 +0100)
api/aio.h
api/maa.h
examples/analogin_a0.c
src/aio/aio.c
src/intel_galileo_rev_d.c
src/maa.c

index 619b831..2bd10a8 100644 (file)
--- a/api/aio.h
+++ b/api/aio.h
  */
 
 #pragma once
+/** @file
+ *
+ * This file defines the aio (analog in) interface for libmaa
+ *
+ */
 
 #include <stdio.h>
 #include <fcntl.h>
 #include "maa.h"
 #include "gpio.h"
 
-#define TOTAL_ANALOG_INPUTS_ON_BOARD    (6)
-
-static const unsigned int A0 = 0;
-static const unsigned int A1 = 1;
-static const unsigned int A2 = 2;
-static const unsigned int A3 = 3;
-static const unsigned int A4 = 4;
-static const unsigned int A5 = 5;
-
 #define ADC_RAW_RESOLUTION_BITS         (12)
 #define ADC_SUPPORTED_RESOLUTION_BITS   (10)
-#define ADC_COMMON_GATE_A4_A5           (29)
-
-static const unsigned int
-adc_gate_pins[TOTAL_ANALOG_INPUTS_ON_BOARD] = {37, 36, 23, 22, 21, 20};
 
 typedef struct {
     unsigned int channel;
index 28ca177..7a5d1be 100644 (file)
--- a/api/maa.h
+++ b/api/maa.h
@@ -54,41 +54,86 @@ typedef enum {
     MAA_ERROR_UNSPECIFIED                    = 99 /**< Unknown Error */
 } maa_result_t;
 
+/**
+ * MAA boolean type
+ * 1 For TRUE
+ */
 typedef unsigned int maa_boolean_t;
 
+/**
+ * A bitfield representing the capabilities of a pin.
+ */
 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;
+    /*@{*/
+    maa_boolean_t valid:1;     /**< Is the pin valid at all */
+    maa_boolean_t gpio:1;      /**< Is the pin gpio capable */
+    maa_boolean_t pwm:1;       /**< Is the pin pwm capable */
+    maa_boolean_t fast_gpio:1; /**< Is the pin fast gpio capable */
+    maa_boolean_t spi:1;       /**< Is the pin spi capable */
+    maa_boolean_t i2c:1;       /**< Is the pin i2c capable */
+    maa_boolean_t aio:1;       /**< Is the pin analog input capable */
+    /*@}*/
+} maa_pincapabilities_t;
 
+/**
+ * A Structure representing a multiplexer and the required value
+ */
 typedef struct {
-    unsigned int pin;
-    unsigned int value;
+    /*@{*/
+    unsigned int pin;   /**< Raw GPIO pin id */
+    unsigned int value; /**< Raw GPIO value */
+    /*@}*/
 } maa_mux_t;
 
+/**
+ * A Structure representing a physical Pin.
+ */
 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;
+    /*@{*/
+    char name[8];                      /**< Pin's real world name */
+    unsigned int pin;                  /**< Pin ID */
+    int parent_id;                     /**< IO Parent ID*/
+    maa_pincapabilities_t capabilites; /**< Pin Capabiliites */
+    maa_mux_t mux[4];                  /**< Multiplexer array */
+    unsigned int mux_total;            /**< Total Multiplexors required */
+    /*@}*/
 } maa_pininfo_t;
 
+/**
+ * A Structure representing a platform/board.
+ */
 typedef struct {
-    unsigned int gpio_count;
-    unsigned int aio_count;
-    unsigned int pwm_count;
-    maa_pininfo_t* pins;
+    /*@{*/
+    unsigned int gpio_count; /**< GPIO Count */
+    unsigned int aio_count;  /**< Analog In Count */
+    unsigned int pwm_count;  /**< PWM Count */
+    maa_pininfo_t* pins;     /**< Pointer to pin array */
+    /*@}*/
 } maa_board_t;
 
+/** Initialise MAA
+ *
+ * Detects running platform and attempts to use included pinmap
+ * @return maa_result_t maa result
+ */
+maa_result_t maa_init();
+
+/** Check GPIO
+ *
+ * Will check input is valid for gpio and will also setup required multiplexers.
+ * @param pin the pin as read from the board surface. i.e IO3 would be 3/
+ * @return the pin as found in the pinmap
+ */
 unsigned int maa_check_gpio(int pin);
-//unsigned int maa_check_aio(int pin);
+
+/** Check AIO
+ *
+ * Will check input is valid for aio and will also setup required multiplexers.
+ * @param pin the pin as read from the board surface. i.e A3 would be 3/
+ * @return the pin as found in the pinmap
+ */
+unsigned int maa_check_aio(int pin);
+
 //unsigned int maa_check_pwm(int pin);
 
 /** Get the version string of maa autogenerated from git tag
index e73285c..ec1cc8d 100644 (file)
 
 int main ()
 {
+    maa_init();
     maa_aio_context* adc_a0;
     unsigned int adc_value = 0;
     int i = 0;
 
-    adc_a0 = maa_aio_init(A0);
+    adc_a0 = maa_aio_init(0);
     if (adc_a0 == NULL) {
         return 1;
     }
index 35734f3..e1d9ad7 100644 (file)
@@ -42,62 +42,6 @@ static maa_result_t aio_get_valid_fp(maa_aio_context* dev)
     return MAA_SUCCESS;
 }
 
-/** Configure multiplexer for Analog Input
- *
- * @param aio_channel = Analog input channel to read
- *
- * @return maa_result_t - result type.
- *
- */
-static maa_result_t maa_aio_set_mux(unsigned int aio_channel)
-{
-    maa_result_t result;
-    maa_gpio_context* aio_gate;
-
-    //Initialise VINx multiplexer gate pins
-    aio_gate = maa_gpio_init(adc_gate_pins[aio_channel]);
-
-    if (NULL == aio_gate) {
-        fprintf(stderr, "Failed to initialise first gate pin %d for  ADC "
-            "channel %d !\n", adc_gate_pins[aio_channel], aio_channel);
-        return MAA_ERROR_INVALID_RESOURCE;
-    }
-
-    //Set direction to output for the ADC input gate
-    result = maa_gpio_dir(aio_gate, MAA_GPIO_OUT);
-
-    if (MAA_SUCCESS == result) {
-        // Write gate configuration output value
-        result = maa_gpio_write(aio_gate, 0);
-
-        if (MAA_SUCCESS == result) {
-            //For A4 and A5 Analog common gate pin should be high for the
-            // Galileo board revision D
-            if (A4 == aio_channel || A5 == aio_channel) {
-                aio_gate = maa_gpio_init(ADC_COMMON_GATE_A4_A5);
-
-                //Set direction to output for the gate
-                if (NULL == aio_gate) {
-                    fprintf(stderr, "Failed to initialise second gate pin %d "
-                    "for  ADC channel %d !\n", ADC_COMMON_GATE_A4_A5,
-                    aio_channel);
-                    return(MAA_ERROR_INVALID_RESOURCE);
-                }
-
-                result = maa_gpio_dir(aio_gate, MAA_GPIO_OUT);
-                if (MAA_SUCCESS == result)
-                // Write gate configuration output value
-                result = maa_gpio_write(aio_gate, 1);
-            }
-        }
-    }
-
-    if (NULL != aio_gate)
-        free(aio_gate);
-
-    return (result);
-}
-
 /** Initialise an Analog input, connected to the specified channel
  *
  * @param aio_channel Analog input channel to read
@@ -109,27 +53,32 @@ maa_aio_context* maa_aio_init(unsigned int aio_channel)
 {
     maa_aio_context* dev;
 
-    // Validate input pins 0-5
-    if (aio_channel > TOTAL_ANALOG_INPUTS_ON_BOARD) {
-           fprintf(stderr, "Invalid Analog  input channel %d specified!\n",
-           aio_channel);
-        return NULL;
-    }
-
-    //Set-up multiplexer for the Analog input channel
-    if (MAA_SUCCESS != maa_aio_set_mux(aio_channel)) {
-       fprintf(stderr, "Failed to set-up  Analog  input channel %d "
-           "multiplexer!\n", aio_channel); return NULL;
+    unsigned int checked_pin = maa_check_aio(aio_channel);
+    if (checked_pin < 0) {
+        switch(checked_pin) {
+            case -1:
+                fprintf(stderr, "Invalid Analog  input channel %d specified!\n",
+                           aio_channel);
+                return NULL;
+            case -2:
+                fprintf(stderr, "Failed to set-up  Analog  input channel %d "
+                        "multiplexer!\n", aio_channel);
+                return NULL;
+            case -3:
+                fprintf(stderr, "Platform Not Initialised");
+                return NULL;
+            default: return NULL;
+        }
     }
 
     //Create ADC device connected to specified channel
     dev = (maa_aio_context*) malloc(sizeof(maa_aio_context));
     if (NULL == dev) {
-       fprintf(stderr, "Insufficient memory for specified Analog input channel "
-           "%d !\n", aio_channel);
+    fprintf(stderr, "Insufficient memory for specified Analog input channel "
+            "%d !\n", aio_channel);
         return NULL;
     }
-    dev->channel = aio_channel;
+    dev->channel = checked_pin;
 
     //Open valid  analog input file and get the pointer.
     if (MAA_SUCCESS != aio_get_valid_fp(dev)) {
index b0a61f8..ae64f45 100644 (file)
@@ -43,7 +43,7 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0,0};
     b->pins[0].mux_total = 1;
     b->pins[0].mux[0].pin = 40;
     b->pins[0].mux[0].value = 1;
@@ -51,7 +51,7 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0,0};
     b->pins[1].mux_total = 1;
     b->pins[1].mux[0].pin = 41;
     b->pins[1].mux[0].value = 1;
@@ -59,7 +59,7 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,0,1,0,0,0};
     b->pins[2].mux_total = 1;
     b->pins[2].mux[0].pin = 31;
     b->pins[2].mux[0].value = 1;
@@ -67,7 +67,7 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,1,1,0,0,0};
     b->pins[3].mux_total = 1;
     b->pins[3].mux[0].pin = 30;
     b->pins[3].mux[0].value = 1;
@@ -75,43 +75,43 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,0,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].capabilites = (maa_pincapabilities_t) {1,1,1,0,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].capabilites = (maa_pincapabilities_t) {1,1,1,0,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].capabilites = (maa_pincapabilities_t) {1,1,0,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].capabilites = (maa_pincapabilities_t) {1,1,0,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].capabilites = (maa_pincapabilities_t) {1,1,1,0,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].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0,0};
     b->pins[10].mux_total = 1;
     b->pins[10].mux[0].pin = 42;
     b->pins[10].mux[0].value = 1;
@@ -119,7 +119,7 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0,0};
     b->pins[11].mux_total = 1;
     b->pins[11].mux[0].pin = 43;
     b->pins[11].mux[0].value = 1;
@@ -127,7 +127,7 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0,0};
     b->pins[12].mux_total = 1;
     b->pins[12].mux[0].pin = 54;
     b->pins[12].mux[0].value = 1;
@@ -135,10 +135,63 @@ maa_intel_galileo_rev_d()
     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].capabilites = (maa_pincapabilities_t) {1,1,1,0,1,0,0};
     b->pins[13].mux_total = 1;
     b->pins[13].mux[0].pin = 55;
     b->pins[13].mux[0].value = 1;
 
+    //Analog in mapping and mux.
+    strncpy(b->pins[14].name, "A0", 8);
+    b->pins[14].pin = 0;
+    b->pins[14].parent_id = 0;
+    b->pins[14].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0,1};
+    b->pins[14].mux_total = 1;
+    b->pins[14].mux[0].pin = 37;
+    b->pins[14].mux[0].value = 0;
+
+    strncpy(b->pins[15].name, "A1", 8);
+    b->pins[15].pin = 1;
+    b->pins[15].parent_id = 0;
+    b->pins[15].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0,1};
+    b->pins[15].mux_total = 1;
+    b->pins[15].mux[0].pin = 36;
+    b->pins[15].mux[0].value = 0;
+
+    strncpy(b->pins[16].name, "A2", 8);
+    b->pins[16].pin = 2;
+    b->pins[16].parent_id = 0;
+    b->pins[16].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0,1};
+    b->pins[16].mux_total = 1;
+    b->pins[16].mux[0].pin = 23;
+    b->pins[16].mux[0].value = 0;
+
+    strncpy(b->pins[17].name, "A3", 8);
+    b->pins[17].pin = 3;
+    b->pins[17].parent_id = 0;
+    b->pins[17].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,0,1};
+    b->pins[17].mux_total = 1;
+    b->pins[17].mux[0].pin = 22;
+    b->pins[17].mux[0].value = 0;
+
+    strncpy(b->pins[18].name, "A4", 8);
+    b->pins[18].pin = 4;
+    b->pins[18].parent_id = 0;
+    b->pins[18].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,1,1};
+    b->pins[18].mux_total = 2;
+    b->pins[18].mux[0].pin = 29;
+    b->pins[18].mux[0].value = 1;
+    b->pins[18].mux[1].pin = 21;
+    b->pins[18].mux[1].value = 0;
+
+    strncpy(b->pins[19].name, "A5", 8);
+    b->pins[19].pin = 5;
+    b->pins[19].parent_id = 0;
+    b->pins[19].capabilites = (maa_pincapabilities_t) {1,1,0,0,0,1,1};
+    b->pins[19].mux_total = 2;
+    b->pins[19].mux[0].pin = 29;
+    b->pins[19].mux[0].value = 1;
+    b->pins[19].mux[1].pin = 20;
+    b->pins[19].mux[1].value = 0;
+
     return b;
 }
index 93e2d1c..d54756d 100644 (file)
--- a/src/maa.c
+++ b/src/maa.c
@@ -42,6 +42,9 @@ maa_get_version()
 maa_result_t
 maa_init()
 {
+    /** Once more board definitions have been added,
+     *  A method for detecting them will need to be devised.
+     */
     plat = maa_intel_galileo_rev_d();
     return MAA_SUCCESS;
 }
@@ -50,14 +53,14 @@ static maa_result_t
 maa_setup_mux_mapped(maa_pininfo_t meta)
 {
     int mi;
-    for(mi = 0; mi < meta.mux_total; 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)
+        if (mux_i == NULL)
             return MAA_ERROR_INVALID_HANDLE;
-        if(maa_gpio_dir(mux_i, MAA_GPIO_OUT) != MAA_SUCCESS)
+        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)
+        if (maa_gpio_write(mux_i, meta.mux[mi].value) != MAA_SUCCESS)
             return MAA_ERROR_INVALID_RESOURCE;
     }
     return MAA_SUCCESS;
@@ -66,14 +69,31 @@ maa_setup_mux_mapped(maa_pininfo_t meta)
 unsigned int
 maa_check_gpio(int pin)
 {
-    if(plat == NULL)
+    if (plat == NULL)
         return -1;
 
-    if(pin < 0 || pin > plat->gpio_count)
+    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)
+    if (plat->pins[pin].mux_total > 0)
+        if (maa_setup_mux_mapped(plat->pins[pin]) != MAA_SUCCESS)
             return -1;
     return plat->pins[pin].pin;
 }
 
+unsigned int
+maa_check_aio(int aio)
+{
+    if (plat == NULL)
+        return -3;
+
+    if (aio < 0 || aio > plat->aio_count)
+        return -1;
+
+    int pin = aio + plat->gpio_count;
+
+    if (plat->pins[pin].mux_total > 0)
+        if (maa_setup_mux_mapped(plat->pins[pin]) != MAA_SUCCESS)
+            return -2;
+
+    return plat->pins[pin].pin;
+}