Revision ed9a1bf5

View differences:

drivers/MPU6050/v1/alld_MPU6050.c
1 1
/*
2
 * sd_hal_mpu6050.c
3
 *
4
 *  Created on: Feb 19, 2016
5
 *      Author: Sina Darvishi
6
 *  Edited on: Jan 15, 2019
7
 *      Editor: Simon Welzel
8
 */
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  Thomas Schöpping et al.
9 4

  
10
/**
11
 * |----------------------------------------------------------------------
12
 * | Copyright (C) Sina Darvishi,2016
13
 * |
14
 * | This program is free software: you can redistribute it and/or modify
15
 * | it under the terms of the GNU General Public License as published by
16
 * | the Free Software Foundation, either version 3 of the License, or
17
 * | any later version.
18
 * |
19
 * | This program is distributed in the hope that it will be useful,
20
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * | GNU General Public License for more details.
23
 * |
24
 * | You should have received a copy of the GNU General Public License
25
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 * |----------------------------------------------------------------------
27
 */
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

  
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

  
15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
28 18

  
29 19
#include <alld_MPU6050.h>
30 20

  
21
#include <string.h>
22

  
31 23
/******************************************************************************/
32 24
/* LOCAL DEFINITIONS                                                          */
33 25
/******************************************************************************/
......
52 44
/* EXPORTED FUNCTIONS                                                         */
53 45
/******************************************************************************/
54 46

  
55
// from alld_ina219.c
56 47
/**
57 48
 * @brief Read the value of one or more of the registers.
58 49
 * @param[in]   i2cd        i2c driver
......
63 54
 * @param[in]   timeout     timeout
64 55
 * @return                  An indicator whether the call was successfull
65 56
 */
66
apalExitStatus_t mpu6050_lld_read_register(const MPU6050Driver* const mpu6050, const uint8_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
57
apalExitStatus_t mpu6050_lld_read_register(const MPU6050Driver* const mpu6050, const mpu6050_lld_register_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
67 58
{
68
  apalDbgAssert(mpu6050 != 0);
69
  apalDbgAssert(mpu6050->i2cd != 0);
70
  apalDbgAssert(data != 0);
59
  apalDbgAssert(mpu6050 != NULL);
60
  apalDbgAssert(mpu6050->i2cd != NULL);
61
  apalDbgAssert(data != NULL || num == 0);
71 62

  
72
  return apalI2CMasterTransmit(mpu6050->i2cd, (MPU6050_LLD_I2C_ADDR_FIXED | mpu6050->addr), &addr, 1, data, num, timeout);
63
  return apalI2CMasterTransmit(mpu6050->i2cd, (MPU6050_LLD_I2C_ADDR_FIXED | mpu6050->addr), (uint8_t*)&addr, 1, data, num, timeout);
73 64
}
74 65

  
75 66
/**
......
82 73
 * @param[in]   timeout     timeout
83 74
 * @return                  An indicator whether the call was successfull
84 75
 */
85
apalExitStatus_t mpu6050_lld_write_register(const MPU6050Driver* const mpu6050, const uint8_t addr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout)
76
apalExitStatus_t mpu6050_lld_write_register(const MPU6050Driver* const mpu6050, const mpu6050_lld_register_t addr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout)
86 77
{
87
  apalDbgAssert(mpu6050 != 0);
88
  apalDbgAssert(mpu6050->i2cd != 0);
89
  apalDbgAssert(data != 0);
78
  apalDbgAssert(mpu6050 != NULL);
79
  apalDbgAssert(mpu6050->i2cd != NULL);
80
  apalDbgAssert(data != NULL || num == 0);
90 81

  
91 82
  uint8_t buffer[1+num];
92 83
  buffer[0] = addr;
93
  for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) {
94
    buffer[dataIdx+1] = data[dataIdx];
95
  }
84
  memcpy(&buffer[1], data, num);
96 85
  return apalI2CMasterTransmit(mpu6050->i2cd, (MPU6050_LLD_I2C_ADDR_FIXED | mpu6050->addr), buffer, 1+num, NULL, 0, timeout);
97 86
}
drivers/MPU6050/v1/alld_MPU6050.h
34 34
/******************************************************************************/
35 35

  
36 36
/**
37
 * @brief Maximum I2C frequency.
37
 * @brief   Maximum I2C frequency.
38 38
 */
39
#define MPU6050_LLD_I2C_MAXFREQUENCY    400000
40

  
41
/* Default I2C address */
42
#define MPU6050_LLD_I2C_ADDR_FIXED      0x68
43
#define MPU6050_LLD_I2C_ADDR_AD0        0x01
44

  
45
/* MPU6050 registers */
46
#define MPU6050_LLD_AUX_VDDIO   0x01
47
#define MPU6050_LLD_SMPLRT_DIV   0x19
48
#define MPU6050_LLD_CONFIG    0x1A
49
#define MPU6050_LLD_GYRO_CONFIG   0x1B
50
#define MPU6050_LLD_ACCEL_CONFIG  0x1C
51
#define MPU6050_LLD_MOTION_THRESH  0x1F
52
#define MPU6050_LLD_INT_PIN_CFG   0x37
53
#define MPU6050_LLD_INT_ENABLE   0x38
54
#define MPU6050_LLD_INT_STATUS   0x3A
55
#define MPU6050_LLD_ACCEL_XOUT_H  0x3B
56
#define MPU6050_LLD_ACCEL_XOUT_L  0x3C
57
#define MPU6050_LLD_ACCEL_YOUT_H  0x3D
58
#define MPU6050_LLD_ACCEL_YOUT_L  0x3E
59
#define MPU6050_LLD_ACCEL_ZOUT_H  0x3F
60
#define MPU6050_LLD_ACCEL_ZOUT_L  0x40
61
#define MPU6050_LLD_TEMP_OUT_H   0x41
62
#define MPU6050_LLD_TEMP_OUT_L   0x42
63
#define MPU6050_LLD_GYRO_XOUT_H   0x43
64
#define MPU6050_LLD_GYRO_XOUT_L   0x44
65
#define MPU6050_LLD_GYRO_YOUT_H   0x45
66
#define MPU6050_LLD_GYRO_YOUT_L   0x46
67
#define MPU6050_LLD_GYRO_ZOUT_H   0x47
68
#define MPU6050_LLD_GYRO_ZOUT_L   0x48
69
#define MPU6050_LLD_MOT_DETECT_STATUS 0x61
70
#define MPU6050_LLD_SIGNAL_PATH_RESET 0x68
71
#define MPU6050_LLD_MOT_DETECT_CTRL  0x69
72
#define MPU6050_LLD_USER_CTRL   0x6A
73
#define MPU6050_LLD_PWR_MGMT_1   0x6B
74
#define MPU6050_LLD_PWR_MGMT_2   0x6C
75
#define MPU6050_LLD_FIFO_COUNTH   0x72
76
#define MPU6050_LLD_FIFO_COUNTL   0x73
77
#define MPU6050_LLD_FIFO_R_W   0x74
78
#define MPU6050_LLD_WHO_AM_I   0x75
79

  
80
#define MPU6050_LLD_DataRate_8KHz        0   /**< Sample rate set to 8 kHz */
81
#define MPU6050_LLD_DataRate_4KHz        1   /**< Sample rate set to 4 kHz */
82
#define MPU6050_LLD_DataRate_2KHz        3   /**< Sample rate set to 2 kHz */
83
#define MPU6050_LLD_DataRate_1KHz        7   /**< Sample rate set to 1 kHz */
84
#define MPU6050_LLD_DataRate_500Hz       15  /**< Sample rate set to 500 Hz */
85
#define MPU6050_LLD_DataRate_250Hz       31  /**< Sample rate set to 250 Hz */
86
#define MPU6050_LLD_DataRate_125Hz       63  /**< Sample rate set to 125 Hz */
87
#define MPU6050_LLD_DataRate_100Hz       79  /**< Sample rate set to 100 Hz */
88

  
89
/* Who I am register value */
90
#define MPU6050_I_AM    MPU6050_LLD_I2C_ADDR_FIXED
91

  
92
/* Gyro sensitivities in degrees/s */
93
#define MPU6050_LLD_GYRO_SENS_250  ((float) 131)
94
#define MPU6050_LLD_GYRO_SENS_500  ((float) 65.5)
95
#define MPU6050_LLD_GYRO_SENS_1000  ((float) 32.8)
96
#define MPU6050_LLD_GYRO_SENS_2000  ((float) 16.4)
97

  
98
/* Acce sensitivities in g/s */
99
#define MPU6050_LLD_ACCE_SENS_2   ((float) 16384)
100
#define MPU6050_LLD_ACCE_SENS_4   ((float) 8192)
101
#define MPU6050_LLD_ACCE_SENS_8   ((float) 4096)
102
#define MPU6050_LLD_ACCE_SENS_16  ((float) 2048)
39
#define MPU6050_LLD_I2C_MAXFREQUENCY            400000
40

  
41
/**
42
 * @brief   Default I2C address
43
 */
44
#define MPU6050_LLD_I2C_ADDR_FIXED              0x68
45

  
46
/**
47
 * @brief   Optional I2C address mask, if AD0 is high.
48
 */
49
#define MPU6050_LLD_I2C_ADDR_AD0                0x01
50

  
51
/**
52
 * @brief   Constant value of the WHO_AM_I register.
53
 */
54
#define MPU6050_LLD_WHO_AM_I                    0x68
103 55

  
104 56
/******************************************************************************/
105 57
/* SETTINGS                                                                   */
......
114 66
/******************************************************************************/
115 67

  
116 68
/**
117
 * @brief  MPU6050 can have 2 different slave addresses, depends on it's input AD0 pin
118
 *         This feature allows you to use 2 different sensors with this library at the same time
119
 */
120
typedef enum  {
121
	SD_MPU6050_Device_0 = 0x00, /*!< AD0 pin is set to low */
122
	SD_MPU6050_Device_1 = 0x02  /*!< AD0 pin is set to high */
123
} SD_MPU6050_Device;
124

  
125
/**
126
 * @brief  MPU6050 result enumeration
127
 */
128
typedef enum  {
129
	SD_MPU6050_Result_Ok = 0x00,          /*!< Everything OK */
130
	SD_MPU6050_Result_Error,              /*!< Unknown error */
131
	SD_MPU6050_Result_DeviceNotConnected, /*!< There is no device with valid slave address */
132
	SD_MPU6050_Result_DeviceInvalid       /*!< Connected device with address is not MPU6050 */
133
} SD_MPU6050_Result;
134

  
135
/**
136
 * @brief  Parameters for accelerometer range
137
 */
138
typedef enum  {
139
	SD_MPU6050_Accelerometer_2G = 0x00, /*!< Range is +- 2G */
140
	SD_MPU6050_Accelerometer_4G = 0x01, /*!< Range is +- 4G */
141
	SD_MPU6050_Accelerometer_8G = 0x02, /*!< Range is +- 8G */
142
	SD_MPU6050_Accelerometer_16G = 0x03 /*!< Range is +- 16G */
143
} SD_MPU6050_Accelerometer;
144

  
145
/**
146
 * @brief  Parameters for gyroscope range
69
 * @brief   Register map.
147 70
 */
148 71
typedef enum {
149
	SD_MPU6050_Gyroscope_250s = 0x00,  /*!< Range is +- 250 degrees/s */
150
	SD_MPU6050_Gyroscope_500s = 0x01,  /*!< Range is +- 500 degrees/s */
151
	SD_MPU6050_Gyroscope_1000s = 0x02, /*!< Range is +- 1000 degrees/s */
152
	SD_MPU6050_Gyroscope_2000s = 0x03  /*!< Range is +- 2000 degrees/s */
153
} SD_MPU6050_Gyroscope;
154

  
155

  
156
/**
157
 * @brief  Interrupts union and structure
158
 */
159
typedef union {
160
	struct {
161
		uint8_t DataReady:1;       /*!< Data ready interrupt */
162
		uint8_t reserved2:2;       /*!< Reserved bits */
163
		uint8_t Master:1;          /*!< Master interrupt. Not enabled with library */
164
		uint8_t FifoOverflow:1;    /*!< FIFO overflow interrupt. Not enabled with library */
165
		uint8_t reserved1:1;       /*!< Reserved bit */
166
		uint8_t MotionDetection:1; /*!< Motion detected interrupt */
167
		uint8_t reserved0:1;       /*!< Reserved bit */
168
	} F;
169
	uint8_t Status;
170
} SD_MPU6050_Interrupt;
72
  MPU6050_LLD_REGISTER_SELF_TEST_X        = 0x0D,
73
  MPU6050_LLD_REGISTER_SELF_TEST_Y        = 0x0E,
74
  MPU6050_LLD_REGISTER_SELF_TEST_Z        = 0x0F,
75
  MPU6050_LLD_REGISTER_SELF_TEST_A        = 0x10,
76
  MPU6050_LLD_REGISTER_SMPLRT_DIV         = 0x19,
77
  MPU6050_LLD_REGISTER_CONFIG             = 0x1A,
78
  MPU6050_LLD_REGISTER_GYRO_CONFIG        = 0x1B,
79
  MPU6050_LLD_REGISTER_ACCEL_CONFIG       = 0x1C,
80
  MPU6050_LLD_REGISTER_FIFO_EN            = 0x23,
81
  MPU6050_LLD_REGISTER_I2C_MST_CTRL       = 0x24,
82
  MPU6050_LLD_REGISTER_I2C_SLV0_ADDR      = 0x25,
83
  MPU6050_LLD_REGISTER_I2C_SLV0_REG       = 0x26,
84
  MPU6050_LLD_REGISTER_I2C_SLV0_CTRL      = 0x27,
85
  MPU6050_LLD_REGISTER_I2C_SLV1_ADDR      = 0x28,
86
  MPU6050_LLD_REGISTER_I2C_SLV1_REG       = 0x29,
87
  MPU6050_LLD_REGISTER_I2C_SLV1_CTRL      = 0x2A,
88
  MPU6050_LLD_REGISTER_I2C_SLV2_ADDR      = 0x2B,
89
  MPU6050_LLD_REGISTER_I2C_SLV2_REG       = 0x2C,
90
  MPU6050_LLD_REGISTER_I2C_SLV2_CTRL      = 0x2D,
91
  MPU6050_LLD_REGISTER_I2C_SLV3_ADDR      = 0x2E,
92
  MPU6050_LLD_REGISTER_I2C_SLV3_REG       = 0x2F,
93
  MPU6050_LLD_REGISTER_I2C_SLV3_CTRL      = 0x30,
94
  MPU6050_LLD_REGISTER_I2C_SLV4_ADDR      = 0x31,
95
  MPU6050_LLD_REGISTER_I2C_SLV4_REG       = 0x32,
96
  MPU6050_LLD_REGISTER_I2C_SLV4_DO        = 0x33,
97
  MPU6050_LLD_REGISTER_I2C_SLV4_CTRL      = 0x34,
98
  MPU6050_LLD_REGISTER_I2C_SLV4_DI        = 0x35,
99
  MPU6050_LLD_REGISTER_I2C_MST_STATUS     = 0x36,
100
  MPU6050_LLD_REGISTER_INT_PIN_CFG        = 0x37,
101
  MPU6050_LLD_REGISTER_INT_ENABLE         = 0x38,
102
  MPU6050_LLD_REGISTER_INT_STATUS         = 0x3A,
103
  MPU6050_LLD_REGISTER_ACCEL_XOUT_H       = 0x3B,
104
  MPU6050_LLD_REGISTER_ACCEL_XOUT_L       = 0x3C,
105
  MPU6050_LLD_REGISTER_ACCEL_YOUT_H       = 0x3D,
106
  MPU6050_LLD_REGISTER_ACCEL_YOUT_L       = 0x3E,
107
  MPU6050_LLD_REGISTER_ACCEL_ZOUT_H       = 0x3F,
108
  MPU6050_LLD_REGISTER_ACCEL_ZOUT_L       = 0x40,
109
  MPU6050_LLD_REGISTER_TEMP_OUT_H         = 0x41,
110
  MPU6050_LLD_REGISTER_TEMP_OUT_L         = 0x42,
111
  MPU6050_LLD_REGISTER_GYRO_XOUT_H        = 0x43,
112
  MPU6050_LLD_REGISTER_GYRO_XOUT_L        = 0x44,
113
  MPU6050_LLD_REGISTER_GYRO_YOUT_H        = 0x45,
114
  MPU6050_LLD_REGISTER_GYRO_YOUT_L        = 0x46,
115
  MPU6050_LLD_REGISTER_GYRO_ZOUT_H        = 0x47,
116
  MPU6050_LLD_REGISTER_GYRO_ZOUT_L        = 0x48,
117
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_00   = 0x49,
118
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_01   = 0x4A,
119
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_02   = 0x4B,
120
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_03   = 0x4C,
121
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_04   = 0x4D,
122
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_05   = 0x4E,
123
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_06   = 0x4F,
124
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_07   = 0x50,
125
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_08   = 0x51,
126
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_09   = 0x52,
127
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_10   = 0x53,
128
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_11   = 0x54,
129
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_12   = 0x55,
130
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_13   = 0x56,
131
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_14   = 0x57,
132
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_15   = 0x58,
133
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_16   = 0x59,
134
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_17   = 0x5A,
135
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_18   = 0x5B,
136
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_19   = 0x5C,
137
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_20   = 0x5D,
138
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_21   = 0x5E,
139
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_22   = 0x5F,
140
  MPU6050_LLD_REGISTER_EXT_SENS_DATA_23   = 0x60,
141
  MPU6050_LLD_REGISTER_I2C_SLV0_DO        = 0x63,
142
  MPU6050_LLD_REGISTER_I2C_SLV1_DO        = 0x64,
143
  MPU6050_LLD_REGISTER_I2C_SLV2_DO        = 0x65,
144
  MPU6050_LLD_REGISTER_I2C_SLV3_DO        = 0x66,
145
  MPU6050_LLD_REGISTER_I2C_MST_DELAY_CTRL = 0x67,
146
  MPU6050_LLD_REGISTER_SIGNAL_PATH_RESET  = 0x68,
147
  MPU6050_LLD_REGISTER_USER_CTRL          = 0x6A,
148
  MPU6050_LLD_REGISTER_PWR_MGMT_1         = 0x6B,
149
  MPU6050_LLD_REGISTER_PWR_MGMT_2         = 0x6C,
150
  MPU6050_LLD_REGISTER_FIFO_COUNTH        = 0x72,
151
  MPU6050_LLD_REGISTER_FIFO_COUNTL        = 0x73,
152
  MPU6050_LLD_REGISTER_FIFO_R_W           = 0x74,
153
  MPU6050_LLD_REGISTER_WHO_AM_I           = 0x75,
154
} mpu6050_lld_register_t;
171 155

  
172 156
/**
173
 * @brief Config register.
174
 */
175
typedef union {
176
  uint16_t data;
177
  struct {
178
    /**
179
    ina219_lld_mode_t mode : 3;
180
    ina219_lld_adc_t sadc : 4;
181
    ina219_lld_adc_t badc : 4;
182
    ina219_lld_gain_t gain : 2;
183
    ina219_lld_brng_t brng : 1;
184
    */
185
    uint8_t zero : 1;
186
    uint8_t reset : 1;
187
  } options;
188
} mpu6050_lld_cfg_t;
189

  
190

  
191
/**
192
 * @brief The MPU6050 struct.
157
 * @brief The MPU6050Driver struct.
193 158
 */
194 159
typedef struct {
195
  apalI2CDriver_t* i2cd;
196
  apalI2Caddr_t addr;   /**<The address of the module for I2C communication */
160
  apalI2CDriver_t* i2cd;  /**< Pointer to the connected I2C driver. */
161
  apalI2Caddr_t addr;     /**< The address of the module for I2C communication. */
197 162
} MPU6050Driver;
198 163

  
199 164
/******************************************************************************/
......
207 172
#ifdef __cplusplus
208 173
extern "C" {
209 174
#endif
210
  apalExitStatus_t mpu6050_lld_read_register(const MPU6050Driver* const mpu6050, const uint8_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout);
211
  apalExitStatus_t mpu6050_lld_write_register(const MPU6050Driver* const mpu6050, const uint8_t addr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout);
175
  apalExitStatus_t mpu6050_lld_read_register(const MPU6050Driver* const mpu6050, const mpu6050_lld_register_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout);
176
  apalExitStatus_t mpu6050_lld_write_register(const MPU6050Driver* const mpu6050, const mpu6050_lld_register_t addr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout);
212 177
#ifdef __cplusplus
213 178
}
214 179
#endif
......
219 184

  
220 185
#endif /* AMIROLLD_MPU6050_H */
221 186

  
222

  
223 187
/** @} */
224

  

Also available in: Unified diff