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 "drv_digital_out.h"
39#include "drv_digital_in.h"
40#include "drv_i2c_master.h"
41#include "drv_spi_master.h"
42
43// -------------------------------------------------------------- PUBLIC MACROS
54#define COMPASS4_MAP_MIKROBUS( cfg, mikrobus ) \
55 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
56 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
57 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
58 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
59 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
60 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
61 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
62 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
69#define COMPASS4_MASTER_I2C 0
70#define COMPASS4_MASTER_SPI 1
77#define COMPASS4_RETVAL uint8_t
78
79#define COMPASS4_OK 0x00
80#define COMPASS4_INIT_ERROR 0xFF
87#define COMPASS4_REG_COMPANY_ID 0x00 // Read only
88#define COMPASS4_REG_DEVICE_ID 0x01 // Read only
89#define COMPASS4_REG_INFO 0x03 // Read only
90#define COMPASS4_REG_STATUS_1 0x10 // Read only
91#define COMPASS4_REG_X_AXIS_LSB 0x11 // Read only
92#define COMPASS4_REG_X_AXIS_MSB 0x12 // Read only
93#define COMPASS4_REG_Y_AXIS_LSB 0x13 // Read only
94#define COMPASS4_REG_Y_AXIS_MSB 0x14 // Read only
95#define COMPASS4_REG_Z_AXIS_LSB 0x15 // Read only
96#define COMPASS4_REG_Z_AXIS_MSB 0x16 // Read only
97#define COMPASS4_REG_TMPS 0x17 // Read only
98#define COMPASS4_REG_STATUS_2 0x18 // Read only
99#define COMPASS4_REG_CONTROL_1 0x30 // Read and write
100#define COMPASS4_REG_CONTROL_2 0x31 // Read and write
101#define COMPASS4_REG_CONTROL_3 0x32 // Read and write
102#define COMPASS4_REG_TS_1 0x33 // Read and write
103#define COMPASS4_REG_TS_2 0x34 // Read and write
104#define COMPASS4_REG_TS_3 0x35 // Read and write
105#define COMPASS4_REG_I2C_DISABLE 0x36 // Read and write
106#define COMPASS4_REG_TS_4 0x37 // Read and write
107#define COMPASS4_REG_ASAX 0x60 // Read only
108#define COMPASS4_REG_ASAY 0x61 // Read only
109#define COMPASS4_REG_ASAZ 0x62 // Read only
110#define COMPASS4_REG_TPH_1 0xC0 // Read and write
111#define COMPASS4_REG_TPH_2 0xC1 // Read and write
112#define COMPASS4_REG_RR 0xC2 // Read and write
113#define COMPASS4_REG_SYT 0xC3 // Read and write
114#define COMPASS4_REG_DT 0xC4 // Read and write
121#define COMPASS4_DEF_COMPANY_ID 0x48
122#define COMPASS4_DEF_DEVICE_ID 0x10
129#define COMPASS4_CTRL1_WM_STEP_1 0x00
130#define COMPASS4_CTRL1_WM_STEPS_2 0x01
131#define COMPASS4_CTRL1_WM_STEPS_3 0x02
132#define COMPASS4_CTRL1_WM_STEPS_4 0x03
133#define COMPASS4_CTRL1_WM_STEPS_5 0x04
134#define COMPASS4_CTRL1_WM_STEPS_6 0x05
135#define COMPASS4_CTRL1_WM_STEPS_7 0x06
136#define COMPASS4_CTRL1_WM_STEPS_8 0x07
137#define COMPASS4_CTRL1_WM_STEPS_9 0x08
138#define COMPASS4_CTRL1_WM_STEPS_10 0x09
139#define COMPASS4_CTRL1_WM_STEPS_11 0x0A
140#define COMPASS4_CTRL1_WM_STEPS_12 0x0B
141#define COMPASS4_CTRL1_WM_STEPS_13 0x0C
142#define COMPASS4_CTRL1_WM_STEPS_14 0x0D
143#define COMPASS4_CTRL1_WM_STEPS_15 0x0E
144#define COMPASS4_CTRL1_WM_STEPS_16 0x0F
145#define COMPASS4_CTRL1_WM_STEPS_17 0x10
146#define COMPASS4_CTRL1_WM_STEPS_18 0x11
147#define COMPASS4_CTRL1_WM_STEPS_19 0x12
148#define COMPASS4_CTRL1_WM_STEPS_20 0x13
149#define COMPASS4_CTRL1_WM_STEPS_21 0x14
150#define COMPASS4_CTRL1_WM_STEPS_22 0x15
151#define COMPASS4_CTRL1_WM_STEPS_23 0x16
152#define COMPASS4_CTRL1_WM_STEPS_24 0x17
153#define COMPASS4_CTRL1_WM_STEPS_25 0x18
154#define COMPASS4_CTRL1_WM_STEPS_26 0x19
155#define COMPASS4_CTRL1_WM_STEPS_27 0x1A
156#define COMPASS4_CTRL1_WM_STEPS_28 0x1B
157#define COMPASS4_CTRL1_WM_STEPS_29 0x1C
158#define COMPASS4_CTRL1_WM_STEPS_30 0x1D
159#define COMPASS4_CTRL1_WM_STEPS_31 0x1E
160#define COMPASS4_CTRL1_WM_STEPS_32 0x1F
161#define COMPASS4_CTRL1_NOISE_DISABLE 0x00
162#define COMPASS4_CTRL1_NOISE_ENABLE 0x20
169#define COMPASS4_CTRL2_MODE_POWER_DOWN 0x00
170#define COMPASS4_CTRL2_MODE_SINGLE 0x01
171#define COMPASS4_CTRL2_MODE_CONT_1 0x02
172#define COMPASS4_CTRL2_MODE_CONT_2 0x04
173#define COMPASS4_CTRL2_MODE_CONT_3 0x06
174#define COMPASS4_CTRL2_MODE_CONT_4 0x08
175#define COMPASS4_CTRL2_MODE_CONT_5 0x0A
176#define COMPASS4_CTRL2_MODE_CONT_6 0x0C
177#define COMPASS4_CTRL2_MODE_SELF_TEST 0x10
178#define COMPASS4_CTRL2_SDR_LOW_POWER 0x00
179#define COMPASS4_CTRL2_SDR_LOW_NOISE 0x40
180#define COMPASS4_CTRL2_FIFO_ENABLE 0x80
181#define COMPASS4_CTRL2_FIFO_DISABLE 0x00
188#define COMPASS4_CTRL3_SOFT_RESET 0x01
195#define COMPASS4_SLAVE_ADDRESS_GND_GND 0x0C
196#define COMPASS4_SLAVE_ADDRESS_GND_VCC 0x0D
197#define COMPASS4_SLAVE_ADDRESS_VCC_GND 0x0E
198#define COMPASS4_SLAVE_ADDRESS_VCC_VCC 0x0F
205#define COMPASS4_GET_STATUS_1 0x01
206#define COMPASS4_GET_STATUS_2 0x02
213#define COMPASS4_X_AXIS_DATA 0x11
214#define COMPASS4_Y_AXIS_DATA 0x13
215#define COMPASS4_Z_AXIS_DATA 0x15
222#define FLUX_CONSTANT 0.15 // End group macro
227// --------------------------------------------------------------- PUBLIC TYPES
233typedef struct
234{
235 int16_t x;
236 int16_t y;
237 int16_t z;
238
240
241typedef struct
242{
243 float x;
244 float y;
245 float z;
246
248
252typedef uint8_t compass4_select_t;
253
257typedef void ( *compass4_master_io_t )( struct compass4_s*, uint8_t, uint8_t*, uint8_t );
258
262typedef struct compass4_s
263{
264 // Output pins
265 digital_out_t cs;
266
267 digital_out_t rst;
268
269 // Input pins
270
271 digital_in_t int_pin;
272
273 // Modules
274
275 i2c_master_t i2c;
276 spi_master_t spi;
277
278 // ctx variable
279
281 pin_name_t chip_select;
285
287
291typedef struct
292{
293 // Communication gpio pins
294
295 pin_name_t scl;
296 pin_name_t sda;
297 pin_name_t miso;
298 pin_name_t mosi;
299 pin_name_t sck;
300 pin_name_t cs;
301
302 // Additional gpio pins
303
304 pin_name_t rst;
305 pin_name_t int_pin;
306
307 // static variable
308
309 uint32_t i2c_speed;
310 uint8_t i2c_address;
311
312 uint32_t spi_speed;
313 spi_master_mode_t spi_mode;
314 spi_master_chip_select_polarity_t cs_polarity;
315
317
319 // End types group
321// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
327#ifdef __cplusplus
328extern "C"{
329#endif
330
340
349
360void compass4_generic_write ( compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
361
372void compass4_generic_read ( compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
373
382
390void compass4_set_rst_pin_state ( compass4_t *ctx, uint8_t state );
391
400
410void compass4_configuration ( compass4_t *ctx, uint8_t cfg1, uint8_t cfg2 );
411
421uint8_t compass4_get_status( compass4_t *ctx, uint8_t status );
422
434
445
456
467uint8_t compass4_get_single_axis ( compass4_t *ctx, uint8_t axis_reg, int16_t *axis_data );
468
479
490uint8_t compass4_get_single_magnetic_flux ( compass4_t *ctx, uint8_t axis, float *flux_data );
491
492#ifdef __cplusplus
493}
494#endif
495#endif // _COMPASS4_H_
496 // End public_function group
499
500// ------------------------------------------------------------------------- END
#define COMPASS4_RETVAL
Definition: compass4.h:77
void compass4_generic_read(compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
void compass4_software_reset(compass4_t *ctx)
Software reset function.
void compass4_hardware_reset(compass4_t *ctx)
Hardware reset function.
uint8_t compass4_get_interrupt(compass4_t *ctx)
Gets INT pin state (DRDY pin)
void compass4_cfg_setup(compass4_cfg_t *cfg)
Config Object Initialization function.
void compass4_set_rst_pin_state(compass4_t *ctx, uint8_t state)
Sets RST pin state.
uint8_t compass4_check_device(compass4_t *ctx)
Check device function.
uint8_t compass4_get_status(compass4_t *ctx, uint8_t status)
Get status.
uint8_t compass4_get_magnetic_flux(compass4_t *ctx, compass4_flux_t *flux)
Gets magnetic flux of X\Y\Z axis value.
void compass4_generic_write(compass4_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
uint8_t compass4_get_single_magnetic_flux(compass4_t *ctx, uint8_t axis, float *flux_data)
Gets single axis flux value.
uint8_t compass4_get_axis(compass4_t *ctx, compass4_axis_t *axis)
Gets X\Y\Z Axis value.
COMPASS4_RETVAL compass4_init(compass4_t *ctx, compass4_cfg_t *cfg)
Initialization function.
void compass4_configuration(compass4_t *ctx, uint8_t cfg1, uint8_t cfg2)
Configuration function.
uint8_t compass4_get_single_axis(compass4_t *ctx, uint8_t axis_reg, int16_t *axis_data)
Gets single axis value.
uint8_t compass4_select_t
Communication type.
Definition: compass4.h:252
struct compass4_s compass4_t
Click ctx object definition.
void(* compass4_master_io_t)(struct compass4_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: compass4.h:257
Definition: compass4.h:234
int16_t z
Definition: compass4.h:237
int16_t x
Definition: compass4.h:235
int16_t y
Definition: compass4.h:236
Click configuration structure definition.
Definition: compass4.h:292
compass4_select_t sel
Definition: compass4.h:316
uint32_t i2c_speed
Definition: compass4.h:309
spi_master_chip_select_polarity_t cs_polarity
Definition: compass4.h:314
pin_name_t sck
Definition: compass4.h:299
spi_master_mode_t spi_mode
Definition: compass4.h:313
pin_name_t mosi
Definition: compass4.h:298
uint32_t spi_speed
Definition: compass4.h:312
pin_name_t scl
Definition: compass4.h:295
pin_name_t int_pin
Definition: compass4.h:305
pin_name_t miso
Definition: compass4.h:297
pin_name_t sda
Definition: compass4.h:296
pin_name_t rst
Definition: compass4.h:304
pin_name_t cs
Definition: compass4.h:300
uint8_t i2c_address
Definition: compass4.h:310
Definition: compass4.h:242
float y
Definition: compass4.h:244
float x
Definition: compass4.h:243
float z
Definition: compass4.h:245
Click ctx object definition.
Definition: compass4.h:263
digital_out_t cs
Definition: compass4.h:265
compass4_select_t master_sel
Definition: compass4.h:284
spi_master_t spi
Definition: compass4.h:276
compass4_master_io_t write_f
Definition: compass4.h:282
compass4_master_io_t read_f
Definition: compass4.h:283
digital_in_t int_pin
Definition: compass4.h:271
i2c_master_t i2c
Definition: compass4.h:275
digital_out_t rst
Definition: compass4.h:267
uint8_t slave_address
Definition: compass4.h:280
pin_name_t chip_select
Definition: compass4.h:281