A mbed-os v5 driver for BM1422AGMV (3 axis magnetic sensor, made by Rohm).
Dependents: rohm-SensorShield-example mbed_blinky
BM1422AGMV.h@0:203311da9813, 2019-02-18 (annotated)
- Committer:
- Ren Boting
- Date:
- Mon Feb 18 13:00:55 2019 +0900
- Revision:
- 0:203311da9813
Init commit for mbed os v5 driver, Rohm BM1422AGMV sensor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ren Boting | 0:203311da9813 | 1 | /* Copyright (c) 2017 Renesas Electronics., MIT License |
Ren Boting | 0:203311da9813 | 2 | * |
Ren Boting | 0:203311da9813 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Ren Boting | 0:203311da9813 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Ren Boting | 0:203311da9813 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Ren Boting | 0:203311da9813 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Ren Boting | 0:203311da9813 | 7 | * Software is furnished to do so, subject to the following conditions: |
Ren Boting | 0:203311da9813 | 8 | * |
Ren Boting | 0:203311da9813 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Ren Boting | 0:203311da9813 | 10 | * substantial portions of the Software. |
Ren Boting | 0:203311da9813 | 11 | * |
Ren Boting | 0:203311da9813 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Ren Boting | 0:203311da9813 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Ren Boting | 0:203311da9813 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Ren Boting | 0:203311da9813 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Ren Boting | 0:203311da9813 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Ren Boting | 0:203311da9813 | 17 | */ |
Ren Boting | 0:203311da9813 | 18 | |
Ren Boting | 0:203311da9813 | 19 | #ifndef _BM1422AGMV_H_ |
Ren Boting | 0:203311da9813 | 20 | #define _BM1422AGMV_H_ |
Ren Boting | 0:203311da9813 | 21 | |
Ren Boting | 0:203311da9813 | 22 | #include "mbed.h" |
Ren Boting | 0:203311da9813 | 23 | |
Ren Boting | 0:203311da9813 | 24 | #define BM1422AGMV_DEVICE_ADDRESS_0E (0x0E << 1) |
Ren Boting | 0:203311da9813 | 25 | #define BM1422AGMV_DEVICE_ADDRESS_0F (0x0F << 1) |
Ren Boting | 0:203311da9813 | 26 | #define BM1422AGMV_WIA_VAL (0x41) |
Ren Boting | 0:203311da9813 | 27 | |
Ren Boting | 0:203311da9813 | 28 | #define BM1422AGMV_WIA (0x0F) |
Ren Boting | 0:203311da9813 | 29 | #define BM1422AGMV_DATAX (0x10) |
Ren Boting | 0:203311da9813 | 30 | #define BM1422AGMV_STA1 (0x18) |
Ren Boting | 0:203311da9813 | 31 | #define BM1422AGMV_CNTL1 (0x1B) |
Ren Boting | 0:203311da9813 | 32 | #define BM1422AGMV_CNTL2 (0x1C) |
Ren Boting | 0:203311da9813 | 33 | #define BM1422AGMV_CNTL3 (0x1D) |
Ren Boting | 0:203311da9813 | 34 | #define BM1422AGMV_AVE_A (0x40) |
Ren Boting | 0:203311da9813 | 35 | #define BM1422AGMV_CNTL4 (0x5C) |
Ren Boting | 0:203311da9813 | 36 | |
Ren Boting | 0:203311da9813 | 37 | #define BM1422AGMV_STA1_RD_DRDY (1 << 6) |
Ren Boting | 0:203311da9813 | 38 | |
Ren Boting | 0:203311da9813 | 39 | #define BM1422AGMV_CNTL1_FS1 (1 << 1) |
Ren Boting | 0:203311da9813 | 40 | #define BM1422AGMV_CNTL1_ODR_10Hz (0 << 3) |
Ren Boting | 0:203311da9813 | 41 | #define BM1422AGMV_CNTL1_RST_LV (1 << 5) |
Ren Boting | 0:203311da9813 | 42 | #define BM1422AGMV_CNTL1_OUT_BIT (1 << 6) |
Ren Boting | 0:203311da9813 | 43 | #define BM1422AGMV_CNTL1_PC1 (1 << 7) |
Ren Boting | 0:203311da9813 | 44 | |
Ren Boting | 0:203311da9813 | 45 | #define BM1422AGMV_CNTL2_DRP (1 << 2) |
Ren Boting | 0:203311da9813 | 46 | #define BM1422AGMV_CNTL2_DREN (1 << 3) |
Ren Boting | 0:203311da9813 | 47 | |
Ren Boting | 0:203311da9813 | 48 | #define BM1422AGMV_CNTL3_FORCE (1 << 6) |
Ren Boting | 0:203311da9813 | 49 | |
Ren Boting | 0:203311da9813 | 50 | #define BM1422AGMV_AVE_A_AVE4 (0 << 2) |
Ren Boting | 0:203311da9813 | 51 | |
Ren Boting | 0:203311da9813 | 52 | #define BM1422AGMV_CNTL1_VAL (BM1422AGMV_CNTL1_FS1 | BM1422AGMV_CNTL1_OUT_BIT | BM1422AGMV_CNTL1_PC1) |
Ren Boting | 0:203311da9813 | 53 | #define BM1422AGMV_CNTL2_VAL (BM1422AGMV_CNTL2_DREN) |
Ren Boting | 0:203311da9813 | 54 | #define BM1422AGMV_CNTL3_VAL (BM1422AGMV_CNTL3_FORCE) |
Ren Boting | 0:203311da9813 | 55 | #define BM1422AGMV_CNTL4_VAL (0x0000) |
Ren Boting | 0:203311da9813 | 56 | #define BM1422AGMV_AVE_A_VAL (BM1422AGMV_AVE_A_AVE4) |
Ren Boting | 0:203311da9813 | 57 | |
Ren Boting | 0:203311da9813 | 58 | #define BM1422AGMV_14BIT_SENS (24) |
Ren Boting | 0:203311da9813 | 59 | #define BM1422AGMV_12BIT_SENS (6) |
Ren Boting | 0:203311da9813 | 60 | |
Ren Boting | 0:203311da9813 | 61 | //#define _DEBUG |
Ren Boting | 0:203311da9813 | 62 | #ifdef _DEBUG |
Ren Boting | 0:203311da9813 | 63 | #undef DEBUG_PRINT |
Ren Boting | 0:203311da9813 | 64 | extern Serial pc; |
Ren Boting | 0:203311da9813 | 65 | #define DEBUG_PRINT(...) pc.printf(__VA_ARGS__) |
Ren Boting | 0:203311da9813 | 66 | #else |
Ren Boting | 0:203311da9813 | 67 | #define DEBUG_PRINT(...) |
Ren Boting | 0:203311da9813 | 68 | #endif |
Ren Boting | 0:203311da9813 | 69 | |
Ren Boting | 0:203311da9813 | 70 | class BM1422AGMV |
Ren Boting | 0:203311da9813 | 71 | { |
Ren Boting | 0:203311da9813 | 72 | public: |
Ren Boting | 0:203311da9813 | 73 | /** |
Ren Boting | 0:203311da9813 | 74 | * BM1422 constructor |
Ren Boting | 0:203311da9813 | 75 | * |
Ren Boting | 0:203311da9813 | 76 | * @param sda SDA pin |
Ren Boting | 0:203311da9813 | 77 | * @param sdl SCL pin |
Ren Boting | 0:203311da9813 | 78 | * @param addr slave address of the I2C peripheral (default: 0x1C) |
Ren Boting | 0:203311da9813 | 79 | */ |
Ren Boting | 0:203311da9813 | 80 | BM1422AGMV(PinName sda, PinName scl, int addr = BM1422AGMV_DEVICE_ADDRESS_0E); |
Ren Boting | 0:203311da9813 | 81 | /** |
Ren Boting | 0:203311da9813 | 82 | * Create a BM1422 instance which is connected to specified I2C pins |
Ren Boting | 0:203311da9813 | 83 | * with specified address |
Ren Boting | 0:203311da9813 | 84 | * |
Ren Boting | 0:203311da9813 | 85 | * @param i2c_obj I2C object (instance) |
Ren Boting | 0:203311da9813 | 86 | * @param addr slave address of the I2C-bus peripheral (default: 0x1C) |
Ren Boting | 0:203311da9813 | 87 | */ |
Ren Boting | 0:203311da9813 | 88 | BM1422AGMV(I2C &i2c_obj, int addr = BM1422AGMV_DEVICE_ADDRESS_0E); |
Ren Boting | 0:203311da9813 | 89 | /** |
Ren Boting | 0:203311da9813 | 90 | * BM1422 destructor |
Ren Boting | 0:203311da9813 | 91 | */ |
Ren Boting | 0:203311da9813 | 92 | ~BM1422AGMV(); |
Ren Boting | 0:203311da9813 | 93 | /** Initializa BM1422 sensor |
Ren Boting | 0:203311da9813 | 94 | * |
Ren Boting | 0:203311da9813 | 95 | * Configure sensor setting |
Ren Boting | 0:203311da9813 | 96 | * |
Ren Boting | 0:203311da9813 | 97 | */ |
Ren Boting | 0:203311da9813 | 98 | void initialize(void); |
Ren Boting | 0:203311da9813 | 99 | /** |
Ren Boting | 0:203311da9813 | 100 | * Get XYZ axis acceleration |
Ren Boting | 0:203311da9813 | 101 | * |
Ren Boting | 0:203311da9813 | 102 | * @returns XYZ (filled in float[3]) axis acceleration |
Ren Boting | 0:203311da9813 | 103 | */ |
Ren Boting | 0:203311da9813 | 104 | void get_val(float *data); |
Ren Boting | 0:203311da9813 | 105 | |
Ren Boting | 0:203311da9813 | 106 | private: |
Ren Boting | 0:203311da9813 | 107 | I2C m_i2c; |
Ren Boting | 0:203311da9813 | 108 | int m_addr; |
Ren Boting | 0:203311da9813 | 109 | void readRegs(int addr, uint8_t * data, int len); |
Ren Boting | 0:203311da9813 | 110 | void writeRegs(uint8_t * data, int len); |
Ren Boting | 0:203311da9813 | 111 | uint8_t _sens; |
Ren Boting | 0:203311da9813 | 112 | /** |
Ren Boting | 0:203311da9813 | 113 | * Get XYZ axis acceleration |
Ren Boting | 0:203311da9813 | 114 | * |
Ren Boting | 0:203311da9813 | 115 | * @returns XYZ (rawdata) axis acceleration |
Ren Boting | 0:203311da9813 | 116 | */ |
Ren Boting | 0:203311da9813 | 117 | void get_rawval(unsigned char *data); |
Ren Boting | 0:203311da9813 | 118 | /** |
Ren Boting | 0:203311da9813 | 119 | * Convert XYZ axis acceleration |
Ren Boting | 0:203311da9813 | 120 | * |
Ren Boting | 0:203311da9813 | 121 | * @returns XYZ (float array [3]) axis acceleration |
Ren Boting | 0:203311da9813 | 122 | */ |
Ren Boting | 0:203311da9813 | 123 | void convert_uT(int16_t *rawdata, float *data); |
Ren Boting | 0:203311da9813 | 124 | }; |
Ren Boting | 0:203311da9813 | 125 | |
Ren Boting | 0:203311da9813 | 126 | #endif |