2014-05-01 16:36:11 +01:00
|
|
|
/*
|
|
|
|
|
* Author: Nandkishor Sonar
|
|
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
{
|
2014-07-28 14:12:28 +01:00
|
|
|
if (advance_func->aio_get_valid_fp != NULL)
|
|
|
|
|
return advance_func->aio_get_valid_fp(dev);
|
2014-09-24 12:06:10 +01:00
|
|
|
|
2014-05-01 16:36:11 +01:00
|
|
|
char file_path[64]= "";
|
|
|
|
|
|
|
|
|
|
//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",
|
2014-09-24 12:06:10 +01:00
|
|
|
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) {
|
2014-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "Failed to open Analog 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
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:06:10 +01:00
|
|
|
mraa_aio_context
|
|
|
|
|
mraa_aio_init(unsigned int aio_channel)
|
2014-05-01 16:36:11 +01:00
|
|
|
{
|
2014-07-28 14:12:28 +01:00
|
|
|
if (advance_func->aio_init_pre != NULL) {
|
|
|
|
|
mraa_result_t pre_ret = (advance_func->aio_init_pre(aio_channel));
|
2014-09-24 12:06:10 +01:00
|
|
|
if (pre_ret != MRAA_SUCCESS)
|
2014-07-28 14:12:28 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
int checked_pin = mraa_setup_aio(aio_channel);
|
2014-05-02 11:52:40 +01:00
|
|
|
if (checked_pin < 0) {
|
2014-09-24 12:06:10 +01:00
|
|
|
switch (checked_pin) {
|
2014-09-25 14:31:04 +01:00
|
|
|
case MRAA_NO_SUCH_IO:
|
|
|
|
|
syslog(LOG_ERR, "Invalid analog input channel %d specified",
|
2014-05-21 12:58:55 +01:00
|
|
|
aio_channel);
|
2014-05-02 11:52:40 +01:00
|
|
|
return NULL;
|
2014-09-25 14:31:04 +01:00
|
|
|
case MRAA_IO_SETUP_FAILURE:
|
|
|
|
|
syslog(LOG_ERR, "Failed to set-up analog input channel %d "
|
|
|
|
|
"multiplexer", aio_channel);
|
2014-05-02 11:52:40 +01:00
|
|
|
return NULL;
|
2014-09-25 14:31:04 +01:00
|
|
|
case MRAA_PLATFORM_NO_INIT:
|
|
|
|
|
syslog(LOG_ERR, "Platform not initialised");
|
2014-05-02 11:52:40 +01:00
|
|
|
return NULL;
|
2014-09-24 12:06:10 +01:00
|
|
|
default:
|
|
|
|
|
return NULL;
|
2014-05-02 11:52:40 +01:00
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Create ADC device connected to specified channel
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_aio_context dev = malloc(sizeof(struct _aio));
|
2014-05-13 21:59:22 +00:00
|
|
|
if (dev == NULL) {
|
2014-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "Insufficient memory for specified Analog input channel "
|
2014-09-24 12:06:10 +01:00
|
|
|
"%d\n", aio_channel);
|
2014-05-01 16:36:11 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-05-02 11:52:40 +01:00
|
|
|
dev->channel = checked_pin;
|
2014-09-22 14:22:17 +01:00
|
|
|
dev->value_bit = DEFAULT_BITS;
|
2014-05-01 16:36:11 +01:00
|
|
|
|
|
|
|
|
//Open valid analog input file and get the pointer.
|
2014-06-24 17:24:54 +01:00
|
|
|
if (MRAA_SUCCESS != aio_get_valid_fp(dev)) {
|
2014-05-01 16:36:11 +01:00
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-06-25 17:32:41 +01:00
|
|
|
raw_bits = mraa_adc_raw_bits();
|
2014-05-01 16:36:11 +01:00
|
|
|
|
2014-07-28 14:12:28 +01:00
|
|
|
if (advance_func->aio_init_post != NULL) {
|
|
|
|
|
mraa_result_t ret = advance_func->aio_init_post(dev);
|
|
|
|
|
if (ret != MRAA_SUCCESS) {
|
|
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-24 12:06:10 +01:00
|
|
|
|
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
|
|
|
{
|
2014-05-13 22:10:41 +00:00
|
|
|
char buffer[16];
|
|
|
|
|
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-05-01 16:36:11 +01:00
|
|
|
aio_get_valid_fp(dev);
|
|
|
|
|
}
|
|
|
|
|
|
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-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "Failed to read a sensible value");
|
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;
|
|
|
|
|
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-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "%s is not a decimal number", buffer);
|
2014-05-13 22:10:41 +00:00
|
|
|
}
|
|
|
|
|
else if (errno != 0) {
|
2014-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "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;
|
2014-06-25 17:32:41 +01:00
|
|
|
analog_value = analog_value >> shifter_value;
|
|
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
if (NULL != dev)
|
|
|
|
|
free(dev);
|
|
|
|
|
|
2014-06-24 17:24:54 +01: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
|
|
|
{
|
|
|
|
|
if (dev == NULL) {
|
2014-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "AIO Device not valid");
|
2014-09-22 14:22:17 +01:00
|
|
|
return MRAA_ERROR_INVALID_RESOURCE;
|
|
|
|
|
}
|
|
|
|
|
if (bits < 1) {
|
2014-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "AIO Device not valid");
|
2014-09-22 14:22:17 +01:00
|
|
|
return MRAA_ERROR_INVALID_PARAMETER;
|
|
|
|
|
}
|
|
|
|
|
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-09-25 14:31:04 +01:00
|
|
|
syslog(LOG_ERR, "AIO Device not valid");
|
2014-09-22 14:22:17 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return dev->value_bit;
|
|
|
|
|
}
|