LELEC2811 - I&S / LIS2MDL

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LIS2MDLSensor.h Source File

LIS2MDLSensor.h

Go to the documentation of this file.
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>&copy; 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 get_m_lp(uint8_t *lp);
00095     int set_m_lp(uint8_t lp);
00096     int get_m_lpf(uint8_t *lpf);
00097     int set_m_lpf(uint8_t lpf);
00098     int get_m_comp_temp_en(uint8_t *comp_temp_en);
00099     int set_m_comp_temp_en(uint8_t comp_temp_en);
00100     int get_m_off_canc(uint8_t *off_canc);
00101     int set_m_off_canc(uint8_t off_canc);
00102     int read_reg(uint8_t reg, uint8_t *data);
00103     int write_reg(uint8_t reg, uint8_t data);
00104     int set_m_self_test(uint8_t status);
00105     int get_m_drdy_status(uint8_t *status);
00106 
00107     /**
00108      * @brief  Attaching an interrupt handler to the INT interrupt.
00109      * @param  fptr An interrupt handler.
00110      * @retval None.
00111      */
00112     void attach_int_irq(void (*fptr)(void))
00113     {
00114         _int_irq.rise(fptr);
00115     }
00116 
00117     /**
00118      * @brief  Enabling the INT interrupt handling.
00119      * @param  None.
00120      * @retval None.
00121      */
00122     void enable_int_irq(void)
00123     {
00124         _int_irq.enable_irq();
00125     }
00126 
00127     /**
00128      * @brief  Disabling the INT interrupt handling.
00129      * @param  None.
00130      * @retval None.
00131      */
00132     void disable_int_irq(void)
00133     {
00134         _int_irq.disable_irq();
00135     }
00136 
00137     /**
00138      * @brief Utility function to read data.
00139      * @param  pBuffer: pointer to data to be read.
00140      * @param  RegisterAddr: specifies internal address register to be read.
00141      * @param  NumByteToRead: number of bytes to be read.
00142      * @retval 0 if ok, an error code otherwise.
00143      */
00144     uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
00145     {
00146         if (_dev_spi) {
00147             /* Write Reg Address */
00148             _dev_spi->lock();
00149             _cs_pin = 0;
00150             if (_spi_type == SPI4W) {
00151                 _dev_spi->write(RegisterAddr | 0x80);
00152                 for (int i = 0; i < NumByteToRead; i++) {
00153                     *(pBuffer + i) = _dev_spi->write(0x00);
00154                 }
00155             } else if (_spi_type == SPI3W) {
00156                 /* Write RD Reg Address with RD bit*/
00157                 uint8_t TxByte = RegisterAddr | 0x80;
00158                 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
00159             }
00160             _cs_pin = 1;
00161             _dev_spi->unlock();
00162             return 0;
00163         }
00164         if (_dev_i2c) {
00165             return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
00166         }
00167         return 1;
00168     }
00169 
00170     /**
00171      * @brief Utility function to write data.
00172      * @param  pBuffer: pointer to data to be written.
00173      * @param  RegisterAddr: specifies internal address register to be written.
00174      * @param  NumByteToWrite: number of bytes to write.
00175      * @retval 0 if ok, an error code otherwise.
00176      */
00177     uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00178     {
00179         if (_dev_spi) {
00180             _dev_spi->lock();
00181             _cs_pin = 0;
00182             _dev_spi->write(RegisterAddr);
00183             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
00184             _cs_pin = 1;
00185             _dev_spi->unlock();
00186             return 0;
00187         }
00188         if (_dev_i2c) {
00189             return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
00190         }
00191         return 1;
00192     }
00193 
00194 private:
00195 
00196     /* Helper classes. */
00197     DevI2C *_dev_i2c;
00198     SPI    *_dev_spi;
00199 
00200     /* Configuration */
00201     uint8_t _address;
00202     DigitalOut  _cs_pin;
00203     InterruptIn _int_irq;
00204     SPI_type_t _spi_type;
00205 
00206     uint8_t _mag_is_enabled;
00207 
00208     lis2mdl_ctx_t _reg_ctx;
00209 
00210 };
00211 
00212 #ifdef __cplusplus
00213 extern "C" {
00214 #endif
00215 int32_t LIS2MDL_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);
00216 int32_t LIS2MDL_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);
00217 #ifdef __cplusplus
00218 }
00219 #endif
00220 
00221 #endif