compass2  2.0.0.0
compass2.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 COMPASS2_H
36 #define COMPASS2_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 #include "drv_spi_master.h"
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
68 #define COMPASS2_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
70  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
71  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
72  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
73  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
74  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
75  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
76  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
77 
83 #define COMPASS2_MASTER_I2C 0
84 #define COMPASS2_MASTER_SPI 1
85 
91 #define COMPASS2_RETVAL uint8_t
92 
93 #define COMPASS2_OK 0x00
94 #define COMPASS2_INIT_ERROR 0xFF
95 
101 #define COMPASS2_REG_DEVICE_ID 0x00
102 #define COMPASS2_REG_INFORMATION 0x01
103 #define COMPASS2_REG_STATUS_1 0x02
104 #define COMPASS2_REG_AXIS_X_LOW 0x03
105 #define COMPASS2_REG_AXIS_X_HIGH 0x04
106 #define COMPASS2_REG_AXIS_Y_LOW 0x05
107 #define COMPASS2_REG_AXIS_Y_HIGH 0x06
108 #define COMPASS2_REG_AXIS_Z_LOW 0x07
109 #define COMPASS2_REG_AXIS_Z_HIGH 0x08
110 #define COMPASS2_REG_STATUS_2 0x09
111 #define COMPASS2_REG_CNTRL1 0x0A
112 #define COMPASS2_REG_CNTRL2 0x0B
113 #define COMPASS2_REG_SELF_TEST 0x0C
114 #define COMPASS2_REG_TEST_1 0x0D
115 #define COMPASS2_REG_TEST_2 0x0E
116 #define COMPASS2_REG_I2C_DISABLE 0x0F
117 #define COMPASS2_REG_X_AXIS_SENS 0x10
118 #define COMPASS2_REG_Y_AXIS_SENS 0x11
119 #define COMPASS2_REG_Z_AXIS_SENS 0x12
120 
121 #define COMPASS2_MODE_POWER_DOWN 0x00
122 #define COMPASS2_MODE_SINGL_MEASUR 0x01
123 #define COMPASS2_MODE_CONT_MEAS_1 0x02
124 #define COMPASS2_MODE_CONT_MEAS_2 0x06
125 #define COMPASS2_MODE_EXT_TRIG 0x04
126 #define COMPASS2_MODE_SELF_TEST 0x08
127 #define COMPASS2_MODE_FUSE_ROM 0x0F
128 
129 #define COMPASS2_SET_RESOLUTION_14bit 0x00
130 #define COMPASS2_SET_RESOLUTION_16bit 0x10
131 
132 #define COMPASS2_MODE_SPI 0x00
133 #define COMPASS2_MODE_I2C 0x01
134 
135 #define COMPASS2_X_AXIS 0x00
136 #define COMPASS2_Y_AXIS 0x01
137 #define COMPASS2_Z_AXIS 0x02
138  // End group macro
141 // --------------------------------------------------------------- PUBLIC TYPES
150 typedef uint8_t compass2_select_t;
151 
155 typedef void ( *compass2_master_io_t )( struct compass2_s*, uint8_t, uint8_t*, uint8_t );
156 
160 typedef struct compass2_s
161 {
162  // Output pins
163 
164  digital_out_t rst;
165  digital_out_t cs;
166 
167  // Input pins
168 
169  digital_in_t int_pin;
170 
171  // Modules
172 
173  i2c_master_t i2c;
174  spi_master_t spi;
175 
176  // ctx variable
177 
178  uint8_t slave_address;
179  pin_name_t chip_select;
183 
185 
187 
191 typedef struct
192 {
193  // Communication gpio pins
194 
195  pin_name_t scl;
196  pin_name_t sda;
197  pin_name_t miso;
198  pin_name_t mosi;
199  pin_name_t sck;
200  pin_name_t cs;
201 
202  // Additional gpio pins
203 
204  pin_name_t rst;
205  pin_name_t int_pin;
206 
207  // static variable
208 
209  uint32_t i2c_speed;
210  uint8_t i2c_address;
211 
212  uint32_t spi_speed;
213  spi_master_mode_t spi_mode;
214  spi_master_chip_select_polarity_t cs_polarity;
215 
217 
219  // End variable group
221 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
227 #ifdef __cplusplus
228 extern "C"{
229 #endif
230 
241 
250 
259 
270 void compass2_generic_write ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
271 
282 void compass2_generic_read ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
283 
293 void compass2_write_byte ( compass2_t *ctx, uint8_t reg, uint8_t data_buf );
294 
303 uint8_t compass2_read_byte ( compass2_t *ctx, uint8_t reg );
304 
312 void compass2_reset ( compass2_t *ctx );
313 
322 
331 
339 float compass2_get_resolution_parameter ( uint8_t res );
340 
348 uint8_t compass2_get_rdy ( compass2_t *ctx );
349 
358 
369 void compass2_get_adjustment( compass2_t *ctx, uint8_t *x, uint8_t *y, uint8_t *z );
370 
379 void compass2_set_out_resolution ( compass2_t *ctx, uint8_t res );
380 
389 void compass2_set_mode ( compass2_t *ctx, uint8_t mode );
390 
401 void compass2_get_all_data ( compass2_t *ctx, int16_t *x, int16_t *y, int16_t *z );
402 
411 int16_t compass2_get_axis_data ( compass2_t *ctx, uint8_t axis );
412 
421 
422 #ifdef __cplusplus
423 }
424 #endif
425 #endif // _COMPASS2_H_
426  // End public_function group
429 
430 // ------------------------------------------------------------------------- END
compass2_s::master_sel
compass2_select_t master_sel
Definition: compass2.h:182
compass2_i2c_disable
void compass2_i2c_disable(compass2_t *ctx)
Switch from I2C to SPI mode function.
compass2_cfg_t::spi_speed
uint32_t spi_speed
Definition: compass2.h:212
compass2_s::cs
digital_out_t cs
Definition: compass2.h:165
compass2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: compass2.h:209
compass2_s::slave_address
uint8_t slave_address
Definition: compass2.h:178
compass2_cfg_t::cs
pin_name_t cs
Definition: compass2.h:200
compass2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: compass2.h:213
compass2_cfg_t::mosi
pin_name_t mosi
Definition: compass2.h:198
compass2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: compass2.h:214
compass2_generic_write
void compass2_generic_write(compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
compass2_s::chip_select
pin_name_t chip_select
Definition: compass2.h:179
compass2_cfg_t
Click configuration structure definition.
Definition: compass2.h:192
compass2_cfg_t::rst
pin_name_t rst
Definition: compass2.h:204
compass2_s::rst
digital_out_t rst
Definition: compass2.h:164
compass2_reset
void compass2_reset(compass2_t *ctx)
Hardware reset function.
compass2_select_t
uint8_t compass2_select_t
Communication type.
Definition: compass2.h:150
compass2_cfg_t::sda
pin_name_t sda
Definition: compass2.h:196
compass2_software_reset
void compass2_software_reset(compass2_t *ctx)
Software reset function.
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:180
compass2_cfg_setup
void compass2_cfg_setup(compass2_cfg_t *cfg)
Config Object Initialization function.
compass2_t
struct compass2_s compass2_t
Click ctx object definition.
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:195
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:199
compass2_s::spi
spi_master_t spi
Definition: compass2.h:174
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:197
compass2_s::read_f
compass2_master_io_t read_f
Definition: compass2.h:181
compass2_s
Click ctx object definition.
Definition: compass2.h:161
compass2_new_measurement
void compass2_new_measurement(compass2_t *ctx)
New measurement function.
compass2_s::output_resolution
uint16_t output_resolution
Definition: compass2.h:184
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:173
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:205
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:155
compass2_s::int_pin
digital_in_t int_pin
Definition: compass2.h:169
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:216
compass2_cfg_t::i2c_address
uint8_t i2c_address
Definition: compass2.h:210
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_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:91