Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD7780.c
3 * @brief Implementation of AD7780 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 "AD7780.h"
47
48 /******************************************************************************/
49 /************************ Functions Definitions *******************************/
50 /******************************************************************************/
51
52 /***************************************************************************//**
53 * @brief Initializes the communication peripheral and checks if the device is
54 * present.
55 *
56 * @return initStatus - The result of the initialization procedure.
57 * Example: -1 - SPI peripheral was not initialized.
58 * 0 - SPI peripheral was initialized.
59 *******************************************************************************/
60 char AD7780_Init(void)
61 {
62 unsigned char ad7780Status = 0;
63 char initStatus = -1;
64
65 /* Set PDRST, FILTER and GAIN pins as output. */
66 AD7780_PDRST_PIN_OUT;
67 AD7780_FILTER_PIN_OUT;
68 AD7780_GAIN_PIN_OUT;
69
70 /* Set the initial value of the PDRST, FILTER and GAIN pins. */
71 AD7780_PDRST_LOW; // Places AD7780 in power-down mode.
72 AD7780_FILTER_LOW; // The update rate is set to 16.7 Hz.
73 AD7780_GAIN_HIGH; // Gain is set to 1.
74 initStatus = SPI_Init(0, // Transfer format.
75 8000000, // SPI clock frequency.
76 1, // SPI clock polarity.
77 0); // SPI clock edge.
78 if(initStatus != 0)
79 {
80 return -1;
81 }
82 AD7780_PDRST_HIGH;
83 initStatus = AD7780_WaitRdyGoLow();
84 if(initStatus != 0)
85 {
86 return -1;
87 }
88 AD7780_ReadSample(&ad7780Status);
89 if((ad7780Status & (AD7780_STAT_ID1 | AD7780_STAT_ID0)) != AD7780_ID_NUMBER)
90 {
91 return -1;
92 }
93
94 return initStatus;
95 }
96
97 /***************************************************************************//**
98 * @brief Waits for DOUT/RDY pin to go low.
99 *
100 * @return The result of the operation.
101 * Example: -1 - DOUT/RDY pin is high.
102 * 0 - DOUT/RDY pin is low.
103 *******************************************************************************/
104 char AD7780_WaitRdyGoLow(void)
105 {
106 unsigned long timeout = 0xFFFFF;
107
108 while(AD7780_RDY_STATE && timeout)
109 {
110 timeout--;
111 }
112 if(timeout == 0)
113 {
114 return -1;
115 }
116 else
117 {
118 return 0;
119 }
120 }
121
122 /***************************************************************************//**
123 * @brief Reads a 24-bit sample from the ADC.
124 *
125 * @param pStatus - Stores the 8-bit status that is read for every conversion.
126 *
127 * @return convSample - The 24-bit sample.
128 *******************************************************************************/
129 long AD7780_ReadSample(unsigned char* pStatus)
130 {
131 unsigned char rxBuff[4] = {0, 0, 0, 0};
132 long convSample = 0;
133
134 SPI_Read(0, rxBuff, 4);
135 convSample = ((long)rxBuff[0] << 16) +
136 ((short)rxBuff[1] << 8) +
137 rxBuff[2];
138 *pStatus = rxBuff[3];
139
140 return convSample;
141 }
142
143 /***************************************************************************//**
144 * @brief Converts the 24-bit raw value to milivolts.
145 *
146 * @param rawSample - 24-bit raw sample(offset binary).
147 * @param vRef - The reference voltage.
148 * @param gain - The gain. Accepted values: 1, 128.
149 *
150 * @return voltage - The voltage obtained from the raw value.
151 *******************************************************************************/
152 float AD7780_ConvertToVoltage(unsigned long rawSample, float vRef, unsigned char gain)
153 {
154 float voltage = 0;
155
156 voltage = 1000 * ((float)rawSample / (1ul << 23) - 1) * vRef / gain;
157
158 return voltage;
159 }
160
161
162
163
164
165
166
167
168