geomagnetic  2.0.0.0
geomagnetic.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 GEOMAGNETIC_H
36 #define GEOMAGNETIC_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_in.h"
49 #include "drv_i2c_master.h"
50 #include "drv_spi_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
63 #define GEOMAGNETIC_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
65  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
66  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
67  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
68  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
69  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
70  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define GEOMAGNETIC_MASTER_I2C 0
78 #define GEOMAGNETIC_MASTER_SPI 1
79 
85 #define GEOMAGNETIC_RETVAL uint8_t
86 
87 #define GEOMAGNETIC_OK 0x00
88 #define GEOMAGNETIC_INIT_ERROR 0xFF
89 
95 #define GEOMAG_DUMMY 0x00
96 #define GEOMAG_SPI_INTERFACE 0x00
97 #define GEOMAG_I2C_INTERFACE 0x01
98 #define GEOMAG_READ_CMD 0x80
99 #define GEOMAG_DEV_ADDR 0x10
100 #define GEOMAG_ID_REG 0x40
101 #define GEOMAG_RESERVED1_REG 0x41
102 #define GEOMAG_DATAX_LSB_REG 0x42
103 #define GEOMAG_DATAX_MSB_REG 0x43
104 #define GEOMAG_DATAY_LSB_REG 0x44
105 #define GEOMAG_DATAY_MSB_REG 0x45
106 #define GEOMAG_DATAZ_LSB_REG 0x46
107 #define GEOMAG_DATAZ_MSB_REG 0x47
108 #define GEOMAG_RHALL_LSB_REG 0x48
109 #define GEOMAG_RHALL_MSB_REG 0x49
110 #define GEOMAG_INTERRUPT_STATUS_REG 0x4A
111 #define GEOMAG_POWER_AND_RESET_CON_REG 0x4B
112 #define GEOMAG_DATARATE_AND_OPMODE_CON_REG 0x4C
113 #define GEOMAG_INTERR_AND_AXIS_EN_REG 0x4D
114 #define GEOMAG_INTERR_AND_CHANNEL_EN_REG 0x4E
115 #define GEOMAG_LOW_THRESHOLD_REG 0x4F
116 #define GEOMAG_HIGH_THRESHOLD_REG 0x50
117 #define GEOMAG_REPETITION_XY_CON_REG 0x51
118 #define GEOMAG_REPETITION_Z_CON_REG 0x52
119 #define GEOMAG_RESERVED2_REG 0x53
120 #define GEOMAG_RESERVED3_REG 0x54
121 #define GEOMAG_RESERVED4_REG 0x55
122 #define GEOMAG_RESERVED5_REG 0x56
123 #define GEOMAG_RESERVED6_REG 0x57
124 #define GEOMAG_RESERVED7_REG 0x58
125 #define GEOMAG_RESERVED8_REG 0x59
126 #define GEOMAG_RESERVED9_REG 0x5A
127 #define GEOMAG_RESERVED10_REG 0x5B
128 #define GEOMAG_RESERVED11_REG 0x5C
129 #define GEOMAG_RESERVED12_REG 0x5D
130 #define GEOMAG_RESERVED13_REG 0x5E
131 #define GEOMAG_RESERVED14_REG 0x5F
132 #define GEOMAG_RESERVED15_REG 0x60
133 #define GEOMAG_RESERVED16_REG 0x61
134 #define GEOMAG_RESERVED17_REG 0x62
135 #define GEOMAG_RESERVED18_REG 0x63
136 #define GEOMAG_RESERVED19_REG 0x64
137 #define GEOMAG_RESERVED20_REG 0x65
138 #define GEOMAG_RESERVED21_REG 0x66
139 #define GEOMAG_RESERVED22_REG 0x67
140 #define GEOMAG_RESERVED23_REG 0x68
141 #define GEOMAG_RESERVED24_REG 0x69
142 #define GEOMAG_RESERVED25_REG 0x6A
143 #define GEOMAG_RESERVED26_REG 0x6B
144 #define GEOMAG_RESERVED27_REG 0x6C
145 #define GEOMAG_RESERVED28_REG 0x6D
146 #define GEOMAG_RESERVED29_REG 0x6E
147 #define GEOMAG_RESERVED30_REG 0x6F
148 #define GEOMAG_RESERVED31_REG 0x70
149 #define GEOMAG_RESERVED32_REG 0x71
150 #define GEOMAG_OK 0x00
151 #define GEOMAG_ADDR_ERR 0x01
152 #define GEOMAG_INTERRUPT_OCCURED 0x02
153 #define GEOMAG_NBYTES_ERR 0x03
154 #define GEOMAG_DATA_READY 0x01
155 #define GEOMAG_DATA_NOT_READY 0x00
156 #define GEOMAG_SUSPEND_MODE_SPI4_EN 0x00
157 #define GEOMAG_SLEEP_MODE 0x01
158 #define GEOMAG_SOFT_RESET_EN 0x82
159 #define GEOMAG_SPI3_EN 0x04
160 #define GEOMAG_NORMAL_OPMODE_DR_10HZ 0x00
161 #define GEOMAG_FORCED_OPMODE 0x02
162 #define GEOMAG_SLEEP_OPMODE 0x06
163 #define GEOMAG_DR_2HZ 0x08
164 #define GEOMAG_DR_6HZ 0x10
165 #define GEOMAG_DR_8HZ 0x18
166 #define GEOMAG_DR_15HZ 0x20
167 #define GEOMAG_DR_20HZ 0x28
168 #define GEOMAG_DR_25HZ 0x30
169 #define GEOMAG_DR_30HZ 0x38
170 #define GEOMAG_INTERR_DISABLED_THRESHOLDS_ENABLED 0x00
171 #define GEOMAG_LOWTHRESHOLD_X_DISABLED 0x01
172 #define GEOMAG_LOWTHRESHOLD_Y_DISABLED 0x02
173 #define GEOMAG_LOWTHRESHOLD_Z_DISABLED 0x04
174 #define GEOMAG_HIGHTHRESHOLD_X_DISABLED 0x08
175 #define GEOMAG_HIGHTHRESHOLD_Y_DISABLED 0x10
176 #define GEOMAG_HIGHTHRESHOLD_Z_DISABLED 0x20
177 #define GEOMAG_OVERFLOW_PIN_IND_EN 0x40
178 #define GEOMAG_DATAOVERRUN_IND_EN 0x80
179 #define GEOMAG_CHANNELS_ENABLED_INTERR_DATAREADY_ACTIVE_LOW 0x00
180 #define GEOMAG_INTERR_PIN_ACTIVE_HIGH 0x01
181 #define GEOMAG_INTERR_LATCHED 0x02
182 #define GEOMAG_DATAREADY_ACTIVE_HIGH 0x04
183 #define GEOMAG_CHANNEL_X_DISABLED 0x08
184 #define GEOMAG_CHANNEL_Y_DISABLED 0x10
185 #define GEOMAG_CHANNEL_Z_DISABLED 0x20
186 #define GEOMAG_INTERR_PIN_EN 0x40
187 #define GEOMAG_DATAREADY_PIN_EN 0x80
188  // End group macro
191 // --------------------------------------------------------------- PUBLIC TYPES
200 typedef uint8_t geomagnetic_select_t;
201 
205 typedef void ( *geomagnetic_master_io_t )( struct geomagnetic_s*, uint8_t, uint8_t*, uint8_t );
206 
210 typedef struct
211 {
212  int16_t x_axis;
213  int16_t y_axis;
214  int16_t z_axis;
215 
217 
221 typedef struct geomagnetic_s
222 {
223  // Output pins
224 
225  digital_out_t cs;
226 
227  // Input pins
228 
229  digital_in_t int_pin;
230 
231  // Modules
232 
233  i2c_master_t i2c;
234  spi_master_t spi;
235 
236  // ctx variable
237 
238  uint8_t slave_address;
239  pin_name_t chip_select;
243 
245 
249 typedef struct
250 {
251  // Communication gpio pins
252 
253  pin_name_t scl;
254  pin_name_t sda;
255  pin_name_t miso;
256  pin_name_t mosi;
257  pin_name_t sck;
258  pin_name_t cs;
259 
260  // Additional gpio pins
261 
262  pin_name_t int_pin;
263 
264  // static variable
265 
266  uint32_t i2c_speed;
267  uint8_t i2c_address;
268 
269  uint32_t spi_speed;
270  spi_master_mode_t spi_mode;
271  spi_master_chip_select_polarity_t cs_polarity;
272 
274 
276  // End types group
278 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
284 #ifdef __cplusplus
285 extern "C"{
286 #endif
287 
297 
307 
316 
327 void geomagnetic_generic_write ( geomagnetic_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
328 
339 void geomagnetic_generic_read ( geomagnetic_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
340 
350 GEOMAGNETIC_RETVAL geomagnetic_write_byte ( geomagnetic_t *ctx, uint8_t input_data, uint8_t address );
351 
362 GEOMAGNETIC_RETVAL geomagnetic_read_data ( geomagnetic_t *ctx, uint8_t *out_buf, uint8_t address, uint8_t len );
363 
372 
383 *geomag_axis, uint16_t *resolution_hall );
384 
393 
401 uint8_t get_interrupt ( geomagnetic_t *ctx );
402 
411 
421 
422 #ifdef __cplusplus
423 }
424 #endif
425 #endif // _GEOMAGNETIC_H_
426  // End public_function group
429 
430 // ------------------------------------------------------------------------- END
geomagnetic_s::read_f
geomagnetic_master_io_t read_f
Definition: geomagnetic.h:241
geomagnetic_power_on_reset
void geomagnetic_power_on_reset(geomagnetic_t *ctx)
Power ON reset function.
geomagnetic_s::spi
spi_master_t spi
Definition: geomagnetic.h:234
geomagnetic_s::cs
digital_out_t cs
Definition: geomagnetic.h:225
geomagnetic_check_ready
uint8_t geomagnetic_check_ready(geomagnetic_t *ctx)
Check ready function.
get_interrupt
uint8_t get_interrupt(geomagnetic_t *ctx)
Get interrupt function.
geomagnetic_axis_t::x_axis
int16_t x_axis
Definition: geomagnetic.h:212
geomagnetic_axis_t::z_axis
int16_t z_axis
Definition: geomagnetic.h:214
geomagnetic_software_reset
void geomagnetic_software_reset(geomagnetic_t *ctx)
Software reset function.
GEOMAGNETIC_RETVAL
#define GEOMAGNETIC_RETVAL
Definition: geomagnetic.h:85
geomagnetic_master_io_t
void(* geomagnetic_master_io_t)(struct geomagnetic_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: geomagnetic.h:205
geomagnetic_generic_read
void geomagnetic_generic_read(geomagnetic_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
geomagnetic_read_data
GEOMAGNETIC_RETVAL geomagnetic_read_data(geomagnetic_t *ctx, uint8_t *out_buf, uint8_t address, uint8_t len)
Read data function.
geomagnetic_s
Click ctx object definition.
Definition: geomagnetic.h:222
geomagnetic_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: geomagnetic.h:266
geomagnetic_axis_t::y_axis
int16_t y_axis
Definition: geomagnetic.h:213
geomagnetic_read_axis_data
void geomagnetic_read_axis_data(geomagnetic_t *ctx, geomagnetic_axis_t *geomag_axis, uint16_t *resolution_hall)
Read axis data function.
geomagnetic_s::chip_select
pin_name_t chip_select
Definition: geomagnetic.h:239
geomagnetic_s::master_sel
geomagnetic_select_t master_sel
Definition: geomagnetic.h:242
geomagnetic_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: geomagnetic.h:271
geomagnetic_generic_write
void geomagnetic_generic_write(geomagnetic_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
geomagnetic_s::int_pin
digital_in_t int_pin
Definition: geomagnetic.h:229
geomagnetic_cfg_t::cs
pin_name_t cs
Definition: geomagnetic.h:258
geomagnetic_cfg_t::int_pin
pin_name_t int_pin
Definition: geomagnetic.h:262
geomagnetic_cfg_t
Click configuration structure definition.
Definition: geomagnetic.h:250
geomagnetic_write_byte
GEOMAGNETIC_RETVAL geomagnetic_write_byte(geomagnetic_t *ctx, uint8_t input_data, uint8_t address)
Write byte function.
geomagnetic_cfg_setup
void geomagnetic_cfg_setup(geomagnetic_cfg_t *cfg)
Config Object Initialization function.
geomagnetic_check_interrupt
GEOMAGNETIC_RETVAL geomagnetic_check_interrupt(geomagnetic_t *ctx)
Check interrupt function.
geomagnetic_cfg_t::sel
geomagnetic_select_t sel
Definition: geomagnetic.h:273
geomagnetic_cfg_t::miso
pin_name_t miso
Definition: geomagnetic.h:255
geomagnetic_cfg_t::sck
pin_name_t sck
Definition: geomagnetic.h:257
geomagnetic_t
struct geomagnetic_s geomagnetic_t
Click ctx object definition.
geomagnetic_cfg_t::sda
pin_name_t sda
Definition: geomagnetic.h:254
geomagnetic_cfg_t::mosi
pin_name_t mosi
Definition: geomagnetic.h:256
geomagnetic_cfg_t::scl
pin_name_t scl
Definition: geomagnetic.h:253
geomagnetic_s::i2c
i2c_master_t i2c
Definition: geomagnetic.h:233
geomagnetic_select_t
uint8_t geomagnetic_select_t
Communication type.
Definition: geomagnetic.h:200
geomagnetic_default_cfg
void geomagnetic_default_cfg(geomagnetic_t *ctx)
Click Default Configuration function.
geomagnetic_axis_t
GeoMagnetic axis structure.
Definition: geomagnetic.h:211
geomagnetic_s::slave_address
uint8_t slave_address
Definition: geomagnetic.h:238
geomagnetic_init
GEOMAGNETIC_RETVAL geomagnetic_init(geomagnetic_t *ctx, geomagnetic_cfg_t *cfg)
Initialization function.
geomagnetic_cfg_t::i2c_address
uint8_t i2c_address
Definition: geomagnetic.h:267
geomagnetic_s::write_f
geomagnetic_master_io_t write_f
Definition: geomagnetic.h:240
geomagnetic_cfg_t::spi_speed
uint32_t spi_speed
Definition: geomagnetic.h:269
geomagnetic_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: geomagnetic.h:270