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.
29 static maa_result_t aio_get_valid_fp(maa_aio_context* dev)
31 char file_path[64]= "";
33 //Open file Analog device input channel raw voltage file for reading.
34 snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw",
37 if (NULL == (dev->adc_in_fp = fopen(file_path, "r"))) {
38 fprintf(stderr, "Failed to open Analog input raw file %s for "
39 "reading!\n", file_path); return( MAA_ERROR_INVALID_RESOURCE);
45 /** Initialise an Analog input, connected to the specified channel
47 * @param aio_channel Analog input channel to read
49 * @returns pointer to maa_aio_context structure after initialisation of
50 * Analog input pin connected to the device successfully, else returns NULL.
52 maa_aio_context* maa_aio_init(unsigned int aio_channel)
56 int checked_pin = maa_check_aio(aio_channel);
57 if (checked_pin < 0) {
60 fprintf(stderr, "Invalid analog input channel %d specified\n",
64 fprintf(stderr, "Failed to set-up analog input channel %d "
65 "multiplexer\n", aio_channel);
68 fprintf(stderr, "Platform not initialised");
74 //Create ADC device connected to specified channel
75 dev = (maa_aio_context*) malloc(sizeof(maa_aio_context));
77 fprintf(stderr, "Insufficient memory for specified Analog input channel "
81 dev->channel = checked_pin;
83 //Open valid analog input file and get the pointer.
84 if (MAA_SUCCESS != aio_get_valid_fp(dev)) {
92 /** Read the input voltage, represented as an unsigned short in the range [0x0,
95 * @param pointer to maa_aio_context structure initialised by
99 * 16-bit unsigned int representing the current input voltage, normalised to
102 unsigned int maa_aio_read_u16(maa_aio_context* dev)
104 char buffer[16] = "";
105 unsigned int raw_value=0;
106 unsigned int analog_value=0;
107 unsigned int shifter_value=0;
109 if (NULL == dev->adc_in_fp) {
110 aio_get_valid_fp(dev);
113 fseek(dev->adc_in_fp, SEEK_SET, 0);
114 fread(buffer, sizeof(buffer), 1, dev->adc_in_fp);
115 fseek(dev->adc_in_fp, SEEK_SET, 0);
117 raw_value = atoi(buffer);
119 /* Adjust the raw analog input reading to supported resolution value*/
120 if (ADC_RAW_RESOLUTION_BITS == ADC_SUPPORTED_RESOLUTION_BITS) {
121 analog_value = raw_value;
124 if (ADC_RAW_RESOLUTION_BITS > ADC_SUPPORTED_RESOLUTION_BITS) {
125 shifter_value = ADC_RAW_RESOLUTION_BITS - ADC_SUPPORTED_RESOLUTION_BITS;
126 analog_value = raw_value >> shifter_value;
128 shifter_value = ADC_SUPPORTED_RESOLUTION_BITS - ADC_RAW_RESOLUTION_BITS;
129 analog_value = raw_value << shifter_value;
136 /** Close the analog input and free context memory
138 * @param dev - the analog input context
140 * @return maa result type.
142 maa_result_t maa_aio_close(maa_aio_context* dev)