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 
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 ACCEL28_REG_TEMP_L 0x0B
72 #define ACCEL28_REG_TEMP_H 0x0C
73 #define ACCEL28_REG_WHO_AM_I 0x0F
74 #define ACCEL28_REG_ACT_THS 0x1E
75 #define ACCEL28_REG_ACT_DUS 0x1F
76 #define ACCEL28_REG_CTRL1 0x20
77 #define ACCEL28_REG_CTRL2 0x21
78 #define ACCEL28_REG_CTRL3 0x22
79 #define ACCEL28_REG_CTRL4 0x23
80 #define ACCEL28_REG_CTRL5 0x24
81 #define ACCEL28_REG_CTRL6 0x25
82 #define ACCEL28_REG_CTRL7 0x26
83 #define ACCEL28_REG_STATUS 0x27
84 #define ACCEL28_REG_OUT_X_L 0x28
85 #define ACCEL28_REG_OUT_X_H 0x29
86 #define ACCEL28_REG_OUT_Y_L 0x2A
87 #define ACCEL28_REG_OUT_Y_H 0x2B
88 #define ACCEL28_REG_OUT_Z_L 0x2C
89 #define ACCEL28_REG_OUT_Z_H 0x2D
90 #define ACCEL28_REG_FIFO_CTRL 0x2E
91 #define ACCEL28_REG_FIFO_SRC 0x2F
92 #define ACCEL28_REG_IG_CFG1 0x30
93 #define ACCEL28_REG_IG_SRC1 0x31
94 #define ACCEL28_REG_IG_THS_X1 0x32
95 #define ACCEL28_REG_IG_THS_Y1 0x33
96 #define ACCEL28_REG_IG_THS_Z1 0x34
97 #define ACCEL28_REG_IG_DUR1 0x35
98 #define ACCEL28_REG_IG_CFG2 0x36
99 #define ACCEL28_REG_IG_SRC2 0x37
100 #define ACCEL28_REG_IG_THS2 0x38
101 #define ACCEL28_REG_IG_DUR2 0x39
102 #define ACCEL28_REG_XL_REFERENCE 0x3A
103 #define ACCEL28_REG_XH_REFERENCE 0x3B
104 #define ACCEL28_REG_YL_REFERENCE 0x3C
105 #define ACCEL28_REG_YH_REFERENCE 0x3D
106 #define ACCEL28_REG_ZL_REFERENCE 0x3E
107 #define ACCEL28_REG_ZH_REFERENCE 0x3F
108  // accel28_reg
110 
125 #define ACCEL28_WHO_AM_I_VALUE 0x41
126 
132 #define ACCEL28_CTRL1_X_AXIS_ENABLE 0x01
133 #define ACCEL28_CTRL1_Y_AXIS_ENABLE 0x02
134 #define ACCEL28_CTRL1_Z_AXIS_ENABLE 0x04
135 #define ACCEL28_CTRL1_BLOCK_DATA_UPATE_EN 0x08
136 #define ACCEL28_CTRL1_ODR_POWER_DOWN 0x00
137 #define ACCEL28_CTRL1_ODR_10HZ 0x10
138 #define ACCEL28_CTRL1_ODR_50HZ 0x20
139 #define ACCEL28_CTRL1_ODR_100HZ 0x30
140 #define ACCEL28_CTRL1_ODR_200HZ 0x40
141 #define ACCEL28_CTRL1_ODR_400HZ 0x50
142 #define ACCEL28_CTRL1_ODR_800HZ 0x60
143 #define ACCEL28_CTRL1_ODR_NA 0x70
144 #define ACCEL28_CTRL1_HR_MODE 0x80
145 
150 #define ACCEL28_CTRL2_HP_ENABLE_INT2 0x01
151 #define ACCEL28_CTRL2_HP_ENABLE_INT1 0x02
152 #define ACCEL28_CTRL2_FDS_ENABLE 0x04
153 #define ACCEL28_CTRL2_HPM_NORMAL_MODE 0x00
154 #define ACCEL28_CTRL2_HPM_REF_SIGNAL_FILTERING 0x08
155 #define ACCEL28_CTRL2_HPM_FREQ_DEV_50 0x00
156 #define ACCEL28_CTRL2_HPM_FREQ_DEV_100 0x20
157 #define ACCEL28_CTRL2_HPM_FREQ_DEV_9 0x40
158 #define ACCEL28_CTRL2_HPM_FREQ_DEV_400 0x60
159 
165 #define ACCEL28_CTRL3_INT1_DRDY_EN 0x01
166 #define ACCEL28_CTRL3_INT1_FTH 0x02
167 #define ACCEL28_CTRL3_INT1_OVR 0x04
168 #define ACCEL28_CTRL3_INT1_IG1 0x08
169 #define ACCEL28_CTRL3_INT1_IG2 0x10
170 #define ACCEL28_CTRL3_INT1_INACT_EN 0x20
171 #define ACCEL28_CTRL3_STOP_FTH 0x40
172 #define ACCEL28_CTRL3_FIFO_EN 0x80
173 
179 #define ACCEL28_CTRL4_SPI_MODE_3_WIRE 0x01
180 #define ACCEL28_CTRL4_SPI_MODE_4_WIRE 0x00
181 #define ACCEL28_CTRL4_I2C_DISABLED 0x02
182 #define ACCEL28_CTRL4_I2C_ENABLED 0x00
183 #define ACCEL28_CTRL4_ADD_INC_ENABLED 0x04
184 #define ACCEL28_CTRL4_BW_SCALE_ODR 0x00
185 #define ACCEL28_CTRL4_BW_SCALE_BW 0x08
186 #define ACCEL28_CTRL4_FS_2G 0x00
187 #define ACCEL28_CTRL4_FS_4G 0x20
188 #define ACCEL28_CTRL4_FS_8G 0x30
189 #define ACCEL28_CTRL4_FS_MASK 0x30
190 #define ACCEL28_CTRL4_BW_400HZ 0x00
191 #define ACCEL28_CTRL4_BW_200HZ 0x40
192 #define ACCEL28_CTRL4_BW_100HZ 0x80
193 #define ACCEL28_CTRL4_BW_50HZ 0xC0
194 
200 #define ACCEL28_CTRL5_INT_PUSH_PULL 0x00
201 #define ACCEL28_CTRL5_INT_OPEN_DRAIN 0x01
202 #define ACCEL28_CTRL5_INT_ACTIVE_HIGH 0x00
203 #define ACCEL28_CTRL5_INT_ACTIVE_LOW 0x02
204 #define ACCEL28_CTRL5_NORMAL_MODE 0x00
205 #define ACCEL28_CTRL5_POSITIVE_SELF_TEST 0x04
206 #define ACCEL28_CTRL5_NEGATIVE_SELF_TEST 0x08
207 #define ACCEL28_CTRL5_DEC_UDATE_OFF 0x00
208 #define ACCEL28_CTRL5_DEC_UDATE_2_SAMPLES 0x10
209 #define ACCEL28_CTRL5_DEC_UDATE_4_SAMPLES 0x20
210 #define ACCEL28_CTRL5_DEC_UDATE_8_SAMPLES 0x30
211 #define ACCEL28_CTRL5_SW_RESET 0x40
212 #define ACCEL28_CTRL5_DEBUG_MODE 0x80
213 
219 #define ACCEL28_CTRL6_INT2_DRDY 0x01
220 #define ACCEL28_CTRL6_INT2_FTH 0x02
221 #define ACCEL28_CTRL6_INT2_EMPTY 0x04
222 #define ACCEL28_CTRL6_INT2_IG1 0x08
223 #define ACCEL28_CTRL6_INT2_IG2 0x10
224 #define ACCEL28_CTRL6_INT2_BOOT 0x20
225 #define ACCEL28_CTRL6_BOOT 0x80
226 
232 #define ACCEL28_STATUS_X_DATA_AVL 0x01
233 #define ACCEL28_STATUS_Y_DATA_AVL 0x02
234 #define ACCEL28_STATUS_Z_DATA_AVL 0x04
235 #define ACCEL28_STATUS_ZYX_DATA_AVL 0x08
236 #define ACCEL28_STATUS_X_DATA_OVERRUN 0x10
237 #define ACCEL28_STATUS_Y_DATA_OVERRUN 0x20
238 #define ACCEL28_STATUS_Z_DATA_OVERRUN 0x40
239 #define ACCEL28_STATUS_ZYX_DATA_OVERRUN 0x80
240 
246 #define ACCEL28_2G_DATA_CONV 0.061f
247 #define ACCEL28_4G_DATA_CONV 0.122f
248 #define ACCEL28_8G_DATA_CONV 0.244f
249 
255 #define ACCEL28_PIN_STATE_HIGH 0x01
256 #define ACCEL28_PIN_STATE_LOW 0x00
257 
263 #define ACCEL28_DEVICE_ADDRESS_0 0x1E
264 #define ACCEL28_DEVICE_ADDRESS_1 0x1D
265 
274 #define ACCEL28_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
275 #define ACCEL28_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
276  // accel28_set
278 
293 #define ACCEL28_MAP_MIKROBUS( cfg, mikrobus ) \
294  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
295  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
296  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
297  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
298  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
299  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
300  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
301  // accel28_map // accel28
304 
309 typedef enum
310 {
315 
320 typedef err_t ( *accel28_master_io_t )( struct accel28_s*, uint8_t, uint8_t*, uint8_t );
326 typedef struct accel28_s
327 {
328  digital_in_t int_pin;
330  i2c_master_t i2c;
331  spi_master_t spi;
333  uint8_t slave_address;
334  pin_name_t chip_select;
341 
346 typedef struct
347 {
348  pin_name_t scl;
349  pin_name_t sda;
350  pin_name_t miso;
351  pin_name_t mosi;
352  pin_name_t sck;
353  pin_name_t cs;
354  pin_name_t int_pin;
356  uint32_t i2c_speed;
357  uint8_t i2c_address;
359  uint32_t spi_speed;
360  spi_master_mode_t spi_mode;
361  spi_master_chip_select_polarity_t cs_polarity;
365 } accel28_cfg_t;
366 
371 typedef struct
372 {
373  float x_data;
374  float y_data;
375  float z_data;
378 
383 typedef enum
384 {
386  ACCEL28_ERROR = -1
387 
389 
406 
422 
436 err_t accel28_init ( accel28_t *ctx, accel28_cfg_t *cfg );
437 
451 
466 err_t accel28_generic_write ( accel28_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
467 
482 err_t accel28_generic_read ( accel28_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
483 
493 
506 
518 err_t accel28_get_data ( accel28_t *ctx, accel28_data_t *data_out );
519 
532 err_t accel28_write_reg ( accel28_t *ctx, uint8_t reg, uint8_t data_in );
533 
547 err_t accel28_read_reg ( accel28_t *ctx, uint8_t reg, uint8_t *data_out );
548 
560 
561 #ifdef __cplusplus
562 }
563 #endif
564 #endif // ACCEL28_H
565  // accel28
567 
568 // ------------------------------------------------------------------------ END
accel28_s::read_f
accel28_master_io_t read_f
Definition: accel28.h:338
accel28_t
struct accel28_s accel28_t
Accel 28 Click context object.
accel28_cfg_t::cs
pin_name_t cs
Definition: accel28.h:353
accel28_cfg_t::sck
pin_name_t sck
Definition: accel28.h:352
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:327
accel28_cfg_t::miso
pin_name_t miso
Definition: accel28.h:350
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:320
accel28_data_t
Accel 28 Click data object.
Definition: accel28.h:372
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:351
accel28_s::slave_address
uint8_t slave_address
Definition: accel28.h:333
accel28_s::int_pin
digital_in_t int_pin
Definition: accel28.h:328
accel28_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel28.h:356
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:347
accel28_data_t::z_data
float z_data
Definition: accel28.h:375
accel28_s::spi
spi_master_t spi
Definition: accel28.h:331
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:385
accel28_s::chip_select
pin_name_t chip_select
Definition: accel28.h:334
accel28_s::i2c
i2c_master_t i2c
Definition: accel28.h:330
accel28_cfg_t::sda
pin_name_t sda
Definition: accel28.h:349
accel28_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel28.h:360
accel28_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel28.h:361
accel28_cfg_t::int_pin
pin_name_t int_pin
Definition: accel28.h:354
ACCEL28_DRV_SEL_I2C
@ ACCEL28_DRV_SEL_I2C
Definition: accel28.h:312
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:357
accel28_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel28.h:359
accel28_s::drv_sel
accel28_drv_t drv_sel
Definition: accel28.h:335
ACCEL28_DRV_SEL_SPI
@ ACCEL28_DRV_SEL_SPI
Definition: accel28.h:311
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:373
accel28_data_t::y_data
float y_data
Definition: accel28.h:374
accel28_drv_t
accel28_drv_t
Accel 28 Click driver selector.
Definition: accel28.h:310
accel28_cfg_t::scl
pin_name_t scl
Definition: accel28.h:348
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:337
ACCEL28_ERROR
@ ACCEL28_ERROR
Definition: accel28.h:386
accel28_cfg_t::drv_sel
accel28_drv_t drv_sel
Definition: accel28.h:363
accel28_return_value_t
accel28_return_value_t
Accel 28 Click return value data.
Definition: accel28.h:384