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 /** Configure multiplexer for Analog Input
47 * @param aio_channel = Analog input channel to read
49 * @return maa_result_t - result type.
52 static maa_result_t maa_aio_set_mux(unsigned int aio_channel)
55 maa_gpio_context* aio_gate;
57 //Initialise VINx multiplexer gate pins
58 aio_gate = maa_gpio_init(adc_gate_pins[aio_channel]);
60 if (NULL == aio_gate) {
61 fprintf(stderr, "Failed to initialise first gate pin %d for ADC "
62 "channel %d !\n", adc_gate_pins[aio_channel], aio_channel);
63 return MAA_ERROR_INVALID_RESOURCE;
66 //Set direction to output for the ADC input gate
67 result = maa_gpio_dir(aio_gate, MAA_GPIO_OUT);
69 if (MAA_SUCCESS == result) {
70 // Write gate configuration output value
71 result = maa_gpio_write(aio_gate, 0);
73 if (MAA_SUCCESS == result) {
74 //For A4 and A5 Analog common gate pin should be high for the
75 // Galileo board revision D
76 if (A4 == aio_channel || A5 == aio_channel) {
77 aio_gate = maa_gpio_init(ADC_COMMON_GATE_A4_A5);
79 //Set direction to output for the gate
80 if (NULL == aio_gate) {
81 fprintf(stderr, "Failed to initialise second gate pin %d "
82 "for ADC channel %d !\n", ADC_COMMON_GATE_A4_A5,
84 return(MAA_ERROR_INVALID_RESOURCE);
87 result = maa_gpio_dir(aio_gate, MAA_GPIO_OUT);
88 if (MAA_SUCCESS == result)
89 // Write gate configuration output value
90 result = maa_gpio_write(aio_gate, 1);
101 /** Initialise an Analog input, connected to the specified channel
103 * @param aio_channel Analog input channel to read
105 * @returns pointer to maa_aio_context structure after initialisation of
106 * Analog input pin connected to the device successfully, else returns NULL.
108 maa_aio_context* maa_aio_init(unsigned int aio_channel)
110 maa_aio_context* dev;
112 // Validate input pins 0-5
113 if (aio_channel > TOTAL_ANALOG_INPUTS_ON_BOARD) {
114 fprintf(stderr, "Invalid Analog input channel %d specified!\n",
119 //Set-up multiplexer for the Analog input channel
120 if (MAA_SUCCESS != maa_aio_set_mux(aio_channel)) {
121 fprintf(stderr, "Failed to set-up Analog input channel %d "
122 "multiplexer!\n", aio_channel); return NULL;
125 //Create ADC device connected to specified channel
126 dev = (maa_aio_context*) malloc(sizeof(maa_aio_context));
128 fprintf(stderr, "Insufficient memory for specified Analog input channel "
129 "%d !\n", aio_channel);
132 dev->channel = aio_channel;
134 //Open valid analog input file and get the pointer.
135 if (MAA_SUCCESS != aio_get_valid_fp(dev)) {
143 /** Read the input voltage, represented as an unsigned short in the range [0x0,
146 * @param pointer to maa_aio_context structure initialised by
150 * 16-bit unsigned int representing the current input voltage, normalised to
153 unsigned int maa_aio_read_u16(maa_aio_context* dev)
155 char buffer[16] = "";
156 unsigned int raw_value=0;
157 unsigned int analog_value=0;
158 unsigned int shifter_value=0;
160 if (NULL == dev->adc_in_fp) {
161 aio_get_valid_fp(dev);
164 fseek(dev->adc_in_fp, SEEK_SET, 0);
165 fread(buffer, sizeof(buffer), 1, dev->adc_in_fp);
166 fseek(dev->adc_in_fp, SEEK_SET, 0);
168 raw_value = atoi(buffer);
170 /* Adjust the raw analog input reading to supported resolution value*/
171 if (ADC_RAW_RESOLUTION_BITS == ADC_SUPPORTED_RESOLUTION_BITS) {
172 analog_value = raw_value;
175 if (ADC_RAW_RESOLUTION_BITS > ADC_SUPPORTED_RESOLUTION_BITS) {
176 shifter_value = ADC_RAW_RESOLUTION_BITS - ADC_SUPPORTED_RESOLUTION_BITS;
177 analog_value = raw_value >> shifter_value;
179 shifter_value = ADC_SUPPORTED_RESOLUTION_BITS - ADC_RAW_RESOLUTION_BITS;
180 analog_value = raw_value << shifter_value;
187 /** Close the analog input and free context memory
189 * @param dev - the analog input context
191 * @return maa result type.
193 maa_result_t maa_aio_close(maa_aio_context* dev)