Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/AD7091R/AD7091R.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD7091R.c
3 * @brief Implementation of AD7091R Driver.
4 * @author DNechita ([email protected])
5 ********************************************************************************
6 * Copyright 2012(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 ********************************************************************************
40 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "AD7091R.h"
47 #include "Communication.h"
48
49 /******************************************************************************/
50 /************************ Functions Definitions *******************************/
51 /******************************************************************************/
52
53 /***************************************************************************//**
54 * @brief Initializes the SPI communication peripheral.
55 *
56 * @return Result of the initialization procedure.
57 * Example: -1 - SPI peripheral was not initialized.
58 * 0 - SPI peripheral is initialized.
59 *******************************************************************************/
60 char AD7091R_Init(void)
61 {
62 unsigned char status = 0;
63 unsigned char tmpVal = 0xFF;
64
65 status = SPI_Init(0, 8000000, 0, 1);
66 /* Ensures that last state of SDO is high. */
67 SPI_Write(0, &tmpVal, 1);
68 AD7091R_SoftwareReset();
69
70 return status;
71 }
72
73 /***************************************************************************//**
74 * @brief Initiate a software reset of the device. The AD7091R requires the user
75 * to initiate a software reset when power is first applied.
76 *
77 * @return None.
78 *******************************************************************************/
79 void AD7091R_SoftwareReset(void)
80 {
81 unsigned char writeByte = 0xBF;
82
83 /* Initiate a conversion. */
84 SPI_Write(0, &writeByte, 1);
85 /* Short cycle the read operation. */
86 writeByte = 0xFF;
87 SPI_Read(AD7091R_SLAVE_ID, &writeByte, 1);
88 /* Perform another conversion in order to reset the device. */
89 writeByte = 0xBF;
90 SPI_Write(0, &writeByte, 1);
91 }
92
93 /***************************************************************************//**
94 * @brief Initiates one conversion and reads back the result. During this
95 * process the device runs in normal mode and operates without the busy
96 * indicator.
97 *
98 * @return conversionResult - 12bit conversion result.
99 *******************************************************************************/
100 unsigned short AD7091R_ReadSample(void)
101 {
102 unsigned short conversionResult = 0;
103 unsigned char writeByte = 0xBF;
104 unsigned char buffer[2] = {0xFF, 0xFF};
105
106 /* Initiate a conversion. */
107 SPI_Write(0, &writeByte, 1);
108 /* Read conversion data. */
109 SPI_Read(AD7091R_SLAVE_ID, buffer, 2);
110 conversionResult = (buffer[0] << 8) + buffer[1];
111 conversionResult >>= 4;
112
113 return conversionResult;
114 }
115
116 /***************************************************************************//**
117 * @brief Puts the device in power-down mode.
118 *
119 * @return None.
120 *******************************************************************************/
121 void AD7091R_PowerDown(void)
122 {
123 unsigned char buffer[2] = {0, 0};
124 unsigned char writeValue = 0x00;
125
126 /* Initiate a conversion. */
127 SPI_Write(0, &writeValue, 1);
128 /* Perform a dummy read. */
129 SPI_Read(AD7091R_SLAVE_ID, buffer, 2);
130 }
131
132 /***************************************************************************//**
133 * @brief Powers up the device by pulling CONVST high. After calling this
134 * function, a time delay is required before initiating another
135 * conversion:
136 * - 50 ms delay, when internal reference is used.(with 2.2uF capacitor)
137 * - 100 us delay, when external reference is used.
138 *
139 * @return None.
140 *******************************************************************************/
141 void AD7091R_PowerUp(void)
142 {
143 unsigned char writeValue = 0xFF;
144
145 /* Pull CONVST signal high. */
146 SPI_Write(0, &writeValue, 1);
147 }
148
149
150 /***************************************************************************//**
151 * @brief Converts a 12-bit raw sample to volts.
152 *
153 * @param rawSample - 12-bit data sample.
154 * @param vRef - The value of the voltage reference used by the device.
155 *
156 * @return voltage - The result of the conversion expressed as volts.
157 *******************************************************************************/
158 float AD7091R_ConvertToVolts(short rawSample, float vRef)
159 {
160 float voltage = 0;
161
162 if(vRef == 0)
163 {
164 vRef = 2.5;
165 }
166 voltage = vRef * (float)rawSample / (1 << 12);
167
168 return voltage;
169 }