3-axis MEMS ultra low power magnetometer
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LIS2MDLSensor.h
00001 /** 00002 ****************************************************************************** 00003 * @file LIS2MDLSensor.h 00004 * @author SRA 00005 * @version V1.0.0 00006 * @date February 2019 00007 * @brief Abstract Class of an LIS2MDL 3 axes magnetometer sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 00039 /* Prevent recursive inclusion -----------------------------------------------*/ 00040 00041 #ifndef __LIS2MDLSensor_H__ 00042 #define __LIS2MDLSensor_H__ 00043 00044 00045 /* Includes ------------------------------------------------------------------*/ 00046 00047 #include "DevI2C.h" 00048 #include "lis2mdl_reg.h" 00049 #include "MagneticSensor.h" 00050 #include <assert.h> 00051 00052 /* Defines -------------------------------------------------------------------*/ 00053 00054 00055 #define LIS2MDL_MAG_SENSITIVITY_FS_50GAUSS 1.500f /**< Sensitivity value for 50 gauss full scale [mgauss/LSB] */ 00056 00057 /* Typedefs ------------------------------------------------------------------*/ 00058 00059 typedef struct { 00060 int16_t x; 00061 int16_t y; 00062 int16_t z; 00063 } LIS2MDL_AxesRaw_t; 00064 00065 typedef struct { 00066 int32_t x; 00067 int32_t y; 00068 int32_t z; 00069 } LIS2MDL_Axes_t; 00070 00071 00072 /* Class Declaration ---------------------------------------------------------*/ 00073 00074 /** 00075 * Abstract class of an LIS2MDL Inertial Measurement Unit (IMU) 3 axes 00076 * sensor. 00077 */ 00078 class LIS2MDLSensor : public MagneticSensor { 00079 public: 00080 enum SPI_type_t {SPI3W, SPI4W}; 00081 LIS2MDLSensor(SPI *spi, PinName cs_pin, PinName int_pin = NC, SPI_type_t spi_type = SPI4W); 00082 LIS2MDLSensor(DevI2C *i2c, uint8_t address = LIS2MDL_I2C_ADD, PinName int_pin = NC); 00083 virtual int init(void *init); 00084 virtual int read_id(uint8_t *id); 00085 virtual int get_m_axes(int32_t *magnetic_field); 00086 virtual int get_m_axes_raw(int16_t *value); 00087 int enable(void); 00088 int disable(void); 00089 int get_m_sensitivity(float *sensitivity); 00090 int get_m_odr(float *odr); 00091 int set_m_odr(float odr); 00092 int get_m_fs(float *full_scale); 00093 int set_m_fs(float full_scale); 00094 int read_reg(uint8_t reg, uint8_t *data); 00095 int write_reg(uint8_t reg, uint8_t data); 00096 int set_m_self_test(uint8_t status); 00097 int get_m_drdy_status(uint8_t *status); 00098 00099 /** 00100 * @brief Attaching an interrupt handler to the INT interrupt. 00101 * @param fptr An interrupt handler. 00102 * @retval None. 00103 */ 00104 void attach_int_irq(void (*fptr)(void)) 00105 { 00106 _int_irq.rise(fptr); 00107 } 00108 00109 /** 00110 * @brief Enabling the INT interrupt handling. 00111 * @param None. 00112 * @retval None. 00113 */ 00114 void enable_int_irq(void) 00115 { 00116 _int_irq.enable_irq(); 00117 } 00118 00119 /** 00120 * @brief Disabling the INT interrupt handling. 00121 * @param None. 00122 * @retval None. 00123 */ 00124 void disable_int_irq(void) 00125 { 00126 _int_irq.disable_irq(); 00127 } 00128 00129 /** 00130 * @brief Utility function to read data. 00131 * @param pBuffer: pointer to data to be read. 00132 * @param RegisterAddr: specifies internal address register to be read. 00133 * @param NumByteToRead: number of bytes to be read. 00134 * @retval 0 if ok, an error code otherwise. 00135 */ 00136 uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) 00137 { 00138 if (_dev_spi) { 00139 /* Write Reg Address */ 00140 _dev_spi->lock(); 00141 _cs_pin = 0; 00142 if (_spi_type == SPI4W) { 00143 _dev_spi->write(RegisterAddr | 0x80); 00144 for (int i = 0; i < NumByteToRead; i++) { 00145 *(pBuffer + i) = _dev_spi->write(0x00); 00146 } 00147 } else if (_spi_type == SPI3W) { 00148 /* Write RD Reg Address with RD bit*/ 00149 uint8_t TxByte = RegisterAddr | 0x80; 00150 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); 00151 } 00152 _cs_pin = 1; 00153 _dev_spi->unlock(); 00154 return 0; 00155 } 00156 if (_dev_i2c) { 00157 return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); 00158 } 00159 return 1; 00160 } 00161 00162 /** 00163 * @brief Utility function to write data. 00164 * @param pBuffer: pointer to data to be written. 00165 * @param RegisterAddr: specifies internal address register to be written. 00166 * @param NumByteToWrite: number of bytes to write. 00167 * @retval 0 if ok, an error code otherwise. 00168 */ 00169 uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) 00170 { 00171 if (_dev_spi) { 00172 _dev_spi->lock(); 00173 _cs_pin = 0; 00174 _dev_spi->write(RegisterAddr); 00175 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); 00176 _cs_pin = 1; 00177 _dev_spi->unlock(); 00178 return 0; 00179 } 00180 if (_dev_i2c) { 00181 return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); 00182 } 00183 return 1; 00184 } 00185 00186 private: 00187 00188 /* Helper classes. */ 00189 DevI2C *_dev_i2c; 00190 SPI *_dev_spi; 00191 00192 /* Configuration */ 00193 uint8_t _address; 00194 DigitalOut _cs_pin; 00195 InterruptIn _int_irq; 00196 SPI_type_t _spi_type; 00197 00198 uint8_t _mag_is_enabled; 00199 00200 lis2mdl_ctx_t _reg_ctx; 00201 00202 }; 00203 00204 #ifdef __cplusplus 00205 extern "C" { 00206 #endif 00207 int32_t LIS2MDL_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite); 00208 int32_t LIS2MDL_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead); 00209 #ifdef __cplusplus 00210 } 00211 #endif 00212 00213 #endif
Generated on Tue Jul 12 2022 20:53:15 by 1.7.2