compass  2.0.0.0
compass.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 COMPASS_H
36 #define COMPASS_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
47 #define COMPASS_CTRL_REG1 0x20
48 #define COMPASS_CTRL_REG2 0x21
49 #define COMPASS_CTRL_REG3 0x22
50 #define COMPASS_CTRL_REG4 0x23
51 #define COMPASS_CTRL_REG5 0x24
52 #define COMPASS_CTRL_REG6 0x25
53 #define COMPASS_FIFO_CTRL_REG 0x2E
54 #define COMPASS_STATUS_REG 0x27
55 #define COMPASS_INT1_CFG 0x30
56 #define COMPASS_INT2_CFG 0x34
57 
62 #define COMPASS_CRA_REG 0x00
63 #define COMPASS_CRB_REG 0x01
64 #define COMPASS_MR_REG 0x02
65 #define COMPASS_SR_REG 0x09
66 
67 #define COMPASS_IRA_REG 0x0A
68 #define COMPASS_IRB_REG 0x0B
69 #define COMPASS_IRC_REG 0x0C
70 
71 #define COMPASS_OUT_MAGNET_X_HIGH 0x03
72 #define COMPASS_OUT_MAGNET_X_LOW 0x04
73 #define COMPASS_OUT_MAGNET_Z_HIGH 0x05
74 #define COMPASS_OUT_MAGNET_Z_LOW 0x06
75 #define COMPASS_OUT_MAGNET_Y_HIGH 0x07
76 #define COMPASS_OUT_MAGNET_Y_LOW 0x08
77 
78 #define COMPASS_TEMP_OUT_HIGH 0x31
79 #define COMPASS_TEMP_OUT_LOW 0x32
80 
85 #define COMPASS_OUT_ACCEL_X_LOW 0x28
86 #define COMPASS_OUT_ACCEL_X_HIGH 0x29
87 #define COMPASS_OUT_ACCEL_Y_LOW 0x2A
88 #define COMPASS_OUT_ACCEL_Y_HIGH 0x2B
89 #define COMPASS_OUT_ACCEL_Z_LOW 0x2C
90 #define COMPASS_OUT_ACCEL_Z_HIGH 0x2D
91 
96 #define COMPASS_MAP_MIKROBUS( cfg, mikrobus ) \
97  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
98  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
99  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_RST ); \
100  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
101 
107 #define COMPASS_RETVAL uint8_t
108 
109 #define COMPASS_OK 0x00
110 #define COMPASS_INIT_ERROR 0xFF
111  // End group macro
114 // --------------------------------------------------------------- PUBLIC TYPES
123 typedef struct
124 {
125  // Input pins
126 
127  digital_in_t rdy;
128  digital_in_t int_pin;
129 
130  // Modules
131 
132  i2c_master_t i2c;
133 
134  // ctx variable
135 
138 
139 } compass_t;
140 
144 typedef struct
145 {
146  // Communication gpio pins
147 
148  pin_name_t scl;
149  pin_name_t sda;
150 
151  // Additional gpio pins
152 
153  pin_name_t rdy;
154  pin_name_t int_pin;
155 
156  // static variable
157 
158  uint32_t i2c_speed;
161 
162 } compass_cfg_t;
163  // End types group
165 
166 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
167 
173 #ifdef __cplusplus
174 extern "C"{
175 #endif
176 
185 void compass_cfg_setup ( compass_cfg_t *cfg );
186 
196 
207 void compass_accel_generic_write ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
208 
219 void compass_magnet_generic_write ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
220 
231 void compass_accel_generic_read ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
232 
243 void compass_magnet_generic_read ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
244 
252 void compass_default_config ( compass_t *ctx );
253 
264 void compass_read_accel_axis ( compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis );
265 
276 void compass_read_magnet_axis ( compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis );
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 #endif // _COMPASS_H_
282  // End public_function group
285 
286 // ------------------------------------------------------------------------- END
void compass_default_config(compass_t *ctx)
Default config function.
i2c_master_t i2c
Definition: compass.h:132
void compass_read_magnet_axis(compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis)
Magnet axes read function.
pin_name_t int_pin
Definition: compass.h:154
pin_name_t rdy
Definition: compass.h:153
void compass_magnet_generic_write(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic magnet write function.
digital_in_t int_pin
Definition: compass.h:128
Click configuration structure definition.
Definition: compass.h:144
void compass_accel_generic_read(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic accel read function.
void compass_cfg_setup(compass_cfg_t *cfg)
Config Object Initialization function.
pin_name_t scl
Definition: compass.h:148
Click ctx object definition.
Definition: compass.h:123
uint32_t i2c_speed
Definition: compass.h:158
uint8_t i2c_magnet_address
Definition: compass.h:160
void compass_read_accel_axis(compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis)
Accel axes read function.
digital_in_t rdy
Definition: compass.h:127
uint8_t accel_slave_address
Definition: compass.h:136
void compass_accel_generic_write(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic accel write function.
pin_name_t sda
Definition: compass.h:149
#define COMPASS_RETVAL
Definition: compass.h:107
COMPASS_RETVAL compass_init(compass_t *ctx, compass_cfg_t *cfg)
Initialization function.
uint8_t i2c_accel_address
Definition: compass.h:159
void compass_magnet_generic_read(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic magnet read function.
uint8_t magnet_slave_address
Definition: compass.h:137