accel31  2.1.0.0
accel31.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 ACCEL31_H
29 #define ACCEL31_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 ACCEL31_REG_CHIP_ID 0x00
76 #define ACCEL31_REG_HEALTH_STATUS 0x02
77 #define ACCEL31_REG_CMD_SUSPEND 0x04
78 #define ACCEL31_REG_CONFIG_STATUS 0x10
79 #define ACCEL31_REG_SENSOR_STATUS 0x11
80 #define ACCEL31_REG_INT_STATUS_INT1_0 0x12
81 #define ACCEL31_REG_INT_STATUS_INT1_1 0x13
82 #define ACCEL31_REG_INT_STATUS_INT2_0 0x14
83 #define ACCEL31_REG_INT_STATUS_INT2_1 0x15
84 #define ACCEL31_REG_INT_STATUS_I3C_0 0x16
85 #define ACCEL31_REG_INT_STATUS_I3C_1 0x17
86 #define ACCEL31_REG_ACC_DATA_0 0x18
87 #define ACCEL31_REG_ACC_DATA_1 0x19
88 #define ACCEL31_REG_ACC_DATA_2 0x1A
89 #define ACCEL31_REG_ACC_DATA_3 0x1B
90 #define ACCEL31_REG_ACC_DATA_4 0x1C
91 #define ACCEL31_REG_ACC_DATA_5 0x1D
92 #define ACCEL31_REG_TEMP_DATA 0x1E
93 #define ACCEL31_REG_SENSOR_TIME_0 0x1F
94 #define ACCEL31_REG_SENSOR_TIME_1 0x20
95 #define ACCEL31_REG_SENSOR_TIME_2 0x21
96 #define ACCEL31_REG_FIFO_LEVEL_0 0x22
97 #define ACCEL31_REG_FIFO_LEVEL_1 0x23
98 #define ACCEL31_REG_FIFO_DATA_OUT 0x24
99 #define ACCEL31_REG_AUX_DATA_0 0x2B
100 #define ACCEL31_REG_AUX_DATA_1 0x2C
101 #define ACCEL31_REG_ACC_CONF_0 0x30
102 #define ACCEL31_REG_ACC_CONF_1 0x31
103 #define ACCEL31_REG_ACC_CONF_2 0x32
104 #define ACCEL31_REG_TEMP_CONF 0x33
105 #define ACCEL31_REG_INT1_CONF 0x34
106 #define ACCEL31_REG_INT2_CONF 0x35
107 #define ACCEL31_REG_INT_MAP_0 0x36
108 #define ACCEL31_REG_INT_MAP_1 0x37
109 #define ACCEL31_REG_INT_MAP_2 0x38
110 #define ACCEL31_REG_INT_MAP_3 0x39
111 #define ACCEL31_REG_IF_CONF_0 0x3A
112 #define ACCEL31_REG_IF_CONF_1 0x3B
113 #define ACCEL31_REG_FIFO_CTRL 0x40
114 #define ACCEL31_REG_FIFO_CONF_0 0x41
115 #define ACCEL31_REG_FIFO_CONF_1 0x42
116 #define ACCEL31_REG_FIFO_WM_0 0x43
117 #define ACCEL31_REG_FIFO_WM_1 0x44
118 #define ACCEL31_REG_FEAT_ENG_CONF 0x50
119 #define ACCEL31_REG_FEAT_ENG_STATUS 0x51
120 #define ACCEL31_REG_FEAT_ENG_GP_FLAGS 0x52
121 #define ACCEL31_REG_FEAT_ENG_GPR_CONF 0x53
122 #define ACCEL31_REG_FEAT_ENG_GPR_CTRL 0x54
123 #define ACCEL31_REG_FEAT_ENG_GPR_0 0x55
124 #define ACCEL31_REG_FEAT_ENG_GPR_1 0x56
125 #define ACCEL31_REG_FEAT_ENG_GPR_2 0x57
126 #define ACCEL31_REG_FEATURE_DATA_ADDR 0x5E
127 #define ACCEL31_REG_FEATURE_DATA_TX 0x5F
128 #define ACCEL31_REG_ACC_OFFSET_0 0x70
129 #define ACCEL31_REG_ACC_OFFSET_1 0x71
130 #define ACCEL31_REG_ACC_OFFSET_2 0x72
131 #define ACCEL31_REG_ACC_OFFSET_3 0x73
132 #define ACCEL31_REG_ACC_OFFSET_4 0x74
133 #define ACCEL31_REG_ACC_OFFSET_5 0x75
134 #define ACCEL31_REG_ACC_SELF_TEST 0x76
135 #define ACCEL31_REG_CMD 0x7E
136  // accel31_reg
138 
153 #define ACCEL31_CHIP_ID 0xC4
154 
159 #define ACCEL31_CMD_SUSPEND_MODE_NORMAL 0x00
160 #define ACCEL31_CMD_SUSPEND_MODE_SUSPEND 0x01
161 #define ACCEL31_CMD_SUSPEND_MODE_MASK 0x01
162 
167 #define ACCEL31_SENSOR_STATUS_SENSOR_RDY 0x04
168 #define ACCEL31_SENSOR_STATUS_TEMP_RDY 0x02
169 #define ACCEL31_SENSOR_STATUS_ACC_RDY 0x01
170 
175 #define ACCEL31_ACC_CONF_0_SENSOR_DISABLE 0x00
176 #define ACCEL31_ACC_CONF_0_SENSOR_ENABLE 0x0F
177 #define ACCEL31_ACC_CONF_0_SENSOR_WRONG_CFG 0x0E
178 #define ACCEL31_ACC_CONF_0_SENSOR_MASK 0x0F
179 
184 #define ACCEL31_ACC_CONF_1_POWER_MODE_MASK 0x80
185 #define ACCEL31_ACC_CONF_1_ACC_BWP_MASK 0x70
186 #define ACCEL31_ACC_CONF_1_ACC_ODR_MASK 0x0F
187 
192 #define ACCEL31_ACC_CONF_2_ACC_DRDY_MASK 0x80
193 #define ACCEL31_ACC_CONF_2_NOISE_MODE_MASK 0x10
194 #define ACCEL31_ACC_CONF_2_ACC_IIR_MASK 0x0C
195 #define ACCEL31_ACC_CONF_2_ACC_RANGE_MASK 0x03
196 
201 #define ACCEL31_TEMP_CONF_EXT_SEL_MASK 0x10
202 #define ACCEL31_TEMP_CONF_MEAS_SRC_MASK 0x08
203 #define ACCEL31_TEMP_CONF_RATE_MASK 0x07
204 
209 #define ACCEL31_INT2_CONF_LVL_MASK 0x08
210 #define ACCEL31_INT2_CONF_OD_MASK 0x04
211 #define ACCEL31_INT2_CONF_MODE_OUT_DIS 0x00
212 #define ACCEL31_INT2_CONF_MODE_LATCH 0x01
213 #define ACCEL31_INT2_CONF_MODE_PULSE_SHORT 0x02
214 #define ACCEL31_INT2_CONF_MODE_PULSE_LONG 0x03
215 #define ACCEL31_INT2_CONF_MODE_MASK 0x03
216 
221 #define ACCEL31_CMD_SOFT_RESET 0xB6
222 
227 #define ACCEL31_ACCEL_ODR_1_5625_HZ 0
228 #define ACCEL31_ACCEL_ODR_3_125_HZ 1
229 #define ACCEL31_ACCEL_ODR_6_25_HZ 2
230 #define ACCEL31_ACCEL_ODR_12_5_HZ 3
231 #define ACCEL31_ACCEL_ODR_25_HZ 4
232 #define ACCEL31_ACCEL_ODR_50_HZ 5
233 #define ACCEL31_ACCEL_ODR_100_HZ 6
234 #define ACCEL31_ACCEL_ODR_200_HZ 7
235 #define ACCEL31_ACCEL_ODR_400_HZ 8
236 #define ACCEL31_ACCEL_ODR_800_HZ 9
237 #define ACCEL31_ACCEL_ODR_1600_HZ 10
238 #define ACCEL31_ACCEL_ODR_3200_HZ 11
239 #define ACCEL31_ACCEL_ODR_6400_HZ 12
240 
245 #define ACCEL31_ACCEL_FS_2G 0
246 #define ACCEL31_ACCEL_FS_4G 1
247 #define ACCEL31_ACCEL_FS_8G 2
248 #define ACCEL31_ACCEL_FS_16G 3
249 
254 #define ACCEL31_TEMP_ODR_1_5625_HZ 0
255 #define ACCEL31_TEMP_ODR_3_125_HZ 1
256 #define ACCEL31_TEMP_ODR_6_25_HZ 2
257 #define ACCEL31_TEMP_ODR_12_5_HZ 3
258 #define ACCEL31_TEMP_ODR_25_HZ 4
259 #define ACCEL31_TEMP_ODR_50_HZ 5
260 #define ACCEL31_TEMP_ODR_100_HZ 6
261 #define ACCEL31_TEMP_ODR_200_HZ 7
262 
267 #define ACCEL31_ACCEL_SENS_G_PER_LSB 0.000061
268 #define ACCEL31_TEMP_OFFSET 23
269 
275 #define ACCEL31_DEVICE_ADDRESS 0x18
276 
285 #define ACCEL31_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
286 #define ACCEL31_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
287  // accel31_set
289 
304 #define ACCEL31_MAP_MIKROBUS( cfg, mikrobus ) \
305  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
306  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
307  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
308  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
309  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
310  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
311  cfg.int2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
312  cfg.int1 = MIKROBUS( mikrobus, MIKROBUS_INT )
313  // accel31_map // accel31
316 
321 typedef enum
322 {
327 
332 struct accel31_s;
333 typedef err_t ( *accel31_master_io_t )( struct accel31_s*, uint8_t, uint8_t*, uint8_t );
339 typedef struct accel31_s
340 {
341  digital_in_t int2;
342  digital_in_t int1;
344  i2c_master_t i2c;
345  spi_master_t spi;
347  uint8_t slave_address;
348  pin_name_t chip_select;
354  float accel_sens;
357 
362 typedef struct
363 {
364  pin_name_t scl;
365  pin_name_t sda;
366  pin_name_t miso;
367  pin_name_t mosi;
368  pin_name_t sck;
369  pin_name_t cs;
370  pin_name_t int2;
371  pin_name_t int1;
373  uint32_t i2c_speed;
374  uint8_t i2c_address;
376  uint32_t spi_speed;
377  spi_master_mode_t spi_mode;
378  spi_master_chip_select_polarity_t cs_polarity;
382 } accel31_cfg_t;
383 
388 typedef struct
389 {
390  float x;
391  float y;
392  float z;
395 
400 typedef struct
401 {
403  int8_t temperature;
406 
411 typedef enum
412 {
416 
418 
435 
451 
465 err_t accel31_init ( accel31_t *ctx, accel31_cfg_t *cfg );
466 
480 
493 err_t accel31_write_reg ( accel31_t *ctx, uint8_t reg, uint8_t data_in );
494 
509 err_t accel31_write_regs ( accel31_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
510 
523 err_t accel31_read_reg ( accel31_t *ctx, uint8_t reg, uint8_t *data_out );
524 
539 err_t accel31_read_regs ( accel31_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
540 
550 
560 
572 
584 
608 err_t accel31_set_accel_odr ( accel31_t *ctx, uint8_t odr );
609 
624 err_t accel31_set_accel_fsr ( accel31_t *ctx, uint8_t fsr );
625 
644 err_t accel31_set_temp_odr ( accel31_t *ctx, uint8_t odr );
645 
658 err_t accel31_get_accel_drdy ( accel31_t *ctx, uint8_t *drdy );
659 
672 err_t accel31_get_temp_drdy ( accel31_t *ctx, uint8_t *drdy );
673 
687 
699 err_t accel31_get_temp ( accel31_t *ctx, int8_t *temp );
700 
713 err_t accel31_get_data ( accel31_t *ctx, accel31_data_t *data_out );
714 
715 #ifdef __cplusplus
716 }
717 #endif
718 #endif // ACCEL31_H
719  // accel31
721 
722 // ------------------------------------------------------------------------ END
accel31_read_regs
err_t accel31_read_regs(accel31_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Accel 31 read regs function.
accel31_s::accel_sens
float accel_sens
Definition: accel31.h:354
accel31_master_io_t
err_t(* accel31_master_io_t)(struct accel31_s *, uint8_t, uint8_t *, uint8_t)
Definition: accel31.h:333
accel31_sw_reset
err_t accel31_sw_reset(accel31_t *ctx)
Accel 31 sw reset function.
accel31_cfg_t::scl
pin_name_t scl
Definition: accel31.h:364
ACCEL31_DRV_SEL_SPI
@ ACCEL31_DRV_SEL_SPI
Definition: accel31.h:323
accel31_cfg_t::int1
pin_name_t int1
Definition: accel31.h:371
accel31_get_accel_drdy
err_t accel31_get_accel_drdy(accel31_t *ctx, uint8_t *drdy)
Accel 31 get accel drdy function.
accel31_drv_interface_sel
void accel31_drv_interface_sel(accel31_cfg_t *cfg, accel31_drv_t drv_sel)
Accel 31 driver interface setup function.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel31_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel31.h:376
accel31_set_accel_odr
err_t accel31_set_accel_odr(accel31_t *ctx, uint8_t odr)
Accel 31 set accel odr function.
accel31_data_t::accel
accel31_axes_t accel
Definition: accel31.h:402
accel31_s::slave_address
uint8_t slave_address
Definition: accel31.h:347
accel31_s::drv_sel
accel31_drv_t drv_sel
Definition: accel31.h:349
accel31_get_data
err_t accel31_get_data(accel31_t *ctx, accel31_data_t *data_out)
Accel 31 get data function.
accel31_set_temp_odr
err_t accel31_set_temp_odr(accel31_t *ctx, uint8_t odr)
Accel 31 set temp odr function.
accel31_s::int2
digital_in_t int2
Definition: accel31.h:341
accel31_axes_t::y
float y
Definition: accel31.h:391
accel31_default_cfg
err_t accel31_default_cfg(accel31_t *ctx)
Accel 31 default configuration function.
accel31_t
struct accel31_s accel31_t
Accel 31 Click context object.
accel31_read_reg
err_t accel31_read_reg(accel31_t *ctx, uint8_t reg, uint8_t *data_out)
Accel 31 read reg function.
accel31_get_accel
err_t accel31_get_accel(accel31_t *ctx, accel31_axes_t *accel)
Accel 31 get accel function.
accel31_write_reg
err_t accel31_write_reg(accel31_t *ctx, uint8_t reg, uint8_t data_in)
Accel 31 write reg function.
accel31_data_t::temperature
int8_t temperature
Definition: accel31.h:403
accel31_get_int1_pin
uint8_t accel31_get_int1_pin(accel31_t *ctx)
Accel 31 get int1 pin function.
accel31_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel31.h:373
accel31_s
Accel 31 Click context object.
Definition: accel31.h:340
accel31_init
err_t accel31_init(accel31_t *ctx, accel31_cfg_t *cfg)
Accel 31 initialization function.
accel31_axes_t::x
float x
Definition: accel31.h:390
accel31_s::write_f
accel31_master_io_t write_f
Definition: accel31.h:351
ACCEL31_DATA_NOT_READY
@ ACCEL31_DATA_NOT_READY
Definition: accel31.h:415
accel31_cfg_t::sck
pin_name_t sck
Definition: accel31.h:368
accel31_cfg_t
Accel 31 Click configuration object.
Definition: accel31.h:363
accel31_set_accel_fsr
err_t accel31_set_accel_fsr(accel31_t *ctx, uint8_t fsr)
Accel 31 set accel fsr function.
ACCEL31_DRV_SEL_I2C
@ ACCEL31_DRV_SEL_I2C
Definition: accel31.h:324
accel31_get_int2_pin
uint8_t accel31_get_int2_pin(accel31_t *ctx)
Accel 31 get int2 pin function.
accel31_cfg_t::miso
pin_name_t miso
Definition: accel31.h:366
accel31_s::chip_select
pin_name_t chip_select
Definition: accel31.h:348
accel31_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel31.h:374
accel31_s::spi
spi_master_t spi
Definition: accel31.h:345
accel31_check_communication
err_t accel31_check_communication(accel31_t *ctx)
Accel 31 check communication function.
accel31_s::i2c
i2c_master_t i2c
Definition: accel31.h:344
accel31_cfg_t::int2
pin_name_t int2
Definition: accel31.h:370
accel31_cfg_t::sda
pin_name_t sda
Definition: accel31.h:365
accel31_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel31.h:378
accel31_drv_t
accel31_drv_t
Accel 31 Click driver selector.
Definition: accel31.h:322
ACCEL31_ERROR
@ ACCEL31_ERROR
Definition: accel31.h:414
accel31_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel31.h:377
accel31_s::int1
digital_in_t int1
Definition: accel31.h:342
accel31_s::read_f
accel31_master_io_t read_f
Definition: accel31.h:352
accel31_return_value_t
accel31_return_value_t
Accel 31 Click return value data.
Definition: accel31.h:412
accel31_get_temp_drdy
err_t accel31_get_temp_drdy(accel31_t *ctx, uint8_t *drdy)
Accel 31 get temp drdy function.
accel31_cfg_t::cs
pin_name_t cs
Definition: accel31.h:369
accel31_get_temp
err_t accel31_get_temp(accel31_t *ctx, int8_t *temp)
Accel 31 get temp function.
ACCEL31_OK
@ ACCEL31_OK
Definition: accel31.h:413
accel31_axes_t
Accel 31 Click axes data structure.
Definition: accel31.h:389
accel31_cfg_t::mosi
pin_name_t mosi
Definition: accel31.h:367
accel31_axes_t::z
float z
Definition: accel31.h:392
accel31_write_regs
err_t accel31_write_regs(accel31_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Accel 31 write regs function.
accel31_cfg_t::drv_sel
accel31_drv_t drv_sel
Definition: accel31.h:380
accel31_data_t
Accel 31 Click data structure.
Definition: accel31.h:401
accel31_cfg_setup
void accel31_cfg_setup(accel31_cfg_t *cfg)
Accel 31 configuration object setup function.