2 * Author: Nandkishor Sonar
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include "maa_internal.h"
37 static maa_result_t aio_get_valid_fp(maa_aio_context dev)
39 char file_path[64]= "";
41 //Open file Analog device input channel raw voltage file for reading.
42 snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw",
45 dev->adc_in_fp = open(file_path, O_RDONLY);
46 if (dev->adc_in_fp == -1) {
47 fprintf(stderr, "Failed to open Analog input raw file %s for "
48 "reading!\n", file_path); return( MAA_ERROR_INVALID_RESOURCE);
54 /** Initialise an Analog input, connected to the specified channel
56 * @param aio_channel Analog input channel to read
58 * @returns pointer to maa_aio_context structure after initialisation of
59 * Analog input pin connected to the device successfully, else returns NULL.
61 maa_aio_context maa_aio_init(unsigned int aio_channel)
63 int checked_pin = maa_setup_aio(aio_channel);
64 if (checked_pin < 0) {
67 fprintf(stderr, "Invalid analog input channel %d specified\n",
71 fprintf(stderr, "Failed to set-up analog input channel %d "
72 "multiplexer\n", aio_channel);
75 fprintf(stderr, "Platform not initialised");
81 //Create ADC device connected to specified channel
82 maa_aio_context dev = malloc(sizeof(struct _aio));
84 fprintf(stderr, "Insufficient memory for specified Analog input channel "
88 dev->channel = checked_pin;
90 //Open valid analog input file and get the pointer.
91 if (MAA_SUCCESS != aio_get_valid_fp(dev)) {
99 /** Read the input voltage, represented as an unsigned short in the range [0x0,
102 * @param pointer to maa_aio_context structure initialised by
106 * unsigned 16 bit int representing the current input voltage, normalised to
109 uint16_t maa_aio_read(maa_aio_context dev)
112 unsigned int shifter_value = 0;
114 if (dev->adc_in_fp == -1) {
115 aio_get_valid_fp(dev);
118 lseek(dev->adc_in_fp, 0, SEEK_SET);
119 if (read(dev->adc_in_fp, buffer, sizeof(buffer)) < 1) {
120 fprintf(stderr, "Failed to read a sensible value\n");
122 lseek(dev->adc_in_fp, 0, SEEK_SET);
126 uint16_t analog_value = (uint16_t) strtoul(buffer, &end, 10);
127 if (end == &buffer[0]) {
128 fprintf(stderr, "%s is not a decimal number\n", buffer);
130 else if (errno != 0) {
131 fprintf(stderr, "errno was set\n");
134 /* Adjust the raw analog input reading to supported resolution value*/
135 if (ADC_RAW_RESOLUTION_BITS > ADC_SUPPORTED_RESOLUTION_BITS) {
136 shifter_value = ADC_RAW_RESOLUTION_BITS - ADC_SUPPORTED_RESOLUTION_BITS;
137 analog_value = analog_value >> shifter_value;
139 shifter_value = ADC_SUPPORTED_RESOLUTION_BITS - ADC_RAW_RESOLUTION_BITS;
140 analog_value = analog_value << shifter_value;
146 /** Close the analog input and free context memory
148 * @param dev - the analog input context
150 * @return maa result type.
152 maa_result_t maa_aio_close(maa_aio_context dev)