compass  2.0.0.0
compass.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright (c) 2019, MikroElektronika - www.mikroe.com
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * 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 
43 // -------------------------------------------------------------- PUBLIC MACROS
48 #define COMPASS_CTRL_REG1 0x20
49 #define COMPASS_CTRL_REG2 0x21
50 #define COMPASS_CTRL_REG3 0x22
51 #define COMPASS_CTRL_REG4 0x23
52 #define COMPASS_CTRL_REG5 0x24
53 #define COMPASS_CTRL_REG6 0x25
54 #define COMPASS_FIFO_CTRL_REG 0x2E
55 #define COMPASS_STATUS_REG 0x27
56 #define COMPASS_INT1_CFG 0x30
57 #define COMPASS_INT2_CFG 0x34
58 
63 #define COMPASS_CRA_REG 0x00
64 #define COMPASS_CRB_REG 0x01
65 #define COMPASS_MR_REG 0x02
66 #define COMPASS_SR_REG 0x09
67 
68 #define COMPASS_IRA_REG 0x0A
69 #define COMPASS_IRB_REG 0x0B
70 #define COMPASS_IRC_REG 0x0C
71 
72 #define COMPASS_OUT_MAGNET_X_HIGH 0x03
73 #define COMPASS_OUT_MAGNET_X_LOW 0x04
74 #define COMPASS_OUT_MAGNET_Z_HIGH 0x05
75 #define COMPASS_OUT_MAGNET_Z_LOW 0x06
76 #define COMPASS_OUT_MAGNET_Y_HIGH 0x07
77 #define COMPASS_OUT_MAGNET_Y_LOW 0x08
78 
79 #define COMPASS_TEMP_OUT_HIGH 0x31
80 #define COMPASS_TEMP_OUT_LOW 0x32
81 
86 #define COMPASS_OUT_ACCEL_X_LOW 0x28
87 #define COMPASS_OUT_ACCEL_X_HIGH 0x29
88 #define COMPASS_OUT_ACCEL_Y_LOW 0x2A
89 #define COMPASS_OUT_ACCEL_Y_HIGH 0x2B
90 #define COMPASS_OUT_ACCEL_Z_LOW 0x2C
91 #define COMPASS_OUT_ACCEL_Z_HIGH 0x2D
92 
97 #define COMPASS_MAP_MIKROBUS( cfg, mikrobus ) \
98  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
99  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
100  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_RST ); \
101  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
102 
108 #define COMPASS_RETVAL uint8_t
109 
110 #define COMPASS_OK 0x00
111 #define COMPASS_INIT_ERROR 0xFF
112  // End group macro
115 // --------------------------------------------------------------- PUBLIC TYPES
124 typedef struct
125 {
126  // Input pins
127 
128  digital_in_t rdy;
129  digital_in_t int_pin;
130 
131  // Modules
132 
133  i2c_master_t i2c;
134 
135  // ctx variable
136 
137  hal_i2c_address_t accel_slave_address;
138  hal_i2c_address_t magnet_slave_address;
139 
140 } compass_t;
141 
145 typedef struct
146 {
147  // Communication gpio pins
148 
149  pin_name_t scl;
150  pin_name_t sda;
151 
152  // Additional gpio pins
153 
154  pin_name_t rdy;
155  pin_name_t int_pin;
156 
157  // static variable
158 
159  hal_i2c_speed_t i2c_speed;
160  hal_i2c_address_t i2c_accel_address;
161  hal_i2c_address_t i2c_magnet_address;
162 
163 } compass_cfg_t;
164  // End types group
166 
167 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
168 
174 #ifdef __cplusplus
175 extern "C"{
176 #endif
177 
186 void compass_cfg_setup ( compass_cfg_t *cfg );
187 
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 
232 void compass_accel_generic_read ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
233 
245 void compass_magnet_generic_read ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
246 
255 void compass_default_config ( compass_t *ctx );
256 
268 void compass_read_accel_axis ( compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis );
269 
281 void compass_read_magnet_axis ( compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis );
282 
283 #ifdef __cplusplus
284 }
285 #endif
286 #endif // _COMPASS_H_
287  // End public_function group
290 
291 // ------------------------------------------------------------------------- END
compass_default_config
void compass_default_config(compass_t *ctx)
Default config function.
compass_cfg_t::int_pin
pin_name_t int_pin
Definition: compass.h:155
compass_magnet_generic_write
void compass_magnet_generic_write(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic magnet write function.
compass_accel_generic_read
void compass_accel_generic_read(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic accel read function.
compass_cfg_t::i2c_accel_address
hal_i2c_address_t i2c_accel_address
Definition: compass.h:160
compass_t::i2c
i2c_master_t i2c
Definition: compass.h:133
compass_cfg_t::rdy
pin_name_t rdy
Definition: compass.h:154
compass_t::int_pin
digital_in_t int_pin
Definition: compass.h:129
compass_t::rdy
digital_in_t rdy
Definition: compass.h:128
compass_cfg_t::scl
pin_name_t scl
Definition: compass.h:149
compass_magnet_generic_read
void compass_magnet_generic_read(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic magnet read function.
compass_cfg_setup
void compass_cfg_setup(compass_cfg_t *cfg)
Config Object Initialization function.
compass_init
COMPASS_RETVAL compass_init(compass_t *ctx, compass_cfg_t *cfg)
Initialization function.
compass_t
Click ctx object definition.
Definition: compass.h:124
compass_t::accel_slave_address
hal_i2c_address_t accel_slave_address
Definition: compass.h:137
compass_read_magnet_axis
void compass_read_magnet_axis(compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis)
Magnet axes read function.
compass_cfg_t::i2c_magnet_address
hal_i2c_address_t i2c_magnet_address
Definition: compass.h:161
compass_read_accel_axis
void compass_read_accel_axis(compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis)
Accel axes read function.
COMPASS_RETVAL
#define COMPASS_RETVAL
Definition: compass.h:108
compass_cfg_t::sda
pin_name_t sda
Definition: compass.h:150
compass_cfg_t
Click configuration structure definition.
Definition: compass.h:145
compass_accel_generic_write
void compass_accel_generic_write(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic accel write function.
compass_t::magnet_slave_address
hal_i2c_address_t magnet_slave_address
Definition: compass.h:138
compass_cfg_t::i2c_speed
hal_i2c_speed_t i2c_speed
Definition: compass.h:159