gyro2  2.0.0.0
gyro2.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 GYRO2_H
36 #define GYRO2_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 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define GYRO2_ADDR0 0x20
63 #define GYRO2_ADDR1 0x21
64 
69 #define GYRO2_STATUS 0x00
70 #define GYRO2_OUT_X_MSB 0x01
71 #define GYRO2_OUT_X_LSB 0x02
72 #define GYRO2_OUT_Y_MSB 0x03
73 #define GYRO2_OUT_Y_LSB 0x04
74 #define GYRO2_OUT_Z_MSB 0x05
75 #define GYRO2_OUT_Z_LSB 0x06
76 #define GYRO2_DR_STATUS 0x07
77 #define GYRO2_F_STATUS 0x08
78 #define GYRO2_F_SETUP 0x09
79 #define GYRO2_F_EVENT 0x0A
80 #define GYRO2_INT_SRC_FLAG 0x0B
81 #define GYRO2_WHO_AM_I 0x0C
82 #define GYRO2_CTRL_REG0 0x0D
83 #define GYRO2_CTRL_REG1 0x13
84 
89 #define GYRO2_SCALE_0 0
90 #define GYRO2_SCALE_1 1
91 #define GYRO2_SCALE_2 2
92 #define GYRO2_SCALE_3 3
93 
94 #define GYRO2_HI_PASS_OFF 0x00
95 #define GYRO2_HI_PASS_MOD0 0x04
96 #define GYRO2_HI_PASS_MOD1 0x0C
97 #define GYRO2_HI_PASS_MOD2 0x14
98 #define GYRO2_HI_PASS_MOD3 0x1C
99 
100 #define GYRO2_LO_PASS_OFF 0x00
101 #define GYRO2_LO_PASS_MOD0 0x20
102 #define GYRO2_LO_PASS_MOD1 0x60
103 #define GYRO2_LO_PASS_MOD2 0xE0
104 
109 #define GYRO2_RT_CFG 0x0E
110 
115 #define GYRO2_RT_CFG_ELE 0x08
116 #define GYRO2_RT_CFG_ZTEFE 0x04
117 #define GYRO2_RT_CFG_YTEFE 0x02
118 #define GYRO2_RT_CFG_XTEFE 0x01
119 
124 #define GYRO2_RT_SRC 0x0F
125 #define GYRO2_RT_THS 0x10
126 #define GYRO2_RT_COUNT 0x11
127 #define GYRO2_TEMP 0x12
128 
133 #define GYRO2_DR_800Hz 0
134 #define GYRO2_DR_400Hz 1
135 #define GYRO2_DR_200Hz 2
136 #define GYRO2_DR_100Hz 3
137 #define GYRO2_DR_50Hz 4
138 #define GYRO2_DR_25Hz 5
139 #define GYRO2_DR_12_5Hz 6
140 #define GYRO2_STANDBY 0
141 #define GYRO2_READY 1
142 #define GYRO2_ACTIVE 3
143 
148 #define GYRO2_CTRL_REG2 0x14
149 
154 #define GYRO2_INT_CFG_FIFO_INT1 0x80
155 #define GYRO2_INT_CFG_FIFO_INT2 0x00
156 #define GYRO2_INT_EN_FIFO 0x40
157 #define GYRO2_INT_DIS_FIFO 0x00
158 #define GYRO2_INT_CFG_RT_INT1 0x20
159 #define GYRO2_INT_CFG_RT_INT2 0x00
160 #define GYRO2_INT_EN_RT 0x10
161 #define GYRO2_DIS_EN_RT 0x00
162 #define GYRO2_INT_CFG_DRDY_INT1 0x08
163 #define GYRO2_INT_CFG_DRDY_INT2 0x00
164 #define GYRO2_INT_EN_DRDY 0x04
165 #define GYRO2_INT_DIS_DRDY 0x00
166 #define GYRO2_IPOL_ACTIVE_HI 0x02
167 #define GYRO2_IPOL_ACTIVE_LO 0x00
168 #define GYRO2_PP_OD_OS 0x01
169 #define GYRO2_PP_OD_PUSH_PULL 0x00
170 
175 #define GYRO2_CTRL_REG3 0x15
176 
182 #define GYRO2_MAP_MIKROBUS( cfg, mikrobus ) \
183  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
184  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
185  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
186  cfg.scs = MIKROBUS( mikrobus, MIKROBUS_AN ); \
187  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
188  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
189  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
190 
196 #define GYRO2_RETVAL uint8_t
197 
198 #define GYRO2_OK 0x00
199 #define GYRO2_INIT_ERROR 0xFF
200  // End group macro
203 // --------------------------------------------------------------- PUBLIC TYPES
212 typedef void ( *gyro2_master_io_t )( struct gyro2_s*, uint8_t, uint8_t*, uint8_t );
213 
214 typedef struct
215 {
216  uint8_t tmp_ctrl_reg0;
217  uint8_t tmp_ctrl_reg1;
218  uint8_t tmp_ctrl_reg2;
219  uint8_t tmp_ctrl_reg3;
220 
221 } ctrl_reg_t;
222 
223 
227 typedef struct gyro2_s
228 {
229  // Output pins
230 
231  digital_out_t scs;
232  digital_out_t rst;
233  digital_out_t rdy_pin;
234  digital_out_t cs;
235 
236  // Input pins
237 
238  digital_in_t rdy;
239  digital_in_t int_pin;
240 
241  // Modules
242 
243  i2c_master_t i2c;
244 
245  // ctx variable
246 
247  uint8_t slave_address;
248 
249  //ctrl register variables
250 
252 
254 
258 typedef struct
259 {
260  // Communication gpio pins
261 
262  pin_name_t scl;
263  pin_name_t sda;
264  pin_name_t cs;
265 
266  // Additional gpio pins
267 
268  pin_name_t scs;
269  pin_name_t rst;
270  pin_name_t rdy;
271  pin_name_t int_pin;
272  pin_name_t rdy_pin;
273 
274  // static variable
275 
276  uint32_t i2c_speed;
277  uint8_t i2c_address;
278 
279 } gyro2_cfg_t;
280  // End types group
282 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
288 #ifdef __cplusplus
289 extern "C"{
290 #endif
291 
301 
310 
318 uint8_t gyro2_default_cfg ( gyro2_t *ctx );
319 
330 void gyro2_generic_write ( gyro2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
331 
342 void gyro2_generic_read ( gyro2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
343 
352 void gyro2_scs_set ( gyro2_t *ctx, uint8_t val );
353 
362 void gyro2_rst_set ( gyro2_t *ctx, uint8_t val );
363 
376 void gyro2_cs_set ( gyro2_t *ctx, uint8_t val );
377 
386 void gyro2_rdy_set ( gyro2_t *ctx, uint8_t val );
387 
395 uint8_t gyro2_rdy_get ( gyro2_t *ctx );
396 
404 uint8_t gyro2_int_get ( gyro2_t *ctx );
405 
413 void gyro2_hw_reset ( gyro2_t *ctx );
414 
422 uint8_t gyro2_get_id ( gyro2_t *ctx );
423 
433 void gyro2_get_axisraw ( gyro2_t *ctx, int16_t *axis_data );
434 
442 void gyro2_calc_axis ( int16_t *axis_data );
443 
452 
460 void gyro2_sw_reset ( gyro2_t *ctx );
461 
470 void gyro2_set_dr ( gyro2_t *ctx, uint8_t rate );
471 
472 void gyro2_set_opmode ( gyro2_t *ctx, uint8_t opmode );
473 
484 void gyro2_interrupt_cfg ( gyro2_t *ctx, uint8_t int_route, uint8_t int_en, uint8_t polarity_outdrvsetting );
485 
494 void gyro2_interrupt_event_generation ( gyro2_t *ctx, uint8_t cfg );
495 
504 void gyro2_sensitivity ( gyro2_t *ctx, uint8_t s_setting );
505 
516 void gyro2_fr_setup ( gyro2_t *ctx, uint8_t low_pass, uint8_t hi_pass, uint8_t scale_set );
517 
518 #ifdef __cplusplu
519 
520 #endif
521 #endif // GYRO2_H
522  // End public_function group
525 
526 // ------------------------------------------------------------------------ END
gyro2_fr_setup
void gyro2_fr_setup(gyro2_t *ctx, uint8_t low_pass, uint8_t hi_pass, uint8_t scale_set)
Filter and scale of measurement functio.
gyro2_rdy_set
void gyro2_rdy_set(gyro2_t *ctx, uint8_t val)
Set ready pin function.
gyro2_rdy_get
uint8_t gyro2_rdy_get(gyro2_t *ctx)
Get ready pin function.
gyro2_scs_set
void gyro2_scs_set(gyro2_t *ctx, uint8_t val)
Set cs pin function.
gyro2_cfg_t::rdy
pin_name_t rdy
Definition: gyro2.h:270
gyro2_get_id
uint8_t gyro2_get_id(gyro2_t *ctx)
ID return function.
gyro2_master_io_t
void(* gyro2_master_io_t)(struct gyro2_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: gyro2.h:212
gyro2_hw_reset
void gyro2_hw_reset(gyro2_t *ctx)
Hardware reset function.
gyro2_cfg_t::i2c_address
uint8_t i2c_address
Definition: gyro2.h:277
gyro2_s::slave_address
uint8_t slave_address
Definition: gyro2.h:247
gyro2_t
struct gyro2_s gyro2_t
Click ctx object definition.
gyro2_s
Click ctx object definition.
Definition: gyro2.h:228
gyro2_cs_set
void gyro2_cs_set(gyro2_t *ctx, uint8_t val)
Comm interface function.
ctrl_reg_t::tmp_ctrl_reg0
uint8_t tmp_ctrl_reg0
Definition: gyro2.h:216
gyro2_cfg_t::rdy_pin
pin_name_t rdy_pin
Definition: gyro2.h:272
gyro2_cfg_t::int_pin
pin_name_t int_pin
Definition: gyro2.h:271
gyro2_sw_reset
void gyro2_sw_reset(gyro2_t *ctx)
Soft reset function.
gyro2_interrupt_cfg
void gyro2_interrupt_cfg(gyro2_t *ctx, uint8_t int_route, uint8_t int_en, uint8_t polarity_outdrvsetting)
Interrupt cfg function.
gyro2_generic_read
void gyro2_generic_read(gyro2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
ctrl_reg_t::tmp_ctrl_reg1
uint8_t tmp_ctrl_reg1
Definition: gyro2.h:217
gyro2_cfg_setup
void gyro2_cfg_setup(gyro2_cfg_t *cfg)
Config Object Initialization function.
gyro2_calc_axis
void gyro2_calc_axis(int16_t *axis_data)
Calc axis funtion.
gyro2_read_temperature
uint8_t gyro2_read_temperature(gyro2_t *ctx)
Temperature read function.
gyro2_s::rdy
digital_in_t rdy
Definition: gyro2.h:238
gyro2_init
GYRO2_RETVAL gyro2_init(gyro2_t *ctx, gyro2_cfg_t *cfg)
Initialization function.
gyro2_cfg_t::rst
pin_name_t rst
Definition: gyro2.h:269
gyro2_s::i2c
i2c_master_t i2c
Definition: gyro2.h:243
gyro2_s::rst
digital_out_t rst
Definition: gyro2.h:232
gyro2_set_opmode
void gyro2_set_opmode(gyro2_t *ctx, uint8_t opmode)
gyro2_int_get
uint8_t gyro2_int_get(gyro2_t *ctx)
Get int pin function.
gyro2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: gyro2.h:276
gyro2_interrupt_event_generation
void gyro2_interrupt_event_generation(gyro2_t *ctx, uint8_t cfg)
Int event generation function.
gyro2_s::int_pin
digital_in_t int_pin
Definition: gyro2.h:239
gyro2_default_cfg
uint8_t gyro2_default_cfg(gyro2_t *ctx)
Click Default Configuration function.
gyro2_s::rdy_pin
digital_out_t rdy_pin
Definition: gyro2.h:233
gyro2_generic_write
void gyro2_generic_write(gyro2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
gyro2_cfg_t::cs
pin_name_t cs
Definition: gyro2.h:264
gyro2_cfg_t::scs
pin_name_t scs
Definition: gyro2.h:268
ctrl_reg_t::tmp_ctrl_reg3
uint8_t tmp_ctrl_reg3
Definition: gyro2.h:219
gyro2_set_dr
void gyro2_set_dr(gyro2_t *ctx, uint8_t rate)
Data rate function.
ctrl_reg_t::tmp_ctrl_reg2
uint8_t tmp_ctrl_reg2
Definition: gyro2.h:218
GYRO2_RETVAL
#define GYRO2_RETVAL
Definition: gyro2.h:196
ctrl_reg_t
Definition: gyro2.h:215
gyro2_sensitivity
void gyro2_sensitivity(gyro2_t *ctx, uint8_t s_setting)
Sensitivity function.
gyro2_s::cs
digital_out_t cs
Definition: gyro2.h:234
gyro2_get_axisraw
void gyro2_get_axisraw(gyro2_t *ctx, int16_t *axis_data)
Axis read function.
gyro2_rst_set
void gyro2_rst_set(gyro2_t *ctx, uint8_t val)
Set rst pin function.
gyro2_s::scs
digital_out_t scs
Definition: gyro2.h:231
gyro2_cfg_t::scl
pin_name_t scl
Definition: gyro2.h:262
gyro2_cfg_t::sda
pin_name_t sda
Definition: gyro2.h:263
gyro2_cfg_t
Click configuration structure definition.
Definition: gyro2.h:259
gyro2_s::tmp_ctrl_reg
ctrl_reg_t tmp_ctrl_reg
Definition: gyro2.h:251