|
| 1 | +/*========================================================================================== |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2023-2025 https://madflight.com |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +===========================================================================================*/ |
| 24 | + |
| 25 | +#include "RM3100.h" |
| 26 | +#include <Arduino.h> //delay() |
| 27 | + |
| 28 | +#define RM3100_BASE_ADDRESS 0x20 // address with Pin 2 and Pin 4 set to LOW |
| 29 | + |
| 30 | +// register addresses |
| 31 | +#define RM3100_REG_POLL 0x00 // Polls for single measurement |
| 32 | +#define RM3100_REG_CMM 0x01 // Continous mode |
| 33 | +#define RM3100_REG_CCX 0x04 // Cycle count X MSB (3*2 bytes) |
| 34 | +#define RM3100_REG_TMRC 0x0B // Continous mode data rate |
| 35 | +#define RM3100_REG_MX 0x24 // Measurement results X MSB (3*3 bytes) |
| 36 | +#define RM3100_REG_BIST 0x33 // Build-In Self Test |
| 37 | +#define RM3100_REG_STATUS 0x34 // Status of DRDY |
| 38 | +#define RM3100_REG_HSHAKE 0x35 // Handshake |
| 39 | +#define RM3100_REG_REVID 0x36 // Revision |
| 40 | + |
| 41 | + |
| 42 | +uint8_t RM3100::probe(MF_I2C *i2c) { |
| 43 | + MF_I2CDevice dev(i2c, 0); |
| 44 | + for(int i = 0; i < 4; i++) { |
| 45 | + dev.adr = RM3100_BASE_ADDRESS + i; |
| 46 | + uint8_t revid = 0; |
| 47 | + int rv = dev.readReg(RM3100_REG_REVID, &revid, 1); //returns 1 when data was received |
| 48 | + //Serial.printf("RM3100.probe(0x%02X) = 0x%02X rv=%d\n", dev.adr, revid, rv); |
| 49 | + if(revid == 0x22) return dev.adr; |
| 50 | + } |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +RM3100::~RM3100() { |
| 56 | + delete dev; |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +RM3100::RM3100(MF_I2C *i2c, uint8_t i2c_adr, uint16_t cycle_count) { |
| 61 | + dev = new MF_I2CDevice(i2c, i2c_adr); |
| 62 | + |
| 63 | + //set cycle count |
| 64 | + uint8_t d[6]; |
| 65 | + for(int i=0;i<6;i+=2) { |
| 66 | + d[i]= cycle_count >> 8; |
| 67 | + d[i+1] = cycle_count; |
| 68 | + } |
| 69 | + dev->writeReg(RM3100_REG_CCX, d, 6); |
| 70 | + |
| 71 | + scale_uT = 1 / ((0.3671 * (float)cycle_count) + 1.5); //scale factor in uT/LSB |
| 72 | + //Note: should be 74LSB/uT at cc=200, but appears to be 10 times less with my sensor... |
| 73 | + |
| 74 | + // Set delay time between readings - this appears not to work as documented, value is read as 0x71 after power-up |
| 75 | + //dev->writeReg(RM3100_REG_TMRC, 0x92); //1.7 ms |
| 76 | + |
| 77 | + int tries = 10; |
| 78 | + while(tries) { |
| 79 | + // Continuous measurement |
| 80 | + dev->writeReg(RM3100_REG_CMM, 0x71); |
| 81 | + delay(10); |
| 82 | + |
| 83 | + // Needed to get cmm started.... |
| 84 | + if(dataReady()) break; |
| 85 | + tries--; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +bool RM3100::dataReady() { |
| 91 | + return ( (dev->readReg(RM3100_REG_STATUS) & 0x80) == 0x80 ); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +void RM3100::readRaw(int32_t *xyz) { |
| 96 | + uint8_t d[9]; |
| 97 | + dev->readReg(RM3100_REG_MX, d, 9); |
| 98 | + //Serial.printf("RM3100.readRaw() %02X%02X%02X %02X%02X%02X %02X%02X%02X\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8]); |
| 99 | + |
| 100 | + int j = 0; |
| 101 | + for(int i=0;i<3;i++) { |
| 102 | + xyz[i] = (d[j]<<16) | (d[j+1]<<8) | d[j+2]; |
| 103 | + if(xyz[i] & 0x00800000) xyz[i] |= 0xFF000000; //24bit sign expansion |
| 104 | + j+=3; |
| 105 | + } |
| 106 | + //Serial.printf("RM3100.readRaw() %d %d %d %f\n", (int)xyz[0], (int)xyz[1], (int)xyz[2], scale_uT); |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +void RM3100::read(float *x, float *y, float *z) { |
| 111 | + int32_t xyz[3]; |
| 112 | + readRaw(xyz); |
| 113 | + *x = xyz[0] * scale_uT; |
| 114 | + *y = xyz[1] * scale_uT; |
| 115 | + *z = xyz[2] * scale_uT; |
| 116 | +} |
0 commit comments