accel28  2.1.0.0
accel28.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 ACCEL28_H
29 #define ACCEL28_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 ACCEL28_REG_TEMP_L 0x0B
76 #define ACCEL28_REG_TEMP_H 0x0C
77 #define ACCEL28_REG_WHO_AM_I 0x0F
78 #define ACCEL28_REG_ACT_THS 0x1E
79 #define ACCEL28_REG_ACT_DUS 0x1F
80 #define ACCEL28_REG_CTRL1 0x20
81 #define ACCEL28_REG_CTRL2 0x21
82 #define ACCEL28_REG_CTRL3 0x22
83 #define ACCEL28_REG_CTRL4 0x23
84 #define ACCEL28_REG_CTRL5 0x24
85 #define ACCEL28_REG_CTRL6 0x25
86 #define ACCEL28_REG_CTRL7 0x26
87 #define ACCEL28_REG_STATUS 0x27
88 #define ACCEL28_REG_OUT_X_L 0x28
89 #define ACCEL28_REG_OUT_X_H 0x29
90 #define ACCEL28_REG_OUT_Y_L 0x2A
91 #define ACCEL28_REG_OUT_Y_H 0x2B
92 #define ACCEL28_REG_OUT_Z_L 0x2C
93 #define ACCEL28_REG_OUT_Z_H 0x2D
94 #define ACCEL28_REG_FIFO_CTRL 0x2E
95 #define ACCEL28_REG_FIFO_SRC 0x2F
96 #define ACCEL28_REG_IG_CFG1 0x30
97 #define ACCEL28_REG_IG_SRC1 0x31
98 #define ACCEL28_REG_IG_THS_X1 0x32
99 #define ACCEL28_REG_IG_THS_Y1 0x33
100 #define ACCEL28_REG_IG_THS_Z1 0x34
101 #define ACCEL28_REG_IG_DUR1 0x35
102 #define ACCEL28_REG_IG_CFG2 0x36
103 #define ACCEL28_REG_IG_SRC2 0x37
104 #define ACCEL28_REG_IG_THS2 0x38
105 #define ACCEL28_REG_IG_DUR2 0x39
106 #define ACCEL28_REG_XL_REFERENCE 0x3A
107 #define ACCEL28_REG_XH_REFERENCE 0x3B
108 #define ACCEL28_REG_YL_REFERENCE 0x3C
109 #define ACCEL28_REG_YH_REFERENCE 0x3D
110 #define ACCEL28_REG_ZL_REFERENCE 0x3E
111 #define ACCEL28_REG_ZH_REFERENCE 0x3F
112  // accel28_reg
114 
129 #define ACCEL28_WHO_AM_I_VALUE 0x41
130 
136 #define ACCEL28_CTRL1_X_AXIS_ENABLE 0x01
137 #define ACCEL28_CTRL1_Y_AXIS_ENABLE 0x02
138 #define ACCEL28_CTRL1_Z_AXIS_ENABLE 0x04
139 #define ACCEL28_CTRL1_BLOCK_DATA_UPATE_EN 0x08
140 #define ACCEL28_CTRL1_ODR_POWER_DOWN 0x00
141 #define ACCEL28_CTRL1_ODR_10HZ 0x10
142 #define ACCEL28_CTRL1_ODR_50HZ 0x20
143 #define ACCEL28_CTRL1_ODR_100HZ 0x30
144 #define ACCEL28_CTRL1_ODR_200HZ 0x40
145 #define ACCEL28_CTRL1_ODR_400HZ 0x50
146 #define ACCEL28_CTRL1_ODR_800HZ 0x60
147 #define ACCEL28_CTRL1_ODR_NA 0x70
148 #define ACCEL28_CTRL1_HR_MODE 0x80
149 
154 #define ACCEL28_CTRL2_HP_ENABLE_INT2 0x01
155 #define ACCEL28_CTRL2_HP_ENABLE_INT1 0x02
156 #define ACCEL28_CTRL2_FDS_ENABLE 0x04
157 #define ACCEL28_CTRL2_HPM_NORMAL_MODE 0x00
158 #define ACCEL28_CTRL2_HPM_REF_SIGNAL_FILTERING 0x08
159 #define ACCEL28_CTRL2_HPM_FREQ_DEV_50 0x00
160 #define ACCEL28_CTRL2_HPM_FREQ_DEV_100 0x20
161 #define ACCEL28_CTRL2_HPM_FREQ_DEV_9 0x40
162 #define ACCEL28_CTRL2_HPM_FREQ_DEV_400 0x60
163 
169 #define ACCEL28_CTRL3_INT1_DRDY_EN 0x01
170 #define ACCEL28_CTRL3_INT1_FTH 0x02
171 #define ACCEL28_CTRL3_INT1_OVR 0x04
172 #define ACCEL28_CTRL3_INT1_IG1 0x08
173 #define ACCEL28_CTRL3_INT1_IG2 0x10
174 #define ACCEL28_CTRL3_INT1_INACT_EN 0x20
175 #define ACCEL28_CTRL3_STOP_FTH 0x40
176 #define ACCEL28_CTRL3_FIFO_EN 0x80
177 
183 #define ACCEL28_CTRL4_SPI_MODE_3_WIRE 0x01
184 #define ACCEL28_CTRL4_SPI_MODE_4_WIRE 0x00
185 #define ACCEL28_CTRL4_I2C_DISABLED 0x02
186 #define ACCEL28_CTRL4_I2C_ENABLED 0x00
187 #define ACCEL28_CTRL4_ADD_INC_ENABLED 0x04
188 #define ACCEL28_CTRL4_BW_SCALE_ODR 0x00
189 #define ACCEL28_CTRL4_BW_SCALE_BW 0x08
190 #define ACCEL28_CTRL4_FS_2G 0x00
191 #define ACCEL28_CTRL4_FS_4G 0x20
192 #define ACCEL28_CTRL4_FS_8G 0x30
193 #define ACCEL28_CTRL4_FS_MASK 0x30
194 #define ACCEL28_CTRL4_BW_400HZ 0x00
195 #define ACCEL28_CTRL4_BW_200HZ 0x40
196 #define ACCEL28_CTRL4_BW_100HZ 0x80
197 #define ACCEL28_CTRL4_BW_50HZ 0xC0
198 
204 #define ACCEL28_CTRL5_INT_PUSH_PULL 0x00
205 #define ACCEL28_CTRL5_INT_OPEN_DRAIN 0x01
206 #define ACCEL28_CTRL5_INT_ACTIVE_HIGH 0x00
207 #define ACCEL28_CTRL5_INT_ACTIVE_LOW 0x02
208 #define ACCEL28_CTRL5_NORMAL_MODE 0x00
209 #define ACCEL28_CTRL5_POSITIVE_SELF_TEST 0x04
210 #define ACCEL28_CTRL5_NEGATIVE_SELF_TEST 0x08
211 #define ACCEL28_CTRL5_DEC_UDATE_OFF 0x00
212 #define ACCEL28_CTRL5_DEC_UDATE_2_SAMPLES 0x10
213 #define ACCEL28_CTRL5_DEC_UDATE_4_SAMPLES 0x20
214 #define ACCEL28_CTRL5_DEC_UDATE_8_SAMPLES 0x30
215 #define ACCEL28_CTRL5_SW_RESET 0x40
216 #define ACCEL28_CTRL5_DEBUG_MODE 0x80
217 
223 #define ACCEL28_CTRL6_INT2_DRDY 0x01
224 #define ACCEL28_CTRL6_INT2_FTH 0x02
225 #define ACCEL28_CTRL6_INT2_EMPTY 0x04
226 #define ACCEL28_CTRL6_INT2_IG1 0x08
227 #define ACCEL28_CTRL6_INT2_IG2 0x10
228 #define ACCEL28_CTRL6_INT2_BOOT 0x20
229 #define ACCEL28_CTRL6_BOOT 0x80
230 
236 #define ACCEL28_STATUS_X_DATA_AVL 0x01
237 #define ACCEL28_STATUS_Y_DATA_AVL 0x02
238 #define ACCEL28_STATUS_Z_DATA_AVL 0x04
239 #define ACCEL28_STATUS_ZYX_DATA_AVL 0x08
240 #define ACCEL28_STATUS_X_DATA_OVERRUN 0x10
241 #define ACCEL28_STATUS_Y_DATA_OVERRUN 0x20
242 #define ACCEL28_STATUS_Z_DATA_OVERRUN 0x40
243 #define ACCEL28_STATUS_ZYX_DATA_OVERRUN 0x80
244 
250 #define ACCEL28_2G_DATA_CONV 0.061f
251 #define ACCEL28_4G_DATA_CONV 0.122f
252 #define ACCEL28_8G_DATA_CONV 0.244f
253 
259 #define ACCEL28_PIN_STATE_HIGH 0x01
260 #define ACCEL28_PIN_STATE_LOW 0x00
261 
267 #define ACCEL28_DEVICE_ADDRESS_0 0x1E
268 #define ACCEL28_DEVICE_ADDRESS_1 0x1D
269 
278 #define ACCEL28_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
279 #define ACCEL28_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
280  // accel28_set
282 
297 #define ACCEL28_MAP_MIKROBUS( cfg, mikrobus ) \
298  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
299  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
300  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
301  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
302  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
303  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
304  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
305  // accel28_map // accel28
308 
313 typedef enum
314 {
319 
324 typedef err_t ( *accel28_master_io_t )( struct accel28_s*, uint8_t, uint8_t*, uint8_t );
330 typedef struct accel28_s
331 {
332  digital_in_t int_pin;
334  i2c_master_t i2c;
335  spi_master_t spi;
337  uint8_t slave_address;
338  pin_name_t chip_select;
345 
350 typedef struct
351 {
352  pin_name_t scl;
353  pin_name_t sda;
354  pin_name_t miso;
355  pin_name_t mosi;
356  pin_name_t sck;
357  pin_name_t cs;
358  pin_name_t int_pin;
360  uint32_t i2c_speed;
361  uint8_t i2c_address;
363  uint32_t spi_speed;
364  spi_master_mode_t spi_mode;
365  spi_master_chip_select_polarity_t cs_polarity;
369 } accel28_cfg_t;
370 
375 typedef struct
376 {
377  float x_data;
378  float y_data;
379  float z_data;
382 
387 typedef enum
388 {
390  ACCEL28_ERROR = -1
391 
393 
410 
426 
440 err_t accel28_init ( accel28_t *ctx, accel28_cfg_t *cfg );
441 
455 
470 err_t accel28_generic_write ( accel28_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
471 
486 err_t accel28_generic_read ( accel28_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
487 
497 
510 
522 err_t accel28_get_data ( accel28_t *ctx, accel28_data_t *data_out );
523 
536 err_t accel28_write_reg ( accel28_t *ctx, uint8_t reg, uint8_t data_in );
537 
551 err_t accel28_read_reg ( accel28_t *ctx, uint8_t reg, uint8_t *data_out );
552 
564 
565 #ifdef __cplusplus
566 }
567 #endif
568 #endif // ACCEL28_H
569  // accel28
571 
572 // ------------------------------------------------------------------------ END
accel28_s::read_f
accel28_master_io_t read_f
Definition: accel28.h:342
accel28_t
struct accel28_s accel28_t
Accel 28 Click context object.
accel28_cfg_t::cs
pin_name_t cs
Definition: accel28.h:357
accel28_cfg_t::sck
pin_name_t sck
Definition: accel28.h:356
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel28_default_cfg
err_t accel28_default_cfg(accel28_t *ctx)
Accel 28 default configuration function.
accel28_s
Accel 28 Click context object.
Definition: accel28.h:331
accel28_cfg_t::miso
pin_name_t miso
Definition: accel28.h:354
accel28_generic_read
err_t accel28_generic_read(accel28_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Accel 28 data reading function.
accel28_get_data
err_t accel28_get_data(accel28_t *ctx, accel28_data_t *data_out)
Accel 28 data reading function.
accel28_write_reg
err_t accel28_write_reg(accel28_t *ctx, uint8_t reg, uint8_t data_in)
Accel 28 register data writing function.
accel28_master_io_t
err_t(* accel28_master_io_t)(struct accel28_s *, uint8_t, uint8_t *, uint8_t)
Accel 28 Click driver interface.
Definition: accel28.h:324
accel28_data_t
Accel 28 Click data object.
Definition: accel28.h:376
accel28_generic_write
err_t accel28_generic_write(accel28_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Accel 28 data writing function.
accel28_drv_interface_selection
void accel28_drv_interface_selection(accel28_cfg_t *cfg, accel28_drv_t drv_sel)
Accel 28 driver interface setup function.
accel28_cfg_t::mosi
pin_name_t mosi
Definition: accel28.h:355
accel28_s::slave_address
uint8_t slave_address
Definition: accel28.h:337
accel28_s::int_pin
digital_in_t int_pin
Definition: accel28.h:332
accel28_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel28.h:360
accel28_cfg_setup
void accel28_cfg_setup(accel28_cfg_t *cfg)
Accel 28 configuration object setup function.
accel28_get_raw_data
err_t accel28_get_raw_data(accel28_t *ctx, accel28_data_t *data_out)
Accel 28 raw data reading function.
accel28_cfg_t
Accel 28 Click configuration object.
Definition: accel28.h:351
accel28_data_t::z_data
float z_data
Definition: accel28.h:379
accel28_s::spi
spi_master_t spi
Definition: accel28.h:335
accel28_read_reg
err_t accel28_read_reg(accel28_t *ctx, uint8_t reg, uint8_t *data_out)
Accel 28 register data reading function.
ACCEL28_OK
@ ACCEL28_OK
Definition: accel28.h:389
accel28_s::chip_select
pin_name_t chip_select
Definition: accel28.h:338
accel28_s::i2c
i2c_master_t i2c
Definition: accel28.h:334
accel28_cfg_t::sda
pin_name_t sda
Definition: accel28.h:353
accel28_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel28.h:364
accel28_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel28.h:365
accel28_cfg_t::int_pin
pin_name_t int_pin
Definition: accel28.h:358
ACCEL28_DRV_SEL_I2C
@ ACCEL28_DRV_SEL_I2C
Definition: accel28.h:316
accel28_get_int_state
uint8_t accel28_get_int_state(accel28_t *ctx)
Accel 28 interrupt reading function.
accel28_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel28.h:361
accel28_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel28.h:363
accel28_s::drv_sel
accel28_drv_t drv_sel
Definition: accel28.h:339
ACCEL28_DRV_SEL_SPI
@ ACCEL28_DRV_SEL_SPI
Definition: accel28.h:315
accel28_sw_reset
err_t accel28_sw_reset(accel28_t *ctx)
Accel 28 SW reset function.
accel28_data_t::x_data
float x_data
Definition: accel28.h:377
accel28_data_t::y_data
float y_data
Definition: accel28.h:378
accel28_drv_t
accel28_drv_t
Accel 28 Click driver selector.
Definition: accel28.h:314
accel28_cfg_t::scl
pin_name_t scl
Definition: accel28.h:352
accel28_init
err_t accel28_init(accel28_t *ctx, accel28_cfg_t *cfg)
Accel 28 initialization function.
accel28_s::write_f
accel28_master_io_t write_f
Definition: accel28.h:341
ACCEL28_ERROR
@ ACCEL28_ERROR
Definition: accel28.h:390
accel28_cfg_t::drv_sel
accel28_drv_t drv_sel
Definition: accel28.h:367
accel28_return_value_t
accel28_return_value_t
Accel 28 Click return value data.
Definition: accel28.h:388