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 
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 
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
67 #define GYRO_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
69  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
70  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
72 
78 #define GYRO_RETVAL uint8_t
79 
80 #define GYRO_OK 0x00
81 #define GYRO_INIT_ERROR 0xFF
82 
88 #define GYRO_L3GD20_I2C_ADDRESS 0x6B
89 #define GYRO_L3GD20_REGISTER_WHO_AM_I 0x0F
90 #define GYRO_L3GD20_REGISTER_CTRL_REG1 0x20
91 #define GYRO_L3GD20_REGISTER_CTRL_REG2 0x21
92 #define GYRO_L3GD20_REGISTER_CTRL_REG3 0x22
93 #define GYRO_L3GD20_REGISTER_CTRL_REG4 0x23
94 #define GYRO_L3GD20_REGISTER_CTRL_REG5 0x24
95 #define GYRO_L3GD20_REGISTER_REFERENCE 0x25
96 #define GYRO_L3GD20_REGISTER_OUT_TEMP 0x26
97 #define GYRO_L3GD20_REGISTER_STATUS_REG 0x27
98 #define GYRO_L3GD20_REGISTER_OUT_X_L 0x28
99 #define GYRO_L3GD20_REGISTER_OUT_X_H 0x29
100 #define GYRO_L3GD20_REGISTER_OUT_Y_L 0x2A
101 #define GYRO_L3GD20_REGISTER_OUT_Y_H 0x2B
102 #define GYRO_L3GD20_REGISTER_OUT_Z_L 0x2C
103 #define GYRO_L3GD20_REGISTER_OUT_Z_H 0x2D
104 #define GYRO_L3GD20_REGISTER_FIFO_CTRL_REG 0x2E
105 #define GYRO_L3GD20_REGISTER_FIFO_SRC_REG 0x2F
106 
112 #define GYRO_L3GD20_REGISTER_INT1_CFG 0x30
113 #define GYRO_L3GD20_REGISTER_INT1_SRC 0x31
114 #define GYRO_L3GD20_REGISTER_TSH_XH 0x32
115 #define GYRO_L3GD20_REGISTER_TSH_XL 0x33
116 #define GYRO_L3GD20_REGISTER_TSH_YH 0x34
117 #define GYRO_L3GD20_REGISTER_TSH_YL 0x35
118 #define GYRO_L3GD20_REGISTER_TSH_ZH 0x36
119 #define GYRO_L3GD20_REGISTER_TSH_ZL 0x37
120 #define GYRO_L3GD20_REGISTER_INT1_DURATION 0x38
121 
127 #define GYRO_L3GD20_REG1_NORMAL_MODE 0x0F
128 #define GYRO_L3GD20_REG4_250DPS 0x00
129 #define GYRO_L3GD20_REG5_FIFO_ENABLE_INT1 0x50
130 #define GYRO_L3GD20_FIFO_CTRL_REG_CONFIG 0x40
131  // End group macro
134 // --------------------------------------------------------------- PUBLIC TYPES
143 typedef struct
144 {
145  // Output pins
146 
147  digital_out_t rst;
148 
149  // Input pins
150 
151  digital_in_t int_pin;
152 
153  // Modules
154 
155  i2c_master_t i2c;
156 
157  // ctx variable
158 
159  uint8_t slave_address;
160 
161 } gyro_t;
162 
166 typedef struct
167 {
168  // Communication gpio pins
169 
170  pin_name_t scl;
171  pin_name_t sda;
172 
173  // Additional gpio pins
174 
175  pin_name_t rst;
176  pin_name_t int_pin;
177 
178  // static variable
179 
180  uint32_t i2c_speed;
181  uint8_t i2c_address;
182 
183 } gyro_cfg_t;
184  // End types group
186 
187 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
188 
194 #ifdef __cplusplus
195 extern "C"{
196 #endif
197 
207 
216 
224 void gyro_default_cfg ( gyro_t *ctx );
225 
238 void gyro_write_data ( gyro_t *ctx, uint8_t address, uint8_t write_command );
239 
251 uint8_t gyro_read_data ( gyro_t *ctx, uint8_t address );
252 
265 int16_t gyro_get_axis ( gyro_t *ctx, uint8_t adr_reg_low );
266 
279 void gyro_read_gyro ( gyro_t *ctx, int16_t *gyro_x, int16_t *gyro_y, int16_t *gyro_z );
280 
281 
282 #ifdef __cplusplus
283 }
284 #endif
285 #endif // GYRO_H
286  // End public_function group
289 
290 // ------------------------------------------------------------------------ END
gyro_cfg_t::sda
pin_name_t sda
Definition: gyro.h:171
gyro_cfg_t::int_pin
pin_name_t int_pin
Definition: gyro.h:176
gyro_cfg_t
Click configuration structure definition.
Definition: gyro.h:167
GYRO_RETVAL
#define GYRO_RETVAL
Definition: gyro.h:78
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:144
gyro_t::rst
digital_out_t rst
Definition: gyro.h:147
gyro_cfg_t::scl
pin_name_t scl
Definition: gyro.h:170
gyro_t::int_pin
digital_in_t int_pin
Definition: gyro.h:151
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:175
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:155
gyro_cfg_t::i2c_address
uint8_t i2c_address
Definition: gyro.h:181
gyro_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: gyro.h:180
gyro_t::slave_address
uint8_t slave_address
Definition: gyro.h:159