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
42#ifdef PREINIT_SUPPORTED
43#include "preinit.h"
44#endif
45
46#ifdef MikroCCoreVersion
47 #if MikroCCoreVersion >= 1
48 #include "delays.h"
49 #endif
50#endif
51
52#include "drv_digital_out.h"
53#include "drv_digital_in.h"
54#include "drv_i2c_master.h"
55
56// -------------------------------------------------------------- PUBLIC MACROS
61#define COMPASS_CTRL_REG1 0x20
62#define COMPASS_CTRL_REG2 0x21
63#define COMPASS_CTRL_REG3 0x22
64#define COMPASS_CTRL_REG4 0x23
65#define COMPASS_CTRL_REG5 0x24
66#define COMPASS_CTRL_REG6 0x25
67#define COMPASS_FIFO_CTRL_REG 0x2E
68#define COMPASS_STATUS_REG 0x27
69#define COMPASS_INT1_CFG 0x30
70#define COMPASS_INT2_CFG 0x34
71
76#define COMPASS_CRA_REG 0x00
77#define COMPASS_CRB_REG 0x01
78#define COMPASS_MR_REG 0x02
79#define COMPASS_SR_REG 0x09
80
81#define COMPASS_IRA_REG 0x0A
82#define COMPASS_IRB_REG 0x0B
83#define COMPASS_IRC_REG 0x0C
84
85#define COMPASS_OUT_MAGNET_X_HIGH 0x03
86#define COMPASS_OUT_MAGNET_X_LOW 0x04
87#define COMPASS_OUT_MAGNET_Z_HIGH 0x05
88#define COMPASS_OUT_MAGNET_Z_LOW 0x06
89#define COMPASS_OUT_MAGNET_Y_HIGH 0x07
90#define COMPASS_OUT_MAGNET_Y_LOW 0x08
91
92#define COMPASS_TEMP_OUT_HIGH 0x31
93#define COMPASS_TEMP_OUT_LOW 0x32
94
99#define COMPASS_OUT_ACCEL_X_LOW 0x28
100#define COMPASS_OUT_ACCEL_X_HIGH 0x29
101#define COMPASS_OUT_ACCEL_Y_LOW 0x2A
102#define COMPASS_OUT_ACCEL_Y_HIGH 0x2B
103#define COMPASS_OUT_ACCEL_Z_LOW 0x2C
104#define COMPASS_OUT_ACCEL_Z_HIGH 0x2D
105
110#define COMPASS_MAP_MIKROBUS( cfg, mikrobus ) \
111 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
112 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
113 cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_RST ); \
114 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
121#define COMPASS_RETVAL uint8_t
122
123#define COMPASS_OK 0x00
124#define COMPASS_INIT_ERROR 0xFF
127 // End group macro
128// --------------------------------------------------------------- PUBLIC TYPES
137typedef struct
138{
139 // Input pins
140
141 digital_in_t rdy;
142 digital_in_t int_pin;
143
144 // Modules
145
146 i2c_master_t i2c;
147
148 // ctx variable
149
152
153} compass_t;
154
158typedef struct
159{
160 // Communication gpio pins
161
162 pin_name_t scl;
163 pin_name_t sda;
164
165 // Additional gpio pins
166
167 pin_name_t rdy;
168 pin_name_t int_pin;
169
170 // static variable
171
172 uint32_t i2c_speed;
175
177
178 // End types group
179
180// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
181
187#ifdef __cplusplus
188extern "C"{
189#endif
190
200
210
221void compass_accel_generic_write ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
222
233void compass_magnet_generic_write ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
234
245void compass_accel_generic_read ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
246
257void compass_magnet_generic_read ( compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
258
267
278void compass_read_accel_axis ( compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis );
279
290void compass_read_magnet_axis ( compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis );
291
292#ifdef __cplusplus
293}
294#endif
295#endif // _COMPASS_H_
296
297 // End public_function group
299
300// ------------------------------------------------------------------------- END
#define COMPASS_RETVAL
Definition compass.h:121
void compass_magnet_generic_write(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic magnet write function.
void compass_accel_generic_read(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic accel read function.
COMPASS_RETVAL compass_init(compass_t *ctx, compass_cfg_t *cfg)
Initialization function.
void compass_magnet_generic_read(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic magnet read function.
void compass_read_magnet_axis(compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis)
Magnet axes read function.
void compass_read_accel_axis(compass_t *ctx, int16_t *x_axis, int16_t *y_axis, int16_t *z_axis)
Accel axes read function.
void compass_accel_generic_write(compass_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic accel write function.
void compass_default_config(compass_t *ctx)
Default config function.
void compass_cfg_setup(compass_cfg_t *cfg)
Config Object Initialization function.
uint8_t i2c_magnet_address
Definition compass.h:174
uint8_t i2c_accel_address
Definition compass.h:173
uint32_t i2c_speed
Definition compass.h:172
uint8_t magnet_slave_address
Definition compass.h:151
uint8_t accel_slave_address
Definition compass.h:150
digital_in_t int_pin
Definition compass.h:142
i2c_master_t i2c
Definition compass.h:146
pin_name_t scl
Definition compass.h:162
pin_name_t int_pin
Definition compass.h:168
pin_name_t rdy
Definition compass.h:167
pin_name_t sda
Definition compass.h:163
digital_in_t rdy
Definition compass.h:141
Click configuration structure definition.
Definition compass.h:159
Click ctx object definition.
Definition compass.h:138