compass4  2.0.0.0
compass4.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 COMPASS4_H
36 #define COMPASS4_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 COMPASS4_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.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
72  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
73 
79 #define COMPASS4_MASTER_I2C 0
80 #define COMPASS4_MASTER_SPI 1
81 
87 #define COMPASS4_RETVAL uint8_t
88 
89 #define COMPASS4_OK 0x00
90 #define COMPASS4_INIT_ERROR 0xFF
91 
97 #define COMPASS4_REG_COMPANY_ID 0x00 // Read only
98 #define COMPASS4_REG_DEVICE_ID 0x01 // Read only
99 #define COMPASS4_REG_INFO 0x03 // Read only
100 #define COMPASS4_REG_STATUS_1 0x10 // Read only
101 #define COMPASS4_REG_X_AXIS_LSB 0x11 // Read only
102 #define COMPASS4_REG_X_AXIS_MSB 0x12 // Read only
103 #define COMPASS4_REG_Y_AXIS_LSB 0x13 // Read only
104 #define COMPASS4_REG_Y_AXIS_MSB 0x14 // Read only
105 #define COMPASS4_REG_Z_AXIS_LSB 0x15 // Read only
106 #define COMPASS4_REG_Z_AXIS_MSB 0x16 // Read only
107 #define COMPASS4_REG_TMPS 0x17 // Read only
108 #define COMPASS4_REG_STATUS_2 0x18 // Read only
109 #define COMPASS4_REG_CONTROL_1 0x30 // Read and write
110 #define COMPASS4_REG_CONTROL_2 0x31 // Read and write
111 #define COMPASS4_REG_CONTROL_3 0x32 // Read and write
112 #define COMPASS4_REG_TS_1 0x33 // Read and write
113 #define COMPASS4_REG_TS_2 0x34 // Read and write
114 #define COMPASS4_REG_TS_3 0x35 // Read and write
115 #define COMPASS4_REG_I2C_DISABLE 0x36 // Read and write
116 #define COMPASS4_REG_TS_4 0x37 // Read and write
117 #define COMPASS4_REG_ASAX 0x60 // Read only
118 #define COMPASS4_REG_ASAY 0x61 // Read only
119 #define COMPASS4_REG_ASAZ 0x62 // Read only
120 #define COMPASS4_REG_TPH_1 0xC0 // Read and write
121 #define COMPASS4_REG_TPH_2 0xC1 // Read and write
122 #define COMPASS4_REG_RR 0xC2 // Read and write
123 #define COMPASS4_REG_SYT 0xC3 // Read and write
124 #define COMPASS4_REG_DT 0xC4 // Read and write
125 
131 #define COMPASS4_DEF_COMPANY_ID 0x48
132 #define COMPASS4_DEF_DEVICE_ID 0x10
133 
139 #define COMPASS4_CTRL1_WM_STEP_1 0x00
140 #define COMPASS4_CTRL1_WM_STEPS_2 0x01
141 #define COMPASS4_CTRL1_WM_STEPS_3 0x02
142 #define COMPASS4_CTRL1_WM_STEPS_4 0x03
143 #define COMPASS4_CTRL1_WM_STEPS_5 0x04
144 #define COMPASS4_CTRL1_WM_STEPS_6 0x05
145 #define COMPASS4_CTRL1_WM_STEPS_7 0x06
146 #define COMPASS4_CTRL1_WM_STEPS_8 0x07
147 #define COMPASS4_CTRL1_WM_STEPS_9 0x08
148 #define COMPASS4_CTRL1_WM_STEPS_10 0x09
149 #define COMPASS4_CTRL1_WM_STEPS_11 0x0A
150 #define COMPASS4_CTRL1_WM_STEPS_12 0x0B
151 #define COMPASS4_CTRL1_WM_STEPS_13 0x0C
152 #define COMPASS4_CTRL1_WM_STEPS_14 0x0D
153 #define COMPASS4_CTRL1_WM_STEPS_15 0x0E
154 #define COMPASS4_CTRL1_WM_STEPS_16 0x0F
155 #define COMPASS4_CTRL1_WM_STEPS_17 0x10
156 #define COMPASS4_CTRL1_WM_STEPS_18 0x11
157 #define COMPASS4_CTRL1_WM_STEPS_19 0x12
158 #define COMPASS4_CTRL1_WM_STEPS_20 0x13
159 #define COMPASS4_CTRL1_WM_STEPS_21 0x14
160 #define COMPASS4_CTRL1_WM_STEPS_22 0x15
161 #define COMPASS4_CTRL1_WM_STEPS_23 0x16
162 #define COMPASS4_CTRL1_WM_STEPS_24 0x17
163 #define COMPASS4_CTRL1_WM_STEPS_25 0x18
164 #define COMPASS4_CTRL1_WM_STEPS_26 0x19
165 #define COMPASS4_CTRL1_WM_STEPS_27 0x1A
166 #define COMPASS4_CTRL1_WM_STEPS_28 0x1B
167 #define COMPASS4_CTRL1_WM_STEPS_29 0x1C
168 #define COMPASS4_CTRL1_WM_STEPS_30 0x1D
169 #define COMPASS4_CTRL1_WM_STEPS_31 0x1E
170 #define COMPASS4_CTRL1_WM_STEPS_32 0x1F
171 #define COMPASS4_CTRL1_NOISE_DISABLE 0x00
172 #define COMPASS4_CTRL1_NOISE_ENABLE 0x20
173 
179 #define COMPASS4_CTRL2_MODE_POWER_DOWN 0x00
180 #define COMPASS4_CTRL2_MODE_SINGLE 0x01
181 #define COMPASS4_CTRL2_MODE_CONT_1 0x02
182 #define COMPASS4_CTRL2_MODE_CONT_2 0x04
183 #define COMPASS4_CTRL2_MODE_CONT_3 0x06
184 #define COMPASS4_CTRL2_MODE_CONT_4 0x08
185 #define COMPASS4_CTRL2_MODE_CONT_5 0x0A
186 #define COMPASS4_CTRL2_MODE_CONT_6 0x0C
187 #define COMPASS4_CTRL2_MODE_SELF_TEST 0x10
188 #define COMPASS4_CTRL2_SDR_LOW_POWER 0x00
189 #define COMPASS4_CTRL2_SDR_LOW_NOISE 0x40
190 #define COMPASS4_CTRL2_FIFO_ENABLE 0x80
191 #define COMPASS4_CTRL2_FIFO_DISABLE 0x00
192 
198 #define COMPASS4_CTRL3_SOFT_RESET 0x01
199 
205 #define COMPASS4_SLAVE_ADDRESS_GND_GND 0x0C
206 #define COMPASS4_SLAVE_ADDRESS_GND_VCC 0x0D
207 #define COMPASS4_SLAVE_ADDRESS_VCC_GND 0x0E
208 #define COMPASS4_SLAVE_ADDRESS_VCC_VCC 0x0F
209 
215 #define COMPASS4_GET_STATUS_1 0x01
216 #define COMPASS4_GET_STATUS_2 0x02
217 
223 #define COMPASS4_X_AXIS_DATA 0x11
224 #define COMPASS4_Y_AXIS_DATA 0x13
225 #define COMPASS4_Z_AXIS_DATA 0x15
226 
232 #define FLUX_CONSTANT 0.15
233  // End group macro
237 // --------------------------------------------------------------- PUBLIC TYPES
243 typedef struct
244 {
245  int16_t x;
246  int16_t y;
247  int16_t z;
248 
250 
251 typedef struct
252 {
253  float x;
254  float y;
255  float z;
256 
258 
262 typedef uint8_t compass4_select_t;
263 
267 typedef void ( *compass4_master_io_t )( struct compass4_s*, uint8_t, uint8_t*, uint8_t );
268 
272 typedef struct compass4_s
273 {
274  // Output pins
275  digital_out_t cs;
276 
277  digital_out_t rst;
278 
279  // Input pins
280 
281  digital_in_t int_pin;
282 
283  // Modules
284 
285  i2c_master_t i2c;
286  spi_master_t spi;
287 
288  // ctx variable
289 
290  uint8_t slave_address;
291  pin_name_t chip_select;
295 
297 
301 typedef struct
302 {
303  // Communication gpio pins
304 
305  pin_name_t scl;
306  pin_name_t sda;
307  pin_name_t miso;
308  pin_name_t mosi;
309  pin_name_t sck;
310  pin_name_t cs;
311 
312  // Additional gpio pins
313 
314  pin_name_t rst;
315  pin_name_t int_pin;
316 
317  // static variable
318 
319  uint32_t i2c_speed;
320  uint8_t i2c_address;
321 
322  uint32_t spi_speed;
323  spi_master_mode_t spi_mode;
324  spi_master_chip_select_polarity_t cs_polarity;
325 
327 
329  // End types group
331 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
337 #ifdef __cplusplus
338 extern "C"{
339 #endif
340 
350 
359 
370 void compass4_generic_write ( compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
371 
382 void compass4_generic_read ( compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
383 
392 
400 void compass4_set_rst_pin_state ( compass4_t *ctx, uint8_t state );
401 
410 
420 void compass4_configuration ( compass4_t *ctx, uint8_t cfg1, uint8_t cfg2 );
421 
431 uint8_t compass4_get_status( compass4_t *ctx, uint8_t status );
432 
444 
455 
466 
477 uint8_t compass4_get_single_axis ( compass4_t *ctx, uint8_t axis_reg, int16_t *axis_data );
478 
489 
500 uint8_t compass4_get_single_magnetic_flux ( compass4_t *ctx, uint8_t axis, float *flux_data );
501 
502 #ifdef __cplusplus
503 }
504 #endif
505 #endif // _COMPASS4_H_
506  // End public_function group
509 
510 // ------------------------------------------------------------------------- END
compass4_flux_t::y
float y
Definition: compass4.h:254
compass4_init
COMPASS4_RETVAL compass4_init(compass4_t *ctx, compass4_cfg_t *cfg)
Initialization function.
compass4_generic_read
void compass4_generic_read(compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
compass4_axis_t::x
int16_t x
Definition: compass4.h:245
compass4_s
Click ctx object definition.
Definition: compass4.h:273
compass4_get_status
uint8_t compass4_get_status(compass4_t *ctx, uint8_t status)
Get status.
compass4_cfg_t::sck
pin_name_t sck
Definition: compass4.h:309
compass4_s::i2c
i2c_master_t i2c
Definition: compass4.h:285
compass4_get_axis
uint8_t compass4_get_axis(compass4_t *ctx, compass4_axis_t *axis)
Gets X\Y\Z Axis value.
compass4_cfg_t
Click configuration structure definition.
Definition: compass4.h:302
compass4_cfg_setup
void compass4_cfg_setup(compass4_cfg_t *cfg)
Config Object Initialization function.
compass4_get_single_axis
uint8_t compass4_get_single_axis(compass4_t *ctx, uint8_t axis_reg, int16_t *axis_data)
Gets single axis value.
compass4_master_io_t
void(* compass4_master_io_t)(struct compass4_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: compass4.h:267
compass4_axis_t::y
int16_t y
Definition: compass4.h:246
compass4_s::int_pin
digital_in_t int_pin
Definition: compass4.h:281
compass4_set_rst_pin_state
void compass4_set_rst_pin_state(compass4_t *ctx, uint8_t state)
Sets RST pin state.
compass4_software_reset
void compass4_software_reset(compass4_t *ctx)
Software reset function.
compass4_cfg_t::i2c_address
uint8_t i2c_address
Definition: compass4.h:320
compass4_s::spi
spi_master_t spi
Definition: compass4.h:286
compass4_select_t
uint8_t compass4_select_t
Communication type.
Definition: compass4.h:262
compass4_cfg_t::miso
pin_name_t miso
Definition: compass4.h:307
compass4_cfg_t::mosi
pin_name_t mosi
Definition: compass4.h:308
compass4_flux_t::z
float z
Definition: compass4.h:255
compass4_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: compass4.h:324
compass4_s::chip_select
pin_name_t chip_select
Definition: compass4.h:291
compass4_cfg_t::spi_speed
uint32_t spi_speed
Definition: compass4.h:322
COMPASS4_RETVAL
#define COMPASS4_RETVAL
Definition: compass4.h:87
compass4_cfg_t::cs
pin_name_t cs
Definition: compass4.h:310
compass4_cfg_t::int_pin
pin_name_t int_pin
Definition: compass4.h:315
compass4_check_device
uint8_t compass4_check_device(compass4_t *ctx)
Check device function.
compass4_flux_t
Definition: compass4.h:252
compass4_get_magnetic_flux
uint8_t compass4_get_magnetic_flux(compass4_t *ctx, compass4_flux_t *flux)
Gets magnetic flux of X\Y\Z axis value.
compass4_s::cs
digital_out_t cs
Definition: compass4.h:275
compass4_get_interrupt
uint8_t compass4_get_interrupt(compass4_t *ctx)
Gets INT pin state (DRDY pin)
compass4_t
struct compass4_s compass4_t
Click ctx object definition.
compass4_s::slave_address
uint8_t slave_address
Definition: compass4.h:290
compass4_configuration
void compass4_configuration(compass4_t *ctx, uint8_t cfg1, uint8_t cfg2)
Configuration function.
compass4_s::write_f
compass4_master_io_t write_f
Definition: compass4.h:292
compass4_axis_t
Definition: compass4.h:244
compass4_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: compass4.h:319
compass4_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: compass4.h:323
compass4_hardware_reset
void compass4_hardware_reset(compass4_t *ctx)
Hardware reset function.
compass4_s::read_f
compass4_master_io_t read_f
Definition: compass4.h:293
compass4_cfg_t::sel
compass4_select_t sel
Definition: compass4.h:326
compass4_s::master_sel
compass4_select_t master_sel
Definition: compass4.h:294
compass4_cfg_t::sda
pin_name_t sda
Definition: compass4.h:306
compass4_cfg_t::rst
pin_name_t rst
Definition: compass4.h:314
compass4_get_single_magnetic_flux
uint8_t compass4_get_single_magnetic_flux(compass4_t *ctx, uint8_t axis, float *flux_data)
Gets single axis flux value.
compass4_flux_t::x
float x
Definition: compass4.h:253
compass4_cfg_t::scl
pin_name_t scl
Definition: compass4.h:305
compass4_generic_write
void compass4_generic_write(compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
compass4_axis_t::z
int16_t z
Definition: compass4.h:247
compass4_s::rst
digital_out_t rst
Definition: compass4.h:277