clang-format: reapply to recently modified sources
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
115
src/gpio/gpio.c
115
src/gpio/gpio.c
@@ -23,9 +23,9 @@
|
|||||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
|
#include "gpio/gpio_chardev.h"
|
||||||
#include "linux/gpio.h"
|
#include "linux/gpio.h"
|
||||||
#include "mraa_internal.h"
|
#include "mraa_internal.h"
|
||||||
#include "gpio/gpio_chardev.h"
|
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -38,8 +38,8 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define SYSFS_CLASS_GPIO "/sys/class/gpio"
|
#define SYSFS_CLASS_GPIO "/sys/class/gpio"
|
||||||
#define MAX_SIZE 64
|
#define MAX_SIZE 64
|
||||||
@@ -69,7 +69,7 @@ mraa_gpio_close_event_handles_sysfs(int fds[], int num_fds)
|
|||||||
|
|
||||||
for (int i = 0; i < num_fds; ++i) {
|
for (int i = 0; i < num_fds; ++i) {
|
||||||
// Check required to avoid closing stdin and of an uninitialized fd
|
// Check required to avoid closing stdin and of an uninitialized fd
|
||||||
if(fds[i] != 0) {
|
if (fds[i] != 0) {
|
||||||
close(fds[i]);
|
close(fds[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,15 +130,13 @@ mraa_gpio_init_internal(mraa_adv_func_t* func_table, int pin)
|
|||||||
} else {
|
} else {
|
||||||
int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
|
int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
|
||||||
if (export == -1) {
|
if (export == -1) {
|
||||||
syslog(LOG_ERR, "gpio%i: init: Failed to open 'export' for writing: %s",
|
syslog(LOG_ERR, "gpio%i: init: Failed to open 'export' for writing: %s", pin, strerror(errno));
|
||||||
pin, strerror(errno));
|
|
||||||
status = MRAA_ERROR_INVALID_RESOURCE;
|
status = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
goto init_internal_cleanup;
|
goto init_internal_cleanup;
|
||||||
}
|
}
|
||||||
length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
||||||
if (write(export, bu, length * sizeof(char)) == -1) {
|
if (write(export, bu, length * sizeof(char)) == -1) {
|
||||||
syslog(LOG_ERR, "gpio%i: init: Failed to write to 'export': %s",
|
syslog(LOG_ERR, "gpio%i: init: Failed to write to 'export': %s", pin, strerror(errno));
|
||||||
pin, strerror(errno));
|
|
||||||
close(export);
|
close(export);
|
||||||
status = MRAA_ERROR_INVALID_RESOURCE;
|
status = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
goto init_internal_cleanup;
|
goto init_internal_cleanup;
|
||||||
@@ -181,11 +179,9 @@ mraa_gpio_init(int pin)
|
|||||||
* example: pin 515, dev->pin = 515, dev->phy_pin = 3
|
* example: pin 515, dev->pin = 515, dev->phy_pin = 3
|
||||||
*/
|
*/
|
||||||
if (mraa_is_sub_platform_id(pin) && (board->sub_platform != NULL)) {
|
if (mraa_is_sub_platform_id(pin) && (board->sub_platform != NULL)) {
|
||||||
syslog(LOG_NOTICE, "gpio%i: initialised on sub platform '%s' physical pin: %i",
|
syslog(LOG_NOTICE, "gpio%i: initialised on sub platform '%s' physical pin: %i", pin,
|
||||||
pin,
|
board->sub_platform->platform_name != NULL ? board->sub_platform->platform_name : "",
|
||||||
board->sub_platform->platform_name != NULL ?
|
mraa_get_sub_platform_index(pin));
|
||||||
board->sub_platform->platform_name : "",
|
|
||||||
mraa_get_sub_platform_index(pin));
|
|
||||||
|
|
||||||
board = board->sub_platform;
|
board = board->sub_platform;
|
||||||
if (board == NULL) {
|
if (board == NULL) {
|
||||||
@@ -201,8 +197,7 @@ mraa_gpio_init(int pin)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pin < 0 || pin >= board->phy_pin_count) {
|
if (pin < 0 || pin >= board->phy_pin_count) {
|
||||||
syslog(LOG_ERR, "gpio: init: pin %i beyond platform pin count (%i)",
|
syslog(LOG_ERR, "gpio: init: pin %i beyond platform pin count (%i)", pin, board->phy_pin_count);
|
||||||
pin, board->phy_pin_count);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (board->pins[pin].capabilities.gpio != 1) {
|
if (board->pins[pin].capabilities.gpio != 1) {
|
||||||
@@ -337,8 +332,8 @@ mraa_gpio_chardev_init(int pins[], int num_pins)
|
|||||||
|
|
||||||
int line_in_group;
|
int line_in_group;
|
||||||
line_in_group = gpio_group[chip_id].num_gpio_lines;
|
line_in_group = gpio_group[chip_id].num_gpio_lines;
|
||||||
gpio_group[chip_id].gpio_lines = realloc(gpio_group[chip_id].gpio_lines,
|
gpio_group[chip_id].gpio_lines =
|
||||||
(line_in_group + 1) * sizeof(unsigned int));
|
realloc(gpio_group[chip_id].gpio_lines, (line_in_group + 1) * sizeof(unsigned int));
|
||||||
if (gpio_group[chip_id].gpio_lines == NULL) {
|
if (gpio_group[chip_id].gpio_lines == NULL) {
|
||||||
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
|
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
|
||||||
mraa_gpio_close(dev);
|
mraa_gpio_close(dev);
|
||||||
@@ -465,9 +460,8 @@ mraa_gpio_wait_interrupt(int fds[],
|
|||||||
,
|
,
|
||||||
int control_fd
|
int control_fd
|
||||||
#endif
|
#endif
|
||||||
,
|
,
|
||||||
mraa_gpio_events_t events
|
mraa_gpio_events_t events)
|
||||||
)
|
|
||||||
{
|
{
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
#ifdef HAVE_PTHREAD_CANCEL
|
#ifdef HAVE_PTHREAD_CANCEL
|
||||||
@@ -564,7 +558,8 @@ mraa_gpio_get_events(mraa_gpio_context dev)
|
|||||||
unsigned int pin_idx;
|
unsigned int pin_idx;
|
||||||
mraa_gpiod_group_t gpio_iter;
|
mraa_gpiod_group_t gpio_iter;
|
||||||
|
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
|
{
|
||||||
for (int i = 0; i < gpio_iter->num_gpio_lines; ++i) {
|
for (int i = 0; i < gpio_iter->num_gpio_lines; ++i) {
|
||||||
if (dev->events[event_idx].id != -1) {
|
if (dev->events[event_idx].id != -1) {
|
||||||
pin_idx = gpio_iter->gpio_group_to_pins_table[i];
|
pin_idx = gpio_iter->gpio_group_to_pins_table[i];
|
||||||
@@ -606,19 +601,21 @@ mraa_gpio_interrupt_handler(void* arg)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int *fps = calloc(dev->num_pins, sizeof(int));
|
int* fps = calloc(dev->num_pins, sizeof(int));
|
||||||
if (!fps) {
|
if (!fps) {
|
||||||
syslog(LOG_ERR, "mraa_gpio_interrupt_handler_multiple() malloc error");
|
syslog(LOG_ERR, "mraa_gpio_interrupt_handler_multiple() malloc error");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Is this pin on a subplatform? Do nothing... */
|
/* Is this pin on a subplatform? Do nothing... */
|
||||||
if (mraa_is_sub_platform_id(dev->pin)) {}
|
if (mraa_is_sub_platform_id(dev->pin)) {
|
||||||
|
}
|
||||||
/* Is the platform chardev_capable? */
|
/* Is the platform chardev_capable? */
|
||||||
else if (plat->chardev_capable) {
|
else if (plat->chardev_capable) {
|
||||||
mraa_gpiod_group_t gpio_group;
|
mraa_gpiod_group_t gpio_group;
|
||||||
|
|
||||||
for_each_gpio_group(gpio_group, dev) {
|
for_each_gpio_group(gpio_group, dev)
|
||||||
|
{
|
||||||
for (int i = 0; i < gpio_group->num_gpio_lines; ++i) {
|
for (int i = 0; i < gpio_group->num_gpio_lines; ++i) {
|
||||||
fps[idx++] = gpio_group->event_handles[i];
|
fps[idx++] = gpio_group->event_handles[i];
|
||||||
}
|
}
|
||||||
@@ -672,12 +669,11 @@ mraa_gpio_interrupt_handler(void* arg)
|
|||||||
} else {
|
} else {
|
||||||
ret = mraa_gpio_wait_interrupt(fps, idx
|
ret = mraa_gpio_wait_interrupt(fps, idx
|
||||||
#ifndef HAVE_PTHREAD_CANCEL
|
#ifndef HAVE_PTHREAD_CANCEL
|
||||||
,
|
,
|
||||||
dev->isr_control_pipe[0]
|
dev->isr_control_pipe[0]
|
||||||
#endif
|
#endif
|
||||||
,
|
,
|
||||||
dev->events
|
dev->events);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -694,7 +690,7 @@ mraa_gpio_interrupt_handler(void* arg)
|
|||||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
// we must have got an error code or exit request so die nicely
|
// we must have got an error code or exit request so die nicely
|
||||||
#ifdef HAVE_PTHREAD_CANCEL
|
#ifdef HAVE_PTHREAD_CANCEL
|
||||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||||
#endif
|
#endif
|
||||||
@@ -789,7 +785,7 @@ mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
|
|||||||
|
|
||||||
/* Initialize events array. */
|
/* Initialize events array. */
|
||||||
if (dev->events == NULL && mode != MRAA_GPIO_EDGE_NONE) {
|
if (dev->events == NULL && mode != MRAA_GPIO_EDGE_NONE) {
|
||||||
dev->events = malloc(dev->num_pins * sizeof (mraa_gpio_event));
|
dev->events = malloc(dev->num_pins * sizeof(mraa_gpio_event));
|
||||||
if (dev->events == NULL) {
|
if (dev->events == NULL) {
|
||||||
syslog(LOG_ERR, "mraa_gpio_edge_mode() malloc error");
|
syslog(LOG_ERR, "mraa_gpio_edge_mode() malloc error");
|
||||||
return MRAA_ERROR_NO_RESOURCES;
|
return MRAA_ERROR_NO_RESOURCES;
|
||||||
@@ -945,8 +941,8 @@ mraa_gpio_isr_exit(mraa_gpio_context dev)
|
|||||||
dev->isr_thread_terminating = 0;
|
dev->isr_thread_terminating = 0;
|
||||||
|
|
||||||
if (dev->events) {
|
if (dev->events) {
|
||||||
free(dev->events);
|
free(dev->events);
|
||||||
dev->events = NULL;
|
dev->events = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -977,8 +973,10 @@ mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
|||||||
_mraa_close_gpio_desc(dev);
|
_mraa_close_gpio_desc(dev);
|
||||||
|
|
||||||
/* We save flag values from the first valid line. */
|
/* We save flag values from the first valid line. */
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
mraa_gpiod_line_info* linfo = mraa_get_line_info_by_chip_number(gpio_iter->gpio_chip, gpio_iter->gpio_lines[0]);
|
{
|
||||||
|
mraa_gpiod_line_info* linfo =
|
||||||
|
mraa_get_line_info_by_chip_number(gpio_iter->gpio_chip, gpio_iter->gpio_lines[0]);
|
||||||
if (!linfo) {
|
if (!linfo) {
|
||||||
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line info");
|
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line info");
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
@@ -1005,8 +1003,10 @@ mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
|||||||
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
line_handle = mraa_get_lines_handle(gpio_iter->dev_fd, gpio_iter->gpio_lines, gpio_iter->num_gpio_lines, flags, 0);
|
{
|
||||||
|
line_handle = mraa_get_lines_handle(gpio_iter->dev_fd, gpio_iter->gpio_lines,
|
||||||
|
gpio_iter->num_gpio_lines, flags, 0);
|
||||||
if (line_handle <= 0) {
|
if (line_handle <= 0) {
|
||||||
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line handle");
|
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line handle");
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
@@ -1071,8 +1071,10 @@ mraa_gpio_chardev_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
unsigned flags = 0;
|
unsigned flags = 0;
|
||||||
mraa_gpiod_group_t gpio_iter;
|
mraa_gpiod_group_t gpio_iter;
|
||||||
|
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
mraa_gpiod_line_info* linfo = mraa_get_line_info_by_chip_number(gpio_iter->gpio_chip, gpio_iter->gpio_lines[0]);
|
{
|
||||||
|
mraa_gpiod_line_info* linfo =
|
||||||
|
mraa_get_line_info_by_chip_number(gpio_iter->gpio_chip, gpio_iter->gpio_lines[0]);
|
||||||
if (!linfo) {
|
if (!linfo) {
|
||||||
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line info");
|
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line info");
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
@@ -1097,7 +1099,8 @@ mraa_gpio_chardev_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
|
{
|
||||||
if (gpio_iter->gpiod_handle != -1) {
|
if (gpio_iter->gpiod_handle != -1) {
|
||||||
close(gpio_iter->gpiod_handle);
|
close(gpio_iter->gpiod_handle);
|
||||||
gpio_iter->gpiod_handle = -1;
|
gpio_iter->gpiod_handle = -1;
|
||||||
@@ -1125,7 +1128,7 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_dir_replace)) {
|
if (IS_FUNC_DEFINED(dev, gpio_dir_replace)) {
|
||||||
return dev->advance_func->gpio_dir_replace(dev, dir);
|
return dev->advance_func->gpio_dir_replace(dev, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_dir_pre)) {
|
if (IS_FUNC_DEFINED(dev, gpio_dir_pre)) {
|
||||||
@@ -1160,7 +1163,7 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
return mraa_gpio_write(it, 0);
|
return mraa_gpio_write(it, 0);
|
||||||
default:
|
default:
|
||||||
syslog(LOG_ERR, "gpio%i: dir: Failed to open 'direction' for writing: %s",
|
syslog(LOG_ERR, "gpio%i: dir: Failed to open 'direction' for writing: %s",
|
||||||
it->pin, strerror(errno));
|
it->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1202,7 +1205,7 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
}
|
}
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
|
mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t* dir)
|
||||||
{
|
{
|
||||||
mraa_result_t result = MRAA_SUCCESS;
|
mraa_result_t result = MRAA_SUCCESS;
|
||||||
|
|
||||||
@@ -1216,8 +1219,10 @@ mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
|
|||||||
if (plat->chardev_capable) {
|
if (plat->chardev_capable) {
|
||||||
mraa_gpiod_group_t gpio_iter;
|
mraa_gpiod_group_t gpio_iter;
|
||||||
|
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
mraa_gpiod_line_info* linfo = mraa_get_line_info_by_chip_number(gpio_iter->gpio_chip, gpio_iter->gpio_lines[0]);
|
{
|
||||||
|
mraa_gpiod_line_info* linfo =
|
||||||
|
mraa_get_line_info_by_chip_number(gpio_iter->gpio_chip, gpio_iter->gpio_lines[0]);
|
||||||
if (!linfo) {
|
if (!linfo) {
|
||||||
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line info");
|
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line info");
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
@@ -1295,7 +1300,7 @@ mraa_gpio_read(mraa_gpio_context dev)
|
|||||||
int output_values[1] = { 0 };
|
int output_values[1] = { 0 };
|
||||||
|
|
||||||
if (mraa_gpio_read_multi(dev, output_values) != MRAA_SUCCESS)
|
if (mraa_gpio_read_multi(dev, output_values) != MRAA_SUCCESS)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return output_values[0];
|
return output_values[0];
|
||||||
}
|
}
|
||||||
@@ -1314,8 +1319,8 @@ mraa_gpio_read(mraa_gpio_context dev)
|
|||||||
}
|
}
|
||||||
char bu[2];
|
char bu[2];
|
||||||
if (read(dev->value_fp, bu, 2 * sizeof(char)) != 2) {
|
if (read(dev->value_fp, bu, 2 * sizeof(char)) != 2) {
|
||||||
syslog(LOG_ERR, "gpio%i: read: Failed to read a sensible value from sysfs: %s",
|
syslog(LOG_ERR, "gpio%i: read: Failed to read a sensible value from sysfs: %s", dev->pin,
|
||||||
dev->pin, strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
lseek(dev->value_fp, 0, SEEK_SET);
|
lseek(dev->value_fp, 0, SEEK_SET);
|
||||||
@@ -1336,21 +1341,22 @@ mraa_gpio_read_multi(mraa_gpio_context dev, int output_values[])
|
|||||||
|
|
||||||
mraa_gpiod_group_t gpio_iter;
|
mraa_gpiod_group_t gpio_iter;
|
||||||
|
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
|
{
|
||||||
int status;
|
int status;
|
||||||
unsigned flags = GPIOHANDLE_REQUEST_INPUT;
|
unsigned flags = GPIOHANDLE_REQUEST_INPUT;
|
||||||
|
|
||||||
if (gpio_iter->gpiod_handle <= 0) {
|
if (gpio_iter->gpiod_handle <= 0) {
|
||||||
gpio_iter->gpiod_handle = mraa_get_lines_handle(gpio_iter->dev_fd, gpio_iter->gpio_lines,
|
gpio_iter->gpiod_handle = mraa_get_lines_handle(gpio_iter->dev_fd, gpio_iter->gpio_lines,
|
||||||
gpio_iter->num_gpio_lines, flags, 0);
|
gpio_iter->num_gpio_lines, flags, 0);
|
||||||
if (gpio_iter->gpiod_handle <= 0) {
|
if (gpio_iter->gpiod_handle <= 0) {
|
||||||
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting gpio line handle");
|
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting gpio line handle");
|
||||||
return MRAA_ERROR_INVALID_HANDLE;
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
status = mraa_get_line_values(gpio_iter->gpiod_handle, gpio_iter->num_gpio_lines,
|
status =
|
||||||
gpio_iter->rw_values);
|
mraa_get_line_values(gpio_iter->gpiod_handle, gpio_iter->num_gpio_lines, gpio_iter->rw_values);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error writing gpio");
|
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error writing gpio");
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
@@ -1460,7 +1466,8 @@ mraa_gpio_write_multi(mraa_gpio_context dev, int input_values[])
|
|||||||
}
|
}
|
||||||
free(counters);
|
free(counters);
|
||||||
|
|
||||||
for_each_gpio_group(gpio_iter, dev) {
|
for_each_gpio_group(gpio_iter, dev)
|
||||||
|
{
|
||||||
int status;
|
int status;
|
||||||
unsigned flags = GPIOHANDLE_REQUEST_OUTPUT;
|
unsigned flags = GPIOHANDLE_REQUEST_OUTPUT;
|
||||||
|
|
||||||
@@ -1473,8 +1480,8 @@ mraa_gpio_write_multi(mraa_gpio_context dev, int input_values[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
status = mraa_set_line_values(gpio_iter->gpiod_handle, gpio_iter->num_gpio_lines,
|
status =
|
||||||
gpio_iter->rw_values);
|
mraa_set_line_values(gpio_iter->gpiod_handle, gpio_iter->num_gpio_lines, gpio_iter->rw_values);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error writing gpio");
|
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error writing gpio");
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
|
|||||||
@@ -23,14 +23,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "mraa_internal.h"
|
#include "mraa_internal.h"
|
||||||
|
|
||||||
@@ -64,9 +64,8 @@ mraa_init_json_platform_get_index(json_object* jobj, const char* io, const char*
|
|||||||
|
|
||||||
*pos = (int) json_object_get_int(jobj_temp);
|
*pos = (int) json_object_get_int(jobj_temp);
|
||||||
if (*pos < 0 || *pos > upper) {
|
if (*pos < 0 || *pos > upper) {
|
||||||
syslog(LOG_ERR,
|
syslog(LOG_ERR, "init_json_platform: %s %s at position: %d, gave: %d which was out of range",
|
||||||
"init_json_platform: %s %s at position: %d, gave: %d which was out of range", io,
|
io, key, index, *pos);
|
||||||
key, index, *pos);
|
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
@@ -291,7 +290,7 @@ mraa_init_json_platform_i2c(json_object* jobj_i2c, mraa_board_t* board, int inde
|
|||||||
json_object* jobj_temp = NULL;
|
json_object* jobj_temp = NULL;
|
||||||
|
|
||||||
// Default to no mux pins defined
|
// Default to no mux pins defined
|
||||||
if(board->pins != NULL) {
|
if (board->pins != NULL) {
|
||||||
board->pins[pin].i2c.mux_total = 0;
|
board->pins[pin].i2c.mux_total = 0;
|
||||||
} else {
|
} else {
|
||||||
return MRAA_ERROR_NO_DATA_AVAILABLE;
|
return MRAA_ERROR_NO_DATA_AVAILABLE;
|
||||||
@@ -559,8 +558,9 @@ mraa_init_json_platform_loop(json_object* jobj_platform, const char* obj_key, mr
|
|||||||
jobj_io = json_object_array_get_idx(jobj_temp, i);
|
jobj_io = json_object_array_get_idx(jobj_temp, i);
|
||||||
// Check to see it's the right type
|
// Check to see it's the right type
|
||||||
if (!json_object_is_type(jobj_io, json_type_object)) {
|
if (!json_object_is_type(jobj_io, json_type_object)) {
|
||||||
syslog(LOG_ERR, "init_json_platform: One of more of the elements in the \"%s\" "
|
syslog(LOG_ERR,
|
||||||
"array where not JSON objects",
|
"init_json_platform: One of more of the elements in the \"%s\" "
|
||||||
|
"array where not JSON objects",
|
||||||
obj_key);
|
obj_key);
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
@@ -595,8 +595,7 @@ mraa_init_json_platform_size_check(json_object* jobj_platform,
|
|||||||
// make sure we don't have more than our range
|
// make sure we don't have more than our range
|
||||||
array_length = json_object_array_length(jobj_temp);
|
array_length = json_object_array_length(jobj_temp);
|
||||||
if (array_length > range) {
|
if (array_length > range) {
|
||||||
syslog(LOG_ERR,
|
syslog(LOG_ERR, "init_json_platform: The size of the %s array given was %d, max was: %d",
|
||||||
"init_json_platform: The size of the %s array given was %d, max was: %d",
|
|
||||||
obj_key, array_length, range);
|
obj_key, array_length, range);
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
@@ -617,7 +616,7 @@ mraa_init_json_platform(const char* platform_json)
|
|||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int file_lock = 0, i = 0;
|
int file_lock = 0, i = 0;
|
||||||
json_object *jobj_platform = NULL;
|
json_object* jobj_platform = NULL;
|
||||||
mraa_board_t* board = NULL;
|
mraa_board_t* board = NULL;
|
||||||
|
|
||||||
// Try to lock the file for use
|
// Try to lock the file for use
|
||||||
|
|||||||
210
src/mraa.c
210
src/mraa.c
@@ -28,43 +28,43 @@
|
|||||||
#define _XOPEN_SOURCE 600 /* Get nftw() and S_IFSOCK declarations */
|
#define _XOPEN_SOURCE 600 /* Get nftw() and S_IFSOCK declarations */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sched.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
#include <sched.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#if !defined(PERIPHERALMAN)
|
#if !defined(PERIPHERALMAN)
|
||||||
#include <glob.h>
|
|
||||||
#include <ftw.h>
|
#include <ftw.h>
|
||||||
|
#include <glob.h>
|
||||||
#endif
|
#endif
|
||||||
#include <dirent.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#if defined(IMRAA)
|
#if defined(IMRAA)
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "mraa_internal.h"
|
#include "aio.h"
|
||||||
#include "firmata/firmata_mraa.h"
|
#include "firmata/firmata_mraa.h"
|
||||||
#include "grovepi/grovepi.h"
|
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
#include "gpio/gpio_chardev.h"
|
#include "gpio/gpio_chardev.h"
|
||||||
#include "version.h"
|
#include "grovepi/grovepi.h"
|
||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
|
#include "mraa_internal.h"
|
||||||
#include "pwm.h"
|
#include "pwm.h"
|
||||||
#include "aio.h"
|
|
||||||
#include "spi.h"
|
#include "spi.h"
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
|
#include "version.h"
|
||||||
|
|
||||||
#if defined(PERIPHERALMAN)
|
#if defined(PERIPHERALMAN)
|
||||||
#include "peripheralmanager/peripheralman.h"
|
#include "peripheralmanager/peripheralman.h"
|
||||||
@@ -100,17 +100,20 @@ mraa_set_log_level(int level)
|
|||||||
return MRAA_ERROR_INVALID_PARAMETER;
|
return MRAA_ERROR_INVALID_PARAMETER;
|
||||||
}
|
}
|
||||||
|
|
||||||
mraa_boolean_t mraa_is_kernel_chardev_interface_compatible()
|
mraa_boolean_t
|
||||||
|
mraa_is_kernel_chardev_interface_compatible()
|
||||||
{
|
{
|
||||||
if (mraa_get_number_of_gpio_chips() <= 0) {
|
if (mraa_get_number_of_gpio_chips() <= 0) {
|
||||||
syslog(LOG_NOTICE, "gpio: platform supports chardev but kernel doesn't, falling back to sysfs");
|
syslog(LOG_NOTICE,
|
||||||
|
"gpio: platform supports chardev but kernel doesn't, falling back to sysfs");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mraa_boolean_t mraa_is_platform_chardev_interface_capable()
|
mraa_boolean_t
|
||||||
|
mraa_is_platform_chardev_interface_capable()
|
||||||
{
|
{
|
||||||
if ((plat != NULL) && (plat->chardev_capable)) {
|
if ((plat != NULL) && (plat->chardev_capable)) {
|
||||||
return mraa_is_kernel_chardev_interface_compatible();
|
return mraa_is_kernel_chardev_interface_compatible();
|
||||||
@@ -202,17 +205,15 @@ imraa_init()
|
|||||||
/* If a usb platform lib is present, attempt to load and look for
|
/* If a usb platform lib is present, attempt to load and look for
|
||||||
* necessary symbols for adding extended I/O */
|
* necessary symbols for adding extended I/O */
|
||||||
void* usblib = dlopen("libmraa-platform-ft4222.so", RTLD_LAZY);
|
void* usblib = dlopen("libmraa-platform-ft4222.so", RTLD_LAZY);
|
||||||
if (usblib)
|
if (usblib) {
|
||||||
{
|
|
||||||
syslog(LOG_NOTICE, "Found USB platform extender library: libmraa-platform-ft4222.so");
|
syslog(LOG_NOTICE, "Found USB platform extender library: libmraa-platform-ft4222.so");
|
||||||
syslog(LOG_NOTICE, "Detecting FT4222 subplatforms...");
|
syslog(LOG_NOTICE, "Detecting FT4222 subplatforms...");
|
||||||
fptr_add_platform_extender add_ft4222_platform =
|
fptr_add_platform_extender add_ft4222_platform =
|
||||||
(fptr_add_platform_extender)dlsym(usblib, "mraa_usb_platform_extender");
|
(fptr_add_platform_extender) dlsym(usblib, "mraa_usb_platform_extender");
|
||||||
|
|
||||||
/* If this method exists, call it to add a subplatform */
|
/* If this method exists, call it to add a subplatform */
|
||||||
syslog(LOG_NOTICE, "Detecting FT4222 subplatforms complete, found %i subplatform/s",
|
syslog(LOG_NOTICE, "Detecting FT4222 subplatforms complete, found %i subplatform/s",
|
||||||
((add_ft4222_platform != NULL) && (add_ft4222_platform(plat) == MRAA_SUCCESS))
|
((add_ft4222_platform != NULL) && (add_ft4222_platform(plat) == MRAA_SUCCESS)) ? 1 : 0);
|
||||||
? 1 : 0);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -250,7 +251,8 @@ imraa_init()
|
|||||||
syslog(LOG_NOTICE, "gpio: support for chardev interface is activated");
|
syslog(LOG_NOTICE, "gpio: support for chardev interface is activated");
|
||||||
}
|
}
|
||||||
|
|
||||||
syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
|
syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(),
|
||||||
|
mraa_get_platform_type());
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,9 +302,8 @@ mraa_deinit()
|
|||||||
* allocate space for device_path, others use #defines or consts,
|
* allocate space for device_path, others use #defines or consts,
|
||||||
* which means this has to be handled differently per platform
|
* which means this has to be handled differently per platform
|
||||||
*/
|
*/
|
||||||
if ((plat->platform_type == MRAA_JSON_PLATFORM) ||
|
if ((plat->platform_type == MRAA_JSON_PLATFORM) || (plat->platform_type == MRAA_UP2) ||
|
||||||
(plat->platform_type == MRAA_UP2) ||
|
(plat->platform_type == MRAA_IEI_TANK)) {
|
||||||
(plat->platform_type == MRAA_IEI_TANK)) {
|
|
||||||
for (i = 0; i < plat->uart_dev_count; i++) {
|
for (i = 0; i < plat->uart_dev_count; i++) {
|
||||||
if (plat->uart_dev[i].device_path != NULL) {
|
if (plat->uart_dev[i].device_path != NULL) {
|
||||||
free(plat->uart_dev[i].device_path);
|
free(plat->uart_dev[i].device_path);
|
||||||
@@ -377,7 +378,7 @@ mraa_iio_detect()
|
|||||||
plat_iio->iio_device_count = num_iio_devices;
|
plat_iio->iio_device_count = num_iio_devices;
|
||||||
plat_iio->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
|
plat_iio->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
|
||||||
struct _iio* device;
|
struct _iio* device;
|
||||||
for (i=0; i < num_iio_devices; i++) {
|
for (i = 0; i < num_iio_devices; i++) {
|
||||||
device = &plat_iio->iio_devices[i];
|
device = &plat_iio->iio_devices[i];
|
||||||
device->num = i;
|
device->num = i;
|
||||||
snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
|
snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
|
||||||
@@ -390,7 +391,7 @@ mraa_iio_detect()
|
|||||||
len = strlen(name);
|
len = strlen(name);
|
||||||
// use strndup
|
// use strndup
|
||||||
device->name = malloc((sizeof(char) * len) + sizeof(char));
|
device->name = malloc((sizeof(char) * len) + sizeof(char));
|
||||||
strncpy(device->name, name, len+1);
|
strncpy(device->name, name, len + 1);
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
@@ -409,22 +410,23 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
|
|||||||
|
|
||||||
for (mi = 0; mi < meta.mux_total; mi++) {
|
for (mi = 0; mi < meta.mux_total; mi++) {
|
||||||
|
|
||||||
switch(meta.mux[mi].pincmd) {
|
switch (meta.mux[mi].pincmd) {
|
||||||
case PINCMD_UNDEFINED: // used for backward compatibility
|
case PINCMD_UNDEFINED: // used for backward compatibility
|
||||||
if(meta.mux[mi].pin != last_pin) {
|
if (meta.mux[mi].pin != last_pin) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
}
|
}
|
||||||
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
||||||
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
|
if (mux_i == NULL)
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
last_pin = meta.mux[mi].pin;
|
last_pin = meta.mux[mi].pin;
|
||||||
}
|
}
|
||||||
// this function will sometimes fail, however this is not critical as
|
// this function will sometimes fail, however this is not critical as
|
||||||
// long as the write succeeds - Test case galileo gen2 pin2
|
// long as the write succeeds - Test case galileo gen2 pin2
|
||||||
mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
|
mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
|
||||||
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
||||||
if(ret != MRAA_SUCCESS) {
|
if (ret != MRAA_SUCCESS) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
@@ -434,19 +436,20 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PINCMD_SET_VALUE:
|
case PINCMD_SET_VALUE:
|
||||||
if(meta.mux[mi].pin != last_pin) {
|
if (meta.mux[mi].pin != last_pin) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
}
|
}
|
||||||
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
||||||
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
|
if (mux_i == NULL)
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
last_pin = meta.mux[mi].pin;
|
last_pin = meta.mux[mi].pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
||||||
|
|
||||||
if(ret != MRAA_SUCCESS) {
|
if (ret != MRAA_SUCCESS) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
@@ -456,19 +459,20 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PINCMD_SET_DIRECTION:
|
case PINCMD_SET_DIRECTION:
|
||||||
if(meta.mux[mi].pin != last_pin) {
|
if (meta.mux[mi].pin != last_pin) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
}
|
}
|
||||||
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
||||||
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
|
if (mux_i == NULL)
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
last_pin = meta.mux[mi].pin;
|
last_pin = meta.mux[mi].pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = mraa_gpio_dir(mux_i, meta.mux[mi].value);
|
ret = mraa_gpio_dir(mux_i, meta.mux[mi].value);
|
||||||
|
|
||||||
if(ret != MRAA_SUCCESS) {
|
if (ret != MRAA_SUCCESS) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
@@ -478,22 +482,23 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PINCMD_SET_IN_VALUE:
|
case PINCMD_SET_IN_VALUE:
|
||||||
if(meta.mux[mi].pin != last_pin) {
|
if (meta.mux[mi].pin != last_pin) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
}
|
}
|
||||||
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
||||||
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
|
if (mux_i == NULL)
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
last_pin = meta.mux[mi].pin;
|
last_pin = meta.mux[mi].pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = mraa_gpio_dir(mux_i, MRAA_GPIO_IN);
|
ret = mraa_gpio_dir(mux_i, MRAA_GPIO_IN);
|
||||||
|
|
||||||
if(ret == MRAA_SUCCESS)
|
if (ret == MRAA_SUCCESS)
|
||||||
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
||||||
|
|
||||||
if(ret != MRAA_SUCCESS) {
|
if (ret != MRAA_SUCCESS) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
@@ -503,22 +508,23 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PINCMD_SET_OUT_VALUE:
|
case PINCMD_SET_OUT_VALUE:
|
||||||
if(meta.mux[mi].pin != last_pin) {
|
if (meta.mux[mi].pin != last_pin) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
}
|
}
|
||||||
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
||||||
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
|
if (mux_i == NULL)
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
last_pin = meta.mux[mi].pin;
|
last_pin = meta.mux[mi].pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
|
ret = mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
|
||||||
|
|
||||||
if(ret == MRAA_SUCCESS)
|
if (ret == MRAA_SUCCESS)
|
||||||
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
ret = mraa_gpio_write(mux_i, meta.mux[mi].value);
|
||||||
|
|
||||||
if(ret != MRAA_SUCCESS) {
|
if (ret != MRAA_SUCCESS) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
@@ -528,19 +534,20 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PINCMD_SET_MODE:
|
case PINCMD_SET_MODE:
|
||||||
if(meta.mux[mi].pin != last_pin) {
|
if (meta.mux[mi].pin != last_pin) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
}
|
}
|
||||||
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
|
||||||
if (mux_i == NULL) return MRAA_ERROR_INVALID_HANDLE;
|
if (mux_i == NULL)
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
last_pin = meta.mux[mi].pin;
|
last_pin = meta.mux[mi].pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = mraa_gpio_mode(mux_i, meta.mux[mi].value);
|
ret = mraa_gpio_mode(mux_i, meta.mux[mi].value);
|
||||||
|
|
||||||
if(ret != MRAA_SUCCESS) {
|
if (ret != MRAA_SUCCESS) {
|
||||||
if (mux_i != NULL) {
|
if (mux_i != NULL) {
|
||||||
mraa_gpio_owner(mux_i, 0);
|
mraa_gpio_owner(mux_i, 0);
|
||||||
mraa_gpio_close(mux_i);
|
mraa_gpio_close(mux_i);
|
||||||
@@ -553,7 +560,8 @@ mraa_setup_mux_mapped(mraa_pin_t meta)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
syslog(LOG_NOTICE, "mraa_setup_mux_mapped: wrong command %d on pin %d with value %d", meta.mux[mi].pincmd, meta.mux[mi].pin, meta.mux[mi].value);
|
syslog(LOG_NOTICE, "mraa_setup_mux_mapped: wrong command %d on pin %d with value %d",
|
||||||
|
meta.mux[mi].pincmd, meta.mux[mi].pin, meta.mux[mi].value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -654,7 +662,8 @@ mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
|
|||||||
pin = mraa_get_sub_platform_index(pin);
|
pin = mraa_get_sub_platform_index(pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM || current_plat->platform_type == MRAA_NULL_PLATFORM) {
|
if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM ||
|
||||||
|
current_plat->platform_type == MRAA_NULL_PLATFORM) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
|
if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
|
||||||
@@ -876,9 +885,9 @@ mraa_get_platform_pin_count(uint8_t platform_offset)
|
|||||||
return mraa_get_pin_count();
|
return mraa_get_pin_count();
|
||||||
else {
|
else {
|
||||||
if (mraa_has_sub_platform())
|
if (mraa_has_sub_platform())
|
||||||
return plat->sub_platform->phy_pin_count;
|
return plat->sub_platform->phy_pin_count;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -920,14 +929,14 @@ mraa_gpio_lookup(const char* pin_name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < plat->phy_pin_count; i++) {
|
for (i = 0; i < plat->phy_pin_count; i++) {
|
||||||
// Skip non GPIO pins
|
// Skip non GPIO pins
|
||||||
if (!(plat->pins[i].capabilities.gpio))
|
if (!(plat->pins[i].capabilities.gpio))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (plat->pins[i].name != NULL &&
|
if (plat->pins[i].name != NULL &&
|
||||||
strncmp(pin_name, plat->pins[i].name, strlen(plat->pins[i].name) + 1) == 0) {
|
strncmp(pin_name, plat->pins[i].name, strlen(plat->pins[i].name) + 1) == 0) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -946,10 +955,10 @@ mraa_i2c_lookup(const char* i2c_name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < plat->i2c_bus_count; i++) {
|
for (i = 0; i < plat->i2c_bus_count; i++) {
|
||||||
if (plat->i2c_bus[i].name != NULL &&
|
if (plat->i2c_bus[i].name != NULL &&
|
||||||
strncmp(i2c_name, plat->i2c_bus[i].name, strlen(plat->i2c_bus[i].name) + 1) == 0) {
|
strncmp(i2c_name, plat->i2c_bus[i].name, strlen(plat->i2c_bus[i].name) + 1) == 0) {
|
||||||
return plat->i2c_bus[i].bus_id;
|
return plat->i2c_bus[i].bus_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -968,10 +977,10 @@ mraa_spi_lookup(const char* spi_name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < plat->spi_bus_count; i++) {
|
for (i = 0; i < plat->spi_bus_count; i++) {
|
||||||
if (plat->spi_bus[i].name != NULL &&
|
if (plat->spi_bus[i].name != NULL &&
|
||||||
strncmp(spi_name, plat->spi_bus[i].name, strlen(plat->spi_bus[i].name) + 1) == 0) {
|
strncmp(spi_name, plat->spi_bus[i].name, strlen(plat->spi_bus[i].name) + 1) == 0) {
|
||||||
return plat->spi_bus[i].bus_id;
|
return plat->spi_bus[i].bus_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -990,10 +999,10 @@ mraa_pwm_lookup(const char* pwm_name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < plat->pwm_dev_count; i++) {
|
for (i = 0; i < plat->pwm_dev_count; i++) {
|
||||||
if (plat->pwm_dev[i].name != NULL &&
|
if (plat->pwm_dev[i].name != NULL &&
|
||||||
strncmp(pwm_name, plat->pwm_dev[i].name, strlen(plat->pwm_dev[i].name) + 1) == 0) {
|
strncmp(pwm_name, plat->pwm_dev[i].name, strlen(plat->pwm_dev[i].name) + 1) == 0) {
|
||||||
return plat->pwm_dev[i].index;
|
return plat->pwm_dev[i].index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1012,10 +1021,10 @@ mraa_uart_lookup(const char* uart_name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < plat->uart_dev_count; i++) {
|
for (i = 0; i < plat->uart_dev_count; i++) {
|
||||||
if (plat->uart_dev[i].name != NULL &&
|
if (plat->uart_dev[i].name != NULL &&
|
||||||
strncmp(uart_name, plat->uart_dev[i].name, strlen(plat->uart_dev[i].name) + 1) == 0) {
|
strncmp(uart_name, plat->uart_dev[i].name, strlen(plat->uart_dev[i].name) + 1) == 0) {
|
||||||
return plat->uart_dev[i].index;
|
return plat->uart_dev[i].index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1029,9 +1038,9 @@ mraa_get_default_i2c_bus(uint8_t platform_offset)
|
|||||||
return plat->def_i2c_bus;
|
return plat->def_i2c_bus;
|
||||||
} else {
|
} else {
|
||||||
if (mraa_has_sub_platform())
|
if (mraa_has_sub_platform())
|
||||||
return plat->sub_platform->def_i2c_bus;
|
return plat->sub_platform->def_i2c_bus;
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1195,23 +1204,22 @@ mraa_count_i2c_files(const char* path, const struct stat* sb, int flag, struct F
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
mraa_find_i2c_bus_pci(const char* pci_device, const char *pci_id, const char* adapter_name)
|
mraa_find_i2c_bus_pci(const char* pci_device, const char* pci_id, const char* adapter_name)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* For example we'd get something like:
|
* For example we'd get something like:
|
||||||
* pci0000:00/0000:00:16.3/i2c_desiignware.3
|
* pci0000:00/0000:00:16.3/i2c_desiignware.3
|
||||||
*/
|
*/
|
||||||
char path[1024];
|
char path[1024];
|
||||||
snprintf(path, 1024-1, "/sys/devices/pci%s/%s/%s/", pci_device, pci_id, adapter_name);
|
snprintf(path, 1024 - 1, "/sys/devices/pci%s/%s/%s/", pci_device, pci_id, adapter_name);
|
||||||
if (mraa_file_exist(path)) {
|
if (mraa_file_exist(path)) {
|
||||||
struct dirent **namelist;
|
struct dirent** namelist;
|
||||||
int n;
|
int n;
|
||||||
n = scandir(path, &namelist, NULL, alphasort);
|
n = scandir(path, &namelist, NULL, alphasort);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
syslog(LOG_ERR, "Failed to get information on i2c");
|
syslog(LOG_ERR, "Failed to get information on i2c");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
while (n--) {
|
while (n--) {
|
||||||
char* dup = strdup(namelist[n]->d_name);
|
char* dup = strdup(namelist[n]->d_name);
|
||||||
char* orig_dup = dup;
|
char* orig_dup = dup;
|
||||||
@@ -1270,7 +1278,7 @@ mraa_find_i2c_bus(const char* devname, int startfrom)
|
|||||||
// i2c devices are numbered numerically so 0 must exist otherwise there is
|
// i2c devices are numbered numerically so 0 must exist otherwise there is
|
||||||
// no i2c-dev loaded
|
// no i2c-dev loaded
|
||||||
if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
|
if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
|
||||||
for (;i < num_i2c_devices; i++) {
|
for (; i < num_i2c_devices; i++) {
|
||||||
off_t size, err;
|
off_t size, err;
|
||||||
snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
|
snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
|
||||||
fd = open(path, O_RDONLY);
|
fd = open(path, O_RDONLY);
|
||||||
@@ -1379,8 +1387,8 @@ mraa_add_subplatform(mraa_platform_t subplatformtype, const char* dev)
|
|||||||
return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
|
return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
|
||||||
}
|
}
|
||||||
int i2c_bus;
|
int i2c_bus;
|
||||||
char *dev_dup = strdup(dev);
|
char* dev_dup = strdup(dev);
|
||||||
if(mraa_atoi(dev_dup, &i2c_bus) != MRAA_SUCCESS && i2c_bus < plat->i2c_bus_count) {
|
if (mraa_atoi(dev_dup, &i2c_bus) != MRAA_SUCCESS && i2c_bus < plat->i2c_bus_count) {
|
||||||
syslog(LOG_NOTICE, "mraa: Cannot add GrovePi subplatform, invalid i2c bus specified");
|
syslog(LOG_NOTICE, "mraa: Cannot add GrovePi subplatform, invalid i2c bus specified");
|
||||||
free(dev_dup);
|
free(dev_dup);
|
||||||
return MRAA_ERROR_INVALID_PARAMETER;
|
return MRAA_ERROR_INVALID_PARAMETER;
|
||||||
@@ -1443,8 +1451,9 @@ mraa_add_from_lockfile(const char* imraa_lock_file)
|
|||||||
int id = -1;
|
int id = -1;
|
||||||
const char* uartdev = NULL;
|
const char* uartdev = NULL;
|
||||||
for (i = 0; i < subplat_num; i++) {
|
for (i = 0; i < subplat_num; i++) {
|
||||||
struct json_object *ioobj = json_object_array_get_idx(ioarray, i);
|
struct json_object* ioobj = json_object_array_get_idx(ioarray, i);
|
||||||
json_object_object_foreach(ioobj, key, val) {
|
json_object_object_foreach(ioobj, key, val)
|
||||||
|
{
|
||||||
if (strncmp(key, "id", strlen("id") + 1) == 0) {
|
if (strncmp(key, "id", strlen("id") + 1) == 0) {
|
||||||
if (mraa_atoi(json_object_get_string(val), &id) != MRAA_SUCCESS) {
|
if (mraa_atoi(json_object_get_string(val), &id) != MRAA_SUCCESS) {
|
||||||
id = -1;
|
id = -1;
|
||||||
@@ -1465,13 +1474,12 @@ mraa_add_from_lockfile(const char* imraa_lock_file)
|
|||||||
}
|
}
|
||||||
if (json_object_object_get_ex(jobj_lock, "IO", &ioarray) == true &&
|
if (json_object_object_get_ex(jobj_lock, "IO", &ioarray) == true &&
|
||||||
json_object_is_type(ioarray, json_type_array)) {
|
json_object_is_type(ioarray, json_type_array)) {
|
||||||
/* assume we have declared IO so we are preinitialised and wipe the
|
/* assume we have declared IO so we are preinitialised and wipe the
|
||||||
* advance func array
|
* advance func array
|
||||||
*/
|
*/
|
||||||
memset(plat->adv_func, 0, sizeof(mraa_adv_func_t));
|
memset(plat->adv_func, 0, sizeof(mraa_adv_func_t));
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
ret = MRAA_ERROR_INVALID_RESOURCE;
|
ret = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
json_object_put(jobj_lock);
|
json_object_put(jobj_lock);
|
||||||
@@ -1598,11 +1606,13 @@ mraa_init_io(const char* desc)
|
|||||||
} else if (strncmp(type, PWM_KEY, strlen(PWM_KEY) + 1) == 0) {
|
} else if (strncmp(type, PWM_KEY, strlen(PWM_KEY) + 1) == 0) {
|
||||||
if (raw) {
|
if (raw) {
|
||||||
if (mraa_init_io_helper(&str, &id, delim) != MRAA_SUCCESS) {
|
if (mraa_init_io_helper(&str, &id, delim) != MRAA_SUCCESS) {
|
||||||
syslog(LOG_ERR, "mraa_init_io: Pwm, unable to convert the chip id string into a useable Int");
|
syslog(LOG_ERR, "mraa_init_io: Pwm, unable to convert the chip id string into a "
|
||||||
|
"useable Int");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (mraa_init_io_helper(&str, &pin, delim) != MRAA_SUCCESS) {
|
if (mraa_init_io_helper(&str, &pin, delim) != MRAA_SUCCESS) {
|
||||||
syslog(LOG_ERR, "mraa_init_io: Pwm, unable to convert the pin string into a useable Int");
|
syslog(LOG_ERR,
|
||||||
|
"mraa_init_io: Pwm, unable to convert the pin string into a useable Int");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return (void*) mraa_pwm_init_raw(id, pin);
|
return (void*) mraa_pwm_init_raw(id, pin);
|
||||||
@@ -1611,11 +1621,13 @@ mraa_init_io(const char* desc)
|
|||||||
} else if (strncmp(type, SPI_KEY, strlen(SPI_KEY) + 1) == 0) {
|
} else if (strncmp(type, SPI_KEY, strlen(SPI_KEY) + 1) == 0) {
|
||||||
if (raw) {
|
if (raw) {
|
||||||
if (mraa_init_io_helper(&str, &id, delim) != MRAA_SUCCESS) {
|
if (mraa_init_io_helper(&str, &id, delim) != MRAA_SUCCESS) {
|
||||||
syslog(LOG_ERR, "mraa_init_io: Spi, unable to convert the bus string into a useable Int");
|
syslog(LOG_ERR,
|
||||||
|
"mraa_init_io: Spi, unable to convert the bus string into a useable Int");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (mraa_init_io_helper(&str, &pin, delim) != MRAA_SUCCESS) {
|
if (mraa_init_io_helper(&str, &pin, delim) != MRAA_SUCCESS) {
|
||||||
syslog(LOG_ERR, "mraa_init_io: Spi, unable to convert the cs string into a useable Int");
|
syslog(LOG_ERR,
|
||||||
|
"mraa_init_io: Spi, unable to convert the cs string into a useable Int");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return (void*) mraa_spi_init_raw(id, pin);
|
return (void*) mraa_spi_init_raw(id, pin);
|
||||||
|
|||||||
Reference in New Issue
Block a user