Oleg Goncharovskiy / LIS3MDL

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lis3mdl_class.h Source File

lis3mdl_class.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    lis3mdl_class.h
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-April-2015
00007  * @brief   Header file for component LIS3MDL
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 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 #ifndef __LIS3MDL_CLASS_H
00039 #define __LIS3MDL_CLASS_H
00040 
00041 /* Includes ------------------------------------------------------------------*/
00042 #include "mbed.h"
00043 #include "DevI2C.h"
00044 #include "lis3mdl.h"
00045 #include "MagneticSensor.h"
00046 #include <assert.h>
00047 
00048 
00049 /* Classes -------------------------------------------------------------------*/
00050 /** Class representing a LIS3MDL sensor component
00051  */
00052 class LIS3MDL : public MagneticSensor {
00053  public:
00054     //my
00055      enum SPI_type_t {SPI3W, SPI4W};    
00056     
00057     //my 
00058     LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W);
00059         
00060     /** Constructor
00061      * @param[in] i2c device I2C to be used for communication
00062      */    
00063      
00064     LIS3MDL(DevI2C *i2c, uint8_t address=LIS3MDL_M_MEMS_ADDRESS_HIGH, PinName int_pin=NC);
00065     
00066     /** Destructor
00067      */
00068         virtual ~LIS3MDL() {}
00069     
00070     /*** Interface Methods ***/
00071     virtual int init(void *init_struct) {
00072         return LIS3MDL_Init((MAGNETO_InitTypeDef*)init_struct);
00073     }
00074 
00075     virtual int read_id(uint8_t *m_id) {
00076         return LIS3MDL_Read_M_ID(m_id);
00077     }
00078 
00079     virtual int get_m_axes(int32_t *pData) {
00080         return LIS3MDL_M_GetAxes(pData);
00081     }
00082 
00083     virtual int get_m_axes_raw(int16_t *pData) {
00084         return LIS3MDL_M_GetAxesRaw(pData);
00085     }
00086     
00087     virtual int set_m_axes_offset(uint8_t axe,int16_t offset) { //my
00088         return LIS3MDL_M_SetAxeOffset(axe,offset);
00089     }
00090     virtual int set_BDU() { //my
00091         uint8_t tmp;
00092         if(LIS3MDL_IO_Read(&tmp, LIS3MDL_M_CTRL_REG5_M, 1) != MAGNETO_OK)
00093             return MAGNETO_ERROR;
00094         tmp=0x40;
00095         return LIS3MDL_IO_Write(&tmp, LIS3MDL_M_CTRL_REG5_M, 1);
00096     }    
00097  protected:
00098     /*** Methods ***/
00099     MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init);
00100     MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id);
00101     MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData);
00102     MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData);
00103     //my 
00104     MAGNETO_StatusTypeDef LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode);
00105     MAGNETO_StatusTypeDef LIS3MDL_M_SetAxeOffset(uint8_t axe,int16_t offset);//my
00106     /**
00107      * @brief  Configures LIS3MDL interrupt lines for NUCLEO boards
00108      */
00109     void LIS3MDL_IO_ITConfig(void)
00110     {
00111         /* To be implemented */
00112     }
00113 
00114     /**
00115      * @brief  Configures LIS3MDL I2C interface
00116      * @return MAGNETO_OK in case of success, an error code otherwise
00117      */
00118     MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void)
00119     {
00120         return MAGNETO_OK; /* done in constructor */
00121     }
00122 
00123     /**
00124      * @brief      Utility function to read data from LIS3MDL
00125      * @param[out] pBuffer pointer to the byte-array to read data in to
00126      * @param[in]  RegisterAddr specifies internal address register to read from.
00127      * @param[in]  NumByteToRead number of bytes to be read.
00128      * @retval     MAGNETO_OK if ok, 
00129      * @retval     MAGNETO_ERROR if an I2C error has occured
00130      */
00131     MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer, 
00132                           uint8_t RegisterAddr, uint16_t NumByteToRead)
00133     {
00134 /*my        
00135         if (_dev_spi) {
00136         // Write Reg Address 
00137             _dev_spi->lock();
00138             _cs_pin = 0;           
00139             if (_spi_type == SPI4W) {            
00140                 _dev_spi->write(RegisterAddr | 0x80);
00141                 for (int i=0; i<NumByteToRead; i++) {
00142                     *(pBuffer+i) = _dev_spi->write(0x00);
00143                 }
00144             } else if (_spi_type == SPI3W){
00145                 // Write RD Reg Address with RD bit
00146                 uint8_t TxByte = RegisterAddr | 0x80;    
00147                 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
00148             }            
00149             _cs_pin = 1;
00150             _dev_spi->unlock(); 
00151             return MAGNETO_OK;
00152         }  
00153 */                          
00154         if (!_dev_i2c) return MAGNETO_ERROR;            
00155         int ret = _dev_i2c->i2c_read(pBuffer,
00156                        _address,
00157                        RegisterAddr,
00158                        NumByteToRead);
00159         if(ret != 0) {
00160             return MAGNETO_ERROR;
00161         }
00162         return MAGNETO_OK;
00163     }
00164     
00165     /**
00166      * @brief      Utility function to write data to LIS3MDL
00167      * @param[in]  pBuffer pointer to the byte-array data to send
00168      * @param[in]  RegisterAddr specifies internal address register to read from.
00169      * @param[in]  NumByteToWrite number of bytes to write.
00170      * @retval     MAGNETO_OK if ok, 
00171      * @retval     MAGNETO_ERROR if an I2C error has occured
00172      */
00173     MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer, 
00174                            uint8_t RegisterAddr, uint16_t NumByteToWrite)
00175     {
00176 /*      
00177         if (_dev_spi) { 
00178             _dev_spi->lock();
00179             _cs_pin = 0;
00180             int data = _dev_spi->write(RegisterAddr);                    
00181             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);                     
00182             _cs_pin = 1;                    
00183             _dev_spi->unlock();
00184             return MAGNETO_OK;                    
00185         }        
00186 */      
00187         if (!_dev_i2c) return MAGNETO_ERROR;    
00188         int ret = _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
00189         if(ret != 0) {
00190             return MAGNETO_ERROR;
00191         }
00192         return MAGNETO_OK;
00193     }
00194     
00195     /*** Instance Variables ***/
00196     /* IO Device */
00197     DevI2C *_dev_i2c;
00198     //my 
00199     SPI    *_dev_spi;
00200     uint8_t _address;   
00201     DigitalOut  _cs_pin; 
00202     InterruptIn _int_pin;    
00203     //my 
00204     SPI_type_t _spi_type;        
00205 };
00206 
00207 #endif // __LIS3MDL_CLASS_H