2014-05-01 16:36:11 +01:00
|
|
|
/*
|
|
|
|
|
* Author: Nandkishor Sonar
|
2015-09-03 15:28:36 +01:00
|
|
|
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
2016-03-07 14:49:42 +00:00
|
|
|
* Copyright (c) 2014-2016 Intel Corporation.
|
2014-05-01 16:36:11 +01:00
|
|
|
*
|
|
|
|
|
* 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 <stdlib.h>
|
2014-05-13 21:59:54 +00:00
|
|
|
#include <fcntl.h>
|
2014-05-13 22:10:41 +00:00
|
|
|
#include <errno.h>
|
2014-05-01 16:36:11 +01:00
|
|
|
|
|
|
|
|
#include "aio.h"
|
2014-06-24 17:24:54 +01:00
|
|
|
#include "mraa_internal.h"
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2014-09-22 14:22:17 +01:00
|
|
|
#define DEFAULT_BITS 10
|
|
|
|
|
|
2014-06-25 17:32:41 +01:00
|
|
|
static int raw_bits;
|
|
|
|
|
|
2014-09-24 12:06:10 +01:00
|
|
|
static mraa_result_t
|
|
|
|
|
aio_get_valid_fp(mraa_aio_context dev)
|
2014-05-01 16:36:11 +01:00
|
|
|
{
|
2015-09-03 15:28:36 +01:00
|
|
|
if (IS_FUNC_DEFINED(dev, aio_get_valid_fp)) {
|
|
|
|
|
return dev->advance_func->aio_get_valid_fp(dev);
|
|
|
|
|
}
|
2014-09-24 12:06:10 +01:00
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
char file_path[64] = "";
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
// Open file Analog device input channel raw voltage file for reading.
|
|
|
|
|
snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw", dev->channel);
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2014-05-13 21:59:54 +00:00
|
|
|
dev->adc_in_fp = open(file_path, O_RDONLY);
|
|
|
|
|
if (dev->adc_in_fp == -1) {
|
2015-03-23 14:39:12 +00:00
|
|
|
syslog(LOG_ERR, "aio: Failed to open input raw file %s for reading!", file_path);
|
2014-09-24 12:06:10 +01:00
|
|
|
return MRAA_ERROR_INVALID_RESOURCE;
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
return MRAA_SUCCESS;
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-03 15:28:36 +01:00
|
|
|
static mraa_aio_context
|
2016-03-07 14:49:42 +00:00
|
|
|
mraa_aio_init_internal(mraa_adv_func_t* func_table, int aio)
|
2015-09-03 15:28:36 +01:00
|
|
|
{
|
mraa: Prefer calloc over malloc
Switch to using calloc on all calls to malloc where the memory isn't
initialized. For things like the mraa_board_t, not allocating all to zero
causes issues such as with the sub_platform member, where if that's not zero
mraa_get_platform_type will try to dereference a random memory location for the
sub_platform->platform_name, which can result in segmentation faults and other
issues.
Note that in some places where immediately after the malloc call is a copy
operation, there is no need for calloc, as all the memory gets overwritten
anyways, but in cases where there may or may not be memory written to (such as
in mraa_file_contains, with reading from a file), even though in most cases the
memory is overwritten, it could be the case that the read operation does
nothing, but the memory still has non-zero values, by virtue of the fact it
wasn't overwritten.
Signed-off-by: Ian Johnson <ijohnson@wolfram.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-01-17 09:39:45 -06:00
|
|
|
mraa_aio_context dev = calloc(1, sizeof(struct _aio));
|
2015-09-03 15:28:36 +01:00
|
|
|
if (dev == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
dev->advance_func = func_table;
|
2015-09-08 14:58:55 +01:00
|
|
|
|
2016-03-07 14:49:42 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open valid analog input file and get the pointer.
|
|
|
|
|
if (MRAA_SUCCESS != aio_get_valid_fp(dev)) {
|
|
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 14:58:55 +01:00
|
|
|
return dev;
|
2015-09-03 15:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:06:10 +01:00
|
|
|
mraa_aio_context
|
2014-11-03 15:16:33 +00:00
|
|
|
mraa_aio_init(unsigned int aio)
|
2014-05-01 16:36:11 +01:00
|
|
|
{
|
2016-02-16 14:53:58 +00:00
|
|
|
mraa_board_t* board = plat;
|
|
|
|
|
int pin;
|
|
|
|
|
if (board == NULL) {
|
2014-11-03 15:16:33 +00:00
|
|
|
syslog(LOG_ERR, "aio: Platform not initialised");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-09-03 15:28:36 +01:00
|
|
|
if (mraa_is_sub_platform_id(aio)) {
|
2016-02-16 14:53:58 +00:00
|
|
|
syslog(LOG_NOTICE, "aio: Using sub platform");
|
|
|
|
|
board = board->sub_platform;
|
|
|
|
|
if (board == NULL) {
|
|
|
|
|
syslog(LOG_ERR, "aio: Sub platform Not Initialised");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-03-07 14:49:42 +00:00
|
|
|
aio = mraa_get_sub_platform_index(aio);
|
2015-09-03 15:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 14:49:42 +00:00
|
|
|
// aio are always past the gpio_count in the pin array
|
2016-02-16 14:53:58 +00:00
|
|
|
pin = aio + board->gpio_count;
|
2015-09-03 15:28:36 +01:00
|
|
|
|
2016-03-07 14:49:42 +00:00
|
|
|
if (pin < 0 || pin >= board->phy_pin_count) {
|
|
|
|
|
syslog(LOG_ERR, "aio: pin %i beyond platform definition", pin);
|
|
|
|
|
return NULL;
|
2014-07-28 14:12:28 +01:00
|
|
|
}
|
2016-02-18 17:22:22 +00:00
|
|
|
if (aio >= board->aio_count) {
|
2014-11-03 15:16:33 +00:00
|
|
|
syslog(LOG_ERR, "aio: requested channel out of range");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-02-16 14:53:58 +00:00
|
|
|
if (board->pins[pin].capabilites.aio != 1) {
|
2016-03-07 14:49:42 +00:00
|
|
|
syslog(LOG_ERR, "aio: pin %i not capable of aio", pin);
|
2014-11-03 15:16:33 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-02-16 14:53:58 +00:00
|
|
|
if (board->pins[pin].aio.mux_total > 0) {
|
|
|
|
|
if (mraa_setup_mux_mapped(board->pins[pin].aio) != MRAA_SUCCESS) {
|
2014-11-03 15:16:33 +00:00
|
|
|
syslog(LOG_ERR, "aio: unable to setup multiplexers for pin");
|
|
|
|
|
return NULL;
|
2014-05-02 11:52:40 +01:00
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 14:49:42 +00:00
|
|
|
// Create ADC device connected to specified channel
|
|
|
|
|
mraa_aio_context dev = mraa_aio_init_internal(board->adv_func, aio);
|
|
|
|
|
if (dev == NULL) {
|
|
|
|
|
syslog(LOG_ERR, "aio: Insufficient memory for specified input channel %d", aio);
|
2014-05-01 16:36:11 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-03-07 14:49:42 +00:00
|
|
|
dev->channel = board->pins[pin].aio.pinmap;
|
|
|
|
|
dev->value_bit = DEFAULT_BITS;
|
|
|
|
|
|
|
|
|
|
if (IS_FUNC_DEFINED(dev, aio_init_pre)) {
|
|
|
|
|
mraa_result_t pre_ret = (dev->advance_func->aio_init_pre(aio));
|
|
|
|
|
if (pre_ret != MRAA_SUCCESS) {
|
|
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2015-09-03 15:28:36 +01:00
|
|
|
if (IS_FUNC_DEFINED(dev, aio_init_post)) {
|
|
|
|
|
mraa_result_t ret = dev->advance_func->aio_init_post(dev);
|
2014-07-28 14:12:28 +01:00
|
|
|
if (ret != MRAA_SUCCESS) {
|
|
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-24 12:06:10 +01:00
|
|
|
|
2016-03-07 14:49:42 +00:00
|
|
|
raw_bits = mraa_adc_raw_bits();
|
|
|
|
|
|
2014-05-01 16:36:11 +01:00
|
|
|
return dev;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:06:10 +01:00
|
|
|
unsigned int
|
|
|
|
|
mraa_aio_read(mraa_aio_context dev)
|
2014-05-01 16:36:11 +01:00
|
|
|
{
|
2016-03-07 14:49:42 +00:00
|
|
|
if (IS_FUNC_DEFINED(dev, aio_read_replace)) {
|
|
|
|
|
return dev->advance_func->aio_read_replace(dev);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 22:50:18 +01:00
|
|
|
char buffer[17];
|
2014-05-13 22:10:41 +00:00
|
|
|
unsigned int shifter_value = 0;
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2014-05-13 21:59:54 +00:00
|
|
|
if (dev->adc_in_fp == -1) {
|
2014-11-26 13:30:10 +00:00
|
|
|
if (aio_get_valid_fp(dev) != MRAA_SUCCESS) {
|
|
|
|
|
syslog(LOG_ERR, "aio: Failed to get to the device");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-13 21:59:54 +00:00
|
|
|
lseek(dev->adc_in_fp, 0, SEEK_SET);
|
|
|
|
|
if (read(dev->adc_in_fp, buffer, sizeof(buffer)) < 1) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "aio: Failed to read a sensible value");
|
2014-05-13 21:59:54 +00:00
|
|
|
}
|
2014-10-03 22:50:18 +01:00
|
|
|
// force NULL termination of string
|
|
|
|
|
buffer[16] = '\0';
|
2014-05-13 21:59:54 +00:00
|
|
|
lseek(dev->adc_in_fp, 0, SEEK_SET);
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2014-05-13 22:10:41 +00:00
|
|
|
errno = 0;
|
2015-03-23 14:39:12 +00:00
|
|
|
char* end;
|
2014-09-22 15:29:02 +01:00
|
|
|
unsigned int analog_value = (unsigned int) strtoul(buffer, &end, 10);
|
2014-05-13 22:10:41 +00:00
|
|
|
if (end == &buffer[0]) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "aio: Value is not a decimal number");
|
2015-03-23 14:39:12 +00:00
|
|
|
} else if (errno != 0) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "aio: Errno was set");
|
2014-05-13 22:10:41 +00:00
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2014-09-22 14:22:17 +01:00
|
|
|
if (dev->value_bit != raw_bits) {
|
|
|
|
|
/* Adjust the raw analog input reading to supported resolution value*/
|
|
|
|
|
if (raw_bits > dev->value_bit) {
|
|
|
|
|
shifter_value = raw_bits - dev->value_bit;
|
2015-03-23 14:39:12 +00:00
|
|
|
analog_value = analog_value >> shifter_value;
|
2014-06-25 17:32:41 +01:00
|
|
|
} else {
|
2014-09-22 14:22:17 +01:00
|
|
|
shifter_value = dev->value_bit - raw_bits;
|
2014-06-25 17:32:41 +01:00
|
|
|
analog_value = analog_value << shifter_value;
|
|
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return analog_value;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-01 20:54:13 +01:00
|
|
|
float
|
|
|
|
|
mraa_aio_read_float(mraa_aio_context dev)
|
|
|
|
|
{
|
|
|
|
|
if (dev == NULL) {
|
|
|
|
|
syslog(LOG_ERR, "aio: Device not valid");
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float max_analog_value = (1 << dev->value_bit) - 1;
|
|
|
|
|
unsigned int analog_value_int = mraa_aio_read(dev);
|
|
|
|
|
|
|
|
|
|
return analog_value_int / max_analog_value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:06:10 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_aio_close(mraa_aio_context dev)
|
2014-05-01 16:36:11 +01:00
|
|
|
{
|
2014-12-06 07:58:00 -08:00
|
|
|
if (NULL != dev) {
|
|
|
|
|
if (dev->adc_in_fp != -1)
|
|
|
|
|
close(dev->adc_in_fp);
|
2014-05-01 16:36:11 +01:00
|
|
|
free(dev);
|
2014-12-06 07:58:00 -08:00
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
return (MRAA_SUCCESS);
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
2014-09-22 14:22:17 +01:00
|
|
|
|
2014-09-24 12:06:10 +01:00
|
|
|
mraa_result_t
|
|
|
|
|
mraa_aio_set_bit(mraa_aio_context dev, int bits)
|
2014-09-22 14:22:17 +01:00
|
|
|
{
|
2014-10-22 21:06:45 +01:00
|
|
|
if (dev == NULL || bits < 1) {
|
|
|
|
|
syslog(LOG_ERR, "aio: Device not valid");
|
2014-09-22 14:22:17 +01:00
|
|
|
return MRAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
|
|
|
|
dev->value_bit = bits;
|
|
|
|
|
return MRAA_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:06:10 +01:00
|
|
|
int
|
|
|
|
|
mraa_aio_get_bit(mraa_aio_context dev)
|
2014-09-22 14:22:17 +01:00
|
|
|
{
|
|
|
|
|
if (dev == NULL) {
|
2014-10-22 21:06:45 +01:00
|
|
|
syslog(LOG_ERR, "aio: Device not valid");
|
2014-09-22 14:22:17 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return dev->value_bit;
|
|
|
|
|
}
|