c6dofimu2  2.0.0.0
c6dofimu2.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 C6DOFIMU2_H
36 #define C6DOFIMU2_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 C6DOFIMU2_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.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
76 
82 #define C6DOFIMU2_MASTER_I2C 0
83 #define C6DOFIMU2_MASTER_SPI 1
84 
90 #define C6DOFIMU2_RETVAL uint8_t
91 
92 #define C6DOFIMU2_OK 0x00
93 #define C6DOFIMU2_INIT_ERROR 0xFF
94 
100 #define C6DOFIMU2_I2C_ADDR 0x68
101 
107 #define C6DOFIMU2_REG_CHIP_ID 0x00
108 #define C6DOFIMU2_REG_ERROR 0x02
109 #define C6DOFIMU2_REG_PMU_STATUS 0x03
110 #define C6DOFIMU2_REG_AUX_DATA 0x04
111 #define C6DOFIMU2_REG_GYRO_DATA 0x0C
112 #define C6DOFIMU2_REG_ACCEL_DATA 0x12
113 #define C6DOFIMU2_REG_STATUS 0x1B
114 #define C6DOFIMU2_REG_INT_STATUS 0x1C
115 #define C6DOFIMU2_REG_FIFO_LENGTH 0x22
116 #define C6DOFIMU2_REG_FIFO_DATA 0x24
117 #define C6DOFIMU2_REG_ACCEL_CONFIG 0x40
118 #define C6DOFIMU2_REG_ACCEL_RANGE 0x41
119 #define C6DOFIMU2_REG_GYRO_CONFIG 0x42
120 #define C6DOFIMU2_REG_GYRO_RANGE 0x43
121 #define C6DOFIMU2_REG_AUX_ODR 0x44
122 #define C6DOFIMU2_REG_FIFO_DOWN 0x45
123 #define C6DOFIMU2_REG_FIFO_CONFIG_0 0x46
124 #define C6DOFIMU2_REG_FIFO_CONFIG_1 0x47
125 #define C6DOFIMU2_REG_AUX_IF_0 0x4B
126 #define C6DOFIMU2_REG_AUX_IF_1 0x4C
127 #define C6DOFIMU2_REG_AUX_IF_2 0x4D
128 #define C6DOFIMU2_REG_AUX_IF_3 0x4E
129 #define C6DOFIMU2_REG_AUX_IF_4 0x4F
130 #define C6DOFIMU2_REG_INT_ENABLE_0 0x50
131 #define C6DOFIMU2_REG_INT_ENABLE_1 0x51
132 #define C6DOFIMU2_REG_INT_ENABLE_2 0x52
133 #define C6DOFIMU2_REG_INT_OUT_CTRL 0x53
134 #define C6DOFIMU2_REG_INT_LATCH 0x54
135 #define C6DOFIMU2_REG_INT_MAP_0 0x55
136 #define C6DOFIMU2_REG_INT_MAP_1 0x56
137 #define C6DOFIMU2_REG_INT_MAP_2 0x57
138 #define C6DOFIMU2_REG_INT_DATA_0 0x58
139 #define C6DOFIMU2_REG_INT_DATA_1 0x59
140 #define C6DOFIMU2_REG_INT_LOWHIGH_0 0x5A
141 #define C6DOFIMU2_REG_INT_LOWHIGH_1 0x5B
142 #define C6DOFIMU2_REG_INT_LOWHIGH_2 0x5C
143 #define C6DOFIMU2_REG_INT_LOWHIGH_3 0x5D
144 #define C6DOFIMU2_REG_INT_LOWHIGH_4 0x5E
145 #define C6DOFIMU2_REG_INT_MOTION_0 0x5F
146 #define C6DOFIMU2_REG_INT_MOTION_1 0x60
147 #define C6DOFIMU2_REG_INT_MOTION_2 0x61
148 #define C6DOFIMU2_REG_INT_MOTION_3 0x62
149 #define C6DOFIMU2_REG_INT_TAP_0 0x63
150 #define C6DOFIMU2_REG_INT_TAP_1 0x64
151 #define C6DOFIMU2_REG_INT_ORIENT_0 0x65
152 #define C6DOFIMU2_REG_INT_ORIENT_1 0x66
153 #define C6DOFIMU2_REG_INT_FLAT_0 0x67
154 #define C6DOFIMU2_REG_INT_FLAT_1 0x68
155 #define C6DOFIMU2_REG_FOC_CONF 0x69
156 #define C6DOFIMU2_REG_CONF 0x6A
157 #define C6DOFIMU2_REG_NV_CONF 0x70
158 
164 #define C6DOFIMU2_CFG_IF_CONF 0x6B
165 #define C6DOFIMU2_CFG_SELF_TEST 0x6D
166 #define C6DOFIMU2_CFG_OFFSET 0x71
167 #define C6DOFIMU2_CFG_OFFSET_CONF 0x77
168 #define C6DOFIMU2_CFG_INT_STEP_CNT_0 0x78
169 #define C6DOFIMU2_CFG_INT_STEP_CONFIG_0 0x7A
170 #define C6DOFIMU2_CFG_INT_STEP_CONFIG_1 0x7B
171 #define C6DOFIMU2_CFG_COMMAND 0x7E
172 #define C6DOFIMU2_CFG_SPI_COMM_TEST 0x7F
173 #define C6DOFIMU2_CFG_INTL_PULLUP_CONF 0x85
174 
180 #define C6DOFIMU2_CMD_ACCEL_NORMAL 0x11
181 #define C6DOFIMU2_CMD_ACCEL_LOW_PWR 0x12
182 #define C6DOFIMU2_CMD_GYRO_NORMAL 0x15
183 #define C6DOFIMU2_CMD_GYRO_FAST 0x7E
184 #define C6DOFIMU2_CMD_SEL_I2C 0x00
185 #define C6DOFIMU2_CMD_SEL_SPI 0x01
186 
193 // GYRO
194 
195 #define C6DOFIMU2_GYRO_X_L 0x0C
196 #define C6DOFIMU2_GYRO_X_H 0x0D
197 #define C6DOFIMU2_GYRO_Y_L 0x0E
198 #define C6DOFIMU2_GYRO_Y_H 0x0F
199 #define C6DOFIMU2_GYRO_Z_L 0x10
200 #define C6DOFIMU2_GYRO_Z_H 0x11
201 
202 // ACCEL
203 
204 #define C6DOFIMU2_ACCEL_X_L 0x12
205 #define C6DOFIMU2_ACCEL_X_H 0x13
206 #define C6DOFIMU2_ACCEL_Y_L 0x14
207 #define C6DOFIMU2_ACCEL_Y_H 0x15
208 #define C6DOFIMU2_ACCEL_Z_L 0x16
209 #define C6DOFIMU2_ACCEL_Z_H 0x17
210  // End group macro
213 
214 // --------------------------------------------------------------- PUBLIC TYPES
223 typedef uint8_t c6dofimu2_select_t;
224 
228 typedef void ( *c6dofimu2_master_io_t )( struct c6dofimu2_s*, uint8_t, uint8_t*, uint8_t );
229 
233 typedef struct c6dofimu2_s
234 {
235  // Output pins
236 
237  digital_out_t cs;
238 
239  // Input pins
240 
241  digital_in_t int_pin;
242 
243  // Modules
244 
245  i2c_master_t i2c;
246  spi_master_t spi;
247 
248  // ctx variable
249 
250  uint8_t slave_address;
251  pin_name_t chip_select;
255 
257 
261 typedef struct
262 {
263  // Communication gpio pins
264 
265  pin_name_t scl;
266  pin_name_t sda;
267  pin_name_t miso;
268  pin_name_t mosi;
269  pin_name_t sck;
270  pin_name_t cs;
271 
272  // Additional gpio pins
273 
274  pin_name_t int_pin;
275 
276  // static variable
277 
278  uint32_t i2c_speed;
279  uint8_t i2c_address;
280  uint32_t spi_speed;
281  spi_master_mode_t spi_mode;
282  spi_master_chip_select_polarity_t cs_polarity;
283 
285 
287  // End types group
289 
298 typedef struct
299 {
300  int16_t accel_x;
301  int16_t accel_y;
302  int16_t accel_z;
303 
305 
309 typedef struct
310 {
311  int16_t gyro_x;
312  int16_t gyro_y;
313  int16_t gyro_z;
314 
316  // Measurement data
318 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
324 #ifdef __cplusplus
325 extern "C"{
326 #endif
327 
337 
347 
368 
379 void c6dofimu2_generic_write ( c6dofimu2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
380 
391 void c6dofimu2_generic_read ( c6dofimu2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
392 
405 int16_t c6dofimu2_get_axis ( c6dofimu2_t *ctx, uint8_t adr_reg_low, uint8_t adr_reg_high );
406 
416 
426 
427 #ifdef __cplusplus
428 }
429 #endif
430 #endif // _C6DOFIMU2_H_
431  // End public_function group
434 
435 // ------------------------------------------------------------------------- END
gyro_data
c6dofimu2_gyro_data_t gyro_data
Definition: main.c:39
c6dofimu2_gyro_data_t
Gyro data object definition.
Definition: c6dofimu2.h:310
c6dofimu2_default_cfg
void c6dofimu2_default_cfg(c6dofimu2_t *ctx, c6dofimu2_cfg_t *cfg)
Click Default Configuration function.
c6dofimu2_init
C6DOFIMU2_RETVAL c6dofimu2_init(c6dofimu2_t *ctx, c6dofimu2_cfg_t *cfg)
Initialization function.
c6dofimu2_cfg_t::i2c_address
uint8_t i2c_address
Definition: c6dofimu2.h:279
c6dofimu2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: c6dofimu2.h:281
c6dofimu2_gyro_data_t::gyro_y
int16_t gyro_y
Definition: c6dofimu2.h:312
c6dofimu2_s::master_sel
c6dofimu2_select_t master_sel
Definition: c6dofimu2.h:254
accel_data
c6dofimu2_accel_data_t accel_data
Definition: main.c:38
c6dofimu2_cfg_t
Click configuration structure definition.
Definition: c6dofimu2.h:262
c6dofimu2_s
Click ctx object definition.
Definition: c6dofimu2.h:234
c6dofimu2_accel_data_t::accel_x
int16_t accel_x
Definition: c6dofimu2.h:300
c6dofimu2_cfg_t::mosi
pin_name_t mosi
Definition: c6dofimu2.h:268
c6dofimu2_t
struct c6dofimu2_s c6dofimu2_t
Click ctx object definition.
c6dofimu2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: c6dofimu2.h:282
c6dofimu2_accel_data_t::accel_z
int16_t accel_z
Definition: c6dofimu2.h:302
c6dofimu2_accel_data_t
Accel data object definition.
Definition: c6dofimu2.h:299
c6dofimu2_cfg_t::scl
pin_name_t scl
Definition: c6dofimu2.h:265
c6dofimu2_cfg_t::sel
c6dofimu2_select_t sel
Definition: c6dofimu2.h:284
c6dofimu2_get_axis
int16_t c6dofimu2_get_axis(c6dofimu2_t *ctx, uint8_t adr_reg_low, uint8_t adr_reg_high)
Low and high register data function.
c6dofimu2_cfg_t::sck
pin_name_t sck
Definition: c6dofimu2.h:269
c6dofimu2_s::int_pin
digital_in_t int_pin
Definition: c6dofimu2.h:241
c6dofimu2_read_gyro
void c6dofimu2_read_gyro(c6dofimu2_t *ctx, c6dofimu2_gyro_data_t *gyro_data)
Read axis function.
c6dofimu2_generic_read
void c6dofimu2_generic_read(c6dofimu2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
c6dofimu2_cfg_t::sda
pin_name_t sda
Definition: c6dofimu2.h:266
C6DOFIMU2_RETVAL
#define C6DOFIMU2_RETVAL
Definition: c6dofimu2.h:90
c6dofimu2_read_accel
void c6dofimu2_read_accel(c6dofimu2_t *ctx, c6dofimu2_accel_data_t *accel_data)
Read axis function.
c6dofimu2_s::spi
spi_master_t spi
Definition: c6dofimu2.h:246
c6dofimu2_accel_data_t::accel_y
int16_t accel_y
Definition: c6dofimu2.h:301
c6dofimu2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: c6dofimu2.h:278
c6dofimu2_s::read_f
c6dofimu2_master_io_t read_f
Definition: c6dofimu2.h:253
c6dofimu2_s::slave_address
uint8_t slave_address
Definition: c6dofimu2.h:250
c6dofimu2_cfg_setup
void c6dofimu2_cfg_setup(c6dofimu2_cfg_t *cfg)
Config Object Initialization function.
c6dofimu2_gyro_data_t::gyro_x
int16_t gyro_x
Definition: c6dofimu2.h:311
c6dofimu2_cfg_t::cs
pin_name_t cs
Definition: c6dofimu2.h:270
c6dofimu2_cfg_t::miso
pin_name_t miso
Definition: c6dofimu2.h:267
c6dofimu2_s::chip_select
pin_name_t chip_select
Definition: c6dofimu2.h:251
c6dofimu2_s::cs
digital_out_t cs
Definition: c6dofimu2.h:237
c6dofimu2_gyro_data_t::gyro_z
int16_t gyro_z
Definition: c6dofimu2.h:313
c6dofimu2_master_io_t
void(* c6dofimu2_master_io_t)(struct c6dofimu2_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: c6dofimu2.h:228
c6dofimu2_s::write_f
c6dofimu2_master_io_t write_f
Definition: c6dofimu2.h:252
c6dofimu2_cfg_t::spi_speed
uint32_t spi_speed
Definition: c6dofimu2.h:280
c6dofimu2_generic_write
void c6dofimu2_generic_write(c6dofimu2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
c6dofimu2_cfg_t::int_pin
pin_name_t int_pin
Definition: c6dofimu2.h:274
c6dofimu2_s::i2c
i2c_master_t i2c
Definition: c6dofimu2.h:245
c6dofimu2_select_t
uint8_t c6dofimu2_select_t
Communication type.
Definition: c6dofimu2.h:223