accel20 2.0.0.0
accel20.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 ACCEL20_H
29#define ACCEL20_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 ACCEL20_REG_MAN_ID 0x00
76#define ACCEL20_REG_PART_ID 0x01
77#define ACCEL20_REG_XADP_L 0x02
78#define ACCEL20_REG_XADP_H 0x03
79#define ACCEL20_REG_YADP_L 0x04
80#define ACCEL20_REG_YADP_H 0x05
81#define ACCEL20_REG_ZADP_L 0x06
82#define ACCEL20_REG_ZADP_H 0x07
83#define ACCEL20_REG_XOUT_L 0x08
84#define ACCEL20_REG_XOUT_H 0x09
85#define ACCEL20_REG_YOUT_L 0x0A
86#define ACCEL20_REG_YOUT_H 0x0B
87#define ACCEL20_REG_ZOUT_L 0x0C
88#define ACCEL20_REG_ZOUT_H 0x0D
89#define ACCEL20_REG_COTR 0x12
90#define ACCEL20_REG_WHO_AM_I 0x13
91#define ACCEL20_REG_TSCP 0x14
92#define ACCEL20_REG_TSPP 0x15
93#define ACCEL20_REG_INS1 0x16
94#define ACCEL20_REG_INS2 0x17
95#define ACCEL20_REG_INS3 0x18
96#define ACCEL20_REG_STATUS_REG 0x19
97#define ACCEL20_REG_INT_REL 0x1A
98#define ACCEL20_REG_CNTL1 0x1B
99#define ACCEL20_REG_CNTL2 0x1C
100#define ACCEL20_REG_CNTL3 0x1D
101#define ACCEL20_REG_CNTL4 0x1E
102#define ACCEL20_REG_CNTL5 0x1F
103#define ACCEL20_REG_CNTL6 0x20
104#define ACCEL20_REG_ODCNTL 0x21
105#define ACCEL20_REG_INC1 0x22
106#define ACCEL20_REG_INC2 0x23
107#define ACCEL20_REG_INC3 0x24
108#define ACCEL20_REG_INC4 0x25
109#define ACCEL20_REG_INC5 0x26
110#define ACCEL20_REG_INC6 0x27
111#define ACCEL20_REG_TILT_TIMER 0x29
112#define ACCEL20_REG_TDTRC 0x2A
113#define ACCEL20_REG_TDTC 0x2B
114#define ACCEL20_REG_TTH 0x2C
115#define ACCEL20_REG_TTL 0x2D
116#define ACCEL20_REG_FTD 0x2E
117#define ACCEL20_REG_STD 0x2F
118#define ACCEL20_REG_TLT 0x30
119#define ACCEL20_REG_TWS 0x31
120#define ACCEL20_REG_FFTH 0x32
121#define ACCEL20_REG_FFC 0x33
122#define ACCEL20_REG_FFCNTL 0x34
123#define ACCEL20_REG_TILT_ANGLE_LL 0x37
124#define ACCEL20_REG_TILT_ANGLE_HL 0x38
125#define ACCEL20_REG_HYST_SET 0x39
126#define ACCEL20_REG_LP_CNTL1 0x3A
127#define ACCEL20_REG_LP_CNTL2 0x3B
128#define ACCEL20_REG_WUFTH 0x49
129#define ACCEL20_REG_BTSWUFTH 0x4A
130#define ACCEL20_REG_BTSTH 0x4B
131#define ACCEL20_REG_BTSC 0x4C
132#define ACCEL20_REG_WUFC 0x4D
133#define ACCEL20_REG_SELF_TEST 0x5D
134#define ACCEL20_REG_BUF_CNTL1 0x5E
135#define ACCEL20_REG_BUF_CNTL2 0x5F
136#define ACCEL20_REG_BUF_STATUS_1 0x60
137#define ACCEL20_REG_BUF_STATUS_2 0x61
138#define ACCEL20_REG_BUF_CLEAR 0x62
139#define ACCEL20_REG_BUF_READ 0x63
140#define ACCEL20_REG_ADP_CNTL1 0x64
141#define ACCEL20_REG_ADP_CNTL2 0x65
142#define ACCEL20_REG_ADP_CNTL3 0x66
143#define ACCEL20_REG_ADP_CNTL4 0x67
144#define ACCEL20_REG_ADP_CNTL5 0x68
145#define ACCEL20_REG_ADP_CNTL6 0x69
146#define ACCEL20_REG_ADP_CNTL7 0x6A
147#define ACCEL20_REG_ADP_CNTL8 0x6B
148#define ACCEL20_REG_ADP_CNTL9 0x6C
149#define ACCEL20_REG_ADP_CNTL10 0x6D
150#define ACCEL20_REG_ADP_CNTL11 0x6E
151#define ACCEL20_REG_ADP_CNTL12 0x6F
152#define ACCEL20_REG_ADP_CNTL13 0x70
153#define ACCEL20_REG_ADP_CNTL14 0x71
154#define ACCEL20_REG_ADP_CNTL15 0x72
155#define ACCEL20_REG_ADP_CNTL16 0x73
156#define ACCEL20_REG_ADP_CNTL17 0x74
157#define ACCEL20_REG_ADP_CNTL18 0x75
158#define ACCEL20_REG_ADP_CNTL19 0x76
159#define ACCEL20_REG_INTERNAL 0x7F
160
161 // accel20_reg
162
177#define ACCEL20_INTERNAL_SOFT_RESET 0x00
178#define ACCEL20_SOFT_RESET_PWR_CYC 0x00
179#define ACCEL20_SOFT_RESET_REBOOT 0x80
180
181#define ACCEL20_CNTL1_RANGE_8g 0x00
182#define ACCEL20_CNTL1_RANGE_16g 0x01
183#define ACCEL20_CNTL1_RANGE_32g 0x02
184#define ACCEL20_CNTL1_RANGE_64g 0x03
185#define ACCEL20_RANGE_BIT_MASK 0xE7
186
187#define ACCEL20_ODCNTL_ODR_0_781_Hz 0x00
188#define ACCEL20_ODCNTL_ODR_1_563_Hz 0x01
189#define ACCEL20_ODCNTL_ODR_3_125_Hz 0x02
190#define ACCEL20_ODCNTL_ODR_6_25_Hz 0x03
191#define ACCEL20_ODCNTL_ODR_12_5_Hz 0x04
192#define ACCEL20_ODCNTL_ODR_25_Hz 0x05
193#define ACCEL20_ODCNTL_ODR_50_Hz 0x06
194#define ACCEL20_ODCNTL_ODR_100_Hz 0x07
195#define ACCEL20_ODCNTL_ODR_200_Hz 0x08
196#define ACCEL20_ODCNTL_ODR_400_Hz 0x09
197#define ACCEL20_ODCNTL_ODR_800_Hz 0x0A
198#define ACCEL20_ODCNTL_ODR_1600_Hz 0x0B
199#define ACCEL20_ODCNTL_ODR_3200_Hz 0x0C
200#define ACCEL20_ODCNTL_ODR_6400_Hz 0x0D
201#define ACCEL20_ODCNTL_ODR_12800_Hz 0x0E
202#define ACCEL20_ODCNTL_ODR_25600_Hz 0x0F
203#define ACCEL20_ODR_BIT_MASK 0x0F
204
205#define ACCEL20_CNTL1_OP_MODE_STB 0x00
206#define ACCEL20_CNTL1_OP_MODE_L_PWR 0x02
207#define ACCEL20_CNTL1_OP_MODE_HP 0x03
208#define ACCEL20_OP_MODE_BIT_MASK 0xC0
209
210#define ACCEL20_ENABLE_INT_1 0x30
211#define ACCEL20_ENABLE_DATA_READY 0x10
212
213#define ACCEL20_DATA_READY_BIT_MASK 0x10
214
215#define ACCEL20_INT1_DATA_READY 0x00
216
217#define ACCEL20_CHIP_ID 0x46
218
219#define ACCEL20_TRIGGER_DISABLE 0x00
220#define ACCEL20_TRIGGER_ENABLE 0x01
221
227#define ACCEL20_SET_DEV_ADDR_GND 0x1E
228#define ACCEL20_SET_DEV_ADDR_VCC 0x1F
229
238#define ACCEL20_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
239#define ACCEL20_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
240
241 // accel20_set
242
257#define ACCEL20_MAP_MIKROBUS( cfg, mikrobus ) \
258 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
259 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
260 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
261 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
262 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
263 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
264 cfg.i2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
265 cfg.trg = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
266 cfg.i1 = MIKROBUS( mikrobus, MIKROBUS_INT )
267
268 // accel20_map
269 // accel20
270
281
286typedef err_t ( *accel20_master_io_t )( struct accel20_s*, uint8_t, uint8_t*, uint8_t );
292typedef struct accel20_s
293{
294 digital_out_t trg;
296 digital_in_t i2;
297 digital_in_t i1;
299 i2c_master_t i2c;
300 spi_master_t spi;
303 pin_name_t chip_select;
310
315typedef struct
316{
317 pin_name_t scl;
318 pin_name_t sda;
319 pin_name_t miso;
320 pin_name_t mosi;
321 pin_name_t sck;
322 pin_name_t cs;
323 pin_name_t i2;
324 pin_name_t trg;
325 pin_name_t i1;
327 uint32_t i2c_speed;
328 uint8_t i2c_address;
330 uint32_t spi_speed;
331 spi_master_mode_t spi_mode;
332 spi_master_chip_select_polarity_t cs_polarity;
337
342typedef struct
343{
344 int16_t x;
345 int16_t y;
346 int16_t z;
347
349
354typedef enum
355{
357 ACCEL20_ERROR = -1
358
360
377
393
409
424
440err_t accel20_generic_write ( accel20_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
441
457err_t accel20_generic_read ( accel20_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
458
473
488
503
521err_t accel20_set_operating_mode ( accel20_t *ctx, uint8_t op_mode );
522
537
552
571err_t accel20_set_accel_range ( accel20_t *ctx, uint8_t range );
572
603err_t accel20_set_output_data_rate ( accel20_t *ctx, uint8_t odr );
604
620
637err_t accel20_set_trigger ( accel20_t *ctx, uint8_t en_trg );
638
653
668
669#ifdef __cplusplus
670}
671#endif
672#endif // ACCEL20_H
673
674 // accel20
675
676// ------------------------------------------------------------------------ END
err_t(* accel20_master_io_t)(struct accel20_s *, uint8_t, uint8_t *, uint8_t)
Accel 20 Click driver interface.
Definition accel20.h:286
accel20_drv_t
Accel 20 Click driver selector.
Definition accel20.h:276
@ ACCEL20_DRV_SEL_SPI
Definition accel20.h:277
@ ACCEL20_DRV_SEL_I2C
Definition accel20.h:278
struct accel20_s accel20_t
Accel 20 Click context object.
accel20_return_value_t
Accel 20 Click return value data.
Definition accel20.h:355
@ ACCEL20_OK
Definition accel20.h:356
@ ACCEL20_ERROR
Definition accel20.h:357
err_t accel20_init(accel20_t *ctx, accel20_cfg_t *cfg)
Accel 20 initialization function.
uint8_t accel20_data_ready(accel20_t *ctx)
Accel 20 check data ready function.
uint8_t accel20_get_int_1(accel20_t *ctx)
Accel 20 get Interrupt 1 function.
void accel20_drv_interface_selection(accel20_cfg_t *cfg, accel20_drv_t drv_sel)
Accel 20 driver interface setup function.
err_t accel20_set_accel_range(accel20_t *ctx, uint8_t range)
Accel 20 set accel range function.
err_t accel20_set_operating_mode(accel20_t *ctx, uint8_t op_mode)
Accel 20 set operating mode function.
err_t accel20_generic_read(accel20_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Accel 20 data reading function.
err_t accel20_check_id(accel20_t *ctx)
Accel 20 check ID function.
err_t accel20_enable_int_1_pin(accel20_t *ctx)
Accel 20 enable INT 1 pin function.
err_t accel20_set_trigger(accel20_t *ctx, uint8_t en_trg)
Accel 20 set trigger function.
err_t accel20_soft_reset(accel20_t *ctx)
Accel 20 soft reset function.
err_t accel20_set_output_data_rate(accel20_t *ctx, uint8_t odr)
Accel 20 set output data rate function.
err_t accel20_get_axis_data(accel20_t *ctx, accel20_axis_t *axis)
Accel 20 get accelerometer axis function.
err_t accel20_generic_write(accel20_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Accel 20 data writing function.
err_t accel20_default_cfg(accel20_t *ctx)
Accel 20 default configuration function.
err_t accel20_enable_int_2_pin(accel20_t *ctx)
Accel 20 enable INT 2 pin function.
void accel20_cfg_setup(accel20_cfg_t *cfg)
Accel 20 configuration object setup function.
uint8_t accel20_get_int_2(accel20_t *ctx)
Accel 20 get Interrupt 2 function.
This file contains SPI specific macros, functions, etc.
Accel 20 axis data structure.
Definition accel20.h:343
int16_t z
Definition accel20.h:346
int16_t x
Definition accel20.h:344
int16_t y
Definition accel20.h:345
Accel 20 Click configuration object.
Definition accel20.h:316
pin_name_t i1
Definition accel20.h:325
uint32_t i2c_speed
Definition accel20.h:327
spi_master_chip_select_polarity_t cs_polarity
Definition accel20.h:332
pin_name_t sck
Definition accel20.h:321
pin_name_t i2
Definition accel20.h:323
spi_master_mode_t spi_mode
Definition accel20.h:331
pin_name_t mosi
Definition accel20.h:320
uint32_t spi_speed
Definition accel20.h:330
accel20_drv_t drv_sel
Definition accel20.h:334
pin_name_t scl
Definition accel20.h:317
pin_name_t trg
Definition accel20.h:324
pin_name_t miso
Definition accel20.h:319
pin_name_t sda
Definition accel20.h:318
pin_name_t cs
Definition accel20.h:322
uint8_t i2c_address
Definition accel20.h:328
Accel 20 Click context object.
Definition accel20.h:293
spi_master_t spi
Definition accel20.h:300
digital_out_t trg
Definition accel20.h:294
accel20_master_io_t write_f
Definition accel20.h:306
digital_in_t i2
Definition accel20.h:296
i2c_master_t i2c
Definition accel20.h:299
accel20_master_io_t read_f
Definition accel20.h:307
accel20_drv_t drv_sel
Definition accel20.h:304
uint8_t slave_address
Definition accel20.h:302
digital_in_t i1
Definition accel20.h:297
pin_name_t chip_select
Definition accel20.h:303