accel29  2.1.0.0
accel29.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 ACCEL29_H
29 #define ACCEL29_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_i2c_master.h"
48 #include "drv_spi_master.h"
49 #include "spi_specifics.h"
50 
71 #define ACCEL29_REG_DEVID 0x00
72 #define ACCEL29_REG_OFSX 0x1E
73 #define ACCEL29_REG_OFSY 0x1F
74 #define ACCEL29_REG_OFSZ 0x20
75 #define ACCEL29_REG_THRESH_ACT 0x24
76 #define ACCEL29_REG_THRESH_INACT 0x25
77 #define ACCEL29_REG_TIME_INACT 0x26
78 #define ACCEL29_REG_ACT_INACT_CTL 0x27
79 #define ACCEL29_REG_BW_RATE 0x2C
80 #define ACCEL29_REG_POWER_CTL 0x2D
81 #define ACCEL29_REG_INT_ENABLE 0x2E
82 #define ACCEL29_REG_INT_MAP 0x2F
83 #define ACCEL29_REG_INT_SOURCE 0x30
84 #define ACCEL29_REG_DATA_FORMAT 0x31
85 #define ACCEL29_REG_DATA_X0 0x32
86 #define ACCEL29_REG_DATA_X1 0x33
87 #define ACCEL29_REG_DATA_Y0 0x34
88 #define ACCEL29_REG_DATA_Y1 0x35
89 #define ACCEL29_REG_DATA_Z0 0x36
90 #define ACCEL29_REG_DATA_Z1 0x37
91 #define ACCEL29_REG_FIFO_CTL 0x38
92 #define ACCEL29_REG_FIFO_STATUS 0x39
93  // accel29_reg
95 
110 #define ACCEL29_INT_ENABLE_DATA_READY 0x80
111 #define ACCEL29_INT_ENABLE_ACTIVITY 0x10
112 #define ACCEL29_INT_ENABLE_INACTIVITY 0x08
113 #define ACCEL29_INT_ENABLE_OVERRUN 0x01
114 
119 #define ACCEL29_INT_MAP_DATA_READY_INT2 0x80
120 #define ACCEL29_INT_MAP_DATA_READY_INT1 0x00
121 #define ACCEL29_INT_MAP_DATA_READY_MASK 0x80
122 #define ACCEL29_INT_MAP_ACTIVITY_INT2 0x10
123 #define ACCEL29_INT_MAP_ACTIVITY_INT1 0x00
124 #define ACCEL29_INT_MAP_ACTIVITY_MASK 0x10
125 #define ACCEL29_INT_MAP_INACTIVITY_INT2 0x08
126 #define ACCEL29_INT_MAP_INACTIVITY_INT1 0x00
127 #define ACCEL29_INT_MAP_INACTIVITY_MASK 0x08
128 #define ACCEL29_INT_MAP_OVERRUN_INT2 0x01
129 #define ACCEL29_INT_MAP_OVERRUN_INT1 0x00
130 #define ACCEL29_INT_MAP_OVERRUN_MASK 0x01
131 
136 #define ACCEL29_INT_SOURCE_DATA_READY 0x80
137 #define ACCEL29_INT_SOURCE_ACTIVITY 0x10
138 #define ACCEL29_INT_SOURCE_INACTIVITY 0x08
139 #define ACCEL29_INT_SOURCE_OVERRUN 0x01
140 
145 #define ACCEL29_BW_RATE_LOW_POWER 0x10
146 #define ACCEL29_BW_RATE_6_25_HZ 0x06
147 #define ACCEL29_BW_RATE_12_5_HZ 0x07
148 #define ACCEL29_BW_RATE_25_HZ 0x08
149 #define ACCEL29_BW_RATE_50_HZ 0x09
150 #define ACCEL29_BW_RATE_100_HZ 0x0A
151 #define ACCEL29_BW_RATE_200_HZ 0x0B
152 #define ACCEL29_BW_RATE_400_HZ 0x0C
153 #define ACCEL29_BW_RATE_800_HZ 0x0D
154 #define ACCEL29_BW_RATE_1600_HZ 0x0E
155 #define ACCEL29_BW_RATE_3200_HZ 0x0F
156 
161 #define ACCEL29_POWER_CTL_LINK 0x20
162 #define ACCEL29_POWER_CTL_AUTO_SLEEP 0x10
163 #define ACCEL29_POWER_CTL_MEASURE 0x08
164 #define ACCEL29_POWER_CTL_SLEEP 0x04
165 #define ACCEL29_POWER_CTL_WAKEUP_8_HZ 0x00
166 #define ACCEL29_POWER_CTL_WAKEUP_4_HZ 0x01
167 #define ACCEL29_POWER_CTL_WAKEUP_2_HZ 0x02
168 #define ACCEL29_POWER_CTL_WAKEUP_1_HZ 0x03
169 #define ACCEL29_POWER_CTL_WAKEUP_MASK 0x03
170 
175 #define ACCEL29_DEVID 0xE5
176 
181 #define ACCEL29_OFS_RESET 0
182 #define ACCEL29_OFS_SCALE_FACTOR_G_PER_LSB 0.195f
183 
188 #define ACCEL29_SCALE_FACTOR_LSB_PER_G 20.48f
189 
194 #define ACCEL29_NUM_OF_SAMPLES 100
195 
200 #define ACCEL29_SPI_RW_BIT 0x80
201 #define ACCEL29_SPI_MB_BIT 0x40
202 #define ACCEL29_ADDRESS_MASK 0x3F
203 
209 #define ACCEL29_DEVICE_ADDRESS_0 0x53
210 #define ACCEL29_DEVICE_ADDRESS_1 0x1D
211 
220 #define ACCEL29_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
221 #define ACCEL29_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
222  // accel29_set
224 
239 #define ACCEL29_MAP_MIKROBUS( cfg, mikrobus ) \
240  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
241  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
242  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
243  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
244  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
245  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
246  cfg.int1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
247  cfg.int2 = MIKROBUS( mikrobus, MIKROBUS_INT )
248  // accel29_map // accel29
251 
256 typedef enum
257 {
262 
267 typedef err_t ( *accel29_master_io_t )( struct accel29_s*, uint8_t, uint8_t*, uint8_t );
273 typedef struct accel29_s
274 {
275  digital_in_t int1;
276  digital_in_t int2;
278  i2c_master_t i2c;
279  spi_master_t spi;
281  uint8_t slave_address;
282  pin_name_t chip_select;
289 
294 typedef struct
295 {
296  pin_name_t scl;
297  pin_name_t sda;
298  pin_name_t miso;
299  pin_name_t mosi;
300  pin_name_t sck;
301  pin_name_t cs;
302  pin_name_t int1;
303  pin_name_t int2;
305  uint32_t i2c_speed;
306  uint8_t i2c_address;
308  uint32_t spi_speed;
309  spi_master_mode_t spi_mode;
310  spi_master_chip_select_polarity_t cs_polarity;
314 } accel29_cfg_t;
315 
320 typedef struct
321 {
322  float x;
323  float y;
324  float z;
327 
332 typedef enum
333 {
335  ACCEL29_ERROR = -1
336 
338 
355 
371 
385 err_t accel29_init ( accel29_t *ctx, accel29_cfg_t *cfg );
386 
400 
415 err_t accel29_generic_write ( accel29_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
416 
431 err_t accel29_generic_read ( accel29_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
432 
445 err_t accel29_write_register ( accel29_t *ctx, uint8_t reg, uint8_t data_in );
446 
459 err_t accel29_read_register ( accel29_t *ctx, uint8_t reg, uint8_t *data_out );
460 
472 
482 
492 
506 
521 err_t accel29_get_avg_axes ( accel29_t *ctx, uint16_t num_samples, accel29_axes_t *avg_axes );
522 
537 
538 #ifdef __cplusplus
539 }
540 #endif
541 #endif // ACCEL29_H
542  // accel29
544 
545 // ------------------------------------------------------------------------ END
accel29_generic_read
err_t accel29_generic_read(accel29_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Accel 29 data reading function.
accel29_cfg_t::mosi
pin_name_t mosi
Definition: accel29.h:299
accel29_t
struct accel29_s accel29_t
Accel 29 Click context object.
accel29_cfg_t::drv_sel
accel29_drv_t drv_sel
Definition: accel29.h:312
accel29_cfg_t::sck
pin_name_t sck
Definition: accel29.h:300
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel29_s::spi
spi_master_t spi
Definition: accel29.h:279
accel29_cfg_t::int1
pin_name_t int1
Definition: accel29.h:302
accel29_get_avg_axes
err_t accel29_get_avg_axes(accel29_t *ctx, uint16_t num_samples, accel29_axes_t *avg_axes)
Accel 29 get avg axes function.
accel29_cfg_setup
void accel29_cfg_setup(accel29_cfg_t *cfg)
Accel 29 configuration object setup function.
accel29_s::write_f
accel29_master_io_t write_f
Definition: accel29.h:285
accel29_return_value_t
accel29_return_value_t
Accel 29 Click return value data.
Definition: accel29.h:333
accel29_cfg_t::sda
pin_name_t sda
Definition: accel29.h:297
accel29_s
Accel 29 Click context object.
Definition: accel29.h:274
accel29_read_register
err_t accel29_read_register(accel29_t *ctx, uint8_t reg, uint8_t *data_out)
Accel 29 read register function.
accel29_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel29.h:308
accel29_s::int1
digital_in_t int1
Definition: accel29.h:275
accel29_s::read_f
accel29_master_io_t read_f
Definition: accel29.h:286
accel29_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel29.h:305
accel29_drv_t
accel29_drv_t
Accel 29 Click driver selector.
Definition: accel29.h:257
accel29_axes_t
Accel 29 Click Axes data structure.
Definition: accel29.h:321
accel29_default_cfg
err_t accel29_default_cfg(accel29_t *ctx)
Accel 29 default configuration function.
accel29_get_int2_pin
uint8_t accel29_get_int2_pin(accel29_t *ctx)
Accel 29 get int2 pin function.
accel29_check_communication
err_t accel29_check_communication(accel29_t *ctx)
Accel 29 check communication function.
accel29_get_axes
err_t accel29_get_axes(accel29_t *ctx, accel29_axes_t *axes)
Accel 29 get axes function.
accel29_cfg_t
Accel 29 Click configuration object.
Definition: accel29.h:295
ACCEL29_DRV_SEL_SPI
@ ACCEL29_DRV_SEL_SPI
Definition: accel29.h:258
accel29_axes_t::z
float z
Definition: accel29.h:324
accel29_master_io_t
err_t(* accel29_master_io_t)(struct accel29_s *, uint8_t, uint8_t *, uint8_t)
Accel 29 Click driver interface.
Definition: accel29.h:267
ACCEL29_ERROR
@ ACCEL29_ERROR
Definition: accel29.h:335
accel29_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel29.h:309
ACCEL29_DRV_SEL_I2C
@ ACCEL29_DRV_SEL_I2C
Definition: accel29.h:259
accel29_cfg_t::miso
pin_name_t miso
Definition: accel29.h:298
accel29_axes_t::y
float y
Definition: accel29.h:323
accel29_write_register
err_t accel29_write_register(accel29_t *ctx, uint8_t reg, uint8_t data_in)
Accel 29 write register function.
accel29_drv_interface_selection
void accel29_drv_interface_selection(accel29_cfg_t *cfg, accel29_drv_t drv_sel)
Accel 29 driver interface setup function.
accel29_cfg_t::cs
pin_name_t cs
Definition: accel29.h:301
accel29_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel29.h:310
accel29_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel29.h:306
accel29_axes_t::x
float x
Definition: accel29.h:322
accel29_cfg_t::int2
pin_name_t int2
Definition: accel29.h:303
accel29_s::drv_sel
accel29_drv_t drv_sel
Definition: accel29.h:283
accel29_s::slave_address
uint8_t slave_address
Definition: accel29.h:281
accel29_s::int2
digital_in_t int2
Definition: accel29.h:276
accel29_get_int1_pin
uint8_t accel29_get_int1_pin(accel29_t *ctx)
Accel 29 get int1 pin function.
accel29_cfg_t::scl
pin_name_t scl
Definition: accel29.h:296
accel29_init
err_t accel29_init(accel29_t *ctx, accel29_cfg_t *cfg)
Accel 29 initialization function.
ACCEL29_OK
@ ACCEL29_OK
Definition: accel29.h:334
accel29_s::chip_select
pin_name_t chip_select
Definition: accel29.h:282
accel29_s::i2c
i2c_master_t i2c
Definition: accel29.h:278
accel29_calibrate_offset
err_t accel29_calibrate_offset(accel29_t *ctx, accel29_axes_t calib_axes)
Accel 29 calibrate offset function.
accel29_generic_write
err_t accel29_generic_write(accel29_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Accel 29 data writing function.