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 "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_i2c_master.h"
38 #include "drv_spi_master.h"
39 #include "spi_specifics.h"
40 
61 #define ACCEL29_REG_DEVID 0x00
62 #define ACCEL29_REG_OFSX 0x1E
63 #define ACCEL29_REG_OFSY 0x1F
64 #define ACCEL29_REG_OFSZ 0x20
65 #define ACCEL29_REG_THRESH_ACT 0x24
66 #define ACCEL29_REG_THRESH_INACT 0x25
67 #define ACCEL29_REG_TIME_INACT 0x26
68 #define ACCEL29_REG_ACT_INACT_CTL 0x27
69 #define ACCEL29_REG_BW_RATE 0x2C
70 #define ACCEL29_REG_POWER_CTL 0x2D
71 #define ACCEL29_REG_INT_ENABLE 0x2E
72 #define ACCEL29_REG_INT_MAP 0x2F
73 #define ACCEL29_REG_INT_SOURCE 0x30
74 #define ACCEL29_REG_DATA_FORMAT 0x31
75 #define ACCEL29_REG_DATA_X0 0x32
76 #define ACCEL29_REG_DATA_X1 0x33
77 #define ACCEL29_REG_DATA_Y0 0x34
78 #define ACCEL29_REG_DATA_Y1 0x35
79 #define ACCEL29_REG_DATA_Z0 0x36
80 #define ACCEL29_REG_DATA_Z1 0x37
81 #define ACCEL29_REG_FIFO_CTL 0x38
82 #define ACCEL29_REG_FIFO_STATUS 0x39
83  // accel29_reg
85 
100 #define ACCEL29_INT_ENABLE_DATA_READY 0x80
101 #define ACCEL29_INT_ENABLE_ACTIVITY 0x10
102 #define ACCEL29_INT_ENABLE_INACTIVITY 0x08
103 #define ACCEL29_INT_ENABLE_OVERRUN 0x01
104 
109 #define ACCEL29_INT_MAP_DATA_READY_INT2 0x80
110 #define ACCEL29_INT_MAP_DATA_READY_INT1 0x00
111 #define ACCEL29_INT_MAP_DATA_READY_MASK 0x80
112 #define ACCEL29_INT_MAP_ACTIVITY_INT2 0x10
113 #define ACCEL29_INT_MAP_ACTIVITY_INT1 0x00
114 #define ACCEL29_INT_MAP_ACTIVITY_MASK 0x10
115 #define ACCEL29_INT_MAP_INACTIVITY_INT2 0x08
116 #define ACCEL29_INT_MAP_INACTIVITY_INT1 0x00
117 #define ACCEL29_INT_MAP_INACTIVITY_MASK 0x08
118 #define ACCEL29_INT_MAP_OVERRUN_INT2 0x01
119 #define ACCEL29_INT_MAP_OVERRUN_INT1 0x00
120 #define ACCEL29_INT_MAP_OVERRUN_MASK 0x01
121 
126 #define ACCEL29_INT_SOURCE_DATA_READY 0x80
127 #define ACCEL29_INT_SOURCE_ACTIVITY 0x10
128 #define ACCEL29_INT_SOURCE_INACTIVITY 0x08
129 #define ACCEL29_INT_SOURCE_OVERRUN 0x01
130 
135 #define ACCEL29_BW_RATE_LOW_POWER 0x10
136 #define ACCEL29_BW_RATE_6_25_HZ 0x06
137 #define ACCEL29_BW_RATE_12_5_HZ 0x07
138 #define ACCEL29_BW_RATE_25_HZ 0x08
139 #define ACCEL29_BW_RATE_50_HZ 0x09
140 #define ACCEL29_BW_RATE_100_HZ 0x0A
141 #define ACCEL29_BW_RATE_200_HZ 0x0B
142 #define ACCEL29_BW_RATE_400_HZ 0x0C
143 #define ACCEL29_BW_RATE_800_HZ 0x0D
144 #define ACCEL29_BW_RATE_1600_HZ 0x0E
145 #define ACCEL29_BW_RATE_3200_HZ 0x0F
146 
151 #define ACCEL29_POWER_CTL_LINK 0x20
152 #define ACCEL29_POWER_CTL_AUTO_SLEEP 0x10
153 #define ACCEL29_POWER_CTL_MEASURE 0x08
154 #define ACCEL29_POWER_CTL_SLEEP 0x04
155 #define ACCEL29_POWER_CTL_WAKEUP_8_HZ 0x00
156 #define ACCEL29_POWER_CTL_WAKEUP_4_HZ 0x01
157 #define ACCEL29_POWER_CTL_WAKEUP_2_HZ 0x02
158 #define ACCEL29_POWER_CTL_WAKEUP_1_HZ 0x03
159 #define ACCEL29_POWER_CTL_WAKEUP_MASK 0x03
160 
165 #define ACCEL29_DEVID 0xE5
166 
171 #define ACCEL29_OFS_RESET 0
172 #define ACCEL29_OFS_SCALE_FACTOR_G_PER_LSB 0.195f
173 
178 #define ACCEL29_SCALE_FACTOR_LSB_PER_G 20.48f
179 
184 #define ACCEL29_NUM_OF_SAMPLES 100
185 
190 #define ACCEL29_SPI_RW_BIT 0x80
191 #define ACCEL29_SPI_MB_BIT 0x40
192 #define ACCEL29_ADDRESS_MASK 0x3F
193 
199 #define ACCEL29_DEVICE_ADDRESS_0 0x53
200 #define ACCEL29_DEVICE_ADDRESS_1 0x1D
201 
210 #define ACCEL29_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
211 #define ACCEL29_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
212  // accel29_set
214 
229 #define ACCEL29_MAP_MIKROBUS( cfg, mikrobus ) \
230  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
231  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
232  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
233  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
234  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
235  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
236  cfg.int1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
237  cfg.int2 = MIKROBUS( mikrobus, MIKROBUS_INT )
238  // accel29_map // accel29
241 
246 typedef enum
247 {
252 
257 typedef err_t ( *accel29_master_io_t )( struct accel29_s*, uint8_t, uint8_t*, uint8_t );
263 typedef struct accel29_s
264 {
265  digital_in_t int1;
266  digital_in_t int2;
268  i2c_master_t i2c;
269  spi_master_t spi;
271  uint8_t slave_address;
272  pin_name_t chip_select;
279 
284 typedef struct
285 {
286  pin_name_t scl;
287  pin_name_t sda;
288  pin_name_t miso;
289  pin_name_t mosi;
290  pin_name_t sck;
291  pin_name_t cs;
292  pin_name_t int1;
293  pin_name_t int2;
295  uint32_t i2c_speed;
296  uint8_t i2c_address;
298  uint32_t spi_speed;
299  spi_master_mode_t spi_mode;
300  spi_master_chip_select_polarity_t cs_polarity;
304 } accel29_cfg_t;
305 
310 typedef struct
311 {
312  float x;
313  float y;
314  float z;
317 
322 typedef enum
323 {
325  ACCEL29_ERROR = -1
326 
328 
345 
361 
375 err_t accel29_init ( accel29_t *ctx, accel29_cfg_t *cfg );
376 
390 
405 err_t accel29_generic_write ( accel29_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
406 
421 err_t accel29_generic_read ( accel29_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
422 
435 err_t accel29_write_register ( accel29_t *ctx, uint8_t reg, uint8_t data_in );
436 
449 err_t accel29_read_register ( accel29_t *ctx, uint8_t reg, uint8_t *data_out );
450 
462 
472 
482 
496 
511 err_t accel29_get_avg_axes ( accel29_t *ctx, uint16_t num_samples, accel29_axes_t *avg_axes );
512 
527 
528 #ifdef __cplusplus
529 }
530 #endif
531 #endif // ACCEL29_H
532  // accel29
534 
535 // ------------------------------------------------------------------------ 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:289
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:302
accel29_cfg_t::sck
pin_name_t sck
Definition: accel29.h:290
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel29_s::spi
spi_master_t spi
Definition: accel29.h:269
accel29_cfg_t::int1
pin_name_t int1
Definition: accel29.h:292
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:275
accel29_return_value_t
accel29_return_value_t
Accel 29 Click return value data.
Definition: accel29.h:323
accel29_cfg_t::sda
pin_name_t sda
Definition: accel29.h:287
accel29_s
Accel 29 Click context object.
Definition: accel29.h:264
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:298
accel29_s::int1
digital_in_t int1
Definition: accel29.h:265
accel29_s::read_f
accel29_master_io_t read_f
Definition: accel29.h:276
accel29_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel29.h:295
accel29_drv_t
accel29_drv_t
Accel 29 Click driver selector.
Definition: accel29.h:247
accel29_axes_t
Accel 29 Click Axes data structure.
Definition: accel29.h:311
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:285
ACCEL29_DRV_SEL_SPI
@ ACCEL29_DRV_SEL_SPI
Definition: accel29.h:248
accel29_axes_t::z
float z
Definition: accel29.h:314
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:257
ACCEL29_ERROR
@ ACCEL29_ERROR
Definition: accel29.h:325
accel29_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel29.h:299
ACCEL29_DRV_SEL_I2C
@ ACCEL29_DRV_SEL_I2C
Definition: accel29.h:249
accel29_cfg_t::miso
pin_name_t miso
Definition: accel29.h:288
accel29_axes_t::y
float y
Definition: accel29.h:313
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:291
accel29_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel29.h:300
accel29_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel29.h:296
accel29_axes_t::x
float x
Definition: accel29.h:312
accel29_cfg_t::int2
pin_name_t int2
Definition: accel29.h:293
accel29_s::drv_sel
accel29_drv_t drv_sel
Definition: accel29.h:273
accel29_s::slave_address
uint8_t slave_address
Definition: accel29.h:271
accel29_s::int2
digital_in_t int2
Definition: accel29.h:266
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:286
accel29_init
err_t accel29_init(accel29_t *ctx, accel29_cfg_t *cfg)
Accel 29 initialization function.
ACCEL29_OK
@ ACCEL29_OK
Definition: accel29.h:324
accel29_s::chip_select
pin_name_t chip_select
Definition: accel29.h:272
accel29_s::i2c
i2c_master_t i2c
Definition: accel29.h:268
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.