compass3  2.0.0.0
compass3.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 COMPASS3_H
36 #define COMPASS3_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_in.h"
49 #include "drv_i2c_master.h"
50 
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define COMPASS3_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
66 
72 #define COMPASS3_RETVAL uint8_t
73 
74 #define COMPASS3_OK 0x00
75 #define COMPASS3_INIT_ERROR 0xFF
76 
82 #define COMPASS3_SLAVE_ADR 0x30
83 
89 #define COMPASS3_XOUT_LSB 0x00
90 #define COMPASS3_XOUT_MSB 0x01
91 #define COMPASS3_YOUT_LSB 0x02
92 #define COMPASS3_YOUT_MSB 0x03
93 #define COMPASS3_ZOUT_LSB 0x04
94 #define COMPASS3_ZOUT_MSB 0x05
95 #define COMPASS3_TEMP 0x06
96 #define COMPASS3_STAT 0x07
97 #define COMPASS3_INTERNAL_CTRL_0 0x08
98 #define COMPASS3_INTERNAL_CTRL_1 0x09
99 #define COMPASS3_INTERNAL_CTRL_2 0x0A
100 #define COMPASS3_X_TRSH 0x0B
101 #define COMPASS3_Y_TRSH 0x0C
102 #define COMPASS3_Z_TRSH 0x0D
103 #define COMPASS3_DEVICE_ID 0x2F
104 
110 #define COMPASS3_MEAS_M_DONE 0x01
111 #define COMPASS3_MEAS_T_DONE 0x02
112 #define COMPASS3_MOTION_DCT 0x04
113 #define COMPASS3_PUMP_ON 0x08
114 #define COMPASS3_OTP_RD_DONE 0x10
115 
121 #define COMPASS3_TEST_PIN_SEL 0x80
122 #define COMPASS3_OTP_RD 0x40
123 #define COMPASS3_RSRVD 0x20
124 #define COMPASS3_RESET 0x10
125 #define COMPASS3_SET 0x08
126 #define COMPASS3_START_MD_T 0x04
127 #define COMPASS3_TM_T 0x02
128 #define COMPASS3_TM_M 0x01
129 
135 #define COMPASS3_SW_RST 0x80
136 #define COMPASS3_OTP_BL_SEL1 0x40
137 #define COMPASS3_OTP_BL_SEL0 0x20
138 #define COMPASS3_Z_INH 0x10
139 #define COMPASS3_Y_INH 0x08
140 #define COMPASS3_X_INH 0x04
141 #define COMPASS3_BW_ODR_600 0x03
142 #define COMPASS3_BW_ODR_400 0x02
143 #define COMPASS3_BW_ODR_200 0x01
144 #define COMPASS3_BW_ODR_100 0x00
145 
151 #define COMPASS3_ULP_SEL 0x80
152 #define COMPASS3_INT_MEAS_DONE_EN 0x40
153 #define COMPASS3_INT_MDT_EN 0x20
154 #define COMPASS3_Force_VDDL_STBY 0x10
155 #define COMPASS3_CM_FREQ_0_015625HZ 0x0A
156 #define COMPASS3_CM_FREQ_0_03125HZ 0x09
157 #define COMPASS3_CM_FREQ_0_0625HZ 0x08
158 #define COMPASS3_CM_FREQ_0_125HZ 0x07
159 #define COMPASS3_CM_FREQ_0_25HZ 0x06
160 #define COMPASS3_CM_FREQ_0_5HZ 0x05
161 #define COMPASS3_CM_FREQ_1HZ 0x04
162 #define COMPASS3_CM_FREQ_2_2HZ 0x03
163 #define COMPASS3_CM_FREQ_5HZ 0x02
164 #define COMPASS3_CM_FREQ_14HZ 0x01
165 #define COMPASS3_CM_FREQ_CONT 0x00
166 
172 #define COMPASS3_DYNAMIC_RANGE 16
173 #define COMPASS3_MAG_LSB_VALUE 0.25
174 #define COMPASS3_TEMP_LSB_VALUE 0.7
175 #define COMPASS3_MAG_RANGE_VALUE 8000
176 #define COMPASS3_TEMP_RANGE_VALUE 75
177 #define COMPASS3_RESOLUTION 65536
178 
184 #define COMPASS3_INTERRUPT 0x01
185 #define COMPASS3_NO_INTERRUPT 0x00
186 
192 #define COMPASS3_DEVICE_ID_NUM 0x0C
193  // End group macro
196 // --------------------------------------------------------------- PUBLIC TYPES
205 typedef struct
206 {
207  // Input pins
208 
209  digital_in_t int_pin;
210 
211  // Modules
212 
213  i2c_master_t i2c;
214 
215  // ctx variable
216 
217  uint8_t slave_address;
218 
219 } compass3_t;
220 
224 typedef struct
225 {
226  // Communication gpio pins
227 
228  pin_name_t scl;
229  pin_name_t sda;
230 
231  // Additional gpio pins
232 
233  pin_name_t int_pin;
234 
235  // static variable
236 
237  uint32_t i2c_speed;
238  uint8_t i2c_address;
239 
241  // End types group
243 // ------------------------------------------------------------------ CONSTANTS // End constants group
252 // ------------------------------------------------------------------ VARIABLES // End variable group
260 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
261 
267 #ifdef __cplusplus
268 extern "C"{
269 #endif
270 
281 
291 
300 
311 void compass3_generic_write ( compass3_t *ctx, uint8_t reg, uint8_t *data_buf,
312  uint8_t len );
313 
325 void compass3_generic_read ( compass3_t *ctx, uint8_t reg, uint8_t *data_buf,
326  uint8_t len );
327 
338 void compass3_full_measurement ( compass3_t *ctx, float *x_val, float *y_val,
339  float *z_val );
340 
351 
362 
363 #ifdef __cplusplus
364 }
365 #endif
366 #endif // _COMPASS3_H_
367  // End public_function group
370 
371 // ------------------------------------------------------------------------- END
compass3_t::int_pin
digital_in_t int_pin
Definition: compass3.h:209
compass3_cfg_t::sda
pin_name_t sda
Definition: compass3.h:229
compass3_cfg_t::i2c_address
uint8_t i2c_address
Definition: compass3.h:238
compass3_cfg_t
Click configuration structure definition.
Definition: compass3.h:225
compass3_t::slave_address
uint8_t slave_address
Definition: compass3.h:217
compass3_read_temp
float compass3_read_temp(compass3_t *ctx)
Read temperature function.
compass3_full_measurement
void compass3_full_measurement(compass3_t *ctx, float *x_val, float *y_val, float *z_val)
Full measurement function.
compass3_init
COMPASS3_RETVAL compass3_init(compass3_t *ctx, compass3_cfg_t *cfg)
Initialization function.
compass3_default_cfg
void compass3_default_cfg(compass3_t *ctx)
Click Default Configuration function.
compass3_cfg_setup
void compass3_cfg_setup(compass3_cfg_t *cfg)
Config Object Initialization function.
compass3_t
Click ctx object definition.
Definition: compass3.h:206
compass3_cfg_t::int_pin
pin_name_t int_pin
Definition: compass3.h:233
COMPASS3_RETVAL
#define COMPASS3_RETVAL
Definition: compass3.h:72
compass3_generic_write
void compass3_generic_write(compass3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
compass3_generic_read
void compass3_generic_read(compass3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
compass3_t::i2c
i2c_master_t i2c
Definition: compass3.h:213
compass3_cfg_t::scl
pin_name_t scl
Definition: compass3.h:228
compass3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: compass3.h:237
compass3_check_int
uint8_t compass3_check_int(compass3_t *ctx)
Check interrupt function.