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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 #include "drv_spi_master.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define COMPASS2_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
66  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
72  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
73 
79 #define COMPASS2_MASTER_I2C 0
80 #define COMPASS2_MASTER_SPI 1
81 
87 #define COMPASS2_RETVAL uint8_t
88 
89 #define COMPASS2_OK 0x00
90 #define COMPASS2_INIT_ERROR 0xFF
91 
97 #define COMPASS2_REG_DEVICE_ID 0x00
98 #define COMPASS2_REG_INFORMATION 0x01
99 #define COMPASS2_REG_STATUS_1 0x02
100 #define COMPASS2_REG_AXIS_X_LOW 0x03
101 #define COMPASS2_REG_AXIS_X_HIGH 0x04
102 #define COMPASS2_REG_AXIS_Y_LOW 0x05
103 #define COMPASS2_REG_AXIS_Y_HIGH 0x06
104 #define COMPASS2_REG_AXIS_Z_LOW 0x07
105 #define COMPASS2_REG_AXIS_Z_HIGH 0x08
106 #define COMPASS2_REG_STATUS_2 0x09
107 #define COMPASS2_REG_CNTRL1 0x0A
108 #define COMPASS2_REG_CNTRL2 0x0B
109 #define COMPASS2_REG_SELF_TEST 0x0C
110 #define COMPASS2_REG_TEST_1 0x0D
111 #define COMPASS2_REG_TEST_2 0x0E
112 #define COMPASS2_REG_I2C_DISABLE 0x0F
113 #define COMPASS2_REG_X_AXIS_SENS 0x10
114 #define COMPASS2_REG_Y_AXIS_SENS 0x11
115 #define COMPASS2_REG_Z_AXIS_SENS 0x12
116 
117 #define COMPASS2_MODE_POWER_DOWN 0x00
118 #define COMPASS2_MODE_SINGL_MEASUR 0x01
119 #define COMPASS2_MODE_CONT_MEAS_1 0x02
120 #define COMPASS2_MODE_CONT_MEAS_2 0x06
121 #define COMPASS2_MODE_EXT_TRIG 0x04
122 #define COMPASS2_MODE_SELF_TEST 0x08
123 #define COMPASS2_MODE_FUSE_ROM 0x0F
124 
125 #define COMPASS2_SET_RESOLUTION_14bit 0x00
126 #define COMPASS2_SET_RESOLUTION_16bit 0x10
127 
128 #define COMPASS2_MODE_SPI 0x00
129 #define COMPASS2_MODE_I2C 0x01
130 
131 #define COMPASS2_X_AXIS 0x00
132 #define COMPASS2_Y_AXIS 0x01
133 #define COMPASS2_Z_AXIS 0x02
134  // End group macro
137 // --------------------------------------------------------------- PUBLIC TYPES
146 typedef uint8_t compass2_select_t;
147 
151 typedef void ( *compass2_master_io_t )( struct compass2_s*, uint8_t, uint8_t*, uint8_t );
152 
156 typedef struct compass2_s
157 {
158  // Output pins
159 
160  digital_out_t rst;
161  digital_out_t cs;
162 
163  // Input pins
164 
165  digital_in_t int_pin;
166 
167  // Modules
168 
169  i2c_master_t i2c;
170  spi_master_t spi;
171 
172  // ctx variable
173 
174  uint8_t slave_address;
175  pin_name_t chip_select;
179 
181 
183 
187 typedef struct
188 {
189  // Communication gpio pins
190 
191  pin_name_t scl;
192  pin_name_t sda;
193  pin_name_t miso;
194  pin_name_t mosi;
195  pin_name_t sck;
196  pin_name_t cs;
197 
198  // Additional gpio pins
199 
200  pin_name_t rst;
201  pin_name_t int_pin;
202 
203  // static variable
204 
205  uint32_t i2c_speed;
206  uint8_t i2c_address;
207 
208  uint32_t spi_speed;
209  spi_master_mode_t spi_mode;
210  spi_master_chip_select_polarity_t cs_polarity;
211 
213 
215  // End variable group
217 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
223 #ifdef __cplusplus
224 extern "C"{
225 #endif
226 
237 
246 
255 
266 void compass2_generic_write ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
267 
278 void compass2_generic_read ( compass2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
279 
289 void compass2_write_byte ( compass2_t *ctx, uint8_t reg, uint8_t data_buf );
290 
299 uint8_t compass2_read_byte ( compass2_t *ctx, uint8_t reg );
300 
308 void compass2_reset ( compass2_t *ctx );
309 
318 
327 
335 float compass2_get_resolution_parameter ( uint8_t res );
336 
344 uint8_t compass2_get_rdy ( compass2_t *ctx );
345 
354 
365 void compass2_get_adjustment( compass2_t *ctx, uint8_t *x, uint8_t *y, uint8_t *z );
366 
375 void compass2_set_out_resolution ( compass2_t *ctx, uint8_t res );
376 
385 void compass2_set_mode ( compass2_t *ctx, uint8_t mode );
386 
397 void compass2_get_all_data ( compass2_t *ctx, int16_t *x, int16_t *y, int16_t *z );
398 
407 int16_t compass2_get_axis_data ( compass2_t *ctx, uint8_t axis );
408 
417 
418 #ifdef __cplusplus
419 }
420 #endif
421 #endif // _COMPASS2_H_
422  // End public_function group
425 
426 // ------------------------------------------------------------------------- END
compass2_s::master_sel
compass2_select_t master_sel
Definition: compass2.h:178
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:208
compass2_s::cs
digital_out_t cs
Definition: compass2.h:161
compass2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: compass2.h:205
compass2_s::slave_address
uint8_t slave_address
Definition: compass2.h:174
compass2_cfg_t::cs
pin_name_t cs
Definition: compass2.h:196
compass2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: compass2.h:209
compass2_cfg_t::mosi
pin_name_t mosi
Definition: compass2.h:194
compass2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: compass2.h:210
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:175
compass2_cfg_t
Click configuration structure definition.
Definition: compass2.h:188
compass2_cfg_t::rst
pin_name_t rst
Definition: compass2.h:200
compass2_s::rst
digital_out_t rst
Definition: compass2.h:160
compass2_reset
void compass2_reset(compass2_t *ctx)
Hardware reset function.
compass2_select_t
uint8_t compass2_select_t
Communication type.
Definition: compass2.h:146
compass2_cfg_t::sda
pin_name_t sda
Definition: compass2.h:192
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:176
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:191
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:195
compass2_s::spi
spi_master_t spi
Definition: compass2.h:170
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:193
compass2_s::read_f
compass2_master_io_t read_f
Definition: compass2.h:177
compass2_s
Click ctx object definition.
Definition: compass2.h:157
compass2_new_measurement
void compass2_new_measurement(compass2_t *ctx)
New measurement function.
compass2_s::output_resolution
uint16_t output_resolution
Definition: compass2.h:180
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:169
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:201
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:151
compass2_s::int_pin
digital_in_t int_pin
Definition: compass2.h:165
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:212
compass2_cfg_t::i2c_address
uint8_t i2c_address
Definition: compass2.h:206
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:87