mpuimu  2.0.0.0
mpuimu.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef MPUIMU_H
36 #define MPUIMU_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_i2c_master.h"
55 #include "drv_spi_master.h"
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
68 #define MPUIMU_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
70  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
71  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
72  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
73  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
74  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
75  cfg.fsy = MIKROBUS( mikrobus, MIKROBUS_RST ); \
76  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
77 
83 #define MPUIMU_MASTER_I2C 0
84 #define MPUIMU_MASTER_SPI 1
85 
91 #define MPUIMU_RETVAL uint8_t
92 
93 #define MPUIMU_OK 0x00
94 #define MPUIMU_INIT_ERROR 0xFF
95 
101 #define MPUIMU_I2C_ADDR_0 0xD2
102 #define MPUIMU_I2C_ADDR_1 0x69
103 
109 #define MPUIMU_REG_SELF_TEST_X 0x0D
110 #define MPUIMU_REG_SELF_TEST_Y 0x0E
111 #define MPUIMU_REG_SELF_TEST_Z 0x0F
112 #define MPUIMU_REG_SELF_TEST_A 0x10
113 #define MPUIMU_REG_SMPLRT_DIV 0x19
114 #define MPUIMU_REG_MOT_THR 0x1F
115 #define MPUIMU_REG_I2C_MST_CTRL 0x24
116 #define MPUIMU_REG_I2C_SLV0_ADDR 0x25
117 #define MPUIMU_REG_I2C_SLV0 0x26
118 #define MPUIMU_REG_I2C_SLV0_CTRL 0x27
119 #define MPUIMU_REG_I2C_SLV1_ADDR 0x28
120 #define MPUIMU_REG_I2C_SLV1 0x29
121 #define MPUIMU_REG_I2C_SLV1_CTRL 0x2A
122 #define MPUIMU_REG_I2C_SLV2_ADDR 0x2B
123 #define MPUIMU_REG_I2C_SLV2 0x2C
124 #define MPUIMU_REG_I2C_SLV2_CTRL 0x2D
125 #define MPUIMU_REG_I2C_SLV3_ADDR 0x2E
126 #define MPUIMU_REG_I2C_SLV3 0x2F
127 #define MPUIMU_REG_I2C_SLV3_CTRL 0x30
128 #define MPUIMU_REG_I2C_SLV4_ADDR 0x31
129 #define MPUIMU_REG_I2C_SLV4 0x32
130 #define MPUIMU_REG_I2C_SLV4_DO 0x33
131 #define MPUIMU_REG_I2C_SLV4_CTRL 0x34
132 #define MPUIMU_REG_I2C_SLV4_DI 0x35
133 #define MPUIMU_REG_I2C_MST_STATUS 0x36
134 #define MPUIMU_REG_I2C_SLV0_DO 0x63
135 #define MPUIMU_REG_I2C_SLV1_DO 0x64
136 #define MPUIMU_REG_I2C_SLV2_DO 0x65
137 #define MPUIMU_REG_I2C_SLV3_DO 0x66
138 #define MPUIMU_REG_I2C_MST_DELAY_CT 0x67
139 #define MPUIMU_REG_INT_PIN_CFG 0x37
140 #define MPUIMU_REG_INT_ENABLE 0x38
141 #define MPUIMU_REG_INT_STATUS 0x3A
142 #define MPUIMU_REG_SIGNAL_PATH_RES 0x68
143 #define MPUIMU_REG_MOT_DETECT_CTRL 0x69
144 #define MPUIMU_REG_USER_CTRL 0x6A
145 #define MPUIMU_REG_PWR_MGMT_1 0x6B
146 #define MPUIMU_REG_PWR_MGMT_2 0x6C
147 #define MPUIMU_REG_FIFO_EN 0x23
148 #define MPUIMU_REG_FIFO_COUNTH 0x72
149 #define MPUIMU_REG_FIFO_COUNTL 0x73
150 #define MPUIMU_REG_FIFO_R_W 0x74
151 #define MPUIMU_REG_WHO_AM_I 0x75
152 
158 #define MPUIMU_CFG_CONFIG 0x1A
159 #define MPUIMU_CFG_RAW_ACCEL 0x3B
160 #define MPUIMU_CFG_RAW_GYRO 0x43
161 #define MPUIMU_CFG_STANDARD_GYRO 0x1B
162 #define MPUIMU_CFG_STANDARD_ACCEL 0x1C
163 #define MPUIMU_CFG_PWR 0x80
164 #define MPUIMU_CFG_PWR_SET 0x00
165 #define MPUIMU_CFG_FIFO_ENABLE 0x78
166 #define MPUIMU_CFG_INT_ENABLE 0x10
167 
173 #define MPUIMU_EXT_SENS_DATA_00 0x49
174 #define MPUIMU_EXT_SENS_DATA_01 0x4A
175 #define MPUIMU_EXT_SENS_DATA_02 0x4B
176 #define MPUIMU_EXT_SENS_DATA_03 0x4C
177 #define MPUIMU_EXT_SENS_DATA_04 0x4D
178 #define MPUIMU_EXT_SENS_DATA_05 0x4E
179 #define MPUIMU_EXT_SENS_DATA_06 0x4F
180 #define MPUIMU_EXT_SENS_DATA_07 0x50
181 #define MPUIMU_EXT_SENS_DATA_08 0x51
182 #define MPUIMU_EXT_SENS_DATA_11 0x54
183 #define MPUIMU_EXT_SENS_DATA_09 0x52
184 #define MPUIMU_EXT_SENS_DATA_10 0x53
185 #define MPUIMU_EXT_SENS_DATA_12 0x55
186 #define MPUIMU_EXT_SENS_DATA_13 0x56
187 #define MPUIMU_EXT_SENS_DATA_14 0x57
188 #define MPUIMU_EXT_SENS_DATA_15 0x58
189 #define MPUIMU_EXT_SENS_DATA_16 0x59
190 #define MPUIMU_EXT_SENS_DATA_17 0x5A
191 #define MPUIMU_EXT_SENS_DATA_18 0x5B
192 #define MPUIMU_EXT_SENS_DATA_19 0x5C
193 #define MPUIMU_EXT_SENS_DATA_20 0x5D
194 #define MPUIMU_EXT_SENS_DATA_21 0x5E
195 #define MPUIMU_EXT_SENS_DATA_22 0x5F
196 #define MPUIMU_EXT_SENS_DATA_23 0x60
197 
204 // GYRO
205 
206 #define MPUIMU_GYRO_XOUT_H 0x43
207 #define MPUIMU_GYRO_XOUT_L 0x44
208 #define MPUIMU_GYRO_YOUT_H 0x45
209 #define MPUIMU_GYRO_YOUT_L 0x46
210 #define MPUIMU_GYRO_ZOUT_H 0x47
211 #define MPUIMU_GYRO_ZOUT_L 0x48
212 
213 // ACCEL
214 
215 #define MPUIMU_ACCEL_XOUT_H 0x3B
216 #define MPUIMU_ACCEL_XOUT_L 0x3C
217 #define MPUIMU_ACCEL_YOUT_H 0x3D
218 #define MPUIMU_ACCEL_YOUT_L 0x3E
219 #define MPUIMU_ACCEL_ZOUT_H 0x3F
220 #define MPUIMU_ACCEL_ZOUT_L 0x40
221 
222 // TEMP
223 
224 #define MPUIMU_TEMP_OUT_H 0x41
225 #define MPUIMU_TEMP_OUT_L 0x42
226  // End group macro
229 
230 // --------------------------------------------------------------- PUBLIC TYPES
239 typedef uint8_t mpuimu_select_t;
240 
244 typedef void ( *mpuimu_master_io_t )( struct mpuimu_s*, uint8_t, uint8_t*, uint8_t );
245 
249 typedef struct mpuimu_s
250 {
251  // Output pins
252 
253  digital_out_t cs;
254  digital_out_t fsy;
255 
256  // Input pins
257 
258  digital_in_t int_pin;
259 
260  // Modules
261 
262  i2c_master_t i2c;
263  spi_master_t spi;
264 
265  // ctx variable
266 
267  uint8_t slave_address;
268  pin_name_t chip_select;
272 
274 
278 typedef struct
279 {
280  // Communication gpio pins
281 
282  pin_name_t scl;
283  pin_name_t sda;
284  pin_name_t miso;
285  pin_name_t mosi;
286  pin_name_t sck;
287  pin_name_t cs;
288 
289  // Additional gpio pins
290 
291  pin_name_t fsy;
292  pin_name_t int_pin;
293 
294  // static variable
295 
296  uint32_t i2c_speed;
297  uint8_t i2c_address;
298 
299  uint32_t spi_speed;
300  spi_master_mode_t spi_mode;
301  spi_master_chip_select_polarity_t cs_polarity;
302 
304 
305 } mpuimu_cfg_t;
306 
311 typedef struct
312 {
313  int16_t accel_x;
314  int16_t accel_y;
315  int16_t accel_z;
316 
318 
322 typedef struct
323 {
324  int16_t gyro_x;
325  int16_t gyro_y;
326  int16_t gyro_z;
327 
329  // Measurement data
331  // End types group
333 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
339 #ifdef __cplusplus
340 extern "C"{
341 #endif
342 
352 
362 
381 
392 void mpuimu_generic_write ( mpuimu_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
393 
404 void mpuimu_generic_read ( mpuimu_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
405 
417 int16_t mpuimu_get_axis ( mpuimu_t *ctx, uint8_t adr_reg_high );
418 
428 
438 
450 
458 void software_reset ( mpuimu_t *ctx );
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 #endif // _MPUIMU_H_
464  // End public_function group
467 
468 // ------------------------------------------------------------------------- END
mpuimu_s
Click ctx object definition.
Definition: mpuimu.h:250
mpuimu_cfg_t::cs
pin_name_t cs
Definition: mpuimu.h:287
mpuimu_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: mpuimu.h:301
mpuimu_s::spi
spi_master_t spi
Definition: mpuimu.h:263
mpuimu_s::fsy
digital_out_t fsy
Definition: mpuimu.h:254
mpuimu_cfg_t::mosi
pin_name_t mosi
Definition: mpuimu.h:285
mpuimu_generic_read
void mpuimu_generic_read(mpuimu_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
mpuimu_cfg_setup
void mpuimu_cfg_setup(mpuimu_cfg_t *cfg)
Config Object Initialization function.
mpuimu_read_accel
void mpuimu_read_accel(mpuimu_t *ctx, mpuimu_accel_data_t *accel_data)
Read Axis function.
mpuimu_s::int_pin
digital_in_t int_pin
Definition: mpuimu.h:258
mpuimu_s::slave_address
uint8_t slave_address
Definition: mpuimu.h:267
mpuimu_cfg_t::miso
pin_name_t miso
Definition: mpuimu.h:284
mpuimu_cfg_t::scl
pin_name_t scl
Definition: mpuimu.h:282
mpuimu_s::write_f
mpuimu_master_io_t write_f
Definition: mpuimu.h:269
mpuimu_s::cs
digital_out_t cs
Definition: mpuimu.h:253
mpuimu_read_gyro
void mpuimu_read_gyro(mpuimu_t *ctx, mpuimu_gyro_data_t *gyro_data)
Read Axis function.
mpuimu_accel_data_t
Definition: mpuimu.h:312
mpuimu_s::i2c
i2c_master_t i2c
Definition: mpuimu.h:262
mpuimu_cfg_t::sck
pin_name_t sck
Definition: mpuimu.h:286
mpuimu_accel_data_t::accel_z
int16_t accel_z
Definition: mpuimu.h:315
mpuimu_cfg_t::sel
mpuimu_select_t sel
Definition: mpuimu.h:303
mpuimu_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: mpuimu.h:300
mpuimu_cfg_t
Click configuration structure definition.
Definition: mpuimu.h:279
mpuimu_select_t
uint8_t mpuimu_select_t
Communication type.
Definition: mpuimu.h:239
mpuimu_s::chip_select
pin_name_t chip_select
Definition: mpuimu.h:268
mpuimu_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: mpuimu.h:296
MPUIMU_RETVAL
#define MPUIMU_RETVAL
Definition: mpuimu.h:91
mpuimu_gyro_data_t::gyro_z
int16_t gyro_z
Definition: mpuimu.h:326
mpuimu_s::master_sel
mpuimu_select_t master_sel
Definition: mpuimu.h:271
mpuimu_get_axis
int16_t mpuimu_get_axis(mpuimu_t *ctx, uint8_t adr_reg_high)
High register data function.
mpuimu_cfg_t::int_pin
pin_name_t int_pin
Definition: mpuimu.h:292
mpuimu_cfg_t::spi_speed
uint32_t spi_speed
Definition: mpuimu.h:299
software_reset
void software_reset(mpuimu_t *ctx)
Software reset function.
mpuimu_t
struct mpuimu_s mpuimu_t
Click ctx object definition.
mpuimu_cfg_t::i2c_address
uint8_t i2c_address
Definition: mpuimu.h:297
mpuimu_read_temperature
float mpuimu_read_temperature(mpuimu_t *ctx)
Temperature read function.
mpuimu_accel_data_t::accel_y
int16_t accel_y
Definition: mpuimu.h:314
mpuimu_s::read_f
mpuimu_master_io_t read_f
Definition: mpuimu.h:270
mpuimu_generic_write
void mpuimu_generic_write(mpuimu_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
mpuimu_cfg_t::fsy
pin_name_t fsy
Definition: mpuimu.h:291
mpuimu_accel_data_t::accel_x
int16_t accel_x
Definition: mpuimu.h:313
mpuimu_gyro_data_t::gyro_x
int16_t gyro_x
Definition: mpuimu.h:324
mpuimu_gyro_data_t
Gyro data object definition.
Definition: mpuimu.h:323
mpuimu_default_cfg
void mpuimu_default_cfg(mpuimu_t *ctx)
Click Default Configuration function.
mpuimu_master_io_t
void(* mpuimu_master_io_t)(struct mpuimu_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: mpuimu.h:244
gyro_data
mpuimu_gyro_data_t gyro_data
Definition: main.c:36
mpuimu_cfg_t::sda
pin_name_t sda
Definition: mpuimu.h:283
accel_data
mpuimu_accel_data_t accel_data
Definition: main.c:35
mpuimu_gyro_data_t::gyro_y
int16_t gyro_y
Definition: mpuimu.h:325
mpuimu_init
MPUIMU_RETVAL mpuimu_init(mpuimu_t *ctx, mpuimu_cfg_t *cfg)
Initialization function.