for my students set_m_axes_offset
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
lis3mdl_class.h@0:7376fbeb1d4a, 2017-09-05 (annotated)
- Committer:
- nikapov
- Date:
- Tue Sep 05 10:15:14 2017 +0000
- Revision:
- 0:7376fbeb1d4a
- Child:
- 2:b3212ad3c306
First version.
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" |
nikapov | 0:7376fbeb1d4a | 46 | |
nikapov | 0:7376fbeb1d4a | 47 | /* Classes -------------------------------------------------------------------*/ |
nikapov | 0:7376fbeb1d4a | 48 | /** Class representing a LIS3MDL sensor component |
nikapov | 0:7376fbeb1d4a | 49 | */ |
nikapov | 0:7376fbeb1d4a | 50 | class LIS3MDL : public MagneticSensor { |
nikapov | 0:7376fbeb1d4a | 51 | public: |
nikapov | 0:7376fbeb1d4a | 52 | /** Constructor |
nikapov | 0:7376fbeb1d4a | 53 | * @param[in] i2c device I2C to be used for communication |
nikapov | 0:7376fbeb1d4a | 54 | */ |
nikapov | 0:7376fbeb1d4a | 55 | LIS3MDL(DevI2C &i2c) : MagneticSensor(), dev_i2c(i2c) { |
nikapov | 0:7376fbeb1d4a | 56 | } |
nikapov | 0:7376fbeb1d4a | 57 | |
nikapov | 0:7376fbeb1d4a | 58 | /** Destructor |
nikapov | 0:7376fbeb1d4a | 59 | */ |
nikapov | 0:7376fbeb1d4a | 60 | virtual ~LIS3MDL() {} |
nikapov | 0:7376fbeb1d4a | 61 | |
nikapov | 0:7376fbeb1d4a | 62 | /*** Interface Methods ***/ |
nikapov | 0:7376fbeb1d4a | 63 | virtual int init(void *init_struct) { |
nikapov | 0:7376fbeb1d4a | 64 | return LIS3MDL_Init((MAGNETO_InitTypeDef*)init_struct); |
nikapov | 0:7376fbeb1d4a | 65 | } |
nikapov | 0:7376fbeb1d4a | 66 | |
nikapov | 0:7376fbeb1d4a | 67 | virtual int read_id(uint8_t *m_id) { |
nikapov | 0:7376fbeb1d4a | 68 | return LIS3MDL_Read_M_ID(m_id); |
nikapov | 0:7376fbeb1d4a | 69 | } |
nikapov | 0:7376fbeb1d4a | 70 | |
nikapov | 0:7376fbeb1d4a | 71 | virtual int get_m_axes(int32_t *pData) { |
nikapov | 0:7376fbeb1d4a | 72 | return LIS3MDL_M_GetAxes(pData); |
nikapov | 0:7376fbeb1d4a | 73 | } |
nikapov | 0:7376fbeb1d4a | 74 | |
nikapov | 0:7376fbeb1d4a | 75 | virtual int get_m_axes_raw(int16_t *pData) { |
nikapov | 0:7376fbeb1d4a | 76 | return LIS3MDL_M_GetAxesRaw(pData); |
nikapov | 0:7376fbeb1d4a | 77 | } |
nikapov | 0:7376fbeb1d4a | 78 | |
nikapov | 0:7376fbeb1d4a | 79 | protected: |
nikapov | 0:7376fbeb1d4a | 80 | /*** Methods ***/ |
nikapov | 0:7376fbeb1d4a | 81 | MAGNETO_StatusTypeDef LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init); |
nikapov | 0:7376fbeb1d4a | 82 | MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id); |
nikapov | 0:7376fbeb1d4a | 83 | MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData); |
nikapov | 0:7376fbeb1d4a | 84 | MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData); |
nikapov | 0:7376fbeb1d4a | 85 | |
nikapov | 0:7376fbeb1d4a | 86 | /** |
nikapov | 0:7376fbeb1d4a | 87 | * @brief Configures LIS3MDL interrupt lines for NUCLEO boards |
nikapov | 0:7376fbeb1d4a | 88 | */ |
nikapov | 0:7376fbeb1d4a | 89 | void LIS3MDL_IO_ITConfig(void) |
nikapov | 0:7376fbeb1d4a | 90 | { |
nikapov | 0:7376fbeb1d4a | 91 | /* To be implemented */ |
nikapov | 0:7376fbeb1d4a | 92 | } |
nikapov | 0:7376fbeb1d4a | 93 | |
nikapov | 0:7376fbeb1d4a | 94 | /** |
nikapov | 0:7376fbeb1d4a | 95 | * @brief Configures LIS3MDL I2C interface |
nikapov | 0:7376fbeb1d4a | 96 | * @return MAGNETO_OK in case of success, an error code otherwise |
nikapov | 0:7376fbeb1d4a | 97 | */ |
nikapov | 0:7376fbeb1d4a | 98 | MAGNETO_StatusTypeDef LIS3MDL_IO_Init(void) |
nikapov | 0:7376fbeb1d4a | 99 | { |
nikapov | 0:7376fbeb1d4a | 100 | return MAGNETO_OK; /* done in constructor */ |
nikapov | 0:7376fbeb1d4a | 101 | } |
nikapov | 0:7376fbeb1d4a | 102 | |
nikapov | 0:7376fbeb1d4a | 103 | /** |
nikapov | 0:7376fbeb1d4a | 104 | * @brief Utility function to read data from LIS3MDL |
nikapov | 0:7376fbeb1d4a | 105 | * @param[out] pBuffer pointer to the byte-array to read data in to |
nikapov | 0:7376fbeb1d4a | 106 | * @param[in] RegisterAddr specifies internal address register to read from. |
nikapov | 0:7376fbeb1d4a | 107 | * @param[in] NumByteToRead number of bytes to be read. |
nikapov | 0:7376fbeb1d4a | 108 | * @retval MAGNETO_OK if ok, |
nikapov | 0:7376fbeb1d4a | 109 | * @retval MAGNETO_ERROR if an I2C error has occured |
nikapov | 0:7376fbeb1d4a | 110 | */ |
nikapov | 0:7376fbeb1d4a | 111 | MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer, |
nikapov | 0:7376fbeb1d4a | 112 | uint8_t RegisterAddr, uint16_t NumByteToRead) |
nikapov | 0:7376fbeb1d4a | 113 | { |
nikapov | 0:7376fbeb1d4a | 114 | int ret = dev_i2c.i2c_read(pBuffer, |
nikapov | 0:7376fbeb1d4a | 115 | LIS3MDL_M_MEMS_ADDRESS, |
nikapov | 0:7376fbeb1d4a | 116 | RegisterAddr, |
nikapov | 0:7376fbeb1d4a | 117 | NumByteToRead); |
nikapov | 0:7376fbeb1d4a | 118 | if(ret != 0) { |
nikapov | 0:7376fbeb1d4a | 119 | return MAGNETO_ERROR; |
nikapov | 0:7376fbeb1d4a | 120 | } |
nikapov | 0:7376fbeb1d4a | 121 | return MAGNETO_OK; |
nikapov | 0:7376fbeb1d4a | 122 | } |
nikapov | 0:7376fbeb1d4a | 123 | |
nikapov | 0:7376fbeb1d4a | 124 | /** |
nikapov | 0:7376fbeb1d4a | 125 | * @brief Utility function to write data to LIS3MDL |
nikapov | 0:7376fbeb1d4a | 126 | * @param[in] pBuffer pointer to the byte-array data to send |
nikapov | 0:7376fbeb1d4a | 127 | * @param[in] RegisterAddr specifies internal address register to read from. |
nikapov | 0:7376fbeb1d4a | 128 | * @param[in] NumByteToWrite number of bytes to write. |
nikapov | 0:7376fbeb1d4a | 129 | * @retval MAGNETO_OK if ok, |
nikapov | 0:7376fbeb1d4a | 130 | * @retval MAGNETO_ERROR if an I2C error has occured |
nikapov | 0:7376fbeb1d4a | 131 | */ |
nikapov | 0:7376fbeb1d4a | 132 | MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer, |
nikapov | 0:7376fbeb1d4a | 133 | uint8_t RegisterAddr, uint16_t NumByteToWrite) |
nikapov | 0:7376fbeb1d4a | 134 | { |
nikapov | 0:7376fbeb1d4a | 135 | int ret = dev_i2c.i2c_write(pBuffer, |
nikapov | 0:7376fbeb1d4a | 136 | LIS3MDL_M_MEMS_ADDRESS, |
nikapov | 0:7376fbeb1d4a | 137 | RegisterAddr, |
nikapov | 0:7376fbeb1d4a | 138 | NumByteToWrite); |
nikapov | 0:7376fbeb1d4a | 139 | if(ret != 0) { |
nikapov | 0:7376fbeb1d4a | 140 | return MAGNETO_ERROR; |
nikapov | 0:7376fbeb1d4a | 141 | } |
nikapov | 0:7376fbeb1d4a | 142 | return MAGNETO_OK; |
nikapov | 0:7376fbeb1d4a | 143 | } |
nikapov | 0:7376fbeb1d4a | 144 | |
nikapov | 0:7376fbeb1d4a | 145 | /*** Instance Variables ***/ |
nikapov | 0:7376fbeb1d4a | 146 | /* IO Device */ |
nikapov | 0:7376fbeb1d4a | 147 | DevI2C &dev_i2c; |
nikapov | 0:7376fbeb1d4a | 148 | }; |
nikapov | 0:7376fbeb1d4a | 149 | |
nikapov | 0:7376fbeb1d4a | 150 | #endif // __LIS3MDL_CLASS_H |