|
| 1 | +/* |
| 2 | + * This file is part of INAV Project. |
| 3 | + * |
| 4 | + * INAV Project is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * INAV Project is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <stdbool.h> |
| 19 | +#include <stdint.h> |
| 20 | + |
| 21 | +#include "platform.h" |
| 22 | + |
| 23 | +#ifdef USE_MAG_LIS2MDL |
| 24 | + |
| 25 | +#include "common/axis.h" |
| 26 | + |
| 27 | +#include "drivers/time.h" |
| 28 | +#include "drivers/sensor.h" |
| 29 | + |
| 30 | +#include "drivers/compass/compass.h" |
| 31 | +#include "drivers/compass/compass_lis2mdl.h" |
| 32 | + |
| 33 | +// LIS2MDL, IIS2MDC, LSM303AGR and LSM303AH are firmware and pin-to-pin compatible solutions. |
| 34 | + |
| 35 | +#define LIS2MDL_DEVICE_ID 0x40 |
| 36 | + |
| 37 | +#define LIS2MDL_REG_WHO_AM_I 0x4F |
| 38 | +#define LIS2MDL_REG_CFG_REG_A 0x60 |
| 39 | +#define LIS2MDL_REG_CFG_REG_B 0x61 |
| 40 | +#define LIS2MDL_REG_CFG_REG_C 0x62 |
| 41 | +#define LIS2MDL_REG_STATUS_REG 0x67 |
| 42 | +#define LIS2MDL_REG_OUTX_L 0x68 |
| 43 | + |
| 44 | +// CFG_REG_A |
| 45 | +#define LIS2MDL_MD_CONTINUOUS 0x00 |
| 46 | +#define LIS2MDL_ODR_100HZ 0x0C |
| 47 | +#define LIS2MDL_COMP_TEMP_EN 0x80 |
| 48 | + |
| 49 | +// CFG_REG_B |
| 50 | +#define LIS2MDL_OFF_CANC 0x02 |
| 51 | + |
| 52 | +// CFG_REG_C |
| 53 | +#define LIS2MDL_BDU 0x10 |
| 54 | + |
| 55 | +// STATUS_REG |
| 56 | +#define LIS2MDL_STATUS_ZYXDA 0x08 |
| 57 | + |
| 58 | +static bool lis2mdlInit(magDev_t *mag) |
| 59 | +{ |
| 60 | + bool ack = true; |
| 61 | + |
| 62 | + ack = ack && busWrite(mag->busDev, LIS2MDL_REG_CFG_REG_A, LIS2MDL_MD_CONTINUOUS | LIS2MDL_ODR_100HZ | LIS2MDL_COMP_TEMP_EN); |
| 63 | + ack = ack && busWrite(mag->busDev, LIS2MDL_REG_CFG_REG_B, LIS2MDL_OFF_CANC); |
| 64 | + ack = ack && busWrite(mag->busDev, LIS2MDL_REG_CFG_REG_C, LIS2MDL_BDU); |
| 65 | + |
| 66 | + return ack; |
| 67 | +} |
| 68 | + |
| 69 | +static bool lis2mdlRead(magDev_t *mag) |
| 70 | +{ |
| 71 | + uint8_t status = 0; |
| 72 | + uint8_t buf[6]; |
| 73 | + |
| 74 | + mag->magADCRaw[X] = 0; |
| 75 | + mag->magADCRaw[Y] = 0; |
| 76 | + mag->magADCRaw[Z] = 0; |
| 77 | + |
| 78 | + bool ack = busRead(mag->busDev, LIS2MDL_REG_STATUS_REG, &status); |
| 79 | + if (!ack || !(status & LIS2MDL_STATUS_ZYXDA)) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + ack = busReadBuf(mag->busDev, LIS2MDL_REG_OUTX_L, buf, 6); |
| 84 | + if (!ack) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + const int16_t x = (int16_t)(buf[1] << 8 | buf[0]); |
| 89 | + const int16_t y = (int16_t)(buf[3] << 8 | buf[2]); |
| 90 | + const int16_t z = (int16_t)(buf[5] << 8 | buf[4]); |
| 91 | + |
| 92 | + // Adapt LIS2MDL left-handed frame to common sensor axis orientation, matching Betaflight. |
| 93 | + mag->magADCRaw[X] = -x; |
| 94 | + mag->magADCRaw[Y] = y; |
| 95 | + mag->magADCRaw[Z] = z; |
| 96 | + |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +#define DETECTION_MAX_RETRY_COUNT 5 |
| 101 | +static bool deviceDetect(magDev_t *mag) |
| 102 | +{ |
| 103 | + for (int retryCount = 0; retryCount < DETECTION_MAX_RETRY_COUNT; retryCount++) { |
| 104 | + delay(10); |
| 105 | + |
| 106 | + uint8_t sig = 0; |
| 107 | + const bool ack = busRead(mag->busDev, LIS2MDL_REG_WHO_AM_I, &sig); |
| 108 | + |
| 109 | + if (ack && sig == LIS2MDL_DEVICE_ID) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return false; |
| 115 | +} |
| 116 | + |
| 117 | +bool lis2mdlDetect(magDev_t *mag) |
| 118 | +{ |
| 119 | + mag->busDev = busDeviceInit(BUSTYPE_I2C, DEVHW_LIS2MDL, mag->magSensorToUse, OWNER_COMPASS); |
| 120 | + if (mag->busDev == NULL) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + if (!deviceDetect(mag)) { |
| 125 | + busDeviceDeInit(mag->busDev); |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + mag->init = lis2mdlInit; |
| 130 | + mag->read = lis2mdlRead; |
| 131 | + |
| 132 | + return true; |
| 133 | +} |
| 134 | + |
| 135 | +#endif |
0 commit comments