diff options
Diffstat (limited to 'peripheral/libmraa/src/aio/aio.c')
-rw-r--r-- | peripheral/libmraa/src/aio/aio.c | 97 |
1 files changed, 36 insertions, 61 deletions
diff --git a/peripheral/libmraa/src/aio/aio.c b/peripheral/libmraa/src/aio/aio.c index 29594fd..13119a7 100644 --- a/peripheral/libmraa/src/aio/aio.c +++ b/peripheral/libmraa/src/aio/aio.c @@ -1,7 +1,7 @@ /* * Author: Nandkishor Sonar * Author: Brendan Le Foll <brendan.le.foll@intel.com> - * Copyright (c) 2014-2016 Intel Corporation. + * Copyright (c) 2014, 2015 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -56,7 +56,7 @@ aio_get_valid_fp(mraa_aio_context dev) } static mraa_aio_context -mraa_aio_init_internal(mraa_adv_func_t* func_table, int aio, unsigned int channel) +mraa_aio_init_internal(mraa_adv_func_t* func_table) { mraa_aio_context dev = calloc(1, sizeof(struct _aio)); if (dev == NULL) { @@ -64,72 +64,29 @@ mraa_aio_init_internal(mraa_adv_func_t* func_table, int aio, unsigned int channe } dev->advance_func = func_table; - if (IS_FUNC_DEFINED(dev, aio_init_internal_replace)) { - if (dev->advance_func->aio_init_internal_replace(dev, aio) == MRAA_SUCCESS) { - return dev; - } - free(dev); - return NULL; - } - - dev->channel = channel; - - // Open valid analog input file and get the pointer. - if (MRAA_SUCCESS != aio_get_valid_fp(dev)) { - free(dev); - return NULL; - } - return dev; } mraa_aio_context mraa_aio_init(unsigned int aio) { - mraa_board_t* board = plat; - int pin; - if (board == NULL) { + if (plat == NULL) { syslog(LOG_ERR, "aio: Platform not initialised"); return NULL; } if (mraa_is_sub_platform_id(aio)) { - syslog(LOG_NOTICE, "aio: Using sub platform"); - board = board->sub_platform; - if (board == NULL) { - syslog(LOG_ERR, "aio: Sub platform Not Initialised"); - return NULL; - } - aio = mraa_get_sub_platform_index(aio); - } - - // aio are always past the gpio_count in the pin array - pin = aio + board->gpio_count; - - if (pin < 0 || pin >= board->phy_pin_count) { - syslog(LOG_ERR, "aio: pin %i beyond platform definition", pin); - return NULL; - } - if (aio >= board->aio_count) { - syslog(LOG_ERR, "aio: requested channel out of range"); + syslog(LOG_NOTICE, "aio: Using sub platform is not supported"); return NULL; } - if (board->pins[pin].capabilites.aio != 1) { - syslog(LOG_ERR, "aio: pin %i not capable of aio", pin); - return NULL; - } - if (board->pins[pin].aio.mux_total > 0) { - if (mraa_setup_mux_mapped(board->pins[pin].aio) != MRAA_SUCCESS) { - syslog(LOG_ERR, "aio: unable to setup multiplexers for pin"); - return NULL; - } - } // Create ADC device connected to specified channel - mraa_aio_context dev = mraa_aio_init_internal(board->adv_func, aio, board->pins[pin].aio.pinmap); + mraa_aio_context dev = mraa_aio_init_internal(plat->adv_func); if (dev == NULL) { syslog(LOG_ERR, "aio: Insufficient memory for specified input channel %d", aio); return NULL; } + int pin = aio + plat->gpio_count; + dev->channel = plat->pins[pin].aio.pinmap; dev->value_bit = DEFAULT_BITS; if (IS_FUNC_DEFINED(dev, aio_init_pre)) { @@ -139,6 +96,32 @@ mraa_aio_init(unsigned int aio) return NULL; } } + if (aio > plat->aio_count) { + syslog(LOG_ERR, "aio: requested channel out of range"); + free(dev); + return NULL; + } + + if (plat->pins[pin].capabilites.aio != 1) { + syslog(LOG_ERR, "aio: pin uncapable of aio"); + free(dev); + return NULL; + } + + if (plat->pins[pin].aio.mux_total > 0) { + if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS) { + free(dev); + syslog(LOG_ERR, "aio: unable to setup multiplexers for pin"); + return NULL; + } + } + + // Open valid analog input file and get the pointer. + if (MRAA_SUCCESS != aio_get_valid_fp(dev)) { + free(dev); + return NULL; + } + raw_bits = mraa_adc_raw_bits(); if (IS_FUNC_DEFINED(dev, aio_init_post)) { mraa_result_t ret = dev->advance_func->aio_init_post(dev); @@ -148,25 +131,19 @@ mraa_aio_init(unsigned int aio) } } - raw_bits = mraa_adc_raw_bits(); - return dev; } -int +unsigned int mraa_aio_read(mraa_aio_context dev) { - if (IS_FUNC_DEFINED(dev, aio_read_replace)) { - return dev->advance_func->aio_read_replace(dev); - } - char buffer[17]; unsigned int shifter_value = 0; if (dev->adc_in_fp == -1) { if (aio_get_valid_fp(dev) != MRAA_SUCCESS) { syslog(LOG_ERR, "aio: Failed to get to the device"); - return -1; + return 0; } } @@ -183,10 +160,8 @@ mraa_aio_read(mraa_aio_context dev) unsigned int analog_value = (unsigned int) strtoul(buffer, &end, 10); if (end == &buffer[0]) { syslog(LOG_ERR, "aio: Value is not a decimal number"); - return -1; } else if (errno != 0) { syslog(LOG_ERR, "aio: Errno was set"); - return -1; } if (dev->value_bit != raw_bits) { @@ -208,7 +183,7 @@ mraa_aio_read_float(mraa_aio_context dev) { if (dev == NULL) { syslog(LOG_ERR, "aio: Device not valid"); - return -1.0; + return 0.0; } float max_analog_value = (1 << dev->value_bit) - 1; |