Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADXRS453.c
3 * @brief Implementation of ADXRS453 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2013(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
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "ADXRS453.h" // ADXRS453 definitions
45 #include "Communication.h" // Communication definitions
46
47 /***************************************************************************//**
48 * @brief Initializes the ADXRS453 and checks if the device is present.
49 *
50 * @return status - Result of the initialization procedure.
51 * Example: 0 - if initialization was successful (ID starts
52 * with 0x52).
53 * -1 - if initialization was unsuccessful.
54 *******************************************************************************/
55 char ADXRS453_Init(void)
56 {
57 char status = 0;
58 unsigned short adxrs453Id = 0;
59
60 /* Initialize the SPI communication peripheral at 8 MHz. */
61 SPI_Init(0, // Transfer format.
62 8000000, // SPI clock frequency.
63 0, // SPI clock polarity.
64 1); // SPI clock edge.
65
66 /* Read the value of the ADXRS453 ID register. */
67 adxrs453Id = ADXRS453_GetRegisterValue(ADXRS453_REG_PID);
68 if((adxrs453Id >> 8) != 0x52)
69 {
70 status = -1;
71 }
72
73 return status;
74 }
75
76 /***************************************************************************//**
77 * @brief Reads the value of a register.
78 *
79 * @param registerAddress - Address of the register.
80 *
81 * @return registerValue - Value of the register.
82 *******************************************************************************/
83 unsigned short ADXRS453_GetRegisterValue(unsigned char registerAddress)
84 {
85 unsigned char dataBuffer[4] = {0, 0, 0, 0};
86 unsigned long command = 0;
87 unsigned char bitNo = 0;
88 unsigned char sum = 0;
89 unsigned short registerValue = 0;
90
91 dataBuffer[0] = ADXRS453_READ | (registerAddress >> 7);
92 dataBuffer[1] = (registerAddress << 1);
93 command = ((unsigned long)dataBuffer[0] << 24) |
94 ((unsigned long)dataBuffer[1] << 16) |
95 ((unsigned short)dataBuffer[2] << 8) |
96 dataBuffer[3];
97 for(bitNo = 31; bitNo > 0; bitNo--)
98 {
99 sum += ((command >> bitNo) & 0x1);
100 }
101 if(!(sum % 2))
102 {
103 dataBuffer[3] |= 1;
104 }
105 SPI_Write(ADXRS453_SLAVE_ID, dataBuffer, 4);
106 SPI_Read(ADXRS453_SLAVE_ID, dataBuffer, 4);
107 registerValue = ((unsigned short)dataBuffer[1] << 11) |
108 ((unsigned short)dataBuffer[2] << 3) |
109 (dataBuffer[3] >> 5);
110
111 return registerValue;
112 }
113
114 /***************************************************************************//**
115 * @brief Writes data into a register.
116 *
117 * @param registerAddress - Address of the register.
118 * @param registerValue - Data value to write.
119 *
120 * @return None.
121 *******************************************************************************/
122 void ADXRS453_SetRegisterValue(unsigned char registerAddress,
123 unsigned short registerValue)
124 {
125 unsigned char dataBuffer[4] = {0, 0, 0, 0};
126 unsigned long command = 0;
127 unsigned char bitNo = 0;
128 unsigned char sum = 0;
129
130 dataBuffer[0] = ADXRS453_WRITE | (registerAddress >> 7);
131 dataBuffer[1] = (registerAddress << 1) |
132 (registerValue >> 15);
133 dataBuffer[2] = (registerValue >> 7);
134 dataBuffer[3] = (registerValue << 1);
135
136 command = ((unsigned long)dataBuffer[0] << 24) |
137 ((unsigned long)dataBuffer[1] << 16) |
138 ((unsigned short)dataBuffer[2] << 8) |
139 dataBuffer[3];
140 for(bitNo = 31; bitNo > 0; bitNo--)
141 {
142 sum += ((command >> bitNo) & 0x1);
143 }
144 if(!(sum % 2))
145 {
146 dataBuffer[3] |= 1;
147 }
148 SPI_Write(ADXRS453_SLAVE_ID, dataBuffer, 4);
149 }
150
151 /***************************************************************************//**
152 * @brief Reads the sensor data.
153 *
154 * @param None.
155 *
156 * @return registerValue - The sensor data.
157 *******************************************************************************/
158 unsigned long ADXRS453_GetSensorData(void)
159 {
160 unsigned char dataBuffer[4] = {0, 0, 0, 0};
161 unsigned long command = 0;
162 unsigned char bitNo = 0;
163 unsigned char sum = 0;
164 unsigned long registerValue = 0;
165
166 dataBuffer[0] = ADXRS453_SENSOR_DATA;
167 command = ((unsigned long)dataBuffer[0] << 24) |
168 ((unsigned long)dataBuffer[1] << 16) |
169 ((unsigned short)dataBuffer[2] << 8) |
170 dataBuffer[3];
171 for(bitNo = 31; bitNo > 0; bitNo--)
172 {
173 sum += ((command >> bitNo) & 0x1);
174 }
175 if(!(sum % 2))
176 {
177 dataBuffer[3] |= 1;
178 }
179 SPI_Write(ADXRS453_SLAVE_ID, dataBuffer, 4);
180 SPI_Read(ADXRS453_SLAVE_ID, dataBuffer, 4);
181 registerValue = ((unsigned long)dataBuffer[0] << 24) |
182 ((unsigned long)dataBuffer[1] << 16) |
183 ((unsigned short)dataBuffer[2] << 8) |
184 dataBuffer[3];
185
186 return registerValue;
187 }
188
189 /***************************************************************************//**
190 * @brief Reads the rate data and converts it to degrees/second.
191 *
192 * @param None.
193 *
194 * @return rate - The rate value in degrees/second.
195 *******************************************************************************/
196 float ADXRS453_GetRate(void)
197 {
198 unsigned short registerValue = 0;
199 float rate = 0.0;
200
201 registerValue = ADXRS453_GetRegisterValue(ADXRS453_REG_RATE);
202
203 /*!< If data received is in positive degree range */
204 if(registerValue < 0x8000)
205 {
206 rate = ((float)registerValue / 80);
207 }
208 /*!< If data received is in negative degree range */
209 else
210 {
211 rate = (-1) * ((float)(0xFFFF - registerValue + 1) / 80.0);
212 }
213
214 return rate;
215 }
216
217 /***************************************************************************//**
218 * @brief Reads the temperature sensor data and converts it to degrees Celsius.
219 *
220 * @param None.
221 *
222 * @return temperature - The temperature value in degrees Celsius.
223 *******************************************************************************/
224 float ADXRS453_GetTemperature(void)
225 {
226 unsigned long registerValue = 0;
227 float temperature = 0;
228
229 registerValue = ADXRS453_GetRegisterValue(ADXRS453_REG_TEM);
230 registerValue = (registerValue >> 6) - 0x31F;
231 temperature = (float) registerValue / 5;
232
233 return temperature;
234 }