gyro  2.0.0.0
gyro.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 GYRO_H
36 #define GYRO_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define GYRO_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
57  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
58 
64 #define GYRO_RETVAL uint8_t
65 
66 #define GYRO_OK 0x00
67 #define GYRO_INIT_ERROR 0xFF
68 
74 #define GYRO_L3GD20_I2C_ADDRESS 0x6B
75 #define GYRO_L3GD20_REGISTER_WHO_AM_I 0x0F
76 #define GYRO_L3GD20_REGISTER_CTRL_REG1 0x20
77 #define GYRO_L3GD20_REGISTER_CTRL_REG2 0x21
78 #define GYRO_L3GD20_REGISTER_CTRL_REG3 0x22
79 #define GYRO_L3GD20_REGISTER_CTRL_REG4 0x23
80 #define GYRO_L3GD20_REGISTER_CTRL_REG5 0x24
81 #define GYRO_L3GD20_REGISTER_REFERENCE 0x25
82 #define GYRO_L3GD20_REGISTER_OUT_TEMP 0x26
83 #define GYRO_L3GD20_REGISTER_STATUS_REG 0x27
84 #define GYRO_L3GD20_REGISTER_OUT_X_L 0x28
85 #define GYRO_L3GD20_REGISTER_OUT_X_H 0x29
86 #define GYRO_L3GD20_REGISTER_OUT_Y_L 0x2A
87 #define GYRO_L3GD20_REGISTER_OUT_Y_H 0x2B
88 #define GYRO_L3GD20_REGISTER_OUT_Z_L 0x2C
89 #define GYRO_L3GD20_REGISTER_OUT_Z_H 0x2D
90 #define GYRO_L3GD20_REGISTER_FIFO_CTRL_REG 0x2E
91 #define GYRO_L3GD20_REGISTER_FIFO_SRC_REG 0x2F
92 
98 #define GYRO_L3GD20_REGISTER_INT1_CFG 0x30
99 #define GYRO_L3GD20_REGISTER_INT1_SRC 0x31
100 #define GYRO_L3GD20_REGISTER_TSH_XH 0x32
101 #define GYRO_L3GD20_REGISTER_TSH_XL 0x33
102 #define GYRO_L3GD20_REGISTER_TSH_YH 0x34
103 #define GYRO_L3GD20_REGISTER_TSH_YL 0x35
104 #define GYRO_L3GD20_REGISTER_TSH_ZH 0x36
105 #define GYRO_L3GD20_REGISTER_TSH_ZL 0x37
106 #define GYRO_L3GD20_REGISTER_INT1_DURATION 0x38
107 
113 #define GYRO_L3GD20_REG1_NORMAL_MODE 0x0F
114 #define GYRO_L3GD20_REG4_250DPS 0x00
115 #define GYRO_L3GD20_REG5_FIFO_ENABLE_INT1 0x50
116 #define GYRO_L3GD20_FIFO_CTRL_REG_CONFIG 0x40
117  // End group macro
120 // --------------------------------------------------------------- PUBLIC TYPES
129 typedef struct
130 {
131  // Output pins
132 
133  digital_out_t rst;
134 
135  // Input pins
136 
137  digital_in_t int_pin;
138 
139  // Modules
140 
141  i2c_master_t i2c;
142 
143  // ctx variable
144 
145  uint8_t slave_address;
146 
147 } gyro_t;
148 
152 typedef struct
153 {
154  // Communication gpio pins
155 
156  pin_name_t scl;
157  pin_name_t sda;
158 
159  // Additional gpio pins
160 
161  pin_name_t rst;
162  pin_name_t int_pin;
163 
164  // static variable
165 
166  uint32_t i2c_speed;
167  uint8_t i2c_address;
168 
169 } gyro_cfg_t;
170  // End types group
172 
173 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
174 
180 #ifdef __cplusplus
181 extern "C"{
182 #endif
183 
193 
202 
210 void gyro_default_cfg ( gyro_t *ctx );
211 
224 void gyro_write_data ( gyro_t *ctx, uint8_t address, uint8_t write_command );
225 
237 uint8_t gyro_read_data ( gyro_t *ctx, uint8_t address );
238 
251 int16_t gyro_get_axis ( gyro_t *ctx, uint8_t adr_reg_low );
252 
265 void gyro_read_gyro ( gyro_t *ctx, int16_t *gyro_x, int16_t *gyro_y, int16_t *gyro_z );
266 
267 
268 #ifdef __cplusplus
269 }
270 #endif
271 #endif // GYRO_H
272  // End public_function group
275 
276 // ------------------------------------------------------------------------ END
gyro_cfg_t::sda
pin_name_t sda
Definition: gyro.h:157
gyro_cfg_t::int_pin
pin_name_t int_pin
Definition: gyro.h:162
gyro_cfg_t
Click configuration structure definition.
Definition: gyro.h:153
GYRO_RETVAL
#define GYRO_RETVAL
Definition: gyro.h:64
gyro_read_data
uint8_t gyro_read_data(gyro_t *ctx, uint8_t address)
Generic read data function.
gyro_write_data
void gyro_write_data(gyro_t *ctx, uint8_t address, uint8_t write_command)
Generic write data function.
gyro_cfg_setup
void gyro_cfg_setup(gyro_cfg_t *cfg)
Config Object Initialization function.
gyro_t
Click ctx object definition.
Definition: gyro.h:130
gyro_t::rst
digital_out_t rst
Definition: gyro.h:133
gyro_cfg_t::scl
pin_name_t scl
Definition: gyro.h:156
gyro_t::int_pin
digital_in_t int_pin
Definition: gyro.h:137
gyro_read_gyro
void gyro_read_gyro(gyro_t *ctx, int16_t *gyro_x, int16_t *gyro_y, int16_t *gyro_z)
Function read axis.
gyro_get_axis
int16_t gyro_get_axis(gyro_t *ctx, uint8_t adr_reg_low)
Function get low and high register data.
gyro_cfg_t::rst
pin_name_t rst
Definition: gyro.h:161
gyro_init
GYRO_RETVAL gyro_init(gyro_t *ctx, gyro_cfg_t *cfg)
Initialization function.
gyro_default_cfg
void gyro_default_cfg(gyro_t *ctx)
Click Default Configuration function.
gyro_t::i2c
i2c_master_t i2c
Definition: gyro.h:141
gyro_cfg_t::i2c_address
uint8_t i2c_address
Definition: gyro.h:167
gyro_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: gyro.h:166
gyro_t::slave_address
uint8_t slave_address
Definition: gyro.h:145