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 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 #include "drv_spi_master.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define C6DOFIMU2_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
66  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
72 
78 #define C6DOFIMU2_MASTER_I2C 0
79 #define C6DOFIMU2_MASTER_SPI 1
80 
86 #define C6DOFIMU2_RETVAL uint8_t
87 
88 #define C6DOFIMU2_OK 0x00
89 #define C6DOFIMU2_INIT_ERROR 0xFF
90 
96 #define C6DOFIMU2_I2C_ADDR 0x68
97 
103 #define C6DOFIMU2_REG_CHIP_ID 0x00
104 #define C6DOFIMU2_REG_ERROR 0x02
105 #define C6DOFIMU2_REG_PMU_STATUS 0x03
106 #define C6DOFIMU2_REG_AUX_DATA 0x04
107 #define C6DOFIMU2_REG_GYRO_DATA 0x0C
108 #define C6DOFIMU2_REG_ACCEL_DATA 0x12
109 #define C6DOFIMU2_REG_STATUS 0x1B
110 #define C6DOFIMU2_REG_INT_STATUS 0x1C
111 #define C6DOFIMU2_REG_FIFO_LENGTH 0x22
112 #define C6DOFIMU2_REG_FIFO_DATA 0x24
113 #define C6DOFIMU2_REG_ACCEL_CONFIG 0x40
114 #define C6DOFIMU2_REG_ACCEL_RANGE 0x41
115 #define C6DOFIMU2_REG_GYRO_CONFIG 0x42
116 #define C6DOFIMU2_REG_GYRO_RANGE 0x43
117 #define C6DOFIMU2_REG_AUX_ODR 0x44
118 #define C6DOFIMU2_REG_FIFO_DOWN 0x45
119 #define C6DOFIMU2_REG_FIFO_CONFIG_0 0x46
120 #define C6DOFIMU2_REG_FIFO_CONFIG_1 0x47
121 #define C6DOFIMU2_REG_AUX_IF_0 0x4B
122 #define C6DOFIMU2_REG_AUX_IF_1 0x4C
123 #define C6DOFIMU2_REG_AUX_IF_2 0x4D
124 #define C6DOFIMU2_REG_AUX_IF_3 0x4E
125 #define C6DOFIMU2_REG_AUX_IF_4 0x4F
126 #define C6DOFIMU2_REG_INT_ENABLE_0 0x50
127 #define C6DOFIMU2_REG_INT_ENABLE_1 0x51
128 #define C6DOFIMU2_REG_INT_ENABLE_2 0x52
129 #define C6DOFIMU2_REG_INT_OUT_CTRL 0x53
130 #define C6DOFIMU2_REG_INT_LATCH 0x54
131 #define C6DOFIMU2_REG_INT_MAP_0 0x55
132 #define C6DOFIMU2_REG_INT_MAP_1 0x56
133 #define C6DOFIMU2_REG_INT_MAP_2 0x57
134 #define C6DOFIMU2_REG_INT_DATA_0 0x58
135 #define C6DOFIMU2_REG_INT_DATA_1 0x59
136 #define C6DOFIMU2_REG_INT_LOWHIGH_0 0x5A
137 #define C6DOFIMU2_REG_INT_LOWHIGH_1 0x5B
138 #define C6DOFIMU2_REG_INT_LOWHIGH_2 0x5C
139 #define C6DOFIMU2_REG_INT_LOWHIGH_3 0x5D
140 #define C6DOFIMU2_REG_INT_LOWHIGH_4 0x5E
141 #define C6DOFIMU2_REG_INT_MOTION_0 0x5F
142 #define C6DOFIMU2_REG_INT_MOTION_1 0x60
143 #define C6DOFIMU2_REG_INT_MOTION_2 0x61
144 #define C6DOFIMU2_REG_INT_MOTION_3 0x62
145 #define C6DOFIMU2_REG_INT_TAP_0 0x63
146 #define C6DOFIMU2_REG_INT_TAP_1 0x64
147 #define C6DOFIMU2_REG_INT_ORIENT_0 0x65
148 #define C6DOFIMU2_REG_INT_ORIENT_1 0x66
149 #define C6DOFIMU2_REG_INT_FLAT_0 0x67
150 #define C6DOFIMU2_REG_INT_FLAT_1 0x68
151 #define C6DOFIMU2_REG_FOC_CONF 0x69
152 #define C6DOFIMU2_REG_CONF 0x6A
153 #define C6DOFIMU2_REG_NV_CONF 0x70
154 
160 #define C6DOFIMU2_CFG_IF_CONF 0x6B
161 #define C6DOFIMU2_CFG_SELF_TEST 0x6D
162 #define C6DOFIMU2_CFG_OFFSET 0x71
163 #define C6DOFIMU2_CFG_OFFSET_CONF 0x77
164 #define C6DOFIMU2_CFG_INT_STEP_CNT_0 0x78
165 #define C6DOFIMU2_CFG_INT_STEP_CONFIG_0 0x7A
166 #define C6DOFIMU2_CFG_INT_STEP_CONFIG_1 0x7B
167 #define C6DOFIMU2_CFG_COMMAND 0x7E
168 #define C6DOFIMU2_CFG_SPI_COMM_TEST 0x7F
169 #define C6DOFIMU2_CFG_INTL_PULLUP_CONF 0x85
170 
176 #define C6DOFIMU2_CMD_ACCEL_NORMAL 0x11
177 #define C6DOFIMU2_CMD_ACCEL_LOW_PWR 0x12
178 #define C6DOFIMU2_CMD_GYRO_NORMAL 0x15
179 #define C6DOFIMU2_CMD_GYRO_FAST 0x7E
180 #define C6DOFIMU2_CMD_SEL_I2C 0x00
181 #define C6DOFIMU2_CMD_SEL_SPI 0x01
182 
189 // GYRO
190 
191 #define C6DOFIMU2_GYRO_X_L 0x0C
192 #define C6DOFIMU2_GYRO_X_H 0x0D
193 #define C6DOFIMU2_GYRO_Y_L 0x0E
194 #define C6DOFIMU2_GYRO_Y_H 0x0F
195 #define C6DOFIMU2_GYRO_Z_L 0x10
196 #define C6DOFIMU2_GYRO_Z_H 0x11
197 
198 // ACCEL
199 
200 #define C6DOFIMU2_ACCEL_X_L 0x12
201 #define C6DOFIMU2_ACCEL_X_H 0x13
202 #define C6DOFIMU2_ACCEL_Y_L 0x14
203 #define C6DOFIMU2_ACCEL_Y_H 0x15
204 #define C6DOFIMU2_ACCEL_Z_L 0x16
205 #define C6DOFIMU2_ACCEL_Z_H 0x17
206  // End group macro
209 
210 // --------------------------------------------------------------- PUBLIC TYPES
219 typedef uint8_t c6dofimu2_select_t;
220 
224 typedef void ( *c6dofimu2_master_io_t )( struct c6dofimu2_s*, uint8_t, uint8_t*, uint8_t );
225 
229 typedef struct c6dofimu2_s
230 {
231  // Output pins
232 
233  digital_out_t cs;
234 
235  // Input pins
236 
237  digital_in_t int_pin;
238 
239  // Modules
240 
241  i2c_master_t i2c;
242  spi_master_t spi;
243 
244  // ctx variable
245 
246  uint8_t slave_address;
247  pin_name_t chip_select;
251 
253 
257 typedef struct
258 {
259  // Communication gpio pins
260 
261  pin_name_t scl;
262  pin_name_t sda;
263  pin_name_t miso;
264  pin_name_t mosi;
265  pin_name_t sck;
266  pin_name_t cs;
267 
268  // Additional gpio pins
269 
270  pin_name_t int_pin;
271 
272  // static variable
273 
274  uint32_t i2c_speed;
275  uint8_t i2c_address;
276  uint32_t spi_speed;
277  spi_master_mode_t spi_mode;
278  spi_master_chip_select_polarity_t cs_polarity;
279 
281 
283  // End types group
285 
294 typedef struct
295 {
296  int16_t accel_x;
297  int16_t accel_y;
298  int16_t accel_z;
299 
301 
305 typedef struct
306 {
307  int16_t gyro_x;
308  int16_t gyro_y;
309  int16_t gyro_z;
310 
312  // Measurement data
314 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
320 #ifdef __cplusplus
321 extern "C"{
322 #endif
323 
333 
343 
364 
375 void c6dofimu2_generic_write ( c6dofimu2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
376 
387 void c6dofimu2_generic_read ( c6dofimu2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
388 
401 int16_t c6dofimu2_get_axis ( c6dofimu2_t *ctx, uint8_t adr_reg_low, uint8_t adr_reg_high );
402 
412 
422 
423 #ifdef __cplusplus
424 }
425 #endif
426 #endif // _C6DOFIMU2_H_
427  // End public_function group
430 
431 // ------------------------------------------------------------------------- END
gyro_data
c6dofimu2_gyro_data_t gyro_data
Definition: main.c:39
c6dofimu2_gyro_data_t
Gyro data object definition.
Definition: c6dofimu2.h:306
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:275
c6dofimu2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: c6dofimu2.h:277
c6dofimu2_gyro_data_t::gyro_y
int16_t gyro_y
Definition: c6dofimu2.h:308
c6dofimu2_s::master_sel
c6dofimu2_select_t master_sel
Definition: c6dofimu2.h:250
accel_data
c6dofimu2_accel_data_t accel_data
Definition: main.c:38
c6dofimu2_cfg_t
Click configuration structure definition.
Definition: c6dofimu2.h:258
c6dofimu2_s
Click ctx object definition.
Definition: c6dofimu2.h:230
c6dofimu2_accel_data_t::accel_x
int16_t accel_x
Definition: c6dofimu2.h:296
c6dofimu2_cfg_t::mosi
pin_name_t mosi
Definition: c6dofimu2.h:264
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:278
c6dofimu2_accel_data_t::accel_z
int16_t accel_z
Definition: c6dofimu2.h:298
c6dofimu2_accel_data_t
Accel data object definition.
Definition: c6dofimu2.h:295
c6dofimu2_cfg_t::scl
pin_name_t scl
Definition: c6dofimu2.h:261
c6dofimu2_cfg_t::sel
c6dofimu2_select_t sel
Definition: c6dofimu2.h:280
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:265
c6dofimu2_s::int_pin
digital_in_t int_pin
Definition: c6dofimu2.h:237
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:262
C6DOFIMU2_RETVAL
#define C6DOFIMU2_RETVAL
Definition: c6dofimu2.h:86
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:242
c6dofimu2_accel_data_t::accel_y
int16_t accel_y
Definition: c6dofimu2.h:297
c6dofimu2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: c6dofimu2.h:274
c6dofimu2_s::read_f
c6dofimu2_master_io_t read_f
Definition: c6dofimu2.h:249
c6dofimu2_s::slave_address
uint8_t slave_address
Definition: c6dofimu2.h:246
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:307
c6dofimu2_cfg_t::cs
pin_name_t cs
Definition: c6dofimu2.h:266
c6dofimu2_cfg_t::miso
pin_name_t miso
Definition: c6dofimu2.h:263
c6dofimu2_s::chip_select
pin_name_t chip_select
Definition: c6dofimu2.h:247
c6dofimu2_s::cs
digital_out_t cs
Definition: c6dofimu2.h:233
c6dofimu2_gyro_data_t::gyro_z
int16_t gyro_z
Definition: c6dofimu2.h:309
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:224
c6dofimu2_s::write_f
c6dofimu2_master_io_t write_f
Definition: c6dofimu2.h:248
c6dofimu2_cfg_t::spi_speed
uint32_t spi_speed
Definition: c6dofimu2.h:276
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:270
c6dofimu2_s::i2c
i2c_master_t i2c
Definition: c6dofimu2.h:241
c6dofimu2_select_t
uint8_t c6dofimu2_select_t
Communication type.
Definition: c6dofimu2.h:219