Oleg Goncharovskiy / LIS3MDL

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
docent
Date:
Mon Sep 27 05:10:52 2021 +0000
Revision:
7:c9c9ed520566
Parent:
4:00493226e59f
AGV_RKP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:7376fbeb1d4a 1 /**
nikapov 0:7376fbeb1d4a 2 ******************************************************************************
nikapov 0:7376fbeb1d4a 3 * @file lis3mdl_class.h
nikapov 0:7376fbeb1d4a 4 * @author AST / EST
nikapov 0:7376fbeb1d4a 5 * @version V0.0.1
nikapov 0:7376fbeb1d4a 6 * @date 14-April-2015
nikapov 0:7376fbeb1d4a 7 * @brief Header file for component LIS3MDL
nikapov 0:7376fbeb1d4a 8 ******************************************************************************
nikapov 0:7376fbeb1d4a 9 * @attention
nikapov 0:7376fbeb1d4a 10 *
nikapov 0:7376fbeb1d4a 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
nikapov 0:7376fbeb1d4a 12 *
nikapov 0:7376fbeb1d4a 13 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:7376fbeb1d4a 14 * are permitted provided that the following conditions are met:
nikapov 0:7376fbeb1d4a 15 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:7376fbeb1d4a 16 * this list of conditions and the following disclaimer.
nikapov 0:7376fbeb1d4a 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:7376fbeb1d4a 18 * this list of conditions and the following disclaimer in the documentation
nikapov 0:7376fbeb1d4a 19 * and/or other materials provided with the distribution.
nikapov 0:7376fbeb1d4a 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:7376fbeb1d4a 21 * may be used to endorse or promote products derived from this software
nikapov 0:7376fbeb1d4a 22 * without specific prior written permission.
nikapov 0:7376fbeb1d4a 23 *
nikapov 0:7376fbeb1d4a 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:7376fbeb1d4a 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:7376fbeb1d4a 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:7376fbeb1d4a 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:7376fbeb1d4a 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:7376fbeb1d4a 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:7376fbeb1d4a 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:7376fbeb1d4a 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:7376fbeb1d4a 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:7376fbeb1d4a 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:7376fbeb1d4a 34 *
nikapov 0:7376fbeb1d4a 35 ******************************************************************************
nikapov 0:7376fbeb1d4a 36 */
nikapov 0:7376fbeb1d4a 37
nikapov 0:7376fbeb1d4a 38 #ifndef __LIS3MDL_CLASS_H
nikapov 0:7376fbeb1d4a 39 #define __LIS3MDL_CLASS_H
nikapov 0:7376fbeb1d4a 40
nikapov 0:7376fbeb1d4a 41 /* Includes ------------------------------------------------------------------*/
nikapov 0:7376fbeb1d4a 42 #include "mbed.h"
nikapov 0:7376fbeb1d4a 43 #include "DevI2C.h"
nikapov 0:7376fbeb1d4a 44 #include "lis3mdl.h"
nikapov 0:7376fbeb1d4a 45 #include "MagneticSensor.h"
mapellil 3:a35a16f3da39 46 #include <assert.h>
nikapov 0:7376fbeb1d4a 47
mapellil 4:00493226e59f 48
nikapov 0:7376fbeb1d4a 49 /* Classes -------------------------------------------------------------------*/
nikapov 0:7376fbeb1d4a 50 /** Class representing a LIS3MDL sensor component
nikapov 0:7376fbeb1d4a 51 */
nikapov 0:7376fbeb1d4a 52 class LIS3MDL : public MagneticSensor {
nikapov 0:7376fbeb1d4a 53 public:
docent 7:c9c9ed520566 54 //my
docent 7:c9c9ed520566 55 enum SPI_type_t {SPI3W, SPI4W};
mapellil 4:00493226e59f 56
docent 7:c9c9ed520566 57 //my
mapellil 4:00493226e59f 58 LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W);
mapellil 4:00493226e59f 59
nikapov 0:7376fbeb1d4a 60 /** Constructor
nikapov 0:7376fbeb1d4a 61 * @param[in] i2c device I2C to be used for communication
mapellil 4:00493226e59f 62 */
mapellil 4:00493226e59f 63
mapellil 4:00493226e59f 64 LIS3MDL(DevI2C *i2c, uint8_t address=LIS3MDL_M_MEMS_ADDRESS_HIGH, PinName int_pin=NC);
nikapov 0:7376fbeb1d4a 65
nikapov 0:7376fbeb1d4a 66 /** Destructor
nikapov 0:7376fbeb1d4a 67 */
nikapov 0:7376fbeb1d4a 68 virtual ~LIS3MDL() {}
nikapov 0:7376fbeb1d4a 69
nikapov 0:7376fbeb1d4a 70 /*** Interface Methods ***/
nikapov 0:7376fbeb1d4a 71 virtual int init(void *init_struct) {
nikapov 0:7376fbeb1d4a 72 return LIS3MDL_Init((MAGNETO_InitTypeDef*)init_struct);
nikapov 0:7376fbeb1d4a 73 }
nikapov 0:7376fbeb1d4a 74
nikapov 0:7376fbeb1d4a 75 virtual int read_id(uint8_t *m_id) {
nikapov 0:7376fbeb1d4a 76 return LIS3MDL_Read_M_ID(m_id);
nikapov 0:7376fbeb1d4a 77 }
nikapov 0:7376fbeb1d4a 78
nikapov 0:7376fbeb1d4a 79 virtual int get_m_axes(int32_t *pData) {
nikapov 0:7376fbeb1d4a 80 return LIS3MDL_M_GetAxes(pData);
nikapov 0:7376fbeb1d4a 81 }
nikapov 0:7376fbeb1d4a 82
nikapov 0:7376fbeb1d4a 83 virtual int get_m_axes_raw(int16_t *pData) {
nikapov 0:7376fbeb1d4a 84 return LIS3MDL_M_GetAxesRaw(pData);
nikapov 0:7376fbeb1d4a 85 }
docent 7:c9c9ed520566 86
docent 7:c9c9ed520566 87 virtual int set_m_axes_offset(uint8_t axe,int16_t offset) { //my
docent 7:c9c9ed520566 88 return LIS3MDL_M_SetAxeOffset(axe,offset);
docent 7:c9c9ed520566 89 }
docent 7:c9c9ed520566 90 virtual int set_BDU() { //my
docent 7:c9c9ed520566 91 uint8_t tmp;
docent 7:c9c9ed520566 92 if(LIS3MDL_IO_Read(&tmp, LIS3MDL_M_CTRL_REG5_M, 1) != MAGNETO_OK)
docent 7:c9c9ed520566 93 return MAGNETO_ERROR;
docent 7:c9c9ed520566 94 tmp=0x40;
docent 7:c9c9ed520566 95 return LIS3MDL_IO_Write(&tmp, LIS3MDL_M_CTRL_REG5_M, 1);
docent 7:c9c9ed520566 96 }
nikapov 0:7376fbeb1d4a 97 protected:
nikapov 0:7376fbeb1d4a 98 /*** Methods ***/
nikapov 0:7376fbeb1d4a 99 MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init);
nikapov 0:7376fbeb1d4a 100 MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id);
nikapov 0:7376fbeb1d4a 101 MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData);
nikapov 0:7376fbeb1d4a 102 MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData);
docent 7:c9c9ed520566 103 //my
mapellil 4:00493226e59f 104 MAGNETO_StatusTypeDef LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode);
docent 7:c9c9ed520566 105 MAGNETO_StatusTypeDef LIS3MDL_M_SetAxeOffset(uint8_t axe,int16_t offset);//my
nikapov 0:7376fbeb1d4a 106 /**
nikapov 0:7376fbeb1d4a 107 * @brief Configures LIS3MDL interrupt lines for NUCLEO boards
nikapov 0:7376fbeb1d4a 108 */
nikapov 0:7376fbeb1d4a 109 void LIS3MDL_IO_ITConfig(void)
nikapov 0:7376fbeb1d4a 110 {
nikapov 0:7376fbeb1d4a 111 /* To be implemented */
nikapov 0:7376fbeb1d4a 112 }
nikapov 0:7376fbeb1d4a 113
nikapov 0:7376fbeb1d4a 114 /**
nikapov 0:7376fbeb1d4a 115 * @brief Configures LIS3MDL I2C interface
nikapov 0:7376fbeb1d4a 116 * @return MAGNETO_OK in case of success, an error code otherwise
nikapov 0:7376fbeb1d4a 117 */
nikapov 0:7376fbeb1d4a 118 MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void)
nikapov 0:7376fbeb1d4a 119 {
nikapov 0:7376fbeb1d4a 120 return MAGNETO_OK; /* done in constructor */
nikapov 0:7376fbeb1d4a 121 }
nikapov 0:7376fbeb1d4a 122
nikapov 0:7376fbeb1d4a 123 /**
nikapov 0:7376fbeb1d4a 124 * @brief Utility function to read data from LIS3MDL
nikapov 0:7376fbeb1d4a 125 * @param[out] pBuffer pointer to the byte-array to read data in to
nikapov 0:7376fbeb1d4a 126 * @param[in] RegisterAddr specifies internal address register to read from.
nikapov 0:7376fbeb1d4a 127 * @param[in] NumByteToRead number of bytes to be read.
nikapov 0:7376fbeb1d4a 128 * @retval MAGNETO_OK if ok,
nikapov 0:7376fbeb1d4a 129 * @retval MAGNETO_ERROR if an I2C error has occured
nikapov 0:7376fbeb1d4a 130 */
nikapov 0:7376fbeb1d4a 131 MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer,
nikapov 0:7376fbeb1d4a 132 uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:7376fbeb1d4a 133 {
docent 7:c9c9ed520566 134 /*my
mapellil 4:00493226e59f 135 if (_dev_spi) {
docent 7:c9c9ed520566 136 // Write Reg Address
mapellil 4:00493226e59f 137 _dev_spi->lock();
mapellil 4:00493226e59f 138 _cs_pin = 0;
mapellil 4:00493226e59f 139 if (_spi_type == SPI4W) {
mapellil 4:00493226e59f 140 _dev_spi->write(RegisterAddr | 0x80);
mapellil 4:00493226e59f 141 for (int i=0; i<NumByteToRead; i++) {
mapellil 4:00493226e59f 142 *(pBuffer+i) = _dev_spi->write(0x00);
mapellil 4:00493226e59f 143 }
mapellil 4:00493226e59f 144 } else if (_spi_type == SPI3W){
docent 7:c9c9ed520566 145 // Write RD Reg Address with RD bit
mapellil 4:00493226e59f 146 uint8_t TxByte = RegisterAddr | 0x80;
mapellil 4:00493226e59f 147 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
mapellil 4:00493226e59f 148 }
mapellil 4:00493226e59f 149 _cs_pin = 1;
mapellil 4:00493226e59f 150 _dev_spi->unlock();
mapellil 4:00493226e59f 151 return MAGNETO_OK;
docent 7:c9c9ed520566 152 }
docent 7:c9c9ed520566 153 */
mapellil 4:00493226e59f 154 if (!_dev_i2c) return MAGNETO_ERROR;
mapellil 4:00493226e59f 155 int ret = _dev_i2c->i2c_read(pBuffer,
mapellil 4:00493226e59f 156 _address,
nikapov 0:7376fbeb1d4a 157 RegisterAddr,
nikapov 0:7376fbeb1d4a 158 NumByteToRead);
nikapov 0:7376fbeb1d4a 159 if(ret != 0) {
nikapov 0:7376fbeb1d4a 160 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 161 }
nikapov 0:7376fbeb1d4a 162 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 163 }
nikapov 0:7376fbeb1d4a 164
nikapov 0:7376fbeb1d4a 165 /**
nikapov 0:7376fbeb1d4a 166 * @brief Utility function to write data to LIS3MDL
nikapov 0:7376fbeb1d4a 167 * @param[in] pBuffer pointer to the byte-array data to send
nikapov 0:7376fbeb1d4a 168 * @param[in] RegisterAddr specifies internal address register to read from.
nikapov 0:7376fbeb1d4a 169 * @param[in] NumByteToWrite number of bytes to write.
nikapov 0:7376fbeb1d4a 170 * @retval MAGNETO_OK if ok,
nikapov 0:7376fbeb1d4a 171 * @retval MAGNETO_ERROR if an I2C error has occured
nikapov 0:7376fbeb1d4a 172 */
nikapov 0:7376fbeb1d4a 173 MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer,
nikapov 0:7376fbeb1d4a 174 uint8_t RegisterAddr, uint16_t NumByteToWrite)
nikapov 0:7376fbeb1d4a 175 {
docent 7:c9c9ed520566 176 /*
mapellil 4:00493226e59f 177 if (_dev_spi) {
mapellil 4:00493226e59f 178 _dev_spi->lock();
mapellil 4:00493226e59f 179 _cs_pin = 0;
mapellil 4:00493226e59f 180 int data = _dev_spi->write(RegisterAddr);
mapellil 4:00493226e59f 181 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
mapellil 4:00493226e59f 182 _cs_pin = 1;
mapellil 4:00493226e59f 183 _dev_spi->unlock();
mapellil 4:00493226e59f 184 return MAGNETO_OK;
mapellil 4:00493226e59f 185 }
docent 7:c9c9ed520566 186 */
mapellil 4:00493226e59f 187 if (!_dev_i2c) return MAGNETO_ERROR;
mapellil 4:00493226e59f 188 int ret = _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
nikapov 0:7376fbeb1d4a 189 if(ret != 0) {
nikapov 0:7376fbeb1d4a 190 return MAGNETO_ERROR;
nikapov 0:7376fbeb1d4a 191 }
nikapov 0:7376fbeb1d4a 192 return MAGNETO_OK;
nikapov 0:7376fbeb1d4a 193 }
nikapov 0:7376fbeb1d4a 194
nikapov 0:7376fbeb1d4a 195 /*** Instance Variables ***/
nikapov 0:7376fbeb1d4a 196 /* IO Device */
mapellil 4:00493226e59f 197 DevI2C *_dev_i2c;
docent 7:c9c9ed520566 198 //my
mapellil 4:00493226e59f 199 SPI *_dev_spi;
mapellil 4:00493226e59f 200 uint8_t _address;
mapellil 4:00493226e59f 201 DigitalOut _cs_pin;
mapellil 4:00493226e59f 202 InterruptIn _int_pin;
docent 7:c9c9ed520566 203 //my
mapellil 4:00493226e59f 204 SPI_type_t _spi_type;
nikapov 0:7376fbeb1d4a 205 };
nikapov 0:7376fbeb1d4a 206
nikapov 0:7376fbeb1d4a 207 #endif // __LIS3MDL_CLASS_H