magaccel 2.1.0.0
magaccel.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
28#ifndef MAGACCEL_H
29#define MAGACCEL_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_i2c_master.h"
52
73#define MAGACCEL_REG_INT_STATUS 0x00
74#define MAGACCEL_REG_TEMP_OUT 0x01
75#define MAGACCEL_REG_VECM_LSB 0x02
76#define MAGACCEL_REG_VECM_MSB 0x03
77#define MAGACCEL_REG_OUT_X_LSB 0x04
78#define MAGACCEL_REG_OUT_X_MSB 0x05
79#define MAGACCEL_REG_OUT_Y_LSB 0x06
80#define MAGACCEL_REG_OUT_Y_MSB 0x07
81#define MAGACCEL_REG_OUT_Z_LSB 0x08
82#define MAGACCEL_REG_OUT_Z_MSB 0x09
83#define MAGACCEL_REG_BUF_STATUS 0x0B
84#define MAGACCEL_REG_BUF_X_LSB 0x0C
85#define MAGACCEL_REG_BUF_X_MSB 0x0D
86#define MAGACCEL_REG_BUF_Y_LSB 0x0E
87#define MAGACCEL_REG_BUF_Y_MSB 0x0F
88#define MAGACCEL_REG_BUF_Z_LSB 0x10
89#define MAGACCEL_REG_BUF_Z_MSB 0x11
90#define MAGACCEL_REG_PROD_REV 0x12
91#define MAGACCEL_REG_WHO_AM_I 0x13
92#define MAGACCEL_REG_SYS_MODE 0x14
93#define MAGACCEL_REG_SENS_CONFIG1 0x15
94#define MAGACCEL_REG_SENS_CONFIG2 0x16
95#define MAGACCEL_REG_SENS_CONFIG3 0x17
96#define MAGACCEL_REG_SENS_CONFIG4 0x18
97#define MAGACCEL_REG_SENS_CONFIG5 0x19
98#define MAGACCEL_REG_WAKE_IDLE_LSB 0x1A
99#define MAGACCEL_REG_WAKE_IDLE_MSB 0x1B
100#define MAGACCEL_REG_SLEEP_IDLE_LSB 0x1C
101#define MAGACCEL_REG_SLEEP_IDLE_MSB 0x1D
102#define MAGACCEL_REG_ASLP_COUNT_LSB 0x1E
103#define MAGACCEL_REG_ASLP_COUNT_MSB 0x1F
104#define MAGACCEL_REG_INT_EN 0x20
105#define MAGACCEL_REG_INT_PIN_SEL 0x21
106#define MAGACCEL_REG_OFF_X 0x22
107#define MAGACCEL_REG_OFF_Y 0x23
108#define MAGACCEL_REG_OFF_Z 0x24
109#define MAGACCEL_REG_BUF_CONFIG1 0x26
110#define MAGACCEL_REG_BUF_CONFIG2 0x27
111#define MAGACCEL_REG_ORIENT_STATUS 0x28
112#define MAGACCEL_REG_ORIENT_CONFIG 0x29
113#define MAGACCEL_REG_ORIENT_DBCOUNT 0x2A
114#define MAGACCEL_REG_ORIENT_BF_ZCOMP 0x2B
115#define MAGACCEL_REG_ORIENT_THS_REG 0x2C
116#define MAGACCEL_REG_SDCD_INT_SRC1 0x2D
117#define MAGACCEL_REG_SDCD_INT_SRC2 0x2E
118#define MAGACCEL_REG_SDCD_CONFIG1 0x2F
119#define MAGACCEL_REG_SDCD_CONFIG2 0x30
120#define MAGACCEL_REG_SDCD_OT_DBCNT 0x31
121#define MAGACCEL_REG_SDCD_WT_DBCNT 0x32
122#define MAGACCEL_REG_SDCD_LTHS_LSB 0x33
123#define MAGACCEL_REG_SDCD_LTHS_MSB 0x34
124#define MAGACCEL_REG_SDCD_UTHS_LSB 0x35
125#define MAGACCEL_REG_SDCD_UTHS_MSB 0x36
126#define MAGACCEL_REG_SELF_TEST_CONFIG1 0x37
127#define MAGACCEL_REG_SELF_TEST_CONFIG2 0x38
128
129 // magaccel_reg
130
145#define MAGACCEL_SRC_BOOT_COMPLETED 0x01
146
151#define MAGACCEL_DEVICE_ID_VAL 0x86
152
157#define MAGACCEL_SW_RESET_BIT_MASK 0x80
158
163#define MAGACCEL_OP_MODE_STANDBY 0x00
164#define MAGACCEL_OP_MODE_ACTIVE 0x01
165#define MAGACCEL_OP_MODE_BIT_MASK 0xFE
166
171#define MAGACCEL_FSR_SEL_2G 0x00
172#define MAGACCEL_FSR_SEL_4G 0x01
173#define MAGACCEL_FSR_SEL_8G 0x02
174#define MAGACCEL_FSR_SEL_16G 0x03
175#define MAGACCEL_FSR_SEL_BIT_MASK 0xF9
176
181#define MAGACCEL_INT_EN_DRDY_DIS 0x00
182#define MAGACCEL_INT_EN_DRDY_EN 0x80
183
188#define MAGACCEL_DATA_READY 0
189#define MAGACCEL_DET_MAG_FIELD 0
190
195#define MAGACCEL_FSR_SENS_VAL_2G 0.98f
196#define MAGACCEL_FSR_SENS_VAL_4G 1.95f
197#define MAGACCEL_FSR_SENS_VAL_8G 3.91f
198#define MAGACCEL_FSR_SENS_VAL_16G 7.81f
199
200
206#define MAGACCEL_DEVICE_ADDRESS 0x18
207
208 // magaccel_set
209
224#define MAGACCEL_MAP_MIKROBUS( cfg, mikrobus ) \
225 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
226 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
227 cfg.fld = MIKROBUS( mikrobus, MIKROBUS_AN ); \
228 cfg.mot = MIKROBUS( mikrobus, MIKROBUS_INT )
229
230 // magaccel_map
231 // magaccel
232
237typedef struct
238{
239 // Input pins
240 digital_in_t fld;
241 digital_in_t mot;
243 // Modules
244 i2c_master_t i2c;
246 // I2C slave address
249 // Sensitivity
252} magaccel_t;
253
258typedef struct
259{
260 pin_name_t scl;
261 pin_name_t sda;
263 pin_name_t fld;
264 pin_name_t mot;
266 uint32_t i2c_speed;
267 uint8_t i2c_address;
270
275typedef struct
276{
277 float x;
278 float y;
279 float z;
282
287typedef enum
288{
290 MAGACCEL_ERROR = -1
291
293
310
325
339
354err_t magaccel_generic_write ( magaccel_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
355
370err_t magaccel_generic_read ( magaccel_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
371
384err_t magaccel_get_device_id ( magaccel_t *ctx, uint8_t *device_id );
385
398err_t magaccel_get_status ( magaccel_t *ctx, uint8_t *status );
399
412
425err_t magaccel_set_op_mode ( magaccel_t *ctx, uint8_t op_mode );
426
439err_t magaccel_set_fsr ( magaccel_t *ctx, uint8_t fsr );
440
453
467
479
491
492#ifdef __cplusplus
493}
494#endif
495#endif // MAGACCEL_H
496
497 // magaccel
498
499// ------------------------------------------------------------------------ END
err_t magaccel_sw_reset(magaccel_t *ctx)
MagAccel soft reset function.
err_t magaccel_set_op_mode(magaccel_t *ctx, uint8_t op_mode)
MagAccel set operating mode function.
err_t magaccel_en_data_ready(magaccel_t *ctx)
MagAccel enable data ready function.
err_t magaccel_default_cfg(magaccel_t *ctx)
MagAccel default configuration function.
err_t magaccel_generic_write(magaccel_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
MagAccel I2C writing function.
err_t magaccel_generic_read(magaccel_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
MagAccel I2C reading function.
uint8_t magaccel_check_data_ready(magaccel_t *ctx)
MagAccel get data ready function.
err_t magaccel_get_status(magaccel_t *ctx, uint8_t *status)
MagAccel get status function.
void magaccel_cfg_setup(magaccel_cfg_t *cfg)
MagAccel configuration object setup function.
uint8_t magaccel_check_mag_field(magaccel_t *ctx)
MagAccel checks the magnetic field function.
err_t magaccel_init(magaccel_t *ctx, magaccel_cfg_t *cfg)
MagAccel initialization function.
err_t magaccel_set_fsr(magaccel_t *ctx, uint8_t fsr)
MagAccel set FSR function.
err_t magaccel_get_device_id(magaccel_t *ctx, uint8_t *device_id)
MagAccel get device ID function.
err_t magaccel_get_axes_data(magaccel_t *ctx, magaccel_axes_t *axes)
MagAccel get the accel sensor axes function.
magaccel_return_value_t
MagAccel Click return value data.
Definition magaccel.h:288
@ MAGACCEL_ERROR
Definition magaccel.h:290
@ MAGACCEL_OK
Definition magaccel.h:289
MagAccel Click axes data.
Definition magaccel.h:276
float y
Definition magaccel.h:278
float x
Definition magaccel.h:277
float z
Definition magaccel.h:279
MagAccel Click configuration object.
Definition magaccel.h:259
uint32_t i2c_speed
Definition magaccel.h:266
pin_name_t fld
Definition magaccel.h:263
pin_name_t scl
Definition magaccel.h:260
pin_name_t sda
Definition magaccel.h:261
pin_name_t mot
Definition magaccel.h:264
uint8_t i2c_address
Definition magaccel.h:267
MagAccel Click context object.
Definition magaccel.h:238
digital_in_t fld
Definition magaccel.h:240
i2c_master_t i2c
Definition magaccel.h:244
digital_in_t mot
Definition magaccel.h:241
float sensitivity
Definition magaccel.h:250
uint8_t slave_address
Definition magaccel.h:247