Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / accgyro / accgyro_spi_lsm6dso.c
blobe80ed1fb8ca56213a1611639f8ee5f1e26431093
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #ifdef USE_ACCGYRO_LSM6DSO
28 #include "drivers/accgyro/accgyro.h"
29 #include "drivers/accgyro/accgyro_spi_lsm6dso.h"
30 #include "drivers/bus_spi.h"
31 #include "drivers/exti.h"
32 #include "drivers/io.h"
33 #include "drivers/io_impl.h"
35 #if defined(USE_GYRO_EXTI) && defined(USE_MPU_DATA_READY_SIGNAL)
36 void lsm6dsoExtiHandler(extiCallbackRec_t *cb)
38 gyroDev_t *gyro = container_of(cb, gyroDev_t, exti);
39 gyro->dataReady = true;
41 #endif
43 bool lsm6dsoAccRead(accDev_t *acc)
45 enum {
46 IDX_ACCEL_XOUT_L,
47 IDX_ACCEL_XOUT_H,
48 IDX_ACCEL_YOUT_L,
49 IDX_ACCEL_YOUT_H,
50 IDX_ACCEL_ZOUT_L,
51 IDX_ACCEL_ZOUT_H,
52 BUFFER_SIZE,
55 uint8_t lsm6dso_rx_buf[BUFFER_SIZE];
57 extDevice_t *dev = &acc->gyro->dev;
58 busReadRegisterBuffer(dev, LSM6DSO_REG_OUTX_L_A, lsm6dso_rx_buf, BUFFER_SIZE);
60 acc->ADCRaw[X] = (int16_t)((lsm6dso_rx_buf[IDX_ACCEL_XOUT_H] << 8) | lsm6dso_rx_buf[IDX_ACCEL_XOUT_L]);
61 acc->ADCRaw[Y] = (int16_t)((lsm6dso_rx_buf[IDX_ACCEL_YOUT_H] << 8) | lsm6dso_rx_buf[IDX_ACCEL_YOUT_L]);
62 acc->ADCRaw[Z] = (int16_t)((lsm6dso_rx_buf[IDX_ACCEL_ZOUT_H] << 8) | lsm6dso_rx_buf[IDX_ACCEL_ZOUT_L]);
64 return true;
67 bool lsm6dsoGyroRead(gyroDev_t *gyro)
69 enum {
70 IDX_GYRO_XOUT_L,
71 IDX_GYRO_XOUT_H,
72 IDX_GYRO_YOUT_L,
73 IDX_GYRO_YOUT_H,
74 IDX_GYRO_ZOUT_L,
75 IDX_GYRO_ZOUT_H,
76 BUFFER_SIZE,
79 uint8_t lsm6dso_rx_buf[BUFFER_SIZE];
81 extDevice_t *dev = &gyro->dev;
82 busReadRegisterBuffer(dev, LSM6DSO_REG_OUTX_L_G, lsm6dso_rx_buf, BUFFER_SIZE);
84 gyro->gyroADCRaw[X] = (int16_t)((lsm6dso_rx_buf[IDX_GYRO_XOUT_H] << 8) | lsm6dso_rx_buf[IDX_GYRO_XOUT_L]);
85 gyro->gyroADCRaw[Y] = (int16_t)((lsm6dso_rx_buf[IDX_GYRO_YOUT_H] << 8) | lsm6dso_rx_buf[IDX_GYRO_YOUT_L]);
86 gyro->gyroADCRaw[Z] = (int16_t)((lsm6dso_rx_buf[IDX_GYRO_ZOUT_H] << 8) | lsm6dso_rx_buf[IDX_GYRO_ZOUT_L]);
88 return true;
90 #endif