for my students set_m_axes_offset
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
lis3mdl_class.h@7:fa19aa1a666d, 2020-02-08 (annotated)
- Committer:
- docent
- Date:
- Sat Feb 08 09:11:31 2020 +0000
- Revision:
- 7:fa19aa1a666d
- Parent:
- 4:00493226e59f
for my students set_m_axes_offset
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: |
docent | 7:fa19aa1a666d | 54 | //my enum SPI_type_t {SPI3W, SPI4W}; |
mapellil | 4:00493226e59f | 55 | |
docent | 7:fa19aa1a666d | 56 | //my 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 | } |
docent | 7:fa19aa1a666d | 84 | |
docent | 7:fa19aa1a666d | 85 | virtual int set_m_axes_offset(uint8_t axe,int16_t offset) { //my |
docent | 7:fa19aa1a666d | 86 | return LIS3MDL_M_SetAxeOffset(axe,offset); |
docent | 7:fa19aa1a666d | 87 | } |
nikapov | 0:7376fbeb1d4a | 88 | protected: |
nikapov | 0:7376fbeb1d4a | 89 | /*** Methods ***/ |
nikapov | 0:7376fbeb1d4a | 90 | MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init); |
nikapov | 0:7376fbeb1d4a | 91 | MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id); |
nikapov | 0:7376fbeb1d4a | 92 | MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData); |
nikapov | 0:7376fbeb1d4a | 93 | MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData); |
docent | 7:fa19aa1a666d | 94 | //my MAGNETO_StatusTypeDef LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode); |
docent | 7:fa19aa1a666d | 95 | MAGNETO_StatusTypeDef LIS3MDL_M_SetAxeOffset(uint8_t axe,int16_t offset);//my |
nikapov | 0:7376fbeb1d4a | 96 | /** |
nikapov | 0:7376fbeb1d4a | 97 | * @brief Configures LIS3MDL interrupt lines for NUCLEO boards |
nikapov | 0:7376fbeb1d4a | 98 | */ |
nikapov | 0:7376fbeb1d4a | 99 | void LIS3MDL_IO_ITConfig(void) |
nikapov | 0:7376fbeb1d4a | 100 | { |
nikapov | 0:7376fbeb1d4a | 101 | /* To be implemented */ |
nikapov | 0:7376fbeb1d4a | 102 | } |
nikapov | 0:7376fbeb1d4a | 103 | |
nikapov | 0:7376fbeb1d4a | 104 | /** |
nikapov | 0:7376fbeb1d4a | 105 | * @brief Configures LIS3MDL I2C interface |
nikapov | 0:7376fbeb1d4a | 106 | * @return MAGNETO_OK in case of success, an error code otherwise |
nikapov | 0:7376fbeb1d4a | 107 | */ |
nikapov | 0:7376fbeb1d4a | 108 | MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void) |
nikapov | 0:7376fbeb1d4a | 109 | { |
nikapov | 0:7376fbeb1d4a | 110 | return MAGNETO_OK; /* done in constructor */ |
nikapov | 0:7376fbeb1d4a | 111 | } |
nikapov | 0:7376fbeb1d4a | 112 | |
nikapov | 0:7376fbeb1d4a | 113 | /** |
nikapov | 0:7376fbeb1d4a | 114 | * @brief Utility function to read data from LIS3MDL |
nikapov | 0:7376fbeb1d4a | 115 | * @param[out] pBuffer pointer to the byte-array to read data in to |
nikapov | 0:7376fbeb1d4a | 116 | * @param[in] RegisterAddr specifies internal address register to read from. |
nikapov | 0:7376fbeb1d4a | 117 | * @param[in] NumByteToRead number of bytes to be read. |
nikapov | 0:7376fbeb1d4a | 118 | * @retval MAGNETO_OK if ok, |
nikapov | 0:7376fbeb1d4a | 119 | * @retval MAGNETO_ERROR if an I2C error has occured |
nikapov | 0:7376fbeb1d4a | 120 | */ |
nikapov | 0:7376fbeb1d4a | 121 | MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer, |
nikapov | 0:7376fbeb1d4a | 122 | uint8_t RegisterAddr, uint16_t NumByteToRead) |
nikapov | 0:7376fbeb1d4a | 123 | { |
docent | 7:fa19aa1a666d | 124 | /*my |
mapellil | 4:00493226e59f | 125 | if (_dev_spi) { |
docent | 7:fa19aa1a666d | 126 | // Write Reg Address |
mapellil | 4:00493226e59f | 127 | _dev_spi->lock(); |
mapellil | 4:00493226e59f | 128 | _cs_pin = 0; |
mapellil | 4:00493226e59f | 129 | if (_spi_type == SPI4W) { |
mapellil | 4:00493226e59f | 130 | _dev_spi->write(RegisterAddr | 0x80); |
mapellil | 4:00493226e59f | 131 | for (int i=0; i<NumByteToRead; i++) { |
mapellil | 4:00493226e59f | 132 | *(pBuffer+i) = _dev_spi->write(0x00); |
mapellil | 4:00493226e59f | 133 | } |
mapellil | 4:00493226e59f | 134 | } else if (_spi_type == SPI3W){ |
docent | 7:fa19aa1a666d | 135 | // Write RD Reg Address with RD bit |
mapellil | 4:00493226e59f | 136 | uint8_t TxByte = RegisterAddr | 0x80; |
mapellil | 4:00493226e59f | 137 | _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); |
mapellil | 4:00493226e59f | 138 | } |
mapellil | 4:00493226e59f | 139 | _cs_pin = 1; |
mapellil | 4:00493226e59f | 140 | _dev_spi->unlock(); |
mapellil | 4:00493226e59f | 141 | return MAGNETO_OK; |
docent | 7:fa19aa1a666d | 142 | } |
docent | 7:fa19aa1a666d | 143 | */ |
mapellil | 4:00493226e59f | 144 | if (!_dev_i2c) return MAGNETO_ERROR; |
mapellil | 4:00493226e59f | 145 | int ret = _dev_i2c->i2c_read(pBuffer, |
mapellil | 4:00493226e59f | 146 | _address, |
nikapov | 0:7376fbeb1d4a | 147 | RegisterAddr, |
nikapov | 0:7376fbeb1d4a | 148 | NumByteToRead); |
nikapov | 0:7376fbeb1d4a | 149 | if(ret != 0) { |
nikapov | 0:7376fbeb1d4a | 150 | return MAGNETO_ERROR; |
nikapov | 0:7376fbeb1d4a | 151 | } |
nikapov | 0:7376fbeb1d4a | 152 | return MAGNETO_OK; |
nikapov | 0:7376fbeb1d4a | 153 | } |
nikapov | 0:7376fbeb1d4a | 154 | |
nikapov | 0:7376fbeb1d4a | 155 | /** |
nikapov | 0:7376fbeb1d4a | 156 | * @brief Utility function to write data to LIS3MDL |
nikapov | 0:7376fbeb1d4a | 157 | * @param[in] pBuffer pointer to the byte-array data to send |
nikapov | 0:7376fbeb1d4a | 158 | * @param[in] RegisterAddr specifies internal address register to read from. |
nikapov | 0:7376fbeb1d4a | 159 | * @param[in] NumByteToWrite number of bytes to write. |
nikapov | 0:7376fbeb1d4a | 160 | * @retval MAGNETO_OK if ok, |
nikapov | 0:7376fbeb1d4a | 161 | * @retval MAGNETO_ERROR if an I2C error has occured |
nikapov | 0:7376fbeb1d4a | 162 | */ |
nikapov | 0:7376fbeb1d4a | 163 | MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer, |
nikapov | 0:7376fbeb1d4a | 164 | uint8_t RegisterAddr, uint16_t NumByteToWrite) |
nikapov | 0:7376fbeb1d4a | 165 | { |
docent | 7:fa19aa1a666d | 166 | /* |
mapellil | 4:00493226e59f | 167 | if (_dev_spi) { |
mapellil | 4:00493226e59f | 168 | _dev_spi->lock(); |
mapellil | 4:00493226e59f | 169 | _cs_pin = 0; |
mapellil | 4:00493226e59f | 170 | int data = _dev_spi->write(RegisterAddr); |
mapellil | 4:00493226e59f | 171 | _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); |
mapellil | 4:00493226e59f | 172 | _cs_pin = 1; |
mapellil | 4:00493226e59f | 173 | _dev_spi->unlock(); |
mapellil | 4:00493226e59f | 174 | return MAGNETO_OK; |
mapellil | 4:00493226e59f | 175 | } |
docent | 7:fa19aa1a666d | 176 | */ |
mapellil | 4:00493226e59f | 177 | if (!_dev_i2c) return MAGNETO_ERROR; |
mapellil | 4:00493226e59f | 178 | int ret = _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); |
nikapov | 0:7376fbeb1d4a | 179 | if(ret != 0) { |
nikapov | 0:7376fbeb1d4a | 180 | return MAGNETO_ERROR; |
nikapov | 0:7376fbeb1d4a | 181 | } |
nikapov | 0:7376fbeb1d4a | 182 | return MAGNETO_OK; |
nikapov | 0:7376fbeb1d4a | 183 | } |
nikapov | 0:7376fbeb1d4a | 184 | |
nikapov | 0:7376fbeb1d4a | 185 | /*** Instance Variables ***/ |
nikapov | 0:7376fbeb1d4a | 186 | /* IO Device */ |
mapellil | 4:00493226e59f | 187 | DevI2C *_dev_i2c; |
docent | 7:fa19aa1a666d | 188 | //my SPI *_dev_spi; |
mapellil | 4:00493226e59f | 189 | uint8_t _address; |
mapellil | 4:00493226e59f | 190 | DigitalOut _cs_pin; |
mapellil | 4:00493226e59f | 191 | InterruptIn _int_pin; |
docent | 7:fa19aa1a666d | 192 | //my SPI_type_t _spi_type; |
nikapov | 0:7376fbeb1d4a | 193 | }; |
nikapov | 0:7376fbeb1d4a | 194 | |
nikapov | 0:7376fbeb1d4a | 195 | #endif // __LIS3MDL_CLASS_H |