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-05-15 22:47:38 +01:00
|
|
|
struct _aio {
|
|
|
|
|
unsigned int channel;
|
|
|
|
|
int adc_in_fp;
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-25 17:32:41 +01:00
|
|
|
static int raw_bits;
|
|
|
|
|
static int sup_bits;
|
|
|
|
|
|
2014-06-24 17:24:54 +01:00
|
|
|
static mraa_result_t aio_get_valid_fp(mraa_aio_context dev)
|
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",
|
|
|
|
|
dev->channel );
|
|
|
|
|
|
2014-05-13 21:59:54 +00:00
|
|
|
dev->adc_in_fp = open(file_path, O_RDONLY);
|
|
|
|
|
if (dev->adc_in_fp == -1) {
|
2014-06-25 14:58:15 +01:00
|
|
|
fprintf(stderr, "Failed to open Analog input raw file %s for "
|
|
|
|
|
"reading!\n", file_path); 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Initialise an Analog input, connected to the specified channel
|
|
|
|
|
*
|
|
|
|
|
* @param aio_channel Analog input channel to read
|
|
|
|
|
*
|
2014-06-24 17:24:54 +01:00
|
|
|
* @returns pointer to mraa_aio_context structure after initialisation of
|
2014-05-01 16:36:11 +01:00
|
|
|
* Analog input pin connected to the device successfully, else returns NULL.
|
|
|
|
|
*/
|
2014-06-24 17:24:54 +01:00
|
|
|
mraa_aio_context mraa_aio_init(unsigned int aio_channel)
|
2014-05-01 16:36:11 +01:00
|
|
|
{
|
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) {
|
|
|
|
|
switch(checked_pin) {
|
|
|
|
|
case -1:
|
2014-05-13 21:59:22 +00:00
|
|
|
fprintf(stderr, "Invalid analog input channel %d specified\n",
|
2014-05-21 12:58:55 +01:00
|
|
|
aio_channel);
|
2014-05-02 11:52:40 +01:00
|
|
|
return NULL;
|
|
|
|
|
case -2:
|
2014-05-13 21:59:22 +00:00
|
|
|
fprintf(stderr, "Failed to set-up analog input channel %d "
|
|
|
|
|
"multiplexer\n", aio_channel);
|
2014-05-02 11:52:40 +01:00
|
|
|
return NULL;
|
|
|
|
|
case -3:
|
2014-05-13 21:59:22 +00:00
|
|
|
fprintf(stderr, "Platform not initialised");
|
2014-05-02 11:52:40 +01:00
|
|
|
return NULL;
|
|
|
|
|
default: return NULL;
|
|
|
|
|
}
|
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) {
|
|
|
|
|
fprintf(stderr, "Insufficient memory for specified Analog input channel "
|
|
|
|
|
"%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-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();
|
|
|
|
|
sup_bits = mraa_adc_supported_bits();
|
2014-05-01 16:36:11 +01:00
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Read the input voltage, represented as an unsigned short in the range [0x0,
|
|
|
|
|
* 0xFFFF]
|
|
|
|
|
*
|
2014-06-24 17:24:54 +01:00
|
|
|
* @param pointer to mraa_aio_context structure initialised by
|
|
|
|
|
* mraa_aio_init()
|
2014-05-01 16:36:11 +01:00
|
|
|
*
|
|
|
|
|
* @returns
|
2014-05-13 22:25:21 +00:00
|
|
|
* unsigned 16 bit int representing the current input voltage, normalised to
|
2014-05-01 16:36:11 +01:00
|
|
|
* a 16-bit value
|
|
|
|
|
*/
|
2014-06-24 17:24:54 +01:00
|
|
|
uint16_t 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-05-13 22:10:41 +00:00
|
|
|
fprintf(stderr, "Failed to read a sensible value\n");
|
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-05-26 16:05:17 +01:00
|
|
|
uint16_t analog_value = (uint16_t) strtoul(buffer, &end, 10);
|
2014-05-13 22:10:41 +00:00
|
|
|
if (end == &buffer[0]) {
|
|
|
|
|
fprintf(stderr, "%s is not a decimal number\n", buffer);
|
|
|
|
|
}
|
|
|
|
|
else if (errno != 0) {
|
|
|
|
|
fprintf(stderr, "errno was set\n");
|
|
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
|
|
|
|
|
/* Adjust the raw analog input reading to supported resolution value*/
|
2014-06-25 17:32:41 +01:00
|
|
|
if (raw_bits =! sup_bits) {
|
|
|
|
|
if (raw_bits > sup_bits) {
|
|
|
|
|
shifter_value = raw_bits - sup_bits;
|
|
|
|
|
analog_value = analog_value >> shifter_value;
|
|
|
|
|
} else {
|
|
|
|
|
shifter_value = sup_bits - raw_bits;
|
|
|
|
|
analog_value = analog_value << shifter_value;
|
|
|
|
|
}
|
2014-05-01 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return analog_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Close the analog input and free context memory
|
|
|
|
|
*
|
|
|
|
|
* @param dev - the analog input context
|
|
|
|
|
*
|
2014-06-24 17:24:54 +01:00
|
|
|
* @return mraa result type.
|
2014-05-01 16:36:11 +01:00
|
|
|
*/
|
2014-06-24 17:24:54 +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
|
|
|
}
|