iNEMO inertial module: 3D accelerometer and 3D gyroscope.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LSM6DSOSensor.h@0:6d69e896ce38, 2019-03-05 (annotated)
- Committer:
- cparata
- Date:
- Tue Mar 05 16:26:47 2019 +0000
- Revision:
- 0:6d69e896ce38
- Child:
- 1:888ac5f8d970
First version of the LSM6DSO library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cparata | 0:6d69e896ce38 | 1 | /** |
cparata | 0:6d69e896ce38 | 2 | ****************************************************************************** |
cparata | 0:6d69e896ce38 | 3 | * @file LSM6DSOSensor.h |
cparata | 0:6d69e896ce38 | 4 | * @author SRA |
cparata | 0:6d69e896ce38 | 5 | * @version V1.0.0 |
cparata | 0:6d69e896ce38 | 6 | * @date February 2019 |
cparata | 0:6d69e896ce38 | 7 | * @brief Abstract Class of an LSM6DSO Inertial Measurement Unit (IMU) 6 axes |
cparata | 0:6d69e896ce38 | 8 | * sensor. |
cparata | 0:6d69e896ce38 | 9 | ****************************************************************************** |
cparata | 0:6d69e896ce38 | 10 | * @attention |
cparata | 0:6d69e896ce38 | 11 | * |
cparata | 0:6d69e896ce38 | 12 | * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> |
cparata | 0:6d69e896ce38 | 13 | * |
cparata | 0:6d69e896ce38 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
cparata | 0:6d69e896ce38 | 15 | * are permitted provided that the following conditions are met: |
cparata | 0:6d69e896ce38 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
cparata | 0:6d69e896ce38 | 17 | * this list of conditions and the following disclaimer. |
cparata | 0:6d69e896ce38 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
cparata | 0:6d69e896ce38 | 19 | * this list of conditions and the following disclaimer in the documentation |
cparata | 0:6d69e896ce38 | 20 | * and/or other materials provided with the distribution. |
cparata | 0:6d69e896ce38 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
cparata | 0:6d69e896ce38 | 22 | * may be used to endorse or promote products derived from this software |
cparata | 0:6d69e896ce38 | 23 | * without specific prior written permission. |
cparata | 0:6d69e896ce38 | 24 | * |
cparata | 0:6d69e896ce38 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
cparata | 0:6d69e896ce38 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
cparata | 0:6d69e896ce38 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cparata | 0:6d69e896ce38 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
cparata | 0:6d69e896ce38 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
cparata | 0:6d69e896ce38 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
cparata | 0:6d69e896ce38 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
cparata | 0:6d69e896ce38 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
cparata | 0:6d69e896ce38 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
cparata | 0:6d69e896ce38 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cparata | 0:6d69e896ce38 | 35 | * |
cparata | 0:6d69e896ce38 | 36 | ****************************************************************************** |
cparata | 0:6d69e896ce38 | 37 | */ |
cparata | 0:6d69e896ce38 | 38 | |
cparata | 0:6d69e896ce38 | 39 | |
cparata | 0:6d69e896ce38 | 40 | /* Prevent recursive inclusion -----------------------------------------------*/ |
cparata | 0:6d69e896ce38 | 41 | |
cparata | 0:6d69e896ce38 | 42 | #ifndef __LSM6DSOSensor_H__ |
cparata | 0:6d69e896ce38 | 43 | #define __LSM6DSOSensor_H__ |
cparata | 0:6d69e896ce38 | 44 | |
cparata | 0:6d69e896ce38 | 45 | |
cparata | 0:6d69e896ce38 | 46 | /* Includes ------------------------------------------------------------------*/ |
cparata | 0:6d69e896ce38 | 47 | #include "DevI2C.h" |
cparata | 0:6d69e896ce38 | 48 | #include "lsm6dso_reg.h" |
cparata | 0:6d69e896ce38 | 49 | #include "MotionSensor.h" |
cparata | 0:6d69e896ce38 | 50 | #include "GyroSensor.h" |
cparata | 0:6d69e896ce38 | 51 | #include <assert.h> |
cparata | 0:6d69e896ce38 | 52 | |
cparata | 0:6d69e896ce38 | 53 | /* Defines -------------------------------------------------------------------*/ |
cparata | 0:6d69e896ce38 | 54 | |
cparata | 0:6d69e896ce38 | 55 | #define LSM6DSO_ACC_SENSITIVITY_FS_2G 0.061f |
cparata | 0:6d69e896ce38 | 56 | #define LSM6DSO_ACC_SENSITIVITY_FS_4G 0.122f |
cparata | 0:6d69e896ce38 | 57 | #define LSM6DSO_ACC_SENSITIVITY_FS_8G 0.244f |
cparata | 0:6d69e896ce38 | 58 | #define LSM6DSO_ACC_SENSITIVITY_FS_16G 0.488f |
cparata | 0:6d69e896ce38 | 59 | |
cparata | 0:6d69e896ce38 | 60 | #define LSM6DSO_GYRO_SENSITIVITY_FS_125DPS 4.375f |
cparata | 0:6d69e896ce38 | 61 | #define LSM6DSO_GYRO_SENSITIVITY_FS_250DPS 8.750f |
cparata | 0:6d69e896ce38 | 62 | #define LSM6DSO_GYRO_SENSITIVITY_FS_500DPS 17.500f |
cparata | 0:6d69e896ce38 | 63 | #define LSM6DSO_GYRO_SENSITIVITY_FS_1000DPS 35.000f |
cparata | 0:6d69e896ce38 | 64 | #define LSM6DSO_GYRO_SENSITIVITY_FS_2000DPS 70.000f |
cparata | 0:6d69e896ce38 | 65 | |
cparata | 0:6d69e896ce38 | 66 | |
cparata | 0:6d69e896ce38 | 67 | /* Typedefs ------------------------------------------------------------------*/ |
cparata | 0:6d69e896ce38 | 68 | |
cparata | 0:6d69e896ce38 | 69 | typedef enum |
cparata | 0:6d69e896ce38 | 70 | { |
cparata | 0:6d69e896ce38 | 71 | LSM6DSO_INT1_PIN, |
cparata | 0:6d69e896ce38 | 72 | LSM6DSO_INT2_PIN, |
cparata | 0:6d69e896ce38 | 73 | } LSM6DSO_Interrupt_Pin_t; |
cparata | 0:6d69e896ce38 | 74 | |
cparata | 0:6d69e896ce38 | 75 | typedef struct |
cparata | 0:6d69e896ce38 | 76 | { |
cparata | 0:6d69e896ce38 | 77 | unsigned int FreeFallStatus : 1; |
cparata | 0:6d69e896ce38 | 78 | unsigned int TapStatus : 1; |
cparata | 0:6d69e896ce38 | 79 | unsigned int DoubleTapStatus : 1; |
cparata | 0:6d69e896ce38 | 80 | unsigned int WakeUpStatus : 1; |
cparata | 0:6d69e896ce38 | 81 | unsigned int StepStatus : 1; |
cparata | 0:6d69e896ce38 | 82 | unsigned int TiltStatus : 1; |
cparata | 0:6d69e896ce38 | 83 | unsigned int D6DOrientationStatus : 1; |
cparata | 0:6d69e896ce38 | 84 | unsigned int SleepStatus : 1; |
cparata | 0:6d69e896ce38 | 85 | } LSM6DSO_Event_Status_t; |
cparata | 0:6d69e896ce38 | 86 | |
cparata | 0:6d69e896ce38 | 87 | |
cparata | 0:6d69e896ce38 | 88 | /* Class Declaration ---------------------------------------------------------*/ |
cparata | 0:6d69e896ce38 | 89 | |
cparata | 0:6d69e896ce38 | 90 | /** |
cparata | 0:6d69e896ce38 | 91 | * Abstract class of an LSM6DSO Inertial Measurement Unit (IMU) 6 axes |
cparata | 0:6d69e896ce38 | 92 | * sensor. |
cparata | 0:6d69e896ce38 | 93 | */ |
cparata | 0:6d69e896ce38 | 94 | class LSM6DSOSensor : public MotionSensor, public GyroSensor |
cparata | 0:6d69e896ce38 | 95 | { |
cparata | 0:6d69e896ce38 | 96 | public: |
cparata | 0:6d69e896ce38 | 97 | enum SPI_type_t {SPI3W, SPI4W}; |
cparata | 0:6d69e896ce38 | 98 | LSM6DSOSensor(SPI *spi, PinName cs_pin, PinName int1_pin=NC, PinName int2_pin=NC, SPI_type_t spi_type=SPI4W); |
cparata | 0:6d69e896ce38 | 99 | LSM6DSOSensor(DevI2C *i2c, uint8_t address=LSM6DSO_I2C_ADD_H, PinName int1_pin=NC, PinName int2_pin=NC); |
cparata | 0:6d69e896ce38 | 100 | virtual int init(void *init); |
cparata | 0:6d69e896ce38 | 101 | virtual int read_id(uint8_t *id); |
cparata | 0:6d69e896ce38 | 102 | virtual int get_x_axes(int32_t *acceleration); |
cparata | 0:6d69e896ce38 | 103 | virtual int get_g_axes(int32_t *angular_rate); |
cparata | 0:6d69e896ce38 | 104 | virtual int get_x_sensitivity(float *sensitivity); |
cparata | 0:6d69e896ce38 | 105 | virtual int get_g_sensitivity(float *sensitivity); |
cparata | 0:6d69e896ce38 | 106 | virtual int get_x_axes_raw(int16_t *value); |
cparata | 0:6d69e896ce38 | 107 | virtual int get_g_axes_raw(int16_t *value); |
cparata | 0:6d69e896ce38 | 108 | virtual int get_x_odr(float *odr); |
cparata | 0:6d69e896ce38 | 109 | virtual int get_g_odr(float *odr); |
cparata | 0:6d69e896ce38 | 110 | virtual int set_x_odr(float odr); |
cparata | 0:6d69e896ce38 | 111 | virtual int set_g_odr(float odr); |
cparata | 0:6d69e896ce38 | 112 | virtual int get_x_fs(float *full_scale); |
cparata | 0:6d69e896ce38 | 113 | virtual int get_g_fs(float *full_scale); |
cparata | 0:6d69e896ce38 | 114 | virtual int set_x_fs(float full_scale); |
cparata | 0:6d69e896ce38 | 115 | virtual int set_g_fs(float full_scale); |
cparata | 0:6d69e896ce38 | 116 | int enable_x(void); |
cparata | 0:6d69e896ce38 | 117 | int enable_g(void); |
cparata | 0:6d69e896ce38 | 118 | int disable_x(void); |
cparata | 0:6d69e896ce38 | 119 | int disable_g(void); |
cparata | 0:6d69e896ce38 | 120 | int enable_free_fall_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); |
cparata | 0:6d69e896ce38 | 121 | int disable_free_fall_detection(void); |
cparata | 0:6d69e896ce38 | 122 | int set_free_fall_threshold(uint8_t thr); |
cparata | 0:6d69e896ce38 | 123 | int set_free_fall_duration(uint8_t dur); |
cparata | 0:6d69e896ce38 | 124 | int enable_pedometer(void); |
cparata | 0:6d69e896ce38 | 125 | int disable_pedometer(void); |
cparata | 0:6d69e896ce38 | 126 | int get_step_counter(uint16_t *step_count); |
cparata | 0:6d69e896ce38 | 127 | int reset_step_counter(void); |
cparata | 0:6d69e896ce38 | 128 | int enable_tilt_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); |
cparata | 0:6d69e896ce38 | 129 | int disable_tilt_detection(void); |
cparata | 0:6d69e896ce38 | 130 | int enable_wake_up_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT2_PIN); |
cparata | 0:6d69e896ce38 | 131 | int disable_wake_up_detection(void); |
cparata | 0:6d69e896ce38 | 132 | int set_wake_up_threshold(uint8_t thr); |
cparata | 0:6d69e896ce38 | 133 | int set_wake_up_duration(uint8_t dur); |
cparata | 0:6d69e896ce38 | 134 | int enable_single_tap_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); |
cparata | 0:6d69e896ce38 | 135 | int disable_single_tap_detection(void); |
cparata | 0:6d69e896ce38 | 136 | int enable_double_tap_detection(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); |
cparata | 0:6d69e896ce38 | 137 | int disable_double_tap_detection(void); |
cparata | 0:6d69e896ce38 | 138 | int set_tap_threshold(uint8_t thr); |
cparata | 0:6d69e896ce38 | 139 | int set_tap_shock_time(uint8_t time); |
cparata | 0:6d69e896ce38 | 140 | int set_tap_quiet_time(uint8_t time); |
cparata | 0:6d69e896ce38 | 141 | int set_tap_duration_time(uint8_t time); |
cparata | 0:6d69e896ce38 | 142 | int enable_6d_orientation(LSM6DSO_Interrupt_Pin_t pin = LSM6DSO_INT1_PIN); |
cparata | 0:6d69e896ce38 | 143 | int disable_6d_orientation(void); |
cparata | 0:6d69e896ce38 | 144 | int set_6d_orientation_threshold(uint8_t thr); |
cparata | 0:6d69e896ce38 | 145 | int get_6d_orientation_xl(uint8_t *xl); |
cparata | 0:6d69e896ce38 | 146 | int get_6d_orientation_xh(uint8_t *xh); |
cparata | 0:6d69e896ce38 | 147 | int get_6d_orientation_yl(uint8_t *yl); |
cparata | 0:6d69e896ce38 | 148 | int get_6d_orientation_yh(uint8_t *yh); |
cparata | 0:6d69e896ce38 | 149 | int get_6d_orientation_zl(uint8_t *zl); |
cparata | 0:6d69e896ce38 | 150 | int get_6d_orientation_zh(uint8_t *zh); |
cparata | 0:6d69e896ce38 | 151 | int get_event_status(LSM6DSO_Event_Status_t *status); |
cparata | 0:6d69e896ce38 | 152 | int read_reg(uint8_t reg, uint8_t *data); |
cparata | 0:6d69e896ce38 | 153 | int write_reg(uint8_t reg, uint8_t data); |
cparata | 0:6d69e896ce38 | 154 | int set_interrupt_latch(uint8_t status); |
cparata | 0:6d69e896ce38 | 155 | int get_x_drdy_status(uint8_t *status); |
cparata | 0:6d69e896ce38 | 156 | int set_x_self_test(uint8_t status); |
cparata | 0:6d69e896ce38 | 157 | int get_g_drdy_status(uint8_t *status); |
cparata | 0:6d69e896ce38 | 158 | int set_g_self_test(uint8_t status); |
cparata | 0:6d69e896ce38 | 159 | int get_fifo_num_samples(uint16_t *num_samples); |
cparata | 0:6d69e896ce38 | 160 | int get_fifo_full_status(uint8_t *status); |
cparata | 0:6d69e896ce38 | 161 | int set_fifo_int1_fifo_full(uint8_t status); |
cparata | 0:6d69e896ce38 | 162 | int set_fifo_watermark_level(uint16_t watermark); |
cparata | 0:6d69e896ce38 | 163 | int set_fifo_stop_on_fth(uint8_t status); |
cparata | 0:6d69e896ce38 | 164 | int set_fifo_mode(uint8_t mode); |
cparata | 0:6d69e896ce38 | 165 | int get_fifo_tag(uint8_t *tag); |
cparata | 0:6d69e896ce38 | 166 | int get_fifo_data(uint8_t *data); |
cparata | 0:6d69e896ce38 | 167 | int get_fifo_x_axes(int32_t *acceleration); |
cparata | 0:6d69e896ce38 | 168 | int set_fifo_x_bdr(float bdr); |
cparata | 0:6d69e896ce38 | 169 | int get_fifo_g_axes(int32_t *angular_velocity); |
cparata | 0:6d69e896ce38 | 170 | int set_fifo_g_bdr(float bdr); |
cparata | 0:6d69e896ce38 | 171 | |
cparata | 0:6d69e896ce38 | 172 | /** |
cparata | 0:6d69e896ce38 | 173 | * @brief Attaching an interrupt handler to the INT1 interrupt. |
cparata | 0:6d69e896ce38 | 174 | * @param fptr An interrupt handler. |
cparata | 0:6d69e896ce38 | 175 | * @retval None. |
cparata | 0:6d69e896ce38 | 176 | */ |
cparata | 0:6d69e896ce38 | 177 | void attach_int1_irq(void (*fptr)(void)) |
cparata | 0:6d69e896ce38 | 178 | { |
cparata | 0:6d69e896ce38 | 179 | _int1_irq.rise(fptr); |
cparata | 0:6d69e896ce38 | 180 | } |
cparata | 0:6d69e896ce38 | 181 | |
cparata | 0:6d69e896ce38 | 182 | /** |
cparata | 0:6d69e896ce38 | 183 | * @brief Enabling the INT1 interrupt handling. |
cparata | 0:6d69e896ce38 | 184 | * @param None. |
cparata | 0:6d69e896ce38 | 185 | * @retval None. |
cparata | 0:6d69e896ce38 | 186 | */ |
cparata | 0:6d69e896ce38 | 187 | void enable_int1_irq(void) |
cparata | 0:6d69e896ce38 | 188 | { |
cparata | 0:6d69e896ce38 | 189 | _int1_irq.enable_irq(); |
cparata | 0:6d69e896ce38 | 190 | } |
cparata | 0:6d69e896ce38 | 191 | |
cparata | 0:6d69e896ce38 | 192 | /** |
cparata | 0:6d69e896ce38 | 193 | * @brief Disabling the INT1 interrupt handling. |
cparata | 0:6d69e896ce38 | 194 | * @param None. |
cparata | 0:6d69e896ce38 | 195 | * @retval None. |
cparata | 0:6d69e896ce38 | 196 | */ |
cparata | 0:6d69e896ce38 | 197 | void disable_int1_irq(void) |
cparata | 0:6d69e896ce38 | 198 | { |
cparata | 0:6d69e896ce38 | 199 | _int1_irq.disable_irq(); |
cparata | 0:6d69e896ce38 | 200 | } |
cparata | 0:6d69e896ce38 | 201 | |
cparata | 0:6d69e896ce38 | 202 | /** |
cparata | 0:6d69e896ce38 | 203 | * @brief Attaching an interrupt handler to the INT2 interrupt. |
cparata | 0:6d69e896ce38 | 204 | * @param fptr An interrupt handler. |
cparata | 0:6d69e896ce38 | 205 | * @retval None. |
cparata | 0:6d69e896ce38 | 206 | */ |
cparata | 0:6d69e896ce38 | 207 | void attach_int2_irq(void (*fptr)(void)) |
cparata | 0:6d69e896ce38 | 208 | { |
cparata | 0:6d69e896ce38 | 209 | _int2_irq.rise(fptr); |
cparata | 0:6d69e896ce38 | 210 | } |
cparata | 0:6d69e896ce38 | 211 | |
cparata | 0:6d69e896ce38 | 212 | /** |
cparata | 0:6d69e896ce38 | 213 | * @brief Enabling the INT2 interrupt handling. |
cparata | 0:6d69e896ce38 | 214 | * @param None. |
cparata | 0:6d69e896ce38 | 215 | * @retval None. |
cparata | 0:6d69e896ce38 | 216 | */ |
cparata | 0:6d69e896ce38 | 217 | void enable_int2_irq(void) |
cparata | 0:6d69e896ce38 | 218 | { |
cparata | 0:6d69e896ce38 | 219 | _int2_irq.enable_irq(); |
cparata | 0:6d69e896ce38 | 220 | } |
cparata | 0:6d69e896ce38 | 221 | |
cparata | 0:6d69e896ce38 | 222 | /** |
cparata | 0:6d69e896ce38 | 223 | * @brief Disabling the INT2 interrupt handling. |
cparata | 0:6d69e896ce38 | 224 | * @param None. |
cparata | 0:6d69e896ce38 | 225 | * @retval None. |
cparata | 0:6d69e896ce38 | 226 | */ |
cparata | 0:6d69e896ce38 | 227 | void disable_int2_irq(void) |
cparata | 0:6d69e896ce38 | 228 | { |
cparata | 0:6d69e896ce38 | 229 | _int2_irq.disable_irq(); |
cparata | 0:6d69e896ce38 | 230 | } |
cparata | 0:6d69e896ce38 | 231 | |
cparata | 0:6d69e896ce38 | 232 | /** |
cparata | 0:6d69e896ce38 | 233 | * @brief Utility function to read data. |
cparata | 0:6d69e896ce38 | 234 | * @param pBuffer: pointer to data to be read. |
cparata | 0:6d69e896ce38 | 235 | * @param RegisterAddr: specifies internal address register to be read. |
cparata | 0:6d69e896ce38 | 236 | * @param NumByteToRead: number of bytes to be read. |
cparata | 0:6d69e896ce38 | 237 | * @retval 0 if ok, an error code otherwise. |
cparata | 0:6d69e896ce38 | 238 | */ |
cparata | 0:6d69e896ce38 | 239 | uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) |
cparata | 0:6d69e896ce38 | 240 | { |
cparata | 0:6d69e896ce38 | 241 | if (_dev_spi) { |
cparata | 0:6d69e896ce38 | 242 | /* Write Reg Address */ |
cparata | 0:6d69e896ce38 | 243 | _dev_spi->lock(); |
cparata | 0:6d69e896ce38 | 244 | _cs_pin = 0; |
cparata | 0:6d69e896ce38 | 245 | if (_spi_type == SPI4W) { |
cparata | 0:6d69e896ce38 | 246 | _dev_spi->write(RegisterAddr | 0x80); |
cparata | 0:6d69e896ce38 | 247 | for (int i=0; i<NumByteToRead; i++) { |
cparata | 0:6d69e896ce38 | 248 | *(pBuffer+i) = _dev_spi->write(0x00); |
cparata | 0:6d69e896ce38 | 249 | } |
cparata | 0:6d69e896ce38 | 250 | } else if (_spi_type == SPI3W){ |
cparata | 0:6d69e896ce38 | 251 | /* Write RD Reg Address with RD bit*/ |
cparata | 0:6d69e896ce38 | 252 | uint8_t TxByte = RegisterAddr | 0x80; |
cparata | 0:6d69e896ce38 | 253 | _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); |
cparata | 0:6d69e896ce38 | 254 | } |
cparata | 0:6d69e896ce38 | 255 | _cs_pin = 1; |
cparata | 0:6d69e896ce38 | 256 | _dev_spi->unlock(); |
cparata | 0:6d69e896ce38 | 257 | return 0; |
cparata | 0:6d69e896ce38 | 258 | } |
cparata | 0:6d69e896ce38 | 259 | if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); |
cparata | 0:6d69e896ce38 | 260 | return 1; |
cparata | 0:6d69e896ce38 | 261 | } |
cparata | 0:6d69e896ce38 | 262 | |
cparata | 0:6d69e896ce38 | 263 | /** |
cparata | 0:6d69e896ce38 | 264 | * @brief Utility function to write data. |
cparata | 0:6d69e896ce38 | 265 | * @param pBuffer: pointer to data to be written. |
cparata | 0:6d69e896ce38 | 266 | * @param RegisterAddr: specifies internal address register to be written. |
cparata | 0:6d69e896ce38 | 267 | * @param NumByteToWrite: number of bytes to write. |
cparata | 0:6d69e896ce38 | 268 | * @retval 0 if ok, an error code otherwise. |
cparata | 0:6d69e896ce38 | 269 | */ |
cparata | 0:6d69e896ce38 | 270 | uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) |
cparata | 0:6d69e896ce38 | 271 | { |
cparata | 0:6d69e896ce38 | 272 | if (_dev_spi) { |
cparata | 0:6d69e896ce38 | 273 | _dev_spi->lock(); |
cparata | 0:6d69e896ce38 | 274 | _cs_pin = 0; |
cparata | 0:6d69e896ce38 | 275 | _dev_spi->write(RegisterAddr); |
cparata | 0:6d69e896ce38 | 276 | _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); |
cparata | 0:6d69e896ce38 | 277 | _cs_pin = 1; |
cparata | 0:6d69e896ce38 | 278 | _dev_spi->unlock(); |
cparata | 0:6d69e896ce38 | 279 | return 0; |
cparata | 0:6d69e896ce38 | 280 | } |
cparata | 0:6d69e896ce38 | 281 | if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); |
cparata | 0:6d69e896ce38 | 282 | return 1; |
cparata | 0:6d69e896ce38 | 283 | } |
cparata | 0:6d69e896ce38 | 284 | |
cparata | 0:6d69e896ce38 | 285 | private: |
cparata | 0:6d69e896ce38 | 286 | int set_x_odr_when_enabled(float odr); |
cparata | 0:6d69e896ce38 | 287 | int set_g_odr_when_enabled(float odr); |
cparata | 0:6d69e896ce38 | 288 | int set_x_odr_when_disabled(float odr); |
cparata | 0:6d69e896ce38 | 289 | int set_g_odr_when_disabled(float odr); |
cparata | 0:6d69e896ce38 | 290 | |
cparata | 0:6d69e896ce38 | 291 | /* Helper classes. */ |
cparata | 0:6d69e896ce38 | 292 | DevI2C *_dev_i2c; |
cparata | 0:6d69e896ce38 | 293 | SPI *_dev_spi; |
cparata | 0:6d69e896ce38 | 294 | |
cparata | 0:6d69e896ce38 | 295 | /* Configuration */ |
cparata | 0:6d69e896ce38 | 296 | uint8_t _address; |
cparata | 0:6d69e896ce38 | 297 | DigitalOut _cs_pin; |
cparata | 0:6d69e896ce38 | 298 | InterruptIn _int1_irq; |
cparata | 0:6d69e896ce38 | 299 | InterruptIn _int2_irq; |
cparata | 0:6d69e896ce38 | 300 | SPI_type_t _spi_type; |
cparata | 0:6d69e896ce38 | 301 | |
cparata | 0:6d69e896ce38 | 302 | uint8_t _x_is_enabled; |
cparata | 0:6d69e896ce38 | 303 | lsm6dso_odr_xl_t _x_last_odr; |
cparata | 0:6d69e896ce38 | 304 | uint8_t _g_is_enabled; |
cparata | 0:6d69e896ce38 | 305 | lsm6dso_odr_g_t _g_last_odr; |
cparata | 0:6d69e896ce38 | 306 | |
cparata | 0:6d69e896ce38 | 307 | lsm6dso_ctx_t _reg_ctx; |
cparata | 0:6d69e896ce38 | 308 | }; |
cparata | 0:6d69e896ce38 | 309 | |
cparata | 0:6d69e896ce38 | 310 | #ifdef __cplusplus |
cparata | 0:6d69e896ce38 | 311 | extern "C" { |
cparata | 0:6d69e896ce38 | 312 | #endif |
cparata | 0:6d69e896ce38 | 313 | int32_t LSM6DSO_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ); |
cparata | 0:6d69e896ce38 | 314 | int32_t LSM6DSO_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ); |
cparata | 0:6d69e896ce38 | 315 | #ifdef __cplusplus |
cparata | 0:6d69e896ce38 | 316 | } |
cparata | 0:6d69e896ce38 | 317 | #endif |
cparata | 0:6d69e896ce38 | 318 | |
cparata | 0:6d69e896ce38 | 319 | #endif |