accel18 2.0.0.0
accel18.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 ACCEL18_H
29#define ACCEL18_H
30
31#ifdef __cplusplus
32extern "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 ACCEL18_REG_DEVICE_STATUS 0x05
76#define ACCEL18_REG_INTERRUPT_ENABLE 0x06
77#define ACCEL18_REG_MODE 0x07
78#define ACCEL18_REG_SAMPLE_RATE 0x08
79#define ACCEL18_REG_MOTION_CONTROL 0x09
80#define ACCEL18_REG_FIFO_STATUS 0x0A
81#define ACCEL18_REG_FIFO_READ_POINTER 0x0B
82#define ACCEL18_REG_FIFO_WRITE_POINTER 0x0C
83#define ACCEL18_REG_XOUT_ACCEL_DATA_LSB 0x0D
84#define ACCEL18_REG_XOUT_ACCEL_DATA_MSB 0x0E
85#define ACCEL18_REG_YOUT_ACCEL_DATA_LSB 0x0F
86#define ACCEL18_REG_YOUT_ACCEL_DATA_MSB 0x10
87#define ACCEL18_REG_ZOUT_ACCEL_DATA_LSB 0x11
88#define ACCEL18_REG_ZOUT_ACCEL_DATA_MSB 0x12
89#define ACCEL18_REG_STATUS 0x13
90#define ACCEL18_REG_INTERRUPT_STATUS 0x14
91#define ACCEL18_REG_RANGE_SELECTION 0x20
92#define ACCEL18_REG_X_OFFSET_LSB 0x21
93#define ACCEL18_REG_X_OFFSET_MSB 0x22
94#define ACCEL18_REG_Y_OFFSET_LSB 0x23
95#define ACCEL18_REG_Y_OFFSET_MSB 0x24
96#define ACCEL18_REG_Z_OFFSET_LSB 0x25
97#define ACCEL18_REG_Z_OFFSET_MSB 0x26
98#define ACCEL18_REG_X_GAIN 0x27
99#define ACCEL18_REG_Y_GAIN 0x28
100#define ACCEL18_REG_Z_GAIN 0x29
101#define ACCEL18_REG_FIFO_CONTROL 0x2D
102#define ACCEL18_REG_FIFO_THRESHOLD 0x2E
103#define ACCEL18_REG_FIFO_INTERRUPT_STATUS 0x2F
104#define ACCEL18_REG_FIFO_CONTROL2 0x30
105#define ACCEL18_REG_COMMUNICATION_CONTROL 0x31
106#define ACCEL18_REG_GPIO_CONTROL 0x33
107#define ACCEL18_REG_TILT_FLIP_THRESHOLD_LSB 0x40
108#define ACCEL18_REG_TILT_FLIP_THRESHOLD_MSB 0x41
109#define ACCEL18_REG_TILT_FLIP_DEBOUNCE 0x42
110#define ACCEL18_REG_ANY_MOTION_THRESHOLD_LSB 0x43
111#define ACCEL18_REG_ANY_MOTION_THRESHOLD_MSB 0x44
112#define ACCEL18_REG_ANY_MOTION_DEBOUNCE 0x45
113#define ACCEL18_REG_SNAKE_THRESHOLD_LSB 0x46
114#define ACCEL18_REG_SNAKE_THRESHOLD_MSB 0x47
115#define ACCEL18_REG_PEAK_TO_PEAK_DURATION_LSB 0x48
116#define ACCEL18_REG_SNAKE_PEAK_TO_PEAK_DURATION_MSB 0x49
117#define ACCEL18_REG_TIMER 0x4A
118#define ACCEL18_REG_READ_COUNT 0x4B
119
120
121 // accel18_reg
122
137#define ACCEL18_STATE_STANDBY 0x00
138#define ACCEL18_STATE_WAKE 0x01
139
144#define ACCEL18_INT_TILT_EN 0x01
145#define ACCEL18_INT_FLIP_EN 0x02
146#define ACCEL18_INT_ANYM_EN 0x04
147#define ACCEL18_INT_SHAKE_EN 0x08
148#define ACCEL18_INT_TILT_35_EN 0x10
149#define ACCEL18_INT_AUTO_CLR_EN 0x40
150#define ACCEL18_INT_ACQ_EN 0x80
151
156#define ACCEL18_RATE_25 0x10
157#define ACCEL18_RATE_50 0x11
158#define ACCEL18_RATE_62p5 0x12
159#define ACCEL18_RATE_100 0x13
160#define ACCEL18_RATE_125 0x14
161#define ACCEL18_RATE_250 0x15
162#define ACCEL18_RATE_500 0x16
163#define ACCEL18_RATE_1000 0x17
164
169#define ACCEL18_FIFO_CTRL2_BURST_MODE 0x80
170#define ACCEL18_FIFO_CTRL2_SELECT_WRAP_ADDR 0x20
171#define ACCEL18_FIFO_CTRL2_ENABLE_WRAP_N 0x10
172#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DISABLE 0x00
173#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_2 0x01
174#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_4 0x02
175#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_5 0x03
176#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_8 0x04
177#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_10 0x05
178#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_16 0x06
179#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_20 0x07
180#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_40 0x08
181#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_67 0x09
182#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_80 0x0A
183#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_100 0x0B
184#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_200 0x0C
185#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_250 0x0D
186#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_500 0x0E
187#define ACCEL18_FIFO_CTRL2_DEC_MODE_RATE_DIV_1000 0x0F
188
193#define ACCEL18_GPIO_CTRL_INT1_ACTIVE_LOW 0x00
194#define ACCEL18_GPIO_CTRL_INT1_ACTIVE_HIGH 0x04
195#define ACCEL18_GPIO_CTRL_INT1_OPEN_DRAIN 0x00
196#define ACCEL18_GPIO_CTRL_INT1_PUSH_PULL 0x08
197#define ACCEL18_GPIO_CTRL_INT2_ACTIVE_LOW 0x00
198#define ACCEL18_GPIO_CTRL_INT2_ACTIVE_HIGH 0x40
199#define ACCEL18_GPIO_CTRL_INT2_OPEN_DRAIN 0x00
200#define ACCEL18_GPIO_CTRL_INT2_PUSH_PULL 0x80
201
206#define ACCEL18_MOTION_TF_EN 0x01
207#define ACCEL18_MOTION_LATCH 0x02
208#define ACCEL18_MOTION_ANYM_EN 0x04
209#define ACCEL18_MOTION_SHAKE_EN 0x08
210#define ACCEL18_MOTION_TILT_35 0x10
211#define ACCEL18_MOTION_Z_AXIS_ORT 0x20
212#define ACCEL18_MOTION_RAW_PROC_STAT 0x40
213#define ACCEL18_MOTION_RESET 0x80
214
219#define ACCEL18_STATUS_TILT 0x01
220#define ACCEL18_STATUS_FLIP 0x02
221#define ACCEL18_STATUS_ANYM 0x04
222#define ACCEL18_STATUS_SHAKE 0x08
223#define ACCEL18_STATUS_TILT35 0x10
224#define ACCEL18_STATUS_FIFO 0x20
225#define ACCEL18_STATUS_NEW_DATA 0x80
226
231#define ACCEL18_RANGE_2G 0x00
232#define ACCEL18_RANGE_4G 0x10
233#define ACCEL18_RANGE_8G 0x20
234#define ACCEL18_RANGE_16G 0x30
235#define ACCEL18_RANGE_12G 0x40
236#define ACCEL18_LOW_PASS_FILTER_EN 0x08
237#define ACCEL18_LOW_PASS_FILTER_DIV4p255 0x01
238#define ACCEL18_LOW_PASS_FILTER_DIV6 0x02
239#define ACCEL18_LOW_PASS_FILTER_DIV12 0x03
240#define ACCEL18_LOW_PASS_FILTER_DIV16 0x05
241
247#define ACCEL18_GND_ADDR 0x4C
248#define ACCEL18_VCC_ADDR 0x6C
249#define ACCEL18_SET_DEV_ADDR ACCEL18_GND_ADDR
250
259#define ACCEL18_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
260#define ACCEL18_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
261
262 // accel18_set
263
278#define ACCEL18_MAP_MIKROBUS( cfg, mikrobus ) \
279 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
280 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
281 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
282 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
283 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
284 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
285 cfg.i1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
286 cfg.i2 = MIKROBUS( mikrobus, MIKROBUS_INT )
287
288 // accel18_map
289 // accel18
290
301
306typedef err_t ( *accel18_master_io_t )( struct accel18_s*, uint8_t, uint8_t*, uint8_t );
330
335typedef struct
336{
337 pin_name_t scl;
338 pin_name_t sda;
339 pin_name_t miso;
340 pin_name_t mosi;
341 pin_name_t sck;
342 pin_name_t cs;
343 pin_name_t i1;
344 pin_name_t i2;
346 uint32_t i2c_speed;
347 uint8_t i2c_address;
349 uint32_t spi_speed;
350 spi_master_mode_t spi_mode;
351 spi_master_chip_select_polarity_t cs_polarity;
356
361typedef struct
362{
363 float x;
364 float y;
365 float z;
368
373typedef enum
374{
376 ACCEL18_ERROR = -1
377
379
396
412
428
443
459err_t accel18_generic_write ( accel18_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
460
474err_t accel18_byte_write ( accel18_t *ctx, uint8_t reg, uint8_t data_in );
475
491err_t accel18_generic_read ( accel18_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
492
506err_t accel18_byte_read ( accel18_t *ctx, uint8_t reg, uint8_t *data_out );
507
521err_t accel18_read_axes ( accel18_t *ctx, accel18_axes_t *axes_data );
522
536err_t accel18_set_range ( accel18_t *ctx, uint8_t range_macro );
537
546
555
556#ifdef __cplusplus
557}
558#endif
559#endif // ACCEL18_H
560
561 // accel18
562
563// ------------------------------------------------------------------------ END
err_t(* accel18_master_io_t)(struct accel18_s *, uint8_t, uint8_t *, uint8_t)
Accel 18 Click driver interface.
Definition accel18.h:306
accel18_drv_t
Accel 18 Click driver selector.
Definition accel18.h:296
@ ACCEL18_DRV_SEL_SPI
Definition accel18.h:297
@ ACCEL18_DRV_SEL_I2C
Definition accel18.h:298
accel18_return_value_t
Accel 18 Click return value data.
Definition accel18.h:374
@ ACCEL18_OK
Definition accel18.h:375
@ ACCEL18_ERROR
Definition accel18.h:376
struct accel18_s accel18_t
Accel 18 Click context object.
err_t accel18_generic_write(accel18_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Accel 18 data writing function.
err_t accel18_generic_read(accel18_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Accel 18 data reading function.
void accel18_cfg_setup(accel18_cfg_t *cfg)
Accel 18 configuration object setup function.
uint8_t accel18_get_interrupt_1(accel18_t *ctx)
Get interrupt 1 pin state.
err_t accel18_set_range(accel18_t *ctx, uint8_t range_macro)
Set range configuration.
err_t accel18_byte_write(accel18_t *ctx, uint8_t reg, uint8_t data_in)
Accel 18 byte writing function.
err_t accel18_init(accel18_t *ctx, accel18_cfg_t *cfg)
Accel 18 initialization function.
err_t accel18_default_cfg(accel18_t *ctx)
Accel 18 default configuration function.
uint8_t accel18_get_interrupt_2(accel18_t *ctx)
Get interrupt 2 pin state.
err_t accel18_byte_read(accel18_t *ctx, uint8_t reg, uint8_t *data_out)
Accel 18 byte reading function.
void accel18_drv_interface_selection(accel18_cfg_t *cfg, accel18_drv_t drv_sel)
Accel 18 driver interface setup function.
err_t accel18_read_axes(accel18_t *ctx, accel18_axes_t *axes_data)
Accel data reading.
This file contains SPI specific macros, functions, etc.
Accel 18 Click axes data.
Definition accel18.h:362
float y
Definition accel18.h:364
float x
Definition accel18.h:363
float z
Definition accel18.h:365
Accel 18 Click configuration object.
Definition accel18.h:336
accel18_drv_t drv_sel
Definition accel18.h:353
pin_name_t i1
Definition accel18.h:343
uint32_t i2c_speed
Definition accel18.h:346
spi_master_chip_select_polarity_t cs_polarity
Definition accel18.h:351
pin_name_t sck
Definition accel18.h:341
pin_name_t i2
Definition accel18.h:344
spi_master_mode_t spi_mode
Definition accel18.h:350
pin_name_t mosi
Definition accel18.h:340
uint32_t spi_speed
Definition accel18.h:349
pin_name_t scl
Definition accel18.h:337
pin_name_t miso
Definition accel18.h:339
pin_name_t sda
Definition accel18.h:338
pin_name_t cs
Definition accel18.h:342
uint8_t i2c_address
Definition accel18.h:347
Accel 18 Click context object.
Definition accel18.h:313
accel18_drv_t drv_sel
Definition accel18.h:322
spi_master_t spi
Definition accel18.h:318
digital_in_t i2
Definition accel18.h:315
accel18_master_io_t read_f
Definition accel18.h:325
i2c_master_t i2c
Definition accel18.h:317
float range_resolution
Definition accel18.h:327
uint8_t slave_address
Definition accel18.h:320
digital_in_t i1
Definition accel18.h:314
pin_name_t chip_select
Definition accel18.h:321
accel18_master_io_t write_f
Definition accel18.h:324