Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
LIS2DH.cpp
00001 /******************************************************************************* 00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL MAXIM 00017 * INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 00018 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00019 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00020 * 00021 * Except as contained in this notice, the name of Maxim Integrated Products, 00022 * Inc. shall not be used except as stated in the Maxim Integrated Products, 00023 * Inc. Branding Policy. 00024 * 00025 * The mere transfer of this software does not imply any licenses of trade 00026 * secrets, proprietary technology, copyrights, patents, trademarks, maskwork 00027 * rights, or any other form of intellectual property whatsoever. Maxim 00028 * Integrated Products, Inc. retains all ownership rights. 00029 ******************************************************************************* 00030 */ 00031 00032 #include "LIS2DH.h" 00033 00034 void lis2dh_int_handler(void); 00035 /** 00036 * @brief buffer array to hold fifo contents for packetizing 00037 */ 00038 uint32_t lis2dh_buffer[LIS2DH_MAX_DATA_SIZE]; 00039 int16_t motion_cached[3]; 00040 LIS2DH *LIS2DH::instance = NULL; 00041 00042 //****************************************************************************** 00043 LIS2DH::LIS2DH(PinName sda, PinName scl, int slaveAddress) : 00044 slaveAddress(slaveAddress) { 00045 i2c = new I2C(sda, scl); 00046 i2c->frequency(400000); 00047 isOwner = true; 00048 instance = this; 00049 } 00050 00051 //****************************************************************************** 00052 LIS2DH::LIS2DH(I2C *i2c, int slaveAddress) : 00053 slaveAddress(slaveAddress) { 00054 this->i2c = i2c; 00055 i2c->frequency(400000); 00056 isOwner = false; 00057 instance = this; 00058 } 00059 00060 //****************************************************************************** 00061 LIS2DH::~LIS2DH(void) { 00062 if (isOwner == true) 00063 delete i2c; 00064 } 00065 00066 //****************************************************************************** 00067 int LIS2DH::writeReg(LIS2DH_REG_map_t reg, char value) { 00068 int result; 00069 char cmdData[2] = {(char)reg, value}; 00070 result = i2c->write(slaveAddress, cmdData, 2); 00071 if (result != 0) 00072 return -1; 00073 return 0; 00074 } 00075 00076 //****************************************************************************** 00077 int LIS2DH::readReg(LIS2DH_REG_map_t reg, char *value) { 00078 int result; 00079 char cmdData[1] = {(char)reg}; 00080 00081 result = i2c->write(slaveAddress, cmdData, 1); 00082 if (result != 0) 00083 return -1; 00084 result = i2c->read(slaveAddress, value, 1); 00085 if (result != 0) 00086 return -1; 00087 return 0; 00088 } 00089 00090 //****************************************************************************** 00091 static void I2c_Reset(uint8_t index, int speed) { 00092 mxc_i2cm_regs_t *regs = MXC_I2CM_GET_I2CM(index); 00093 /* Reset module */ 00094 regs->ctrl = MXC_F_I2CM_CTRL_MSTR_RESET_EN; 00095 regs->ctrl = 0; 00096 /* Enable tx_fifo and rx_fifo */ 00097 regs->ctrl |= (MXC_F_I2CM_CTRL_TX_FIFO_EN | MXC_F_I2CM_CTRL_RX_FIFO_EN); 00098 } 00099 00100 //****************************************************************************** 00101 // Interrupt handler, empties the hardware fifo and packetizes it for streaming 00102 void LIS2DH::int_handler(void) { 00103 char fifo_src; 00104 int16_t valueX, valueY, valueZ; 00105 int num, index; 00106 00107 I2c_Reset(2, 1); 00108 num = 0; 00109 index = 0; 00110 fifo_src = 0; 00111 while ((fifo_src & 0x20) != 0x20) { 00112 get_motion_fifo(&valueX, &valueY, &valueZ); 00113 lis2dh_buffer[index++] = valueX; 00114 lis2dh_buffer[index++] = valueY; 00115 lis2dh_buffer[index++] = valueZ; 00116 readReg(LIS2DH_FIFO_SRC_REG, &fifo_src); 00117 num++; 00118 if (num >= 32) 00119 break; 00120 } 00121 motion_cached[0] = valueX; 00122 motion_cached[1] = valueY; 00123 motion_cached[2] = valueZ; 00124 } 00125 00126 //****************************************************************************** 00127 void LIS2DH::init(void) { 00128 stop(); 00129 configure_interrupt(); 00130 } 00131 00132 //****************************************************************************** 00133 void LIS2DH::configure_interrupt(void) { 00134 lis2dh_ctrl_reg6_t ctrl_reg6; 00135 // Interrupt enabled on INT1, interrupt active low 00136 ctrl_reg6.all = 0; 00137 ctrl_reg6.bit.I2_INT1 = 1; // Interrupt 1 function enabled on int1 pin 00138 ctrl_reg6.bit.H_LACTIVE = 1; // Interrupt active low 00139 writeReg(LIS2DH_CTRL_REG6, ctrl_reg6.all); 00140 } 00141 00142 //****************************************************************************** 00143 int LIS2DH::initStart(int dataRate, int fifoThreshold) { 00144 lis2dh_ctrl_reg5_t ctrl_reg5; 00145 lis2dh_fifo_ctrl_reg_t fifo_ctrl_reg; 00146 lis2dh_ctrl_reg1_t ctrl_reg1; 00147 lis2dh_ctrl_reg3_t ctrl_reg3; 00148 __disable_irq(); 00149 configure_interrupt(); 00150 // Enable FIFO 00151 ctrl_reg5.all = 0x0; 00152 ctrl_reg5.bit.FIFO_EN = 0x1; 00153 if (writeReg(LIS2DH_CTRL_REG5, ctrl_reg5.all) == -1) { 00154 __enable_irq(); 00155 return -1; 00156 } 00157 // Set FIFO to stream mode, trigger select INT1 00158 fifo_ctrl_reg.all = 0x0; 00159 fifo_ctrl_reg.bit.FTH = fifoThreshold; 00160 fifo_ctrl_reg.bit.FM = LIS2DH_FIFOMODE_STREAM; 00161 fifo_ctrl_reg.bit.TR = 0x0; 00162 if (writeReg(LIS2DH_FIFO_CTRL_REG, fifo_ctrl_reg.all) == -1) { 00163 __enable_irq(); 00164 return -1; 00165 } 00166 // Low power mode at 100Hz ODR0-ODR3 00167 ctrl_reg1.bit.ODR = 5; // Set the data rate 00168 ctrl_reg1.bit.LPen = 0x1; // Enable Low power 00169 ctrl_reg1.bit.Zen = 0x1; // Enable Z 00170 ctrl_reg1.bit.Yen = 0x1; // Enable Y 00171 ctrl_reg1.bit.Xen = 0x1; // Enable X 00172 if (writeReg(LIS2DH_CTRL_REG1, ctrl_reg1.all) == -1) { 00173 __enable_irq(); 00174 return -1; 00175 } 00176 // Enable watermark interrupt 00177 ctrl_reg3.all = 0x00; 00178 ctrl_reg3.bit.I1_WTM = 0x1; 00179 if (writeReg(LIS2DH_CTRL_REG3, ctrl_reg3.all) == -1) { 00180 __enable_irq(); 00181 return -1; 00182 } 00183 __enable_irq(); 00184 return 0; 00185 } 00186 00187 //****************************************************************************** 00188 int LIS2DH::detect(char *detected) { 00189 char val; 00190 *detected = 0; 00191 if (readReg(LIS2DH_WHO_AM_I, &val) == -1) 00192 return -1; 00193 if (val == LIS2DH_ID) 00194 *detected = 1; 00195 return 0; 00196 } 00197 00198 //****************************************************************************** 00199 int LIS2DH::get_motion_cached(int16_t *valueX, int16_t *valueY, 00200 int16_t *valueZ) { 00201 *valueX = motion_cached[0]; 00202 *valueY = motion_cached[1]; 00203 *valueZ = motion_cached[2]; 00204 return 0; 00205 } 00206 00207 //****************************************************************************** 00208 int LIS2DH::get_motion_fifo(int16_t *valueX, int16_t *valueY, int16_t *valueZ) { 00209 char reg = LIS2DH_OUT_X_L | 0x80; 00210 char values[6]; 00211 int i; 00212 00213 reg = LIS2DH_OUT_X_L; 00214 for (i = 0; i < 6; i++) { 00215 if (readReg((LIS2DH_REG_map_t)reg, &values[i]) != 0) 00216 return -1; 00217 reg++; 00218 } 00219 *valueX = ((short)values[1] << 8) + values[0]; 00220 *valueY = ((short)values[3] << 8) + values[2]; 00221 *valueZ = ((short)values[5] << 8) + values[4]; 00222 motion_cached[0] = *valueX; 00223 motion_cached[1] = *valueY; 00224 motion_cached[2] = *valueZ; 00225 return 0; 00226 } 00227 00228 //****************************************************************************** 00229 char LIS2DH::readId(void) { 00230 char val; 00231 readReg(LIS2DH_WHO_AM_I, &val); 00232 return val; 00233 } 00234 00235 //****************************************************************************** 00236 void LIS2DH::stop(void) { 00237 __disable_irq(); 00238 writeReg(LIS2DH_CTRL_REG3, 0x00); // Disable watermark interrupt 00239 writeReg(LIS2DH_CTRL_REG1, 0x00); // Data rate = 0Hz 00240 writeReg(LIS2DH_FIFO_CTRL_REG, 0x00); // Set to bypass mode, 00241 // clears FIFO_SRC_REG 00242 __enable_irq(); 00243 } 00244 00245 //****************************************************************************** 00246 void LIS2DHIntHandler(void) { 00247 char value; 00248 // Read the data rate axis enable register, if this is zero then just return, 00249 // we are not ready for interrupts 00250 LIS2DH::instance->readReg(LIS2DH::LIS2DH_CTRL_REG1, &value); 00251 if (value == 0x0) 00252 return; 00253 LIS2DH::instance->int_handler(); 00254 }
Generated on Tue Jul 12 2022 18:56:24 by
