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 
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  digital_out_t cs;
152 
153  // Input pins
154 
155  digital_in_t int_pin;
156 
157  // Modules
158 
159  i2c_master_t i2c;
160  spi_master_t spi;
161 
162  // ctx variable
163 
164  uint8_t slave_address;
165  pin_name_t chip_select;
169 
171 
172 } compass2_t;
173 
177 typedef struct
178 {
179  // Communication gpio pins
180 
181  pin_name_t scl;
182  pin_name_t sda;
183  pin_name_t miso;
184  pin_name_t mosi;
185  pin_name_t sck;
186  pin_name_t cs;
187 
188  // Additional gpio pins
189 
190  pin_name_t rst;
191  pin_name_t int_pin;
192 
193  // static variable
194 
195  uint32_t i2c_speed;
196  uint8_t i2c_address;
197 
198  uint32_t spi_speed;
199  spi_master_mode_t spi_mode;
200  spi_master_chip_select_polarity_t cs_polarity;
201 
203 
205  // End variable group
207 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
213 #ifdef __cplusplus
214 extern "C"{
215 #endif
216 
226 void compass2_cfg_setup ( compass2_cfg_t *cfg );
227 
236 
244 void compass2_default_cfg ( compass2_t *ctx );
245 
256 void compass2_generic_write ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
257 
268 void compass2_generic_read ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
269 
279 void compass2_write_byte ( compass2_t *ctx, uint8_t reg, uint8_t data_buf );
280 
289 uint8_t compass2_read_byte ( compass2_t *ctx, uint8_t reg );
290 
298 void compass2_reset ( compass2_t *ctx );
299 
307 void compass2_i2c_disable ( compass2_t *ctx );
308 
316 void compass2_software_reset ( compass2_t *ctx );
317 
325 float compass2_get_resolution_parameter ( uint8_t res );
326 
334 uint8_t compass2_get_rdy ( compass2_t *ctx );
335 
343 uint8_t compass2_get_data_overrun ( compass2_t *ctx );
344 
355 void compass2_get_adjustment( compass2_t *ctx, uint8_t *x, uint8_t *y, uint8_t *z );
356 
365 void compass2_set_out_resolution ( compass2_t *ctx, uint8_t res );
366 
375 void compass2_set_mode ( compass2_t *ctx, uint8_t mode );
376 
387 void compass2_get_all_data ( compass2_t *ctx, int16_t *x, int16_t *y, int16_t *z );
388 
397 int16_t compass2_get_axis_data ( compass2_t *ctx, uint8_t axis );
398 
406 void compass2_new_measurement ( compass2_t *ctx );
407 
408 #ifdef __cplusplus
409 }
410 #endif
411 #endif // _COMPASS2_H_
412  // End public_function group
415 
416 // ------------------------------------------------------------------------- END
compass2_s::master_sel
compass2_select_t master_sel
Definition: compass2.h:168
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:198
compass2_s::cs
digital_out_t cs
Definition: compass2.h:151
compass2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: compass2.h:195
compass2_s::slave_address
uint8_t slave_address
Definition: compass2.h:164
compass2_cfg_t::cs
pin_name_t cs
Definition: compass2.h:186
compass2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: compass2.h:199
compass2_cfg_t::mosi
pin_name_t mosi
Definition: compass2.h:184
compass2_t
struct compass2_s compass2_t
Click ctx object definition.
compass2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: compass2.h:200
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:165
compass2_cfg_t
Click configuration structure definition.
Definition: compass2.h:177
compass2_cfg_t::rst
pin_name_t rst
Definition: compass2.h:190
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:182
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:166
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:181
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:185
compass2_s::spi
spi_master_t spi
Definition: compass2.h:160
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:183
compass2_s::read_f
compass2_master_io_t read_f
Definition: compass2.h:167
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:170
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:159
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:191
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:155
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:202
compass2_cfg_t::i2c_address
uint8_t i2c_address
Definition: compass2.h:196
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:77