Si7210 driver
Dependents: TBSense2_Sensor_Demo
Si7210.h@0:9bce98da584b, 2017-05-04 (annotated)
- Committer:
- stevew817
- Date:
- Thu May 04 12:15:44 2017 +0000
- Revision:
- 0:9bce98da584b
Initial WIP;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stevew817 | 0:9bce98da584b | 1 | /***************************************************************************//** |
stevew817 | 0:9bce98da584b | 2 | * @file Si7210.h |
stevew817 | 0:9bce98da584b | 3 | * @brief Driver class for the Silicon Labs Si7210 I2C Hall-effect sensor |
stevew817 | 0:9bce98da584b | 4 | ******************************************************************************* |
stevew817 | 0:9bce98da584b | 5 | * @section License |
stevew817 | 0:9bce98da584b | 6 | * <b>(C) Copyright 2017 Silicon Labs, http://www.silabs.com</b> |
stevew817 | 0:9bce98da584b | 7 | ******************************************************************************* |
stevew817 | 0:9bce98da584b | 8 | * |
stevew817 | 0:9bce98da584b | 9 | * Permission is granted to anyone to use this software for any purpose, |
stevew817 | 0:9bce98da584b | 10 | * including commercial applications, and to alter it and redistribute it |
stevew817 | 0:9bce98da584b | 11 | * freely, subject to the following restrictions: |
stevew817 | 0:9bce98da584b | 12 | * |
stevew817 | 0:9bce98da584b | 13 | * 1. The origin of this software must not be misrepresented; you must not |
stevew817 | 0:9bce98da584b | 14 | * claim that you wrote the original software. |
stevew817 | 0:9bce98da584b | 15 | * 2. Altered source versions must be plainly marked as such, and must not be |
stevew817 | 0:9bce98da584b | 16 | * misrepresented as being the original software. |
stevew817 | 0:9bce98da584b | 17 | * 3. This notice may not be removed or altered from any source distribution. |
stevew817 | 0:9bce98da584b | 18 | * |
stevew817 | 0:9bce98da584b | 19 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
stevew817 | 0:9bce98da584b | 20 | * obligation to support this Software. Silicon Labs is providing the |
stevew817 | 0:9bce98da584b | 21 | * Software "AS IS", with no express or implied warranties of any kind, |
stevew817 | 0:9bce98da584b | 22 | * including, but not limited to, any implied warranties of merchantability |
stevew817 | 0:9bce98da584b | 23 | * or fitness for any particular purpose or warranties against infringement |
stevew817 | 0:9bce98da584b | 24 | * of any proprietary rights of a third party. |
stevew817 | 0:9bce98da584b | 25 | * |
stevew817 | 0:9bce98da584b | 26 | * Silicon Labs will not be liable for any consequential, incidental, or |
stevew817 | 0:9bce98da584b | 27 | * special damages, or any other relief, or for any claim by any third party, |
stevew817 | 0:9bce98da584b | 28 | * arising from your use of this Software. |
stevew817 | 0:9bce98da584b | 29 | * |
stevew817 | 0:9bce98da584b | 30 | ******************************************************************************/ |
stevew817 | 0:9bce98da584b | 31 | #ifndef SI7210_H |
stevew817 | 0:9bce98da584b | 32 | #define SI7210_H |
stevew817 | 0:9bce98da584b | 33 | |
stevew817 | 0:9bce98da584b | 34 | #include "mbed.h" |
stevew817 | 0:9bce98da584b | 35 | |
stevew817 | 0:9bce98da584b | 36 | /* Possible I2C slave addresses */ |
stevew817 | 0:9bce98da584b | 37 | #define SI7210_ADDRESS_0 (0x30U << 1) |
stevew817 | 0:9bce98da584b | 38 | #define SI7210_ADDRESS_1 (0x31U << 1) |
stevew817 | 0:9bce98da584b | 39 | #define SI7210_ADDRESS_2 (0x32U << 1) |
stevew817 | 0:9bce98da584b | 40 | #define SI7210_ADDRESS_3 (0x33U << 1) |
stevew817 | 0:9bce98da584b | 41 | |
stevew817 | 0:9bce98da584b | 42 | /* Register addresses */ |
stevew817 | 0:9bce98da584b | 43 | #define SI72XX_HREVID 0xC0U |
stevew817 | 0:9bce98da584b | 44 | #define SI72XX_DSPSIGM 0xC1U |
stevew817 | 0:9bce98da584b | 45 | #define SI72XX_DSPSIGL 0xC2U |
stevew817 | 0:9bce98da584b | 46 | #define SI72XX_DSPSIGSEL 0xC3U |
stevew817 | 0:9bce98da584b | 47 | #define SI72XX_POWER_CTRL 0xC4U |
stevew817 | 0:9bce98da584b | 48 | #define SI72XX_ARAUTOINC 0xC5U |
stevew817 | 0:9bce98da584b | 49 | #define SI72XX_CTRL1 0xC6U |
stevew817 | 0:9bce98da584b | 50 | #define SI72XX_CTRL2 0xC7U |
stevew817 | 0:9bce98da584b | 51 | #define SI72XX_SLTIME 0xC8U |
stevew817 | 0:9bce98da584b | 52 | #define SI72XX_CTRL3 0xC9U |
stevew817 | 0:9bce98da584b | 53 | #define SI72XX_A0 0xCAU |
stevew817 | 0:9bce98da584b | 54 | #define SI72XX_A1 0xCBU |
stevew817 | 0:9bce98da584b | 55 | #define SI72XX_A2 0xCCU |
stevew817 | 0:9bce98da584b | 56 | #define SI72XX_CTRL4 0xCDU |
stevew817 | 0:9bce98da584b | 57 | #define SI72XX_A3 0xCEU |
stevew817 | 0:9bce98da584b | 58 | #define SI72XX_A4 0xCFU |
stevew817 | 0:9bce98da584b | 59 | #define SI72XX_A5 0xD0U |
stevew817 | 0:9bce98da584b | 60 | #define SI72XX_OTP_ADDR 0xE1U |
stevew817 | 0:9bce98da584b | 61 | #define SI72XX_OTP_DATA 0xE2U |
stevew817 | 0:9bce98da584b | 62 | #define SI72XX_OTP_CTRL 0xE3U |
stevew817 | 0:9bce98da584b | 63 | #define SI72XX_TM_FG 0xE4U |
stevew817 | 0:9bce98da584b | 64 | |
stevew817 | 0:9bce98da584b | 65 | #define SI72XX_ERROR_BUSY 0xFEU |
stevew817 | 0:9bce98da584b | 66 | #define SI72XX_ERROR_NODATA 0xFDU |
stevew817 | 0:9bce98da584b | 67 | |
stevew817 | 0:9bce98da584b | 68 | /* Possible (bipolar) measurement range settings */ |
stevew817 | 0:9bce98da584b | 69 | typedef enum { |
stevew817 | 0:9bce98da584b | 70 | RANGE_20mT, |
stevew817 | 0:9bce98da584b | 71 | RANGE_200mT |
stevew817 | 0:9bce98da584b | 72 | } Si7210_range_t; |
stevew817 | 0:9bce98da584b | 73 | |
stevew817 | 0:9bce98da584b | 74 | namespace silabs { |
stevew817 | 0:9bce98da584b | 75 | class Si7210 |
stevew817 | 0:9bce98da584b | 76 | { |
stevew817 | 0:9bce98da584b | 77 | public: |
stevew817 | 0:9bce98da584b | 78 | Si7210(PinName sda, PinName scl, uint8_t address); |
stevew817 | 0:9bce98da584b | 79 | Si7210(I2C *i2c_bus, uint8_t address = SI7210_ADDRESS_0); |
stevew817 | 0:9bce98da584b | 80 | ~Si7210(); |
stevew817 | 0:9bce98da584b | 81 | |
stevew817 | 0:9bce98da584b | 82 | /* |
stevew817 | 0:9bce98da584b | 83 | * Get last measured temperature data |
stevew817 | 0:9bce98da584b | 84 | * return: int = temperature in 1/4th degrees centigrade |
stevew817 | 0:9bce98da584b | 85 | */ |
stevew817 | 0:9bce98da584b | 86 | int getTemperature(); |
stevew817 | 0:9bce98da584b | 87 | |
stevew817 | 0:9bce98da584b | 88 | /* |
stevew817 | 0:9bce98da584b | 89 | * Get last measured field strength |
stevew817 | 0:9bce98da584b | 90 | * return: int = field strength in micro-Tesla. |
stevew817 | 0:9bce98da584b | 91 | */ |
stevew817 | 0:9bce98da584b | 92 | int getFieldStrength(); |
stevew817 | 0:9bce98da584b | 93 | |
stevew817 | 0:9bce98da584b | 94 | /* |
stevew817 | 0:9bce98da584b | 95 | * Set measurement range. |
stevew817 | 0:9bce98da584b | 96 | * Return true if successful, false if device is not responding. |
stevew817 | 0:9bce98da584b | 97 | */ |
stevew817 | 0:9bce98da584b | 98 | bool setFieldStrength(Si7210_range_t range); |
stevew817 | 0:9bce98da584b | 99 | |
stevew817 | 0:9bce98da584b | 100 | /* |
stevew817 | 0:9bce98da584b | 101 | * Return true if successful, false if device is not responding. |
stevew817 | 0:9bce98da584b | 102 | */ |
stevew817 | 0:9bce98da584b | 103 | bool fetchFieldStrength(); |
stevew817 | 0:9bce98da584b | 104 | |
stevew817 | 0:9bce98da584b | 105 | bool measureOnce(); |
stevew817 | 0:9bce98da584b | 106 | |
stevew817 | 0:9bce98da584b | 107 | /* |
stevew817 | 0:9bce98da584b | 108 | * Check if the sensor is active and responding. |
stevew817 | 0:9bce98da584b | 109 | */ |
stevew817 | 0:9bce98da584b | 110 | bool check(); |
stevew817 | 0:9bce98da584b | 111 | |
stevew817 | 0:9bce98da584b | 112 | /* Read a register from Si7210 */ |
stevew817 | 0:9bce98da584b | 113 | bool readRegister(uint8_t reg, uint8_t *result); |
stevew817 | 0:9bce98da584b | 114 | bool writeRegister(uint8_t reg, uint8_t data); |
stevew817 | 0:9bce98da584b | 115 | |
stevew817 | 0:9bce98da584b | 116 | bool wakeup(); |
stevew817 | 0:9bce98da584b | 117 | bool sleep(); |
stevew817 | 0:9bce98da584b | 118 | |
stevew817 | 0:9bce98da584b | 119 | private: |
stevew817 | 0:9bce98da584b | 120 | |
stevew817 | 0:9bce98da584b | 121 | I2C *_i2c; |
stevew817 | 0:9bce98da584b | 122 | bool _ownI2C; |
stevew817 | 0:9bce98da584b | 123 | |
stevew817 | 0:9bce98da584b | 124 | uint8_t _address; |
stevew817 | 0:9bce98da584b | 125 | |
stevew817 | 0:9bce98da584b | 126 | int8_t _temperatureOffset; |
stevew817 | 0:9bce98da584b | 127 | int8_t _temperatureGain; |
stevew817 | 0:9bce98da584b | 128 | |
stevew817 | 0:9bce98da584b | 129 | int _rawTemperature; |
stevew817 | 0:9bce98da584b | 130 | int _rawField; |
stevew817 | 0:9bce98da584b | 131 | |
stevew817 | 0:9bce98da584b | 132 | Si7210_range_t _range; |
stevew817 | 0:9bce98da584b | 133 | |
stevew817 | 0:9bce98da584b | 134 | }; // class Si7210 |
stevew817 | 0:9bce98da584b | 135 | |
stevew817 | 0:9bce98da584b | 136 | } // namespace silabs |
stevew817 | 0:9bce98da584b | 137 | |
stevew817 | 0:9bce98da584b | 138 | #endif |