compass2  2.0.0.0
compass2.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 COMPASS2_H
36 #define COMPASS2_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 COMPASS2_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 );
63 
69 #define COMPASS2_MASTER_I2C 0
70 #define COMPASS2_MASTER_SPI 1
71 
77 #define COMPASS2_RETVAL uint8_t
78 
79 #define COMPASS2_OK 0x00
80 #define COMPASS2_INIT_ERROR 0xFF
81 
87 #define COMPASS2_REG_DEVICE_ID 0x00
88 #define COMPASS2_REG_INFORMATION 0x01
89 #define COMPASS2_REG_STATUS_1 0x02
90 #define COMPASS2_REG_AXIS_X_LOW 0x03
91 #define COMPASS2_REG_AXIS_X_HIGH 0x04
92 #define COMPASS2_REG_AXIS_Y_LOW 0x05
93 #define COMPASS2_REG_AXIS_Y_HIGH 0x06
94 #define COMPASS2_REG_AXIS_Z_LOW 0x07
95 #define COMPASS2_REG_AXIS_Z_HIGH 0x08
96 #define COMPASS2_REG_STATUS_2 0x09
97 #define COMPASS2_REG_CNTRL1 0x0A
98 #define COMPASS2_REG_CNTRL2 0x0B
99 #define COMPASS2_REG_SELF_TEST 0x0C
100 #define COMPASS2_REG_TEST_1 0x0D
101 #define COMPASS2_REG_TEST_2 0x0E
102 #define COMPASS2_REG_I2C_DISABLE 0x0F
103 #define COMPASS2_REG_X_AXIS_SENS 0x10
104 #define COMPASS2_REG_Y_AXIS_SENS 0x11
105 #define COMPASS2_REG_Z_AXIS_SENS 0x12
106 
107 #define COMPASS2_MODE_POWER_DOWN 0x00
108 #define COMPASS2_MODE_SINGL_MEASUR 0x01
109 #define COMPASS2_MODE_CONT_MEAS_1 0x02
110 #define COMPASS2_MODE_CONT_MEAS_2 0x06
111 #define COMPASS2_MODE_EXT_TRIG 0x04
112 #define COMPASS2_MODE_SELF_TEST 0x08
113 #define COMPASS2_MODE_FUSE_ROM 0x0F
114 
115 #define COMPASS2_SET_RESOLUTION_14bit 0x00
116 #define COMPASS2_SET_RESOLUTION_16bit 0x10
117 
118 #define COMPASS2_MODE_SPI 0x00
119 #define COMPASS2_MODE_I2C 0x01
120 
121 #define COMPASS2_X_AXIS 0x00
122 #define COMPASS2_Y_AXIS 0x01
123 #define COMPASS2_Z_AXIS 0x02
124  // End group macro
127 // --------------------------------------------------------------- PUBLIC TYPES
136 typedef uint8_t compass2_select_t;
137 
141 typedef void ( *compass2_master_io_t )( struct compass2_s*, uint8_t, uint8_t*, uint8_t );
142 
146 typedef struct compass2_s
147 {
148  // Output pins
149 
150  digital_out_t rst;
151 
152  // Input pins
153 
154  digital_in_t int_pin;
155 
156  // Modules
157 
158  i2c_master_t i2c;
159  spi_master_t spi;
160 
161  // ctx variable
162 
163  hal_i2c_address_t slave_address;
167 
169 
170 } compass2_t;
171 
175 typedef struct
176 {
177  // Communication gpio pins
178 
179  pin_name_t scl;
180  pin_name_t sda;
181  pin_name_t miso;
182  pin_name_t mosi;
183  pin_name_t sck;
184  pin_name_t cs;
185 
186  // Additional gpio pins
187 
188  pin_name_t rst;
189  pin_name_t int_pin;
190 
191  // static variable
192 
193  hal_i2c_speed_t i2c_speed;
194  hal_i2c_address_t i2c_address;
195  hal_spi_speed_t spi_speed;
196  hal_spi_mode_t spi_mode;
197 
199 
201  // End variable group
203 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
209 #ifdef __cplusplus
210 extern "C"{
211 #endif
212 
222 void compass2_cfg_setup ( compass2_cfg_t *cfg );
223 
232 
240 void compass2_default_cfg ( compass2_t *ctx );
241 
252 void compass2_generic_write ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
253 
264 void compass2_generic_read ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
265 
275 void compass2_write_byte ( compass2_t *ctx, uint8_t reg, uint8_t data_buf );
276 
285 uint8_t compass2_read_byte ( compass2_t *ctx, uint8_t reg );
286 
294 void compass2_reset ( compass2_t *ctx );
295 
303 void compass2_i2c_disable ( compass2_t *ctx );
304 
312 void compass2_software_reset ( compass2_t *ctx );
313 
321 float compass2_get_resolution_parameter ( uint8_t res );
322 
330 uint8_t compass2_get_rdy ( compass2_t *ctx );
331 
339 uint8_t compass2_get_data_overrun ( compass2_t *ctx );
340 
351 void compass2_get_adjustment( compass2_t *ctx, uint8_t *x, uint8_t *y, uint8_t *z );
352 
361 void compass2_set_out_resolution ( compass2_t *ctx, uint8_t res );
362 
371 void compass2_set_mode ( compass2_t *ctx, uint8_t mode );
372 
383 void compass2_get_all_data ( compass2_t *ctx, int16_t *x, int16_t *y, int16_t *z );
384 
393 int16_t compass2_get_axis_data ( compass2_t *ctx, uint8_t axis );
394 
402 void compass2_new_measurement ( compass2_t *ctx );
403 
404 #ifdef __cplusplus
405 }
406 #endif
407 #endif // _COMPASS2_H_
408  // End public_function group
411 
412 // ------------------------------------------------------------------------- END
compass2_s::master_sel
compass2_select_t master_sel
Definition: compass2.h:166
compass2_i2c_disable
void compass2_i2c_disable(compass2_t *ctx)
Switch from I2C to SPI mode function.
compass2_cfg_t::cs
pin_name_t cs
Definition: compass2.h:184
compass2_cfg_t::mosi
pin_name_t mosi
Definition: compass2.h:182
compass2_cfg_t::i2c_address
hal_i2c_address_t i2c_address
Definition: compass2.h:194
compass2_t
struct compass2_s compass2_t
Click ctx object definition.
compass2_generic_write
void compass2_generic_write(compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
compass2_cfg_t::spi_mode
hal_spi_mode_t spi_mode
Definition: compass2.h:196
compass2_cfg_t
Click configuration structure definition.
Definition: compass2.h:175
compass2_cfg_t::rst
pin_name_t rst
Definition: compass2.h:188
compass2_s::rst
digital_out_t rst
Definition: compass2.h:150
compass2_reset
void compass2_reset(compass2_t *ctx)
Hardware reset function.
compass2_select_t
uint8_t compass2_select_t
Communication type.
Definition: compass2.h:136
compass2_cfg_t::sda
pin_name_t sda
Definition: compass2.h:180
compass2_software_reset
void compass2_software_reset(compass2_t *ctx)
Software reset function.
compass2_cfg_t::i2c_speed
hal_i2c_speed_t i2c_speed
Definition: compass2.h:193
compass2_get_axis_data
int16_t compass2_get_axis_data(compass2_t *ctx, uint8_t axis)
Get one axis data function.
compass2_get_rdy
uint8_t compass2_get_rdy(compass2_t *ctx)
Get ready function.
compass2_s::write_f
compass2_master_io_t write_f
Definition: compass2.h:164
compass2_cfg_setup
void compass2_cfg_setup(compass2_cfg_t *cfg)
Config Object Initialization function.
compass2_read_byte
uint8_t compass2_read_byte(compass2_t *ctx, uint8_t reg)
Read byte function.
compass2_cfg_t::scl
pin_name_t scl
Definition: compass2.h:179
compass2_generic_read
void compass2_generic_read(compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
compass2_get_data_overrun
uint8_t compass2_get_data_overrun(compass2_t *ctx)
Get data overrun function.
compass2_cfg_t::sck
pin_name_t sck
Definition: compass2.h:183
compass2_s::spi
spi_master_t spi
Definition: compass2.h:159
compass2_init
COMPASS2_RETVAL compass2_init(compass2_t *ctx, compass2_cfg_t *cfg)
Initialization function.
compass2_set_mode
void compass2_set_mode(compass2_t *ctx, uint8_t mode)
Set output resolution function.
compass2_cfg_t::miso
pin_name_t miso
Definition: compass2.h:181
compass2_cfg_t::spi_speed
hal_spi_speed_t spi_speed
Definition: compass2.h:195
compass2_s::read_f
compass2_master_io_t read_f
Definition: compass2.h:165
compass2_s
Click ctx object definition.
Definition: compass2.h:146
compass2_new_measurement
void compass2_new_measurement(compass2_t *ctx)
New measurement function.
compass2_s::output_resolution
uint16_t output_resolution
Definition: compass2.h:168
compass2_set_out_resolution
void compass2_set_out_resolution(compass2_t *ctx, uint8_t res)
Set output resolution function.
compass2_s::i2c
i2c_master_t i2c
Definition: compass2.h:158
compass2_get_resolution_parameter
float compass2_get_resolution_parameter(uint8_t res)
Get resolution function.
compass2_cfg_t::int_pin
pin_name_t int_pin
Definition: compass2.h:189
compass2_master_io_t
void(* compass2_master_io_t)(struct compass2_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: compass2.h:141
compass2_s::int_pin
digital_in_t int_pin
Definition: compass2.h:154
compass2_get_adjustment
void compass2_get_adjustment(compass2_t *ctx, uint8_t *x, uint8_t *y, uint8_t *z)
Get axis adjustment function.
compass2_default_cfg
void compass2_default_cfg(compass2_t *ctx)
Click Default Configuration function.
compass2_cfg_t::sel
compass2_select_t sel
Definition: compass2.h:198
compass2_get_all_data
void compass2_get_all_data(compass2_t *ctx, int16_t *x, int16_t *y, int16_t *z)
Get all data function.
compass2_s::slave_address
hal_i2c_address_t slave_address
Definition: compass2.h:163
compass2_write_byte
void compass2_write_byte(compass2_t *ctx, uint8_t reg, uint8_t data_buf)
Write byte function.
COMPASS2_RETVAL
#define COMPASS2_RETVAL
Definition: compass2.h:77