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