| 1 | /***************************************************************************//** |
| 2 | * @file ADXL362.c |
| 3 | * @brief Implementation of ADXL362 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 "ADXL362.h" |
| 47 | #include "Communication.h" |
| 48 | |
| 49 | /******************************************************************************/ |
| 50 | /************************* Variables Declarations *****************************/ |
| 51 | /******************************************************************************/ |
| 52 | char selectedRange = 0; |
| 53 | |
| 54 | /******************************************************************************/ |
| 55 | /************************ Functions Definitions *******************************/ |
| 56 | /******************************************************************************/ |
| 57 | |
| 58 | /***************************************************************************//** |
| 59 | * @brief Initializes communication with the device and checks if the part is |
| 60 | * present by reading the device id. |
| 61 | * |
| 62 | * @return 0 - the initialization was successful and the device is present; |
| 63 | * -1 - an error occurred. |
| 64 | *******************************************************************************/ |
| 65 | char ADXL362_Init(void) |
| 66 | { |
| 67 | unsigned char regValue = 0; |
| 68 | char status = -1; |
| 69 | |
| 70 | status = SPI_Init(0, 4000000, 0, 1); |
| 71 | ADXL362_GetRegisterValue(®Value, ADXL362_REG_PARTID, 1); |
| 72 | if((regValue != ADXL362_PART_ID)) |
| 73 | { |
| 74 | status = -1; |
| 75 | } |
| 76 | selectedRange = 2; // Measurement Range: +/- 2g (reset default). |
| 77 | |
| 78 | return status; |
| 79 | } |
| 80 | |
| 81 | /***************************************************************************//** |
| 82 | * @brief Writes data into a register. |
| 83 | * |
| 84 | * @param registerValue - Data value to write. |
| 85 | * @param registerAddress - Address of the register. |
| 86 | * @param bytesNumber - Number of bytes. Accepted values: 0 - 1. |
| 87 | * |
| 88 | * @return None. |
| 89 | *******************************************************************************/ |
| 90 | void ADXL362_SetRegisterValue(unsigned short registerValue, |
| 91 | unsigned char registerAddress, |
| 92 | unsigned char bytesNumber) |
| 93 | { |
| 94 | unsigned char buffer[4] = {0, 0, 0, 0}; |
| 95 | |
| 96 | buffer[0] = ADXL362_WRITE_REG; |
| 97 | buffer[1] = registerAddress; |
| 98 | buffer[2] = (registerValue & 0x00FF); |
| 99 | buffer[3] = (registerValue >> 8); |
| 100 | SPI_Write(ADXL362_SLAVE_ID, buffer, bytesNumber + 2); |
| 101 | } |
| 102 | |
| 103 | /***************************************************************************//** |
| 104 | * @brief Performs a burst read of a specified number of registers. |
| 105 | * |
| 106 | * @param pReadData - The read values are stored in this buffer. |
| 107 | * @param registerAddress - The start address of the burst read. |
| 108 | * @param bytesNumber - Number of bytes to read. |
| 109 | * |
| 110 | * @return None. |
| 111 | *******************************************************************************/ |
| 112 | void ADXL362_GetRegisterValue(unsigned char* pReadData, |
| 113 | unsigned char registerAddress, |
| 114 | unsigned char bytesNumber) |
| 115 | { |
| 116 | unsigned char buffer[32]; |
| 117 | unsigned char index = 0; |
| 118 | |
| 119 | buffer[0] = ADXL362_READ_REG; |
| 120 | buffer[1] = registerAddress; |
| 121 | for(index = 0; index < bytesNumber; index++) |
| 122 | { |
| 123 | buffer[index + 2] = pReadData[index]; |
| 124 | } |
| 125 | SPI_Read(ADXL362_SLAVE_ID, buffer, bytesNumber + 2); |
| 126 | for(index = 0; index < bytesNumber; index++) |
| 127 | { |
| 128 | pReadData[index] = buffer[index + 2]; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /***************************************************************************//** |
| 133 | * @brief Reads multiple bytes from the device's FIFO buffer. |
| 134 | * |
| 135 | * @param pBuffer - Stores the read bytes. |
| 136 | * @param bytesNumber - Number of bytes to read. |
| 137 | * |
| 138 | * @return None. |
| 139 | *******************************************************************************/ |
| 140 | void ADXL362_GetFifoValue(unsigned char* pBuffer, unsigned short bytesNumber) |
| 141 | { |
| 142 | unsigned char buffer[512]; |
| 143 | unsigned short index = 0; |
| 144 | |
| 145 | buffer[0] = ADXL362_WRITE_FIFO; |
| 146 | for(index = 0; index < bytesNumber; index++) |
| 147 | { |
| 148 | buffer[index + 1] = pBuffer[index]; |
| 149 | } |
| 150 | SPI_Read(ADXL362_SLAVE_ID, buffer, bytesNumber + 1); |
| 151 | for(index = 0; index < bytesNumber; index++) |
| 152 | { |
| 153 | pBuffer[index] = buffer[index + 1]; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /***************************************************************************//** |
| 158 | * @brief Resets the device via SPI communication bus. |
| 159 | * |
| 160 | * @return None. |
| 161 | *******************************************************************************/ |
| 162 | void ADXL362_SoftwareReset(void) |
| 163 | { |
| 164 | ADXL362_SetRegisterValue(ADXL362_RESET_KEY, ADXL362_REG_SOFT_RESET, 1); |
| 165 | } |
| 166 | |
| 167 | /***************************************************************************//** |
| 168 | * @brief Places the device into standby/measure mode. |
| 169 | * |
| 170 | * @param pwrMode - Power mode. |
| 171 | * Example: 0 - standby mode. |
| 172 | * 1 - measure mode. |
| 173 | * |
| 174 | * @return None. |
| 175 | *******************************************************************************/ |
| 176 | void ADXL362_SetPowerMode(unsigned char pwrMode) |
| 177 | { |
| 178 | unsigned char oldPowerCtl = 0; |
| 179 | unsigned char newPowerCtl = 0; |
| 180 | |
| 181 | ADXL362_GetRegisterValue(&oldPowerCtl, ADXL362_REG_POWER_CTL, 1); |
| 182 | newPowerCtl = oldPowerCtl & ~ADXL362_POWER_CTL_MEASURE(0x3); |
| 183 | newPowerCtl = newPowerCtl | |
| 184 | (pwrMode * ADXL362_POWER_CTL_MEASURE(ADXL362_MEASURE_ON)); |
| 185 | ADXL362_SetRegisterValue(newPowerCtl, ADXL362_REG_POWER_CTL, 1); |
| 186 | } |
| 187 | |
| 188 | /***************************************************************************//** |
| 189 | * @brief Selects the measurement range. |
| 190 | * |
| 191 | * @param gRange - Range option. |
| 192 | * Example: ADXL362_RANGE_2G - +-2 g |
| 193 | * ADXL362_RANGE_4G - +-4 g |
| 194 | * ADXL362_RANGE_8G - +-8 g |
| 195 | * |
| 196 | * @return None. |
| 197 | *******************************************************************************/ |
| 198 | void ADXL362_SetRange(unsigned char gRange) |
| 199 | { |
| 200 | unsigned char oldFilterCtl = 0; |
| 201 | unsigned char newFilterCtl = 0; |
| 202 | |
| 203 | ADXL362_GetRegisterValue(&oldFilterCtl, ADXL362_REG_FILTER_CTL, 1); |
| 204 | newFilterCtl = oldFilterCtl & ~ADXL362_FILTER_CTL_RANGE(0x3); |
| 205 | newFilterCtl = newFilterCtl | ADXL362_FILTER_CTL_RANGE(gRange); |
| 206 | ADXL362_SetRegisterValue(newFilterCtl, ADXL362_REG_FILTER_CTL, 1); |
| 207 | selectedRange = (1 << gRange) * 2; |
| 208 | } |
| 209 | |
| 210 | /***************************************************************************//** |
| 211 | * @brief Selects the Output Data Rate of the device. |
| 212 | * |
| 213 | * @param outRate - Output Data Rate option. |
| 214 | * Example: ADXL362_ODR_12_5_HZ - 12.5Hz |
| 215 | * ADXL362_ODR_25_HZ - 25Hz |
| 216 | * ADXL362_ODR_50_HZ - 50Hz |
| 217 | * ADXL362_ODR_100_HZ - 100Hz |
| 218 | * ADXL362_ODR_200_HZ - 200Hz |
| 219 | * ADXL362_ODR_400_HZ - 400Hz |
| 220 | * |
| 221 | * @return None. |
| 222 | *******************************************************************************/ |
| 223 | void ADXL362_SetOutputRate(unsigned char outRate) |
| 224 | { |
| 225 | unsigned char oldFilterCtl = 0; |
| 226 | unsigned char newFilterCtl = 0; |
| 227 | |
| 228 | ADXL362_GetRegisterValue(&oldFilterCtl, ADXL362_REG_FILTER_CTL, 1); |
| 229 | newFilterCtl = oldFilterCtl & ~ADXL362_FILTER_CTL_ODR(0x7); |
| 230 | newFilterCtl = newFilterCtl | ADXL362_FILTER_CTL_ODR(outRate); |
| 231 | ADXL362_SetRegisterValue(newFilterCtl, ADXL362_REG_FILTER_CTL, 1); |
| 232 | } |
| 233 | |
| 234 | /***************************************************************************//** |
| 235 | * @brief Reads the 3-axis raw data from the accelerometer. |
| 236 | * |
| 237 | * @param x - Stores the X-axis data(as two's complement). |
| 238 | * @param y - Stores the Y-axis data(as two's complement). |
| 239 | * @param z - Stores the Z-axis data(as two's complement). |
| 240 | * |
| 241 | * @return None. |
| 242 | *******************************************************************************/ |
| 243 | void ADXL362_GetXyz(short* x, short* y, short* z) |
| 244 | { |
| 245 | unsigned char xyzValues[6] = {0, 0, 0, 0, 0, 0}; |
| 246 | |
| 247 | ADXL362_GetRegisterValue(xyzValues, ADXL362_REG_XDATA_L, 6); |
| 248 | *x = ((short)xyzValues[1] << 8) + xyzValues[0]; |
| 249 | *y = ((short)xyzValues[3] << 8) + xyzValues[2]; |
| 250 | *z = ((short)xyzValues[5] << 8) + xyzValues[4]; |
| 251 | } |
| 252 | |
| 253 | /***************************************************************************//** |
| 254 | * @brief Reads the 3-axis raw data from the accelerometer and converts it to g. |
| 255 | * |
| 256 | * @param x - Stores the X-axis data. |
| 257 | * @param y - Stores the Y-axis data. |
| 258 | * @param z - Stores the Z-axis data. |
| 259 | * |
| 260 | * @return None. |
| 261 | *******************************************************************************/ |
| 262 | void ADXL362_GetGxyz(float* x, float* y, float* z) |
| 263 | { |
| 264 | unsigned char xyzValues[6] = {0, 0, 0, 0, 0, 0}; |
| 265 | |
| 266 | ADXL362_GetRegisterValue(xyzValues, ADXL362_REG_XDATA_L, 6); |
| 267 | *x = ((short)xyzValues[1] << 8) + xyzValues[0]; |
| 268 | *x /= (1000 / (selectedRange / 2)); |
| 269 | *y = ((short)xyzValues[3] << 8) + xyzValues[2]; |
| 270 | *y /= (1000 / (selectedRange / 2)); |
| 271 | *z = ((short)xyzValues[5] << 8) + xyzValues[4]; |
| 272 | *z /= (1000 / (selectedRange / 2)); |
| 273 | } |
| 274 | |
| 275 | /***************************************************************************//** |
| 276 | * @brief Reads the temperature of the device. |
| 277 | * |
| 278 | * @return tempCelsius - The value of the temperature(degrees Celsius). |
| 279 | *******************************************************************************/ |
| 280 | float ADXL362_ReadTemperature(void) |
| 281 | { |
| 282 | unsigned char rawTempData[2] = {0, 0}; |
| 283 | short signedTemp = 0; |
| 284 | float tempCelsius = 0; |
| 285 | |
| 286 | ADXL362_GetRegisterValue(rawTempData, ADXL362_REG_TEMP_L, 2); |
| 287 | signedTemp = (short)(rawTempData[1] << 8) + rawTempData[0]; |
| 288 | tempCelsius = (float)signedTemp * 0.065; |
| 289 | |
| 290 | return tempCelsius; |
| 291 | } |
| 292 | |
| 293 | /***************************************************************************//** |
| 294 | * @brief Configures the FIFO feature. |
| 295 | * |
| 296 | * @param mode - Mode selection. |
| 297 | * Example: ADXL362_FIFO_DISABLE - FIFO is disabled. |
| 298 | * ADXL362_FIFO_OLDEST_SAVED - Oldest saved mode. |
| 299 | * ADXL362_FIFO_STREAM - Stream mode. |
| 300 | * ADXL362_FIFO_TRIGGERED - Triggered mode. |
| 301 | * @param waterMarkLvl - Specifies the number of samples to store in the FIFO. |
| 302 | * @param enTempRead - Store Temperature Data to FIFO. |
| 303 | * Example: 1 - temperature data is stored in the FIFO |
| 304 | * together with x-, y- and x-axis data. |
| 305 | * 0 - temperature data is skipped. |
| 306 | * |
| 307 | * @return None. |
| 308 | *******************************************************************************/ |
| 309 | void ADXL362_FifoSetup(unsigned char mode, |
| 310 | unsigned short waterMarkLvl, |
| 311 | unsigned char enTempRead) |
| 312 | { |
| 313 | unsigned char writeVal = 0; |
| 314 | |
| 315 | writeVal = ADXL362_FIFO_CTL_FIFO_MODE(mode) | |
| 316 | (enTempRead * ADXL362_FIFO_CTL_FIFO_TEMP) | |
| 317 | ADXL362_FIFO_CTL_AH; |
| 318 | ADXL362_SetRegisterValue(writeVal, ADXL362_REG_FIFO_CTL, 1); |
| 319 | ADXL362_SetRegisterValue(waterMarkLvl, ADXL362_REG_FIFO_SAMPLES, 2); |
| 320 | } |
| 321 | |
| 322 | /***************************************************************************//** |
| 323 | * @brief Configures activity detection. |
| 324 | * |
| 325 | * @param refOrAbs - Referenced/Absolute Activity Select. |
| 326 | * Example: 0 - absolute mode. |
| 327 | * 1 - referenced mode. |
| 328 | * @param threshold - 11-bit unsigned value that the adxl362 samples are |
| 329 | * compared to. |
| 330 | * @param time - 8-bit value written to the activity timer register. The |
| 331 | * amount of time (in seconds) is: time / ODR, where ODR - is |
| 332 | * the output data rate. |
| 333 | * |
| 334 | * @return None. |
| 335 | *******************************************************************************/ |
| 336 | void ADXL362_SetupActivityDetection(unsigned char refOrAbs, |
| 337 | unsigned short threshold, |
| 338 | unsigned char time) |
| 339 | { |
| 340 | unsigned char oldActInactReg = 0; |
| 341 | unsigned char newActInactReg = 0; |
| 342 | |
| 343 | /* Configure motion threshold and activity timer. */ |
| 344 | ADXL362_SetRegisterValue((threshold & 0x7FF), ADXL362_REG_THRESH_ACT_L, 2); |
| 345 | ADXL362_SetRegisterValue(time, ADXL362_REG_TIME_ACT, 1); |
| 346 | /* Enable activity interrupt and select a referenced or absolute |
| 347 | configuration. */ |
| 348 | ADXL362_GetRegisterValue(&oldActInactReg, ADXL362_REG_ACT_INACT_CTL, 1); |
| 349 | newActInactReg = oldActInactReg & ~ADXL362_ACT_INACT_CTL_ACT_REF; |
| 350 | newActInactReg |= ADXL362_ACT_INACT_CTL_ACT_EN | |
| 351 | (refOrAbs * ADXL362_ACT_INACT_CTL_ACT_REF); |
| 352 | ADXL362_SetRegisterValue(newActInactReg, ADXL362_REG_ACT_INACT_CTL, 1); |
| 353 | } |
| 354 | |
| 355 | /***************************************************************************//** |
| 356 | * @brief Configures inactivity detection. |
| 357 | * |
| 358 | * @param refOrAbs - Referenced/Absolute Inactivity Select. |
| 359 | * Example: 0 - absolute mode. |
| 360 | * 1 - referenced mode. |
| 361 | * @param threshold - 11-bit unsigned value that the adxl362 samples are |
| 362 | * compared to. |
| 363 | * @param time - 16-bit value written to the inactivity timer register. The |
| 364 | * amount of time (in seconds) is: time / ODR, where ODR - is |
| 365 | * the output data rate. |
| 366 | * |
| 367 | * @return None. |
| 368 | *******************************************************************************/ |
| 369 | void ADXL362_SetupInactivityDetection(unsigned char refOrAbs, |
| 370 | unsigned short threshold, |
| 371 | unsigned short time) |
| 372 | { |
| 373 | unsigned char oldActInactReg = 0; |
| 374 | unsigned char newActInactReg = 0; |
| 375 | |
| 376 | /* Configure motion threshold and inactivity timer. */ |
| 377 | ADXL362_SetRegisterValue((threshold & 0x7FF), |
| 378 | ADXL362_REG_THRESH_INACT_L, |
| 379 | 2); |
| 380 | ADXL362_SetRegisterValue(time, ADXL362_REG_TIME_INACT_L, 2); |
| 381 | /* Enable inactivity interrupt and select a referenced or absolute |
| 382 | configuration. */ |
| 383 | ADXL362_GetRegisterValue(&oldActInactReg, ADXL362_REG_ACT_INACT_CTL, 1); |
| 384 | newActInactReg = oldActInactReg & ~ADXL362_ACT_INACT_CTL_INACT_REF; |
| 385 | newActInactReg |= ADXL362_ACT_INACT_CTL_INACT_EN | |
| 386 | (refOrAbs * ADXL362_ACT_INACT_CTL_INACT_REF); |
| 387 | ADXL362_SetRegisterValue(newActInactReg, ADXL362_REG_ACT_INACT_CTL, 1); |
| 388 | } |