Oleg Goncharovskiy / LIS3MDL_my

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   Exam_TK Exam_TK

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 enum SPI_type_t {SPI3W, SPI4W};    
00055     
00056     //my LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W);
00057         
00058     /** Constructor
00059      * @param[in] i2c device I2C to be used for communication
00060      */    
00061      
00062     LIS3MDL(DevI2C *i2c, uint8_t address=LIS3MDL_M_MEMS_ADDRESS_HIGH, PinName int_pin=NC);
00063     
00064     /** Destructor
00065      */
00066         virtual ~LIS3MDL() {}
00067     
00068     /*** Interface Methods ***/
00069     virtual int init(void *init_struct) {
00070         return LIS3MDL_Init((MAGNETO_InitTypeDef*)init_struct);
00071     }
00072 
00073     virtual int read_id(uint8_t *m_id) {
00074         return LIS3MDL_Read_M_ID(m_id);
00075     }
00076 
00077     virtual int get_m_axes(int32_t *pData) {
00078         return LIS3MDL_M_GetAxes(pData);
00079     }
00080 
00081     virtual int get_m_axes_raw(int16_t *pData) {
00082         return LIS3MDL_M_GetAxesRaw(pData);
00083     }
00084     
00085     virtual int set_m_axes_offset(uint8_t axe,int16_t offset) { //my
00086       return LIS3MDL_M_SetAxeOffset(axe,offset);
00087     }
00088  protected:
00089     /*** Methods ***/
00090     MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init);
00091     MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id);
00092     MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData);
00093     MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData);
00094     //my MAGNETO_StatusTypeDef LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode);
00095     MAGNETO_StatusTypeDef LIS3MDL_M_SetAxeOffset(uint8_t axe,int16_t offset);//my
00096     /**
00097      * @brief  Configures LIS3MDL interrupt lines for NUCLEO boards
00098      */
00099     void LIS3MDL_IO_ITConfig(void)
00100     {
00101         /* To be implemented */
00102     }
00103 
00104     /**
00105      * @brief  Configures LIS3MDL I2C interface
00106      * @return MAGNETO_OK in case of success, an error code otherwise
00107      */
00108     MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void)
00109     {
00110         return MAGNETO_OK; /* done in constructor */
00111     }
00112 
00113     /**
00114      * @brief      Utility function to read data from LIS3MDL
00115      * @param[out] pBuffer pointer to the byte-array to read data in to
00116      * @param[in]  RegisterAddr specifies internal address register to read from.
00117      * @param[in]  NumByteToRead number of bytes to be read.
00118      * @retval     MAGNETO_OK if ok, 
00119      * @retval     MAGNETO_ERROR if an I2C error has occured
00120      */
00121     MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer, 
00122                           uint8_t RegisterAddr, uint16_t NumByteToRead)
00123     {
00124 /*my        
00125         if (_dev_spi) {
00126         // Write Reg Address 
00127             _dev_spi->lock();
00128             _cs_pin = 0;           
00129             if (_spi_type == SPI4W) {            
00130                 _dev_spi->write(RegisterAddr | 0x80);
00131                 for (int i=0; i<NumByteToRead; i++) {
00132                     *(pBuffer+i) = _dev_spi->write(0x00);
00133                 }
00134             } else if (_spi_type == SPI3W){
00135                 // Write RD Reg Address with RD bit
00136                 uint8_t TxByte = RegisterAddr | 0x80;    
00137                 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
00138             }            
00139             _cs_pin = 1;
00140             _dev_spi->unlock(); 
00141             return MAGNETO_OK;
00142         }  
00143 */                          
00144         if (!_dev_i2c) return MAGNETO_ERROR;            
00145         int ret = _dev_i2c->i2c_read(pBuffer,
00146                        _address,
00147                        RegisterAddr,
00148                        NumByteToRead);
00149         if(ret != 0) {
00150             return MAGNETO_ERROR;
00151         }
00152         return MAGNETO_OK;
00153     }
00154     
00155     /**
00156      * @brief      Utility function to write data to LIS3MDL
00157      * @param[in]  pBuffer pointer to the byte-array data to send
00158      * @param[in]  RegisterAddr specifies internal address register to read from.
00159      * @param[in]  NumByteToWrite number of bytes to write.
00160      * @retval     MAGNETO_OK if ok, 
00161      * @retval     MAGNETO_ERROR if an I2C error has occured
00162      */
00163     MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer, 
00164                            uint8_t RegisterAddr, uint16_t NumByteToWrite)
00165     {
00166 /*      
00167         if (_dev_spi) { 
00168             _dev_spi->lock();
00169             _cs_pin = 0;
00170             int data = _dev_spi->write(RegisterAddr);                    
00171             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);                     
00172             _cs_pin = 1;                    
00173             _dev_spi->unlock();
00174             return MAGNETO_OK;                    
00175         }        
00176 */      
00177         if (!_dev_i2c) return MAGNETO_ERROR;    
00178         int ret = _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
00179         if(ret != 0) {
00180             return MAGNETO_ERROR;
00181         }
00182         return MAGNETO_OK;
00183     }
00184     
00185     /*** Instance Variables ***/
00186     /* IO Device */
00187     DevI2C *_dev_i2c;
00188     //my SPI    *_dev_spi;
00189     uint8_t _address;   
00190     DigitalOut  _cs_pin; 
00191     InterruptIn _int_pin;    
00192     //my SPI_type_t _spi_type;        
00193 };
00194 
00195 #endif // __LIS3MDL_CLASS_H