for my students set_m_axes_offset
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
lis3mdl_class.h@4:00493226e59f, 2017-10-06 (annotated)
- Committer:
- mapellil
- Date:
- Fri Oct 06 11:48:36 2017 +0000
- Revision:
- 4:00493226e59f
- Parent:
- 3:a35a16f3da39
- Child:
- 7:fa19aa1a666d
Added SPI3/4W interface
Who changed what in which revision?
User | Revision | Line number | New 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>© 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: |
mapellil | 4:00493226e59f | 54 | enum SPI_type_t {SPI3W, SPI4W}; |
mapellil | 4:00493226e59f | 55 | |
mapellil | 4:00493226e59f | 56 | LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W); |
mapellil | 4:00493226e59f | 57 | |
nikapov | 0:7376fbeb1d4a | 58 | /** Constructor |
nikapov | 0:7376fbeb1d4a | 59 | * @param[in] i2c device I2C to be used for communication |
mapellil | 4:00493226e59f | 60 | */ |
mapellil | 4:00493226e59f | 61 | |
mapellil | 4:00493226e59f | 62 | LIS3MDL(DevI2C *i2c, uint8_t address=LIS3MDL_M_MEMS_ADDRESS_HIGH, PinName int_pin=NC); |
nikapov | 0:7376fbeb1d4a | 63 | |
nikapov | 0:7376fbeb1d4a | 64 | /** Destructor |
nikapov | 0:7376fbeb1d4a | 65 | */ |
nikapov | 0:7376fbeb1d4a | 66 | virtual ~LIS3MDL() {} |
nikapov | 0:7376fbeb1d4a | 67 | |
nikapov | 0:7376fbeb1d4a | 68 | /*** Interface Methods ***/ |
nikapov | 0:7376fbeb1d4a | 69 | virtual int init(void *init_struct) { |
nikapov | 0:7376fbeb1d4a | 70 | return LIS3MDL_Init((MAGNETO_InitTypeDef*)init_struct); |
nikapov | 0:7376fbeb1d4a | 71 | } |
nikapov | 0:7376fbeb1d4a | 72 | |
nikapov | 0:7376fbeb1d4a | 73 | virtual int read_id(uint8_t *m_id) { |
nikapov | 0:7376fbeb1d4a | 74 | return LIS3MDL_Read_M_ID(m_id); |
nikapov | 0:7376fbeb1d4a | 75 | } |
nikapov | 0:7376fbeb1d4a | 76 | |
nikapov | 0:7376fbeb1d4a | 77 | virtual int get_m_axes(int32_t *pData) { |
nikapov | 0:7376fbeb1d4a | 78 | return LIS3MDL_M_GetAxes(pData); |
nikapov | 0:7376fbeb1d4a | 79 | } |
nikapov | 0:7376fbeb1d4a | 80 | |
nikapov | 0:7376fbeb1d4a | 81 | virtual int get_m_axes_raw(int16_t *pData) { |
nikapov | 0:7376fbeb1d4a | 82 | return LIS3MDL_M_GetAxesRaw(pData); |
nikapov | 0:7376fbeb1d4a | 83 | } |
nikapov | 0:7376fbeb1d4a | 84 | |
nikapov | 0:7376fbeb1d4a | 85 | protected: |
nikapov | 0:7376fbeb1d4a | 86 | /*** Methods ***/ |
nikapov | 0:7376fbeb1d4a | 87 | MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init); |
nikapov | 0:7376fbeb1d4a | 88 | MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id); |
nikapov | 0:7376fbeb1d4a | 89 | MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData); |
nikapov | 0:7376fbeb1d4a | 90 | MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData); |
mapellil | 4:00493226e59f | 91 | MAGNETO_StatusTypeDef LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode); |
nikapov | 0:7376fbeb1d4a | 92 | |
nikapov | 0:7376fbeb1d4a | 93 | /** |
nikapov | 0:7376fbeb1d4a | 94 | * @brief Configures LIS3MDL interrupt lines for NUCLEO boards |
nikapov | 0:7376fbeb1d4a | 95 | */ |
nikapov | 0:7376fbeb1d4a | 96 | void LIS3MDL_IO_ITConfig(void) |
nikapov | 0:7376fbeb1d4a | 97 | { |
nikapov | 0:7376fbeb1d4a | 98 | /* To be implemented */ |
nikapov | 0:7376fbeb1d4a | 99 | } |
nikapov | 0:7376fbeb1d4a | 100 | |
nikapov | 0:7376fbeb1d4a | 101 | /** |
nikapov | 0:7376fbeb1d4a | 102 | * @brief Configures LIS3MDL I2C interface |
nikapov | 0:7376fbeb1d4a | 103 | * @return MAGNETO_OK in case of success, an error code otherwise |
nikapov | 0:7376fbeb1d4a | 104 | */ |
nikapov | 0:7376fbeb1d4a | 105 | MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void) |
nikapov | 0:7376fbeb1d4a | 106 | { |
nikapov | 0:7376fbeb1d4a | 107 | return MAGNETO_OK; /* done in constructor */ |
nikapov | 0:7376fbeb1d4a | 108 | } |
nikapov | 0:7376fbeb1d4a | 109 | |
nikapov | 0:7376fbeb1d4a | 110 | /** |
nikapov | 0:7376fbeb1d4a | 111 | * @brief Utility function to read data from LIS3MDL |
nikapov | 0:7376fbeb1d4a | 112 | * @param[out] pBuffer pointer to the byte-array to read data in to |
nikapov | 0:7376fbeb1d4a | 113 | * @param[in] RegisterAddr specifies internal address register to read from. |
nikapov | 0:7376fbeb1d4a | 114 | * @param[in] NumByteToRead number of bytes to be read. |
nikapov | 0:7376fbeb1d4a | 115 | * @retval MAGNETO_OK if ok, |
nikapov | 0:7376fbeb1d4a | 116 | * @retval MAGNETO_ERROR if an I2C error has occured |
nikapov | 0:7376fbeb1d4a | 117 | */ |
nikapov | 0:7376fbeb1d4a | 118 | MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer, |
nikapov | 0:7376fbeb1d4a | 119 | uint8_t RegisterAddr, uint16_t NumByteToRead) |
nikapov | 0:7376fbeb1d4a | 120 | { |
mapellil | 4:00493226e59f | 121 | if (_dev_spi) { |
mapellil | 4:00493226e59f | 122 | /* Write Reg Address */ |
mapellil | 4:00493226e59f | 123 | _dev_spi->lock(); |
mapellil | 4:00493226e59f | 124 | _cs_pin = 0; |
mapellil | 4:00493226e59f | 125 | if (_spi_type == SPI4W) { |
mapellil | 4:00493226e59f | 126 | _dev_spi->write(RegisterAddr | 0x80); |
mapellil | 4:00493226e59f | 127 | for (int i=0; i<NumByteToRead; i++) { |
mapellil | 4:00493226e59f | 128 | *(pBuffer+i) = _dev_spi->write(0x00); |
mapellil | 4:00493226e59f | 129 | } |
mapellil | 4:00493226e59f | 130 | } else if (_spi_type == SPI3W){ |
mapellil | 4:00493226e59f | 131 | /* Write RD Reg Address with RD bit*/ |
mapellil | 4:00493226e59f | 132 | uint8_t TxByte = RegisterAddr | 0x80; |
mapellil | 4:00493226e59f | 133 | _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); |
mapellil | 4:00493226e59f | 134 | } |
mapellil | 4:00493226e59f | 135 | _cs_pin = 1; |
mapellil | 4:00493226e59f | 136 | _dev_spi->unlock(); |
mapellil | 4:00493226e59f | 137 | return MAGNETO_OK; |
mapellil | 4:00493226e59f | 138 | } |
mapellil | 4:00493226e59f | 139 | if (!_dev_i2c) return MAGNETO_ERROR; |
mapellil | 4:00493226e59f | 140 | int ret = _dev_i2c->i2c_read(pBuffer, |
mapellil | 4:00493226e59f | 141 | _address, |
nikapov | 0:7376fbeb1d4a | 142 | RegisterAddr, |
nikapov | 0:7376fbeb1d4a | 143 | NumByteToRead); |
nikapov | 0:7376fbeb1d4a | 144 | if(ret != 0) { |
nikapov | 0:7376fbeb1d4a | 145 | return MAGNETO_ERROR; |
nikapov | 0:7376fbeb1d4a | 146 | } |
nikapov | 0:7376fbeb1d4a | 147 | return MAGNETO_OK; |
nikapov | 0:7376fbeb1d4a | 148 | } |
nikapov | 0:7376fbeb1d4a | 149 | |
nikapov | 0:7376fbeb1d4a | 150 | /** |
nikapov | 0:7376fbeb1d4a | 151 | * @brief Utility function to write data to LIS3MDL |
nikapov | 0:7376fbeb1d4a | 152 | * @param[in] pBuffer pointer to the byte-array data to send |
nikapov | 0:7376fbeb1d4a | 153 | * @param[in] RegisterAddr specifies internal address register to read from. |
nikapov | 0:7376fbeb1d4a | 154 | * @param[in] NumByteToWrite number of bytes to write. |
nikapov | 0:7376fbeb1d4a | 155 | * @retval MAGNETO_OK if ok, |
nikapov | 0:7376fbeb1d4a | 156 | * @retval MAGNETO_ERROR if an I2C error has occured |
nikapov | 0:7376fbeb1d4a | 157 | */ |
nikapov | 0:7376fbeb1d4a | 158 | MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer, |
nikapov | 0:7376fbeb1d4a | 159 | uint8_t RegisterAddr, uint16_t NumByteToWrite) |
nikapov | 0:7376fbeb1d4a | 160 | { |
mapellil | 4:00493226e59f | 161 | |
mapellil | 4:00493226e59f | 162 | if (_dev_spi) { |
mapellil | 4:00493226e59f | 163 | _dev_spi->lock(); |
mapellil | 4:00493226e59f | 164 | _cs_pin = 0; |
mapellil | 4:00493226e59f | 165 | int data = _dev_spi->write(RegisterAddr); |
mapellil | 4:00493226e59f | 166 | _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); |
mapellil | 4:00493226e59f | 167 | _cs_pin = 1; |
mapellil | 4:00493226e59f | 168 | _dev_spi->unlock(); |
mapellil | 4:00493226e59f | 169 | return MAGNETO_OK; |
mapellil | 4:00493226e59f | 170 | } |
mapellil | 4:00493226e59f | 171 | |
mapellil | 4:00493226e59f | 172 | if (!_dev_i2c) return MAGNETO_ERROR; |
mapellil | 4:00493226e59f | 173 | int ret = _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); |
nikapov | 0:7376fbeb1d4a | 174 | if(ret != 0) { |
nikapov | 0:7376fbeb1d4a | 175 | return MAGNETO_ERROR; |
nikapov | 0:7376fbeb1d4a | 176 | } |
nikapov | 0:7376fbeb1d4a | 177 | return MAGNETO_OK; |
nikapov | 0:7376fbeb1d4a | 178 | } |
nikapov | 0:7376fbeb1d4a | 179 | |
nikapov | 0:7376fbeb1d4a | 180 | /*** Instance Variables ***/ |
nikapov | 0:7376fbeb1d4a | 181 | /* IO Device */ |
mapellil | 4:00493226e59f | 182 | DevI2C *_dev_i2c; |
mapellil | 4:00493226e59f | 183 | SPI *_dev_spi; |
mapellil | 4:00493226e59f | 184 | uint8_t _address; |
mapellil | 4:00493226e59f | 185 | DigitalOut _cs_pin; |
mapellil | 4:00493226e59f | 186 | InterruptIn _int_pin; |
mapellil | 4:00493226e59f | 187 | SPI_type_t _spi_type; |
nikapov | 0:7376fbeb1d4a | 188 | }; |
nikapov | 0:7376fbeb1d4a | 189 | |
nikapov | 0:7376fbeb1d4a | 190 | #endif // __LIS3MDL_CLASS_H |