accel22  2.0.0.0
accel22.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 ACCEL22_H
29 #define ACCEL22_H
30 
31 #ifdef __cplusplus
32 extern "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 #include "drv_spi_master.h"
53 #include "spi_specifics.h"
54 
75 #define ACCEL22_REG_DEVID_AD 0x00
76 #define ACCEL22_REG_DEVID_MST 0x01
77 #define ACCEL22_REG_PART_ID 0x02
78 #define ACCEL22_REG_REV_ID 0x03
79 #define ACCEL22_REG_SERIAL_NUMBER_3 0x04
80 #define ACCEL22_REG_SERIAL_NUMBER_2 0x05
81 #define ACCEL22_REG_SERIAL_NUMBER_1 0x06
82 #define ACCEL22_REG_SERIAL_NUMBER_0 0x07
83 #define ACCEL22_REG_XDATA 0x08
84 #define ACCEL22_REG_YDATA 0x09
85 #define ACCEL22_REG_ZDATA 0x0A
86 #define ACCEL22_REG_STATUS 0x0B
87 #define ACCEL22_REG_FIFO_ENTRIES_L 0x0C
88 #define ACCEL22_REG_FIFO_ENTRIES_H 0x0D
89 #define ACCEL22_REG_XDATA_H 0x0E
90 #define ACCEL22_REG_XDATA_L 0x0F
91 #define ACCEL22_REG_YDATA_H 0x10
92 #define ACCEL22_REG_YDATA_L 0x11
93 #define ACCEL22_REG_ZDATA_H 0x12
94 #define ACCEL22_REG_ZDATA_L 0x13
95 #define ACCEL22_REG_TEMP_H 0x14
96 #define ACCEL22_REG_TEMP_L 0x15
97 #define ACCEL22_REG_EX_ADC_H 0x16
98 #define ACCEL22_REG_EX_ADC_L 0x17
99 #define ACCEL22_REG_I2C_FIFO_DATA 0x18
100 #define ACCEL22_REG_SOFT_RESET 0x19
101 #define ACCEL22_REG_THRESH_ACT_H 0x20
102 #define ACCEL22_REG_THRESH_ACT_L 0x21
103 #define ACCEL22_REG_TIME_ACT 0x22
104 #define ACCEL22_REG_THRESH_INACT_H 0x23
105 #define ACCEL22_REG_THRESH_INACT_L 0x24
106 #define ACCEL22_REG_TIME_INACT_H 0x25
107 #define ACCEL22_REG_TIME_INACT_L 0x26
108 #define ACCEL22_REG_ACT_INACT_CTL 0x27
109 #define ACCEL22_REG_FIFO_CONTROL 0x28
110 #define ACCEL22_REG_FIFO_SAMPLES 0x29
111 #define ACCEL22_REG_INTMAP1_LOWER 0x2A
112 #define ACCEL22_REG_INTMAP2_LOWER 0x2B
113 #define ACCEL22_REG_FILTER_CTL 0x2C
114 #define ACCEL22_REG_POWER_CTL 0x2D
115 #define ACCEL22_REG_SELF_TEST 0x2E
116 #define ACCEL22_REG_TAP_THRESH 0x2F
117 #define ACCEL22_REG_TAP_DUR 0x30
118 #define ACCEL22_REG_TAP_LATENT 0x31
119 #define ACCEL22_REG_TAP_WINDOW 0x32
120 #define ACCEL22_REG_X_OFFSET 0x33
121 #define ACCEL22_REG_Y_OFFSET 0x34
122 #define ACCEL22_REG_Z_OFFSET 0x35
123 #define ACCEL22_REG_X_SENS 0x36
124 #define ACCEL22_REG_Y_SENS 0x37
125 #define ACCEL22_REG_Z_SENS 0x38
126 #define ACCEL22_REG_TIMER_CTL 0x39
127 #define ACCEL22_REG_INTMAP1_UPPER 0x3A
128 #define ACCEL22_REG_INTMAP2_UPPER 0x3B
129 #define ACCEL22_REG_ADC_CTL 0x3C
130 #define ACCEL22_REG_TEMP_CTL 0x3D
131 #define ACCEL22_REG_TEMP_ADC_OVER_THRSH_H 0x3E
132 #define ACCEL22_REG_TEMP_ADC_OVER_THRSH_L 0x3F
133 #define ACCEL22_REG_TEMP_ADC_UNDER_THRSH_H 0x40
134 #define ACCEL22_REG_TEMP_ADC_UNDER_THRSH_L 0x41
135 #define ACCEL22_REG_TEMP_ADC_TIMER 0x42
136 #define ACCEL22_REG_AXIS_MASK 0x43
137 #define ACCEL22_REG_STATUS_COPY 0x44
138 #define ACCEL22_REG_STATUS_2 0x45
139  // accel22_reg
141 
156 #define ACCEL22_INTMAP1_LOWER_INT_LOW 0x80
157 #define ACCEL22_INTMAP1_LOWER_AWAKE 0x40
158 #define ACCEL22_INTMAP1_LOWER_INACT 0x20
159 #define ACCEL22_INTMAP1_LOWER_ACT 0x10
160 #define ACCEL22_INTMAP1_LOWER_FIFO_OR 0x08
161 #define ACCEL22_INTMAP1_LOWER_FIFO_WM 0x04
162 #define ACCEL22_INTMAP1_LOWER_FIFO_RDY 0x02
163 #define ACCEL22_INTMAP1_LOWER_DATA_RDY 0x01
164 
169 #define ACCEL22_POWER_CTL_EXT_CLK 0x40
170 #define ACCEL22_POWER_CTL_NOISE_NORMAL 0x00
171 #define ACCEL22_POWER_CTL_NOISE_LOW 0x10
172 #define ACCEL22_POWER_CTL_NOISE_ULTRALOW 0x20
173 #define ACCEL22_POWER_CTL_NOISE_BIT_MASK 0x30
174 #define ACCEL22_POWER_CTL_WAKEUP 0x08
175 #define ACCEL22_POWER_CTL_AUTOSLEEP 0x04
176 #define ACCEL22_POWER_CTL_STANDBY_MODE 0x00
177 #define ACCEL22_POWER_CTL_MEASUREMENT_MODE 0x02
178 
183 #define ACCEL22_SOFT_RESET 0x52
184 
189 #define ACCEL22_ENABLE_TEMPERATURE 0x01
190 #define ACCEL22_ENABLE_ADC 0x01
191 
196 #define ACCEL22_ACCEL_2G_RES 0.25
197 #define ACCEL22_ACCEL_4G_RES 0.5
198 #define ACCEL22_ACCEL_8G_RES 1.0
199 #define ACCEL22_ROOM_TEMPERATURE 25.0
200 #define ACCEL22_TEMPERATURE_BIAS 165
201 #define ACCEL22_TEMPERATURE_RES 54.0
202 #define ACCEL22_TEMPERATURE_CAL_SAMPLE_NUM 100
203 #define ACCEL22_ADC_VREG 1.098
204 #define ACCEL22_ADC_RES 0x3FFF
205 #define ACCEL22_ADC_OFFSET 0x2000
206 
211 #define ACCEL22_ADI_DEVICE_ID 0xAD
212 #define ACCEL22_MEMS_DEVICE_ID 0x1D
213 #define ACCEL22_PART_ID 0xF7
214 
219 #define ACCEL22_SPI_WRITE_REG 0x0A
220 #define ACCEL22_SPI_READ_REG 0x0B
221 #define ACCEL22_SPI_READ_FIFO 0x0D
222 
228 #define ACCEL22_DEVICE_ADDRESS_0 0x1D
229 #define ACCEL22_DEVICE_ADDRESS_1 0x53
230 
239 #define ACCEL22_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
240 #define ACCEL22_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
241  // accel22_set
243 
258 #define ACCEL22_MAP_MIKROBUS( cfg, mikrobus ) \
259  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
260  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
261  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
262  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
263  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
264  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
265  cfg.int2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
266  cfg.int1 = MIKROBUS( mikrobus, MIKROBUS_INT )
267  // accel22_map // accel22
270 
275 typedef enum
276 {
281 
286 typedef err_t ( *accel22_master_io_t )( struct accel22_s*, uint8_t, uint8_t*, uint8_t );
292 typedef struct accel22_s
293 {
294  digital_in_t int2;
295  digital_in_t int1;
297  i2c_master_t i2c;
298  spi_master_t spi;
300  uint8_t slave_address;
301  pin_name_t chip_select;
306  float resolution;
310 
315 typedef struct
316 {
317  pin_name_t scl;
318  pin_name_t sda;
319  pin_name_t miso;
320  pin_name_t mosi;
321  pin_name_t sck;
322  pin_name_t cs;
323  pin_name_t int2;
324  pin_name_t int1;
326  uint32_t i2c_speed;
327  uint8_t i2c_address;
329  uint32_t spi_speed;
330  spi_master_mode_t spi_mode;
331  spi_master_chip_select_polarity_t cs_polarity;
335 } accel22_cfg_t;
336 
341 typedef struct
342 {
343  float x;
344  float y;
345  float z;
348 
353 typedef enum
354 {
355  ACCEL22_2G = 0x00,
356  ACCEL22_4G = 0x01,
357  ACCEL22_8G = 0x02,
359 
361 
366 typedef enum
367 {
374  ACCEL22_ODR_BIT_MASK = 0x07
375 
377 
382 typedef enum
383 {
385  ACCEL22_ERROR = -1
386 
388 
405 
421 
435 err_t accel22_init ( accel22_t *ctx, accel22_cfg_t *cfg );
436 
450 
465 err_t accel22_generic_write ( accel22_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
466 
481 err_t accel22_generic_read ( accel22_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
482 
495 err_t accel22_write_register ( accel22_t *ctx, uint8_t reg, uint8_t data_in );
496 
509 err_t accel22_read_register ( accel22_t *ctx, uint8_t reg, uint8_t *data_out );
510 
522 
532 
542 
558 
572 
584 
596 
608 err_t accel22_calibrate_temperature ( accel22_t *ctx, float room_temperature );
609 
622 err_t accel22_get_temperature ( accel22_t *ctx, float *temperature );
623 
636 err_t accel22_get_adc ( accel22_t *ctx, float *v_adc );
637 
638 #ifdef __cplusplus
639 }
640 #endif
641 #endif // ACCEL22_H
642  // accel22
644 
645 // ------------------------------------------------------------------------ END
accel22_cfg_t::int1
pin_name_t int1
Definition: accel22.h:324
accel22_cfg_t::sda
pin_name_t sda
Definition: accel22.h:318
accel22_cfg_t::miso
pin_name_t miso
Definition: accel22.h:319
accel22_set_filter_config
err_t accel22_set_filter_config(accel22_t *ctx, accel22_range_t range, accel22_odr_t odr)
Accel 22 set filter config function.
accel22_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel22.h:330
accel22_axes_t::x
float x
Definition: accel22.h:343
accel22_get_adc
err_t accel22_get_adc(accel22_t *ctx, float *v_adc)
Accel 22 get adc function.
accel22_t
struct accel22_s accel22_t
Accel 22 Click context object.
ACCEL22_DRV_SEL_I2C
@ ACCEL22_DRV_SEL_I2C
Definition: accel22.h:278
ACCEL22_12p5HZ
@ ACCEL22_12p5HZ
Definition: accel22.h:368
accel22_read_register
err_t accel22_read_register(accel22_t *ctx, uint8_t reg, uint8_t *data_out)
Accel 22 read register function.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel22_cfg_t::cs
pin_name_t cs
Definition: accel22.h:322
accel22_return_value_t
accel22_return_value_t
Accel 22 Click return value data.
Definition: accel22.h:383
accel22_s::spi
spi_master_t spi
Definition: accel22.h:298
accel22_cfg_t::mosi
pin_name_t mosi
Definition: accel22.h:320
ACCEL22_RANGE_BIT_MASK
@ ACCEL22_RANGE_BIT_MASK
Definition: accel22.h:358
accel22_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel22.h:329
ACCEL22_200HZ
@ ACCEL22_200HZ
Definition: accel22.h:372
ACCEL22_ERROR
@ ACCEL22_ERROR
Definition: accel22.h:385
ACCEL22_25HZ
@ ACCEL22_25HZ
Definition: accel22.h:369
accel22_range_t
accel22_range_t
Accel 22 Click accel range selection.
Definition: accel22.h:354
accel22_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel22.h:331
accel22_drv_interface_selection
void accel22_drv_interface_selection(accel22_cfg_t *cfg, accel22_drv_t drv_sel)
Accel 22 driver interface setup function.
accel22_enable_adc_measurement
err_t accel22_enable_adc_measurement(accel22_t *ctx)
Accel 22 enable ADC measurement function.
accel22_drv_t
accel22_drv_t
Accel 22 Click driver selector.
Definition: accel22.h:276
ACCEL22_50HZ
@ ACCEL22_50HZ
Definition: accel22.h:370
accel22_odr_t
accel22_odr_t
Accel 22 Click output data rate.
Definition: accel22.h:367
accel22_cfg_t::int2
pin_name_t int2
Definition: accel22.h:323
accel22_s::int1
digital_in_t int1
Definition: accel22.h:295
accel22_get_temperature
err_t accel22_get_temperature(accel22_t *ctx, float *temperature)
Accel 22 get temperature function.
accel22_default_cfg
err_t accel22_default_cfg(accel22_t *ctx)
Accel 22 default configuration function.
accel22_cfg_t
Accel 22 Click configuration object.
Definition: accel22.h:316
accel22_calibrate_temperature
err_t accel22_calibrate_temperature(accel22_t *ctx, float room_temperature)
Accel 22 calibrate temperature function.
accel22_s::room_temp_offset
float room_temp_offset
Definition: accel22.h:307
ACCEL22_400HZ
@ ACCEL22_400HZ
Definition: accel22.h:373
accel22_cfg_t::sck
pin_name_t sck
Definition: accel22.h:321
accel22_get_int2_pin
uint8_t accel22_get_int2_pin(accel22_t *ctx)
Accel 22 get int2 pin function.
accel22_axes_t
Accel 22 Click Axes data structure.
Definition: accel22.h:342
accel22_get_int1_pin
uint8_t accel22_get_int1_pin(accel22_t *ctx)
Accel 22 get int1 pin function.
accel22_check_communication
err_t accel22_check_communication(accel22_t *ctx)
Accel 22 check communication function.
accel22_init
err_t accel22_init(accel22_t *ctx, accel22_cfg_t *cfg)
Accel 22 initialization function.
accel22_axes_t::z
float z
Definition: accel22.h:345
ACCEL22_2G
@ ACCEL22_2G
Definition: accel22.h:355
ACCEL22_ODR_BIT_MASK
@ ACCEL22_ODR_BIT_MASK
Definition: accel22.h:374
accel22_cfg_setup
void accel22_cfg_setup(accel22_cfg_t *cfg)
Accel 22 configuration object setup function.
accel22_s::write_f
accel22_master_io_t write_f
Definition: accel22.h:304
accel22_get_axes
err_t accel22_get_axes(accel22_t *ctx, accel22_axes_t *axes)
Accel 22 get axes function.
ACCEL22_DRV_SEL_SPI
@ ACCEL22_DRV_SEL_SPI
Definition: accel22.h:277
accel22_s::drv_sel
accel22_drv_t drv_sel
Definition: accel22.h:302
ACCEL22_8G
@ ACCEL22_8G
Definition: accel22.h:357
accel22_enable_temperature_measurement
err_t accel22_enable_temperature_measurement(accel22_t *ctx)
Accel 22 enable temperature measurement function.
accel22_s::resolution
float resolution
Definition: accel22.h:306
ACCEL22_4G
@ ACCEL22_4G
Definition: accel22.h:356
accel22_cfg_t::drv_sel
accel22_drv_t drv_sel
Definition: accel22.h:333
accel22_cfg_t::scl
pin_name_t scl
Definition: accel22.h:317
accel22_s::chip_select
pin_name_t chip_select
Definition: accel22.h:301
accel22_s::i2c
i2c_master_t i2c
Definition: accel22.h:297
ACCEL22_100HZ
@ ACCEL22_100HZ
Definition: accel22.h:371
accel22_s::slave_address
uint8_t slave_address
Definition: accel22.h:300
accel22_generic_read
err_t accel22_generic_read(accel22_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Accel 22 data reading function.
accel22_write_register
err_t accel22_write_register(accel22_t *ctx, uint8_t reg, uint8_t data_in)
Accel 22 write register function.
accel22_master_io_t
err_t(* accel22_master_io_t)(struct accel22_s *, uint8_t, uint8_t *, uint8_t)
Accel 22 Click driver interface.
Definition: accel22.h:286
accel22_s::int2
digital_in_t int2
Definition: accel22.h:294
accel22_s
Accel 22 Click context object.
Definition: accel22.h:293
accel22_generic_write
err_t accel22_generic_write(accel22_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Accel 22 data writing function.
accel22_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel22.h:327
accel22_axes_t::y
float y
Definition: accel22.h:344
accel22_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel22.h:326
ACCEL22_OK
@ ACCEL22_OK
Definition: accel22.h:384
accel22_s::read_f
accel22_master_io_t read_f
Definition: accel22.h:305