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.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
lis3mdl_class.h
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>© 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 enum SPI_type_t {SPI3W, SPI4W}; 00055 00056 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 protected: 00086 /*** Methods ***/ 00087 MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init); 00088 MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id); 00089 MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData); 00090 MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData); 00091 MAGNETO_StatusTypeDef LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode); 00092 00093 /** 00094 * @brief Configures LIS3MDL interrupt lines for NUCLEO boards 00095 */ 00096 void LIS3MDL_IO_ITConfig(void) 00097 { 00098 /* To be implemented */ 00099 } 00100 00101 /** 00102 * @brief Configures LIS3MDL I2C interface 00103 * @return MAGNETO_OK in case of success, an error code otherwise 00104 */ 00105 MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void) 00106 { 00107 return MAGNETO_OK; /* done in constructor */ 00108 } 00109 00110 /** 00111 * @brief Utility function to read data from LIS3MDL 00112 * @param[out] pBuffer pointer to the byte-array to read data in to 00113 * @param[in] RegisterAddr specifies internal address register to read from. 00114 * @param[in] NumByteToRead number of bytes to be read. 00115 * @retval MAGNETO_OK if ok, 00116 * @retval MAGNETO_ERROR if an I2C error has occured 00117 */ 00118 MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer, 00119 uint8_t RegisterAddr, uint16_t NumByteToRead) 00120 { 00121 if (_dev_spi) { 00122 /* Write Reg Address */ 00123 _dev_spi->lock(); 00124 _cs_pin = 0; 00125 if (_spi_type == SPI4W) { 00126 _dev_spi->write(RegisterAddr | 0x80); 00127 for (int i=0; i<NumByteToRead; i++) { 00128 *(pBuffer+i) = _dev_spi->write(0x00); 00129 } 00130 } else if (_spi_type == SPI3W){ 00131 /* Write RD Reg Address with RD bit*/ 00132 uint8_t TxByte = RegisterAddr | 0x80; 00133 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); 00134 } 00135 _cs_pin = 1; 00136 _dev_spi->unlock(); 00137 return MAGNETO_OK; 00138 } 00139 if (!_dev_i2c) return MAGNETO_ERROR; 00140 int ret = _dev_i2c->i2c_read(pBuffer, 00141 _address, 00142 RegisterAddr, 00143 NumByteToRead); 00144 if(ret != 0) { 00145 return MAGNETO_ERROR; 00146 } 00147 return MAGNETO_OK; 00148 } 00149 00150 /** 00151 * @brief Utility function to write data to LIS3MDL 00152 * @param[in] pBuffer pointer to the byte-array data to send 00153 * @param[in] RegisterAddr specifies internal address register to read from. 00154 * @param[in] NumByteToWrite number of bytes to write. 00155 * @retval MAGNETO_OK if ok, 00156 * @retval MAGNETO_ERROR if an I2C error has occured 00157 */ 00158 MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer, 00159 uint8_t RegisterAddr, uint16_t NumByteToWrite) 00160 { 00161 00162 if (_dev_spi) { 00163 _dev_spi->lock(); 00164 _cs_pin = 0; 00165 int data = _dev_spi->write(RegisterAddr); 00166 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); 00167 _cs_pin = 1; 00168 _dev_spi->unlock(); 00169 return MAGNETO_OK; 00170 } 00171 00172 if (!_dev_i2c) return MAGNETO_ERROR; 00173 int ret = _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); 00174 if(ret != 0) { 00175 return MAGNETO_ERROR; 00176 } 00177 return MAGNETO_OK; 00178 } 00179 00180 /*** Instance Variables ***/ 00181 /* IO Device */ 00182 DevI2C *_dev_i2c; 00183 SPI *_dev_spi; 00184 uint8_t _address; 00185 DigitalOut _cs_pin; 00186 InterruptIn _int_pin; 00187 SPI_type_t _spi_type; 00188 }; 00189 00190 #endif // __LIS3MDL_CLASS_H
Generated on Fri Jul 15 2022 04:42:54 by
