gyro3  2.0.0.0
gyro3.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 GYRO3_H
36 #define GYRO3_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 GYRO3_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.di2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
72  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
73 
79 #define GYRO3_MASTER_I2C 0
80 #define GYRO3_MASTER_SPI 1
81 
87 #define GYRO3_RETVAL uint8_t
88 
89 #define GYRO3_OK 0x00
90 #define GYRO3_INIT_ERROR 0xFF
91 
97 #define GYRO3_DEVICE_ADDR_SA0 0x68
98 #define GYRO3_DEVICE_ADDR_SA1 0x69
99 
105 #define GYRO3_MODE_POWER_DOWN 0x00
106 #define GYRO3_MODE_SLEEP 0x08
107 #define GYRO3_MODE_NORMAL 0x0F
108 
114 #define GYRO3_CTRL_REG1 0x20
115 #define GYRO3_CTRL_REG2 0x21
116 #define GYRO3_CTRL_REG3 0x22
117 #define GYRO3_CTRL_REG4 0x23
118 #define GYRO3_CTRL_REG5 0x24
119 
125 #define GYRO3_ODR_100_CUTOFF_12p5 0x00
126 #define GYRO3_ODR_100_CUTOFF_25 0x10
127 #define GYRO3_ODR_200_CUTOFF_12p5 0x40
128 #define GYRO3_ODR_200_CUTOFF_25 0x50
129 #define GYRO3_ODR_200_CUTOFF_50 0x60
130 #define GYRO3_ODR_200_CUTOFF_70 0x70
131 #define GYRO3_ODR_400_CUTOFF_20 0x80
132 #define GYRO3_ODR_400_CUTOFF_25 0x90
133 #define GYRO3_ODR_400_CUTOFF_50 0xa0
134 #define GYRO3_ODR_400_CUTOFF_110 0xb0
135 #define GYRO3_ODR_800_CUTOFF_30 0xc0
136 #define GYRO3_ODR_800_CUTOFF_35 0xd0
137 #define GYRO3_ODR_800_CUTOFF_50 0xe0
138 #define GYRO3_ODR_800_CUTOFF_110 0xf0
139 
145 #define GYRO3_FIFO_MODE_BYPASS 0x00
146 #define GYRO3_FIFO_EMPTY_INT_ON_INT2 0x01
147 #define GYRO3_FIFO_OVERRUN_INT_ON_INT2 0x02
148 #define GYRO3_FIFO_WATERMARK_INT_ON_INT2 0x04
149 #define GYRO3_FIFO_MODE_FIFO 0x20
150 #define GYRO3_FIFO_MODE_STREAM 0x40
151 #define GYRO3_FIFO_WTM_STATUS_MASK 0x80
152 #define GYRO3_FIFO_OVERRUN_MASK 0x40
153 #define GYRO3_FIFO_EMPTY_STATUS 0x20
154 
160 #define GYRO3_WHO_AM_I 0x0f
161 #define GYRO3_REF_DATACAP 0x25
162 #define GYRO3_OUT_TEMP 0x26
163 #define GYRO3_STATUS_REG 0x27
164 #define GYRO3_OUT_X_L 0x28
165 #define GYRO3_OUT_X_H 0x29
166 #define GYRO3_OUT_Y_L 0x2a
167 #define GYRO3_OUT_Y_H 0x2b
168 #define GYRO3_OUT_Z_L 0x2c
169 #define GYRO3_OUT_Z_H 0x2d
170 #define GYRO3_FIFO_CTRL 0x2e
171 #define GYRO3_FIFO_SRC 0x2f
172 #define GYRO3_INT1_CFG 0x30
173 #define GYRO3_INT1_SRC 0x31
174 #define GYRO3_INT1_THS_XH 0x32
175 #define GYRO3_INT1_THS_XL 0x33
176 #define GYRO3_INT1_THS_YH 0x34
177 #define GYRO3_INT1_THS_YL 0x35
178 #define GYRO3_INT1_THS_ZH 0x36
179 #define GYRO3_INT1_THS_ZL 0x37
180 #define GYRO3_INT1_DURATION 0x38
181 #define GYRO3_HPF_MODE_NORMAL 0x00
182 #define GYRO3_HPF_MODE_REF_SIGNAL 0x10
183 #define GYRO3_HPF_MODE_AUTO_RESET_ON_INT 0x30
184 #define GYRO3_HPF_CUTOFF_FREQ_8 0x00
185 #define GYRO3_HPF_CUTOFF_FREQ_4 0x01
186 #define GYRO3_HPF_CUTOFF_FREQ_2 0x02
187 #define GYRO3_HPF_CUTOFF_FREQ_1 0x03
188 #define GYRO3_HPF_CUTOFF_FREQ_0p5 0x04
189 #define GYRO3_HPF_CUTOFF_FREQ_0p2 0x05
190 #define GYRO3_HPF_CUTOFF_FREQ_0p1 0x06
191 #define GYRO3_HPF_CUTOFF_FREQ_0p05 0x07
192 #define GYRO3_HPF_CUTOFF_FREQ_0p02 0x08
193 #define GYRO3_HPF_CUTOFF_FREQ_0p01 0x09
194 #define GYRO3_INT_ON_INT1 0x80
195 #define GYRO3_BOOT_STATUS_ON_INT1 0x40
196 #define GYRO3_INT_ACTIVE_CFG_ON_INT1 0x20
197 #define GYRO3_PUSH_PULL 0x00
198 #define GYRO3_OPEN_DRAIN 0x10
199 #define GYRO3_DRDY_ON_INT2 0x08
200 #define GYRO3_BIG_ENDIAN 0x40
201 #define GYRO3_LITTLE_ENDIAN 0x00
202 #define GYRO3_FULL_SCACLE_245 0x00
203 #define GYRO3_FULL_SCACLE_500 0x10
204 #define GYRO3_FULL_SCACLE_1000 0x20
205 #define GYRO3_FULL_SCACLE_2000 0x30
206 #define GYRO3_SELF_TEST_NORMAL 0x00
207 #define GYRO3_SELF_TEST_0 0x02
208 #define GYRO3_SELF_TEST_1 0x06
209 #define GYRO3_SPI_MODE_4_WIRE 0x00
210 #define GYRO3_SPI_MODE_3_WIRE 0x01
211 #define GYRO3_REBOOT_MEMORY 0x80
212 #define GYRO3_FIFO_EN 0x40
213 #define GYRO3_HPF_EN 0x10
214 #define GYRO3_INT_SEL_00 0x00
215 #define GYRO3_INT_SEL_01 0x40
216 #define GYRO3_INT_SEL_10 0x80
217 #define GYRO3_OUT_SEL_00 0x00
218 #define GYRO3_OUT_SEL_01 0x01
219 #define GYRO3_OUT_SEL_10 0x02
220 #define GYRO3_ZYX_OVERRUN_MASK 0x80
221 #define GYRO3_Z_MASK 0x40
222 #define GYRO3_Y_MASK 0x20
223 #define GYRO3_X_MASK 0x10
224 #define GYRO3_ZYX_NEW_DATA_MASK 0x08
225 #define GYRO3_Z_NEW_DATA_MASK 0x04
226 #define GYRO3_Y_NEW_DATA_MASK 0x02
227 #define GYRO3_X_NEW_DATA_MASK 0x01
228 #define GYRO3_INT1_AND_OR 0x80
229 #define GYRO3_INT1_LATCH 0x40
230 #define GYRO3_INT1_ON_HIGH_Z 0x20
231 #define GYRO3_INT1_ON_LOW_Z 0x10
232 #define GYRO3_INT1_ON_HIGH_Y 0x08
233 #define GYRO3_INT1_ON_LOW_Y 0x04
234 #define GYRO3_INT1_ON_HIGH_X 0x02
235 #define GYRO3_INT1_ON_LOW_X 0x01
236 #define GYRO3_INT1_INT_ACTIVE_MASK 0x40
237 #define GYRO3_INT1_Z_HIGH_INT_MASK 0x20
238 #define GYRO3_INT1_Z_LOW_INT_MASK 0x10
239 #define GYRO3_INT1_Y_HIGH_INT_MASK 0x08
240 #define GYRO3_INT1_Y_LOW_INT_MASK 0x04
241 #define GYRO3_INT1_X_HIGH_INT_MASK 0x02
242 #define GYRO3_INT1_X_LOW_INT_MASK 0x01
243 #define GYRO3_WAIT_ENABLE 0x80
244 #define GYRO3_WAIT_DISABLE 0x00
245 #define GYRO3_MEAS_RANGE_245 0x01
246 #define GYRO3_MEAS_RANGE_500 0x02
247 #define GYRO3_MEAS_RANGE_2000 0x03
248  // End group macro
250 // --------------------------------------------------------------- PUBLIC TYPES
259 typedef uint8_t gyro3_select_t;
260 
264 typedef void ( *gyro3_master_io_t )( struct gyro3_s*, uint8_t, uint8_t*, uint8_t );
265 
269 typedef struct gyro3_s
270 {
271  // Output pins
272 
273  digital_out_t cs;
274 
275  // Input pins
276 
277  digital_in_t di2;
278  digital_in_t int_pin;
279 
280  // Modules
281 
282  i2c_master_t i2c;
283  spi_master_t spi;
284 
285  // ctx variable
286 
287  uint8_t slave_address;
288  pin_name_t chip_select;
293 
297 typedef struct
298 {
299  // Communication gpio pins
300 
301  pin_name_t scl;
302  pin_name_t sda;
303  pin_name_t miso;
304  pin_name_t mosi;
305  pin_name_t sck;
306  pin_name_t cs;
307 
308  // Additional gpio pins
309 
310  pin_name_t di2;
311  pin_name_t int_pin;
312 
313  // static variable
314 
315  uint32_t i2c_speed;
316  uint8_t i2c_address;
317 
318  uint32_t spi_speed;
319  spi_master_mode_t spi_mode;
320  spi_master_chip_select_polarity_t cs_polarity;
321 
323 
324 } gyro3_cfg_t;
325  // End types group
327 
328 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
334 #ifdef __cplusplus
335 extern "C"{
336 #endif
337 
347 
357 
366 
377 void gyro3_generic_write ( gyro3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
378 
389 void gyro3_generic_read ( gyro3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
390 
400 uint8_t gyro3_int_get ( gyro3_t *ctx );
401 
411 uint8_t gyro3_di2_get( gyro3_t *ctx );
412 
424 void gyro3_read ( gyro3_t *ctx, uint8_t register_address, uint8_t * read_buffer, uint8_t n_bytes );
425 
435 void gyro3_write ( gyro3_t *ctx, uint8_t register_address, uint8_t value );
436 
445 void gyro3_get_temp ( gyro3_t *ctx, uint8_t *temperature_value );
446 
459 void gyro3_get_axes ( gyro3_t *ctx, float *x_axis, float *y_axis, float *z_axis, uint8_t measurement_range );
460 
469 void gyro3_get_status ( gyro3_t *ctx, uint8_t * status_register );
470 
480 void gyro3_get_fifo_data_level ( gyro3_t *ctx, uint8_t * fifo_data_level );
481 
492 void gyro3_set_int1_thresholds ( gyro3_t *ctx, int16_t x_threshold, int16_t y_threshold, int16_t z_threshold );
493 
505 uint8_t gyro3_set_int1_duration ( gyro3_t *ctx, uint8_t wait_enable_bit, uint8_t duration_value );
506 
507 
508 #ifdef __cplusplus
509 }
510 #endif
511 #endif // _GYRO3_H_
512  // End public_function group
515 
516 // ------------------------------------------------------------------------- END
gyro3_select_t
uint8_t gyro3_select_t
Communication type.
Definition: gyro3.h:259
gyro3_s::read_f
gyro3_master_io_t read_f
Definition: gyro3.h:290
gyro3_cfg_t::di2
pin_name_t di2
Definition: gyro3.h:310
gyro3_cfg_t::i2c_address
uint8_t i2c_address
Definition: gyro3.h:316
gyro3_get_fifo_data_level
void gyro3_get_fifo_data_level(gyro3_t *ctx, uint8_t *fifo_data_level)
Get FIFO data level.
gyro3_int_get
uint8_t gyro3_int_get(gyro3_t *ctx)
Get INT pin state.
gyro3_cfg_t::spi_speed
uint32_t spi_speed
Definition: gyro3.h:318
gyro3_get_axes
void gyro3_get_axes(gyro3_t *ctx, float *x_axis, float *y_axis, float *z_axis, uint8_t measurement_range)
Get XYZ axes dps values.
gyro3_init
GYRO3_RETVAL gyro3_init(gyro3_t *ctx, gyro3_cfg_t *cfg)
Initialization function.
gyro3_cfg_t::miso
pin_name_t miso
Definition: gyro3.h:303
gyro3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: gyro3.h:315
gyro3_default_cfg
void gyro3_default_cfg(gyro3_t *ctx)
Click Default Configuration function.
gyro3_read
void gyro3_read(gyro3_t *ctx, uint8_t register_address, uint8_t *read_buffer, uint8_t n_bytes)
Reading register(s)
gyro3_cfg_t::sda
pin_name_t sda
Definition: gyro3.h:302
gyro3_cfg_t::sck
pin_name_t sck
Definition: gyro3.h:305
gyro3_generic_read
void gyro3_generic_read(gyro3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
gyro3_cfg_t::sel
gyro3_select_t sel
Definition: gyro3.h:322
gyro3_cfg_t::mosi
pin_name_t mosi
Definition: gyro3.h:304
gyro3_generic_write
void gyro3_generic_write(gyro3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
gyro3_t
struct gyro3_s gyro3_t
Click ctx object definition.
gyro3_s::i2c
i2c_master_t i2c
Definition: gyro3.h:282
gyro3_s::cs
digital_out_t cs
Definition: gyro3.h:273
gyro3_s
Click ctx object definition.
Definition: gyro3.h:270
gyro3_set_int1_duration
uint8_t gyro3_set_int1_duration(gyro3_t *ctx, uint8_t wait_enable_bit, uint8_t duration_value)
Setting interrupt duration time on INT1 pin.
gyro3_s::master_sel
gyro3_select_t master_sel
Definition: gyro3.h:291
gyro3_cfg_t::scl
pin_name_t scl
Definition: gyro3.h:301
gyro3_cfg_setup
void gyro3_cfg_setup(gyro3_cfg_t *cfg)
Config Object Initialization function.
GYRO3_RETVAL
#define GYRO3_RETVAL
Definition: gyro3.h:87
gyro3_cfg_t::cs
pin_name_t cs
Definition: gyro3.h:306
gyro3_s::slave_address
uint8_t slave_address
Definition: gyro3.h:287
gyro3_s::write_f
gyro3_master_io_t write_f
Definition: gyro3.h:289
gyro3_s::di2
digital_in_t di2
Definition: gyro3.h:277
gyro3_di2_get
uint8_t gyro3_di2_get(gyro3_t *ctx)
Get di2 (AN) pin state.
gyro3_master_io_t
void(* gyro3_master_io_t)(struct gyro3_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: gyro3.h:264
gyro3_s::spi
spi_master_t spi
Definition: gyro3.h:283
gyro3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: gyro3.h:320
gyro3_write
void gyro3_write(gyro3_t *ctx, uint8_t register_address, uint8_t value)
Write byte to regiter.
gyro3_s::chip_select
pin_name_t chip_select
Definition: gyro3.h:288
gyro3_get_temp
void gyro3_get_temp(gyro3_t *ctx, uint8_t *temperature_value)
Read value from temperature register.
gyro3_set_int1_thresholds
void gyro3_set_int1_thresholds(gyro3_t *ctx, int16_t x_threshold, int16_t y_threshold, int16_t z_threshold)
Setting values for interrupt thresholds.
gyro3_cfg_t::int_pin
pin_name_t int_pin
Definition: gyro3.h:311
gyro3_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: gyro3.h:319
gyro3_cfg_t
Click configuration structure definition.
Definition: gyro3.h:298
gyro3_s::int_pin
digital_in_t int_pin
Definition: gyro3.h:278
gyro3_get_status
void gyro3_get_status(gyro3_t *ctx, uint8_t *status_register)
Getting status register content.