Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD7791/ad7791.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad7791.c
3 * @brief Implementation of AD7791 Driver for Microblaze processor.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2014(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 "ad7791.h"
46 #include "Communication.h"
47 #include "TIME.h"
48
49 /***************************************************************************//**
50 * @brief Initializes SPI and initial values for AD7791 device.
51 *
52 * @param None.
53 *
54 * @return status - Result of the initialization procedure.
55 * Example: 0 - if initialization was successful;
56 * -1 - if initialization was unsuccessful.
57 *******************************************************************************/
58 char AD7791_Init(void)
59 {
60 char status = -1;
61
62 status = SPI_Init(0, 1000000, 1, 0);
63
64 return status;
65 }
66
67 /***************************************************************************//**
68 * @brief Reads data from AD7791.
69 *
70 * @param regAddress - Address of the register.
71 * @param bytesNumber - Number of bytes.
72 *
73 * @return readData - Received data.
74 *******************************************************************************/
75 unsigned long AD7791_Read(unsigned char regAddress,
76 unsigned char bytesNumber)
77 {
78 unsigned long receivedData = 0x0;
79 unsigned char command = 0x0;
80 unsigned char dataBuf[4] = {0x00, 0x00, 0x00, 0x00};
81
82 command = AD7791_COMM_READ + AD7791_COMM_ADDR(regAddress);
83 dataBuf[0] = command;
84 SPI_Read(AD7791_SLAVE_ID, (unsigned char*)dataBuf, bytesNumber + 1);
85 receivedData = (dataBuf[1] << 16) + (dataBuf[2] << 8) + (dataBuf[3] << 0);
86
87 return(receivedData);
88 }
89
90 /***************************************************************************//**
91 * @brief Writes data to AD7791.
92 *
93 * @param regAddress - Address of the register.
94 * @param bytesNumber - Number of bytes.
95 * @param regData - Data that will be loaded.
96 *
97 * @return None.
98 *******************************************************************************/
99 void AD7791_Write(unsigned char regAddress,
100 unsigned char bytesNumber,
101 unsigned char regData)
102 {
103 unsigned char command = 0x0;
104 unsigned char txBuf[2] = {0x00, 0x00};
105
106 command = AD7791_COMM_WRITE + AD7791_COMM_ADDR(regAddress);
107 txBuf[0] = command;
108 txBuf[1] = regData;
109 bytesNumber = 1;
110 SPI_Write(AD7791_SLAVE_ID, (unsigned char*)txBuf, bytesNumber + 1);
111
112 }
113
114 /***************************************************************************//**
115 * @brief Converts 24-bit raw data to millivolts.
116 *
117 * @param rawData - 24-bit data sample.
118 * @param vRef - The value of the voltage reference used by the device in volts.
119 * @param polarity - The set polarity of the input voltage.
120 * Example: 0 - Bipolar mode.
121 * 1 - Unipolar mode.
122 *
123 * @return voltage - The result of the conversion expressed in millivolts.
124 *******************************************************************************/
125 float AD7791_ConvertToVolts(unsigned long rawData,
126 float vRef,
127 unsigned char polarity)
128 {
129 float voltage = 0;
130
131 if(polarity == 0) // Bipolar mode
132 {
133 voltage = 1000 * (((float)rawData / (1ul << 23)) - 1) * vRef;
134 }
135 else
136 {
137 if (polarity == 1) // Unipolar mode
138 {
139 voltage = 1000 * ((float)rawData * vRef) / (1ul << 24);
140 }
141 }
142
143 return voltage;
144 }
145
146 /***************************************************************************//**
147 * @brief Resets the serial interface with the AD7791.
148 *
149 * @param None.
150 *
151 * @return None.
152 *******************************************************************************/
153 void AD7791_Reset(void)
154 {
155 unsigned char bytesNumber = 5;
156 unsigned char txBuf[5] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
157
158 SPI_Write(AD7791_SLAVE_ID, (unsigned char*)txBuf, bytesNumber);
159 }