Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD7171/ad7171.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file ad7171.c
3 * @brief Implementation of ad7171 Driver for Microblaze processor.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2013(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *******************************************************************************/
41
42 /******************************************************************************/
43 /***************************** Include Files **********************************/
44 /******************************************************************************/
45 #include "ad7171.h"
46
47 /******************************************************************************/
48 /************************ Constants Definitions *******************************/
49 /******************************************************************************/
50 const unsigned char MAX_RESOLUTION = 16;
51
52 /******************************************************************************/
53 /************************** Variables Definitions *****************************/
54 /******************************************************************************/
55 struct ad7171_chip_info {
56 unsigned char resolution;
57 unsigned short midscale;
58 };
59
60 static const struct ad7171_chip_info ad7171_chip_info[] = {
61 [ID_AD7170] = {
62 .resolution = 12,
63 .midscale = 2048,
64 },
65 [ID_AD7171] = {
66 .resolution = 16,
67 .midscale = 32768,
68 }
69 };
70
71 AD7171_type act_device;
72
73 /******************************************************************************/
74 /*************************** Functions Definitions ****************************/
75 /******************************************************************************/
76
77 /**************************************************************************//**
78 * @brief Initializes the communication with the device.
79 *
80 * @param None.
81 *
82 * @return status - Result of the initialization procedure.
83 * Example: 0 - if initialization was successful;
84 * -1 - if initialization was unsuccessful.
85 ******************************************************************************/
86 char ad7171_Init(AD7171_type device)
87 {
88
89 char status = -1;
90
91 act_device = device;
92 /* Setup SPI Interface */
93 status = SPI_Init(0, 1000000, 1, 0);
94
95 return status;
96 }
97
98 /**************************************************************************//**
99 * @brief Receives 24 bits from SPI and transmits 0x0.
100 *
101 * @param ctrl - control word of 24 bits.
102 *
103 * @return rxData - Received data in case of success.
104 ******************************************************************************/
105 unsigned long ad7171_Read24Bits(unsigned long ctrl)
106 {
107 unsigned long rxData;
108 char bytesRead = 0;
109 unsigned char data[3] = {0, 0, 0};
110
111 data[0] = (ctrl & 0x00FF0000) >> 16;
112 data[1] = (ctrl & 0x0000FF00) >> 8;
113 data[2] = (ctrl & 0x000000FF) >> 0;
114
115 bytesRead = SPI_Read(ad7171_SLAVE_ID, data, 3);
116 if (bytesRead != 3)
117 {
118 return -1;
119 }
120
121 rxData = ((unsigned long)data[0] << 16) + ((unsigned long)data[1] << 8) +
122 ((unsigned long)data[2] << 0);
123
124 return(rxData);
125 }
126
127 /**************************************************************************//**
128 * @brief Calculates the input voltage and the pattern of the conversion as
129 * parameter.
130 *
131 * @param ctrl - control word of 24 bits;
132 * pattern - pointer to the pattern of the conversion.
133 *
134 * @return vin - input voltage.
135 ******************************************************************************/
136 float ad7171_GetVoltage(unsigned long ctrl,
137 unsigned char* pattern,
138 float vRef)
139 {
140 unsigned long data = 0;
141 unsigned short dataValue = 0;
142 float vin = 0;
143
144 data = ad7171_Read24Bits(ctrl);
145 /* Extract the ADC code from the 24 bits word. */
146 dataValue = (data & 0xFFFF00) >> (8 + MAX_RESOLUTION -
147 ad7171_chip_info[act_device].resolution);
148 /* Calculate the input voltage corresponding to the current ADC code. */
149 vin = (((float)dataValue / ad7171_chip_info[act_device].midscale) - 1) *
150 vRef;
151 /* Extract the pattern from the 24 bits word. */
152 *pattern = (unsigned char)(data & 0x1F);
153
154 return vin;
155 }
156
157 /**************************************************************************//**
158 * @brief Returns the ADC code and the pattern of the conversion as
159 * parameter.
160 *
161 * @param ctrl - control word of 24 bits;
162 * pattern - pointer to the pattern of the conversion.
163 *
164 * @return adcCode.
165 ******************************************************************************/
166 unsigned short ad7171_GetAdcCode(unsigned long ctrl, unsigned char* pattern)
167 {
168 unsigned long data = 0;
169 unsigned short dataValue = 0;
170 unsigned short adcCode = 0;
171
172 data = ad7171_Read24Bits(ctrl);
173 /* Extract the ADC code from the 24 bits word. */
174 dataValue = (data & 0xFFFF00) >> (8 + MAX_RESOLUTION -
175 ad7171_chip_info[act_device].resolution);
176 adcCode = dataValue;
177 /* Extract the pattern from the 24 bits word. */
178 *pattern = (unsigned char)(data & 0x1F);
179
180 return adcCode;
181 }