Skip to content

Commit

Permalink
gpio : Add support for input pull up/down modes
Browse files Browse the repository at this point in the history
Make use of 'active_low' interface in sysfs for configuring input pin
in pull up / pull down mode. C++ binding also has been added.

Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
  • Loading branch information
Mani-Sadhasivam authored and arfoll committed Jun 28, 2017
1 parent 3f932ac commit bd3d9d8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/mraa/gpio.h
Expand Up @@ -84,6 +84,14 @@ typedef enum {
MRAA_GPIO_EDGE_FALLING = 3 /**< Interrupt on falling only */
} mraa_gpio_edge_t;

/**
* Gpio input modes
*/
typedef enum {
MRAA_GPIO_ACTIVE_HIGH = 0, /**< Resistive High */
MRAA_GPIO_ACTIVE_LOW = 1, /**< Resistive Low */
} mraa_gpio_input_mode_t;

/**
* Initialise gpio_context, based on board number
*
Expand Down Expand Up @@ -218,6 +226,16 @@ int mraa_gpio_get_pin(mraa_gpio_context dev);
*/
int mraa_gpio_get_pin_raw(mraa_gpio_context dev);

/**
* Set Gpio input mode
*
* @param dev The Gpio context
* @param mode Mode to set input pin state
* @return Result of operation
*/
mraa_result_t mraa_gpio_input_mode(mraa_gpio_context dev, mraa_gpio_input_mode_t);


#ifdef __cplusplus
}
#endif
21 changes: 21 additions & 0 deletions api/mraa/gpio.hpp
Expand Up @@ -69,6 +69,14 @@ typedef enum {
EDGE_FALLING = 3 /**< Interrupt on falling only */
} Edge;

/**
* Gpio Input modes
*/
typedef enum {
MODE_IN_ACTIVE_HIGH = 0, /**< Resistive High */
MODE_IN_ACTIVE_LOW = 1, /**< Resistive Low */
} InputMode;

/**
* @brief API to General Purpose IO
*
Expand Down Expand Up @@ -316,6 +324,19 @@ class Gpio
return mraa_gpio_get_pin(m_gpio);
}

/**
* Change Gpio input mode
*
* @param mode The mode to change the gpio input
* @return Result of operation
*/
Result
inputMode(InputMode mode)
{
return (Result )mraa_gpio_input_mode(m_gpio, (mraa_gpio_input_mode_t) mode);
}


private:
mraa_gpio_context m_gpio;
#if defined(SWIGJAVASCRIPT)
Expand Down
42 changes: 42 additions & 0 deletions src/gpio/gpio.c
Expand Up @@ -839,3 +839,45 @@ mraa_gpio_get_pin_raw(mraa_gpio_context dev)
}
return dev->pin;
}

mraa_result_t
mraa_gpio_input_mode(mraa_gpio_context dev, mraa_gpio_input_mode_t mode)
{
if (dev == NULL) {
syslog(LOG_ERR, "gpio: in_mode: context is invalid");
return MRAA_ERROR_INVALID_HANDLE;
}

char filepath[MAX_SIZE];
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/active_low", dev->pin);

int active_low = open(filepath, O_WRONLY);
if (active_low == -1) {
syslog(LOG_ERR, "gpio%i: mode: Failed to open 'active_low' for writing: %s", dev->pin, strerror(errno));
return MRAA_ERROR_INVALID_RESOURCE;
}

char bu[MAX_SIZE];
int length;
switch (mode) {
case MRAA_GPIO_ACTIVE_HIGH:
length = snprintf(bu, sizeof(bu), "%d", 0);
break;
case MRAA_GPIO_ACTIVE_LOW:
length = snprintf(bu, sizeof(bu), "%d", 1);
break;
default:
close(active_low);
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
}

if (write(active_low, bu, length * sizeof(char)) == -1) {
syslog(LOG_ERR, "gpio%i: mode: Failed to write to 'active_low': %s", dev->pin, strerror(errno));
close(active_low);
return MRAA_ERROR_INVALID_RESOURCE;
}

close(active_low);

return MRAA_SUCCESS;
}

0 comments on commit bd3d9d8

Please sign in to comment.