gyro5  2.0.0.0
gyro5.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 GYRO5_H
36 #define GYRO5_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 #include "drv_spi_master.h"
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define GYRO5_MAP_MIKROBUS( cfg, mikrobus ) \
55  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
56  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
57  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
58  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
59  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
60  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
61  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
62 
68 #define GYRO5_MASTER_I2C 0
69 #define GYRO5_MASTER_SPI 1
70 #define GYRO5_COM_I2C 0x01
71 #define GYRO5_COM_SPI 0x02
72 
78 #define GYRO5_RETVAL uint8_t
79 
80 #define GYRO5_OK 0x00
81 #define GYRO5_INIT_ERROR 0xFF
82 
88 #define GYRO5_I2C_DEV_ADR_GND 0x68
89 #define GYRO5_I2C_DEV_ADR_VCC 0x69
90 
96 #define GYRO5_SLEEP_DISABLE 0x00
97 #define GYRO5_SLEEP_ENABLE 0x01
98 
104 #define GYRO5_INT_DATA_RDY 0x00
105 #define GYRO5_INT_FSYNC 0x01
106 #define GYRO5_INT_FIFO_OVF 0x02
107 
113 #define GYRO5_REG_XG_OFFS_TC_H 0x04
114 #define GYRO5_REG_XG_OFFS_TC_L 0x05
115 #define GYRO5_REG_YG_OFFS_TC_H 0x07
116 #define GYRO5_REG_YG_OFFS_TC_L 0x08
117 #define GYRO5_REG_ZG_OFFS_TC_H 0x0A
118 #define GYRO5_REG_ZG_OFFS_TC_L 0x0B
119 #define GYRO5_REG_XG_OFFS_USRH 0x13
120 #define GYRO5_REG_XG_OFFS_USRL 0x14
121 #define GYRO5_REG_YG_OFFS_USRH 0x15
122 #define GYRO5_REG_YG_OFFS_USRL 0x16
123 #define GYRO5_REG_ZG_OFFS_USRH 0x17
124 #define GYRO5_REG_G_OFFS_USRL 0x18
125 #define GYRO5_REG_SMPLRT_DIV 0x19
126 #define GYRO5_REG_CONFIG 0x1A
127 #define GYRO5_REG_GYRO_CONFIG 0x1B
128 #define GYRO5_REG_FIFO_EN 0x23
129 #define GYRO5_REG_INT_PIN_CFG 0x37
130 #define GYRO5_REG_INT_ENABLE 0x38
131 #define GYRO5_REG_INT_STATUS 0x3A
132 #define GYRO5_REG_TEMP_OUT_H 0x41
133 #define GYRO5_REG_TEMP_OUT_L 0x42
134 #define GYRO5_REG_GYRO_XOUT_H 0x43
135 #define GYRO5_REG_GYRO_XOUT_L 0x44
136 #define GYRO5_REG_GYRO_YOUT_H 0x45
137 #define GYRO5_REG_GYRO_YOUT_L 0x46
138 #define GYRO5_REG_GYRO_ZOUT_H 0x47
139 #define GYRO5_REG_GYRO_ZOUT_L 0x48
140 #define GYRO5_REG_USER_CTRL 0x6A
141 #define GYRO5_REG_PWR_MGMT_1 0x6B
142 #define GYRO5_REG_PWR_MGMT_2 0x6C
143 #define GYRO5_REG_FIFO_COUNTH 0x72
144 #define GYRO5_REG_FIFO_COUNTL 0x73
145 #define GYRO5_REG_FIFO_R_W 0x74
146 #define GYRO5_REG_WHO_AM_I 0x75
147 
153 #define GYRO5_CFG_PWR_MGMT_1_DEV_RST 0x80
154 #define GYRO5_CFG_PWR_MGMT_1_SLEEP 0x40
155 #define GYRO5_CFG_PWR_MGMT_1_TEMP_DIS 0x10
156 #define GYRO5_CFG_PWR_MGMT_1_CLKSEL_EXT_OSC 0x06
157 #define GYRO5_CFG_PWR_MGMT_1_CLKSEL_PLL 0x01
158 #define GYRO5_CFG_PWR_MGMT_1_CLKSEL_INT_OSC 0x00
159 
165 #define GYRO5_CFG_PWR_MGMT_2_STBY_XG 0x04
166 #define GYRO5_CFG_PWR_MGMT_2_STBY_YG 0x02
167 #define GYRO5_CFG_PWR_MGMT_2_STBY_ZG 0x01
168 #define GYRO5_CFG_PWR_MGMT_2_DIS_ALL 0x00
169 
175 #define GYRO5_CFG_USER_CTRL_FIFO_EN 0x40
176 #define GYRO5_CFG_USER_CTRL_I2C_IF_DIS 0x10
177 #define GYRO5_CFG_USER_CTRL_FIFO_RESET 0x08
178 #define GYRO5_CFG_USER_CTRL_SIG_COND_RST 0x01
179 
185 #define GYRO5_CFG_SMPLRT_DIV_DEF 0x00
186 
192 #define GYRO5_CFG_CONFIG_EXT_SYNC_SET_GYRO_ZOUT_L 0x20
193 #define GYRO5_CFG_CONFIG_EXT_SYNC_SET_GYRO_YOUT_L 0x18
194 #define GYRO5_CFG_CONFIG_EXT_SYNC_SET_GYRO_XOUT_L 0x10
195 #define GYRO5_CFG_CONFIG_EXT_SYNC_SET_TEMP_OUT_L 0x08
196 #define GYRO5_CFG_CONFIG_EXT_SYNC_SET_IN_DIS 0x00
197 #define GYRO5_CFG_CONFIG_DEF 0x00
198 
204 #define GYRO5_CFG_GYRO_CONFIG_FS_SEL_4000 0x18
205 #define GYRO5_CFG_GYRO_CONFIG_FS_SEL_2000 0x10
206 #define GYRO5_CFG_GYRO_CONFIG_FS_SEL_1000 0x08
207 #define GYRO5_CFG_GYRO_CONFIG_FS_SEL_500 0x00
208 
214 #define GYRO5_CFG_FIFO_EN_TEMP 0x80
215 #define GYRO5_CFG_FIFO_EN_GYRO_X 0x40
216 #define GYRO5_CFG_FIFO_EN_GYRO_Y 0x20
217 #define GYRO5_CFG_FIFO_EN_GYRO_Z 0x10
218 #define GYRO5_CFG_FIFO_EN_DIS_ALL 0x00
219 
225 #define GYRO5_CFG_INT_PIN_CFG_INT_LEVEL 0x80
226 #define GYRO5_CFG_INT_PIN_CFG_INT_OPEN 0x40
227 #define GYRO5_CFG_INT_PIN_CFG_LATCH_INT_EN 0x20
228 #define GYRO5_CFG_INT_PIN_CFG_INT_RD_CLEAR 0x10
229 #define GYRO5_CFG_INT_PIN_CFG_FSYNC_INT_LEVEL 0x08
230 #define GYRO5_CFG_INT_PIN_CFG_FSYNC_INT_MODE_EN 0x04
231 #define GYRO5_CFG_INT_PIN_CFG_DIS_ALL 0x00
232 
238 #define GYRO5_CFG_INT_ENABLE_FIFO_OFLOW_EN 0x10
239 #define GYRO5_CFG_INT_ENABLE_FSYNC_INT_EN 0x08
240 #define GYRO5_CFG_INT_ENABLE_DATA_RDY_EN 0x01
241 
247 #define GYRO5_STATUS_INT_FIFO_OFLOW 0x10
248 #define GYRO5_STATUS_INT_FSYNC 0x08
249 #define GYRO5_STATUS_INT_DATA_RDY 0x01
250 #define GYRO5_STATUS_INT_NOT_DATA_RDY 0x00
251 
257 #define GYRO5_STATUS_INT_TYPES_SEL_FIFO_OFLOW 0x02
258 #define GYRO5_STATUS_INT_TYPES_INT_FSYNC 0x01
259 #define GYRO5_STATUS_INT_TYPES_INT_DATA_RDY 0x00
260 
266 #define GYRO5_SLEEP_STATUS_EN 0x01
267 #define GYRO5_SLEEP_STATUS_DIS 0x00
268  // End group macro
271 // --------------------------------------------------------------- PUBLIC TYPES
280 typedef uint8_t gyro5_select_t;
281 
285 typedef void ( *gyro5_master_io_t )( struct gyro5_s*, uint8_t, uint8_t*, uint8_t );
286 
290 typedef struct gyro5_s
291 {
292  // Output pins
293 
294  digital_out_t cs;
295 
296  // Input pins
297 
298  digital_in_t int_pin;
299 
300  // Modules
301 
302  i2c_master_t i2c;
303  spi_master_t spi;
304 
305  // ctx variable
306 
307  uint8_t slave_address;
308  pin_name_t chip_select;
312 
313 } gyro5_t;
314 
318 typedef struct
319 {
320  // Communication gpio pins
321 
322  pin_name_t scl;
323  pin_name_t sda;
324  pin_name_t miso;
325  pin_name_t mosi;
326  pin_name_t sck;
327  pin_name_t cs;
328 
329  pin_name_t int_pin;
330 
331  // static variable
332 
333  uint32_t i2c_speed;
334  uint8_t i2c_address;
335 
336  uint32_t spi_speed;
337  spi_master_mode_t spi_mode;
338  spi_master_chip_select_polarity_t cs_polarity;
339 
341 
342 } gyro5_cfg_t;
343  // End types group
345 
346 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
352 #ifdef __cplusplus
353 extern "C"{
354 #endif
355 
364 void gyro5_cfg_setup ( gyro5_cfg_t *cfg );
365 
375 
383 void gyro5_default_cfg ( gyro5_t *ctx );
384 
395 void gyro5_generic_write ( gyro5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
396 
407 void gyro5_generic_read ( gyro5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
408 
420 uint8_t gyro5_get_int ( gyro5_t *ctx );
421 
434 uint8_t gyro5_int_status ( gyro5_t *ctx, uint8_t int_type );
435 
445 void gyro5_get_temperature ( gyro5_t *ctx, float *temperature );
446 
458 void gyro5_get_axes ( gyro5_t *ctx, float *x_axis, float *y_axis, float *z_axis );
459 
470 void gyro5_sleep ( gyro5_t *ctx, uint8_t en_sleep );
471 
472 #ifdef __cplusplus
473 }
474 #endif
475 #endif // _GYRO5_H_
476  // End public_function group
479 
480 // ------------------------------------------------------------------------- END
void gyro5_cfg_setup(gyro5_cfg_t *cfg)
Config Object Initialization function.
Click configuration structure definition.
Definition: gyro5.h:318
digital_in_t int_pin
Definition: gyro5.h:298
gyro5_master_io_t read_f
Definition: gyro5.h:310
pin_name_t miso
Definition: gyro5.h:324
void gyro5_sleep(gyro5_t *ctx, uint8_t en_sleep)
Set sleep mode.
pin_name_t int_pin
Definition: gyro5.h:329
pin_name_t chip_select
Definition: gyro5.h:308
#define GYRO5_RETVAL
Definition: gyro5.h:78
Click ctx object definition.
Definition: gyro5.h:290
struct gyro5_s gyro5_t
Click ctx object definition.
pin_name_t mosi
Definition: gyro5.h:325
uint8_t i2c_address
Definition: gyro5.h:334
uint8_t gyro5_int_status(gyro5_t *ctx, uint8_t int_type)
Getting interrupt status.
void gyro5_get_axes(gyro5_t *ctx, float *x_axis, float *y_axis, float *z_axis)
Getting gyroscope axes values.
gyro5_select_t master_sel
Definition: gyro5.h:311
pin_name_t sda
Definition: gyro5.h:323
uint8_t slave_address
Definition: gyro5.h:307
pin_name_t sck
Definition: gyro5.h:326
digital_out_t cs
Definition: gyro5.h:294
spi_master_mode_t spi_mode
Definition: gyro5.h:337
gyro5_master_io_t write_f
Definition: gyro5.h:309
void(* gyro5_master_io_t)(struct gyro5_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: gyro5.h:285
uint32_t spi_speed
Definition: gyro5.h:336
void gyro5_generic_write(gyro5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
pin_name_t scl
Definition: gyro5.h:322
i2c_master_t i2c
Definition: gyro5.h:302
void gyro5_generic_read(gyro5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
spi_master_t spi
Definition: gyro5.h:303
gyro5_select_t sel
Definition: gyro5.h:340
uint8_t gyro5_select_t
Communication type.
Definition: gyro5.h:280
uint8_t gyro5_get_int(gyro5_t *ctx)
Getting INT pin state.
pin_name_t cs
Definition: gyro5.h:327
void gyro5_get_temperature(gyro5_t *ctx, float *temperature)
Getting temperature value.
GYRO5_RETVAL gyro5_init(gyro5_t *ctx, gyro5_cfg_t *cfg)
Initialization function.
uint32_t i2c_speed
Definition: gyro5.h:333
spi_master_chip_select_polarity_t cs_polarity
Definition: gyro5.h:338
void gyro5_default_cfg(gyro5_t *ctx)
Click Default Configuration function.