accel  2.0.0.0
accel.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef ACCEL_H
36 #define ACCEL_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_in.h"
49 #include "drv_i2c_master.h"
50 #include "drv_spi_master.h"
51 #include "spi_specifics.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define ACCEL_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
66  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
72 
78 #define ACCEL_MASTER_I2C 0
79 #define ACCEL_MASTER_SPI 1
80 
86 #define ACCEL_OK 0
87 #define ACCEL_ERROR -1
88 
94 #define ACCEL_I2C_ADR_VCC 0x1D
95 #define ACCEL_I2C_ADR_GND 0x53
96 
102 #define ACCEL_REG_DEVID 0x00
103 #define ACCEL_REG_THRESH_TAP 0x1D
104 #define ACCEL_REG_OFSX 0x1E
105 #define ACCEL_REG_OFSY 0x1F
106 #define ACCEL_REG_OFSZ 0x20
107 #define ACCEL_REG_DUR 0x21
108 #define ACCEL_REG_LATENT 0x22
109 #define ACCEL_REG_WINDOW 0x23
110 #define ACCEL_REG_THRESH_ACT 0x24
111 #define ACCEL_REG_THRESH_INACT 0x25
112 #define ACCEL_REG_TIME_INACT 0x26
113 #define ACCEL_REG_ACT_INACT_CTL 0x27
114 #define ACCEL_REG_THRESH_FF 0x28
115 #define ACCEL_REG_TIME_FF 0x29
116 #define ACCEL_REG_TAP_AXES 0x2A
117 #define ACCEL_REG_ACT_TAP_STATUS 0x2B
118 #define ACCEL_REG_BW_RATE 0x2C
119 #define ACCEL_REG_POWER_CTL 0x2D
120 #define ACCEL_REG_INT_ENABLE 0x2E
121 #define ACCEL_REG_INT_MAP 0x2F
122 #define ACCEL_REG_INT_SOURCE 0x30
123 #define ACCEL_REG_DATA_FORMAT 0x31
124 #define ACCEL_REG_DATA_X_LSB 0x32
125 #define ACCEL_REG_DATA_X_MSB 0x33
126 #define ACCEL_REG_DATA_Y_LSB 0x34
127 #define ACCEL_REG_DATA_Y_MSB 0x35
128 #define ACCEL_REG_DATA_Z_LSB 0x36
129 #define ACCEL_REG_DATA_Z_MSB 0x37
130 #define ACCEL_REG_FIFO_CTL 0x38
131 #define ACCEL_REG_FIFO_STATUS 0x39
132 
138 #define ACCEL_DEVID 0xE5
139 
145 #define ACCEL_POWER_CTL_LINK 0x20
146 #define ACCEL_POWER_CTL_AUTO_SLEEP 0x10
147 #define ACCEL_POWER_CTL_MEASURE 0x08
148 #define ACCEL_POWER_CTL_SLEEP 0x04
149 #define ACCEL_POWER_CTL_WAKEUP_8 0x03
150 #define ACCEL_POWER_CTL_WAKEUP_4 0x02
151 #define ACCEL_POWER_CTL_WAKEUP_2 0x01
152 #define ACCEL_POWER_CTL_WAKEUP_1 0x00
153 
159 #define ACCEL_DATA_FORMAT_SELF_TEST 0x80
160 #define ACCEL_DATA_FORMAT_SPI 0x40
161 #define ACCEL_DATA_FORMAT_INT_INVERT 0x20
162 #define ACCEL_DATA_FORMAT_FULL_RES 0x08
163 #define ACCEL_DATA_FORMAT_JUSTIFY 0x04
164 #define ACCEL_DATA_FORMAT_RANGE_16 0x03
165 #define ACCEL_DATA_FORMAT_RANGE_8 0x02
166 #define ACCEL_DATA_FORMAT_RANGE_4 0x01
167 #define ACCEL_DATA_FORMAT_RANGE_2 0x00
168 
174 #define ACCEL_BW_RATE_LOW_POWER 0x10
175 #define ACCEL_BW_RATE_1600 0x0F
176 #define ACCEL_BW_RATE_800 0x0E
177 #define ACCEL_BW_RATE_400 0x0D
178 #define ACCEL_BW_RATE_200 0x0C
179 #define ACCEL_BW_RATE_100 0x0B
180 #define ACCEL_BW_RATE_50 0x0A
181 #define ACCEL_BW_RATE_25 0x09
182 #define ACCEL_BW_RATE_12_5 0x08
183 #define ACCEL_BW_RATE_6_25 0x07
184 #define ACCEL_BW_RATE_3_13 0x06
185 #define ACCEL_BW_RATE_1_56 0x05
186 #define ACCEL_BW_RATE_0_78 0x04
187 #define ACCEL_BW_RATE_0_39 0x03
188 #define ACCEL_BW_RATE_0_2 0x02
189 #define ACCEL_BW_RATE_0_1 0x01
190 #define ACCEL_BW_RATE_0_05 0x00
191 
197 #define ACCEL_FIFO_CTL_FIFO_MODE_BYPASS 0x00
198 #define ACCEL_FIFO_CTL_FIFO_MODE_ENABLE 0x40
199 #define ACCEL_FIFO_CTL_FIFO_MODE_STREAM 0x80
200 #define ACCEL_FIFO_CTL_FIFO_MODE_TRIGGER 0x60
201 #define ACCEL_FIFO_CTL_TRIGGER_INT2 0x20
202 
208 #define ACCEL_DATA_RES_LSB_PER_G 256.0f
209 
219 #define ACCEL_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
220 #define ACCEL_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
221 
222  // End group macro
224 // --------------------------------------------------------------- PUBLIC TYPES
233 typedef uint8_t accel_select_t;
234 
238 typedef void ( *accel_master_io_t )( struct accel_s*, uint8_t, uint8_t*, uint8_t );
239 
243 typedef struct accel_s
244 {
245  digital_out_t cs;
246 
247  // Input pins
248  digital_in_t int_pin;
249 
250  // Modules
251  i2c_master_t i2c;
252  spi_master_t spi;
253 
254  // ctx variable
255  uint8_t slave_address;
256  pin_name_t chip_select;
260 
262 
266 typedef struct
267 {
268  // Communication gpio pins
269  pin_name_t scl;
270  pin_name_t sda;
271  pin_name_t miso;
272  pin_name_t mosi;
273  pin_name_t sck;
274  pin_name_t cs;
275 
276  // Additional gpio pins
277  pin_name_t int_pin;
278 
279  // static variable
280  uint32_t i2c_speed;
281  uint8_t i2c_address;
282  uint32_t spi_speed;
283  spi_master_mode_t spi_mode;
284  spi_master_chip_select_polarity_t cs_polarity;
285 
287 
288 } accel_cfg_t;
289  // End types group
291 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
297 #ifdef __cplusplus
298 extern "C"{
299 #endif
300 
310 
318 err_t accel_init ( accel_t *ctx, accel_cfg_t *cfg );
319 
358 
369 void accel_generic_write ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
370 
381 void accel_generic_read ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
382 
392 int16_t accel_read_x_axis ( accel_t *ctx );
393 
403 int16_t accel_read_y_axis ( accel_t *ctx );
404 
414 int16_t accel_read_z_axis ( accel_t *ctx );
415 
426 uint8_t accel_check_int_pin ( accel_t *ctx );
427 
428 #ifdef __cplusplus
429 }
430 #endif
431 #endif // _ACCEL_H_
432  // End public_function group
435 
436 // ------------------------------------------------------------------------- END
accel_cfg_t::sda
pin_name_t sda
Definition: accel.h:270
accel_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel.h:283
accel_read_y_axis
int16_t accel_read_y_axis(accel_t *ctx)
Function raw read Y axis.
accel_generic_read
void accel_generic_read(accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
accel_s::slave_address
uint8_t slave_address
Definition: accel.h:255
accel_default_cfg
void accel_default_cfg(accel_t *ctx)
Click Default Configuration function.
accel_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel.h:280
accel_master_io_t
void(* accel_master_io_t)(struct accel_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel.h:238
accel_cfg_t
Click configuration structure definition.
Definition: accel.h:267
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel_s::int_pin
digital_in_t int_pin
Definition: accel.h:248
accel_select_t
uint8_t accel_select_t
Communication type.
Definition: accel.h:233
accel_s::chip_select
pin_name_t chip_select
Definition: accel.h:256
accel_s
Click ctx object definition.
Definition: accel.h:244
accel_s::i2c
i2c_master_t i2c
Definition: accel.h:251
accel_s::write_f
accel_master_io_t write_f
Definition: accel.h:257
accel_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel.h:284
accel_s::cs
digital_out_t cs
Definition: accel.h:245
accel_cfg_t::int_pin
pin_name_t int_pin
Definition: accel.h:277
accel_cfg_t::scl
pin_name_t scl
Definition: accel.h:269
accel_read_z_axis
int16_t accel_read_z_axis(accel_t *ctx)
Function raw read Z axis.
accel_check_int_pin
uint8_t accel_check_int_pin(accel_t *ctx)
INT Pin Get function.
accel_cfg_t::sck
pin_name_t sck
Definition: accel.h:273
accel_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel.h:282
accel_cfg_t::mosi
pin_name_t mosi
Definition: accel.h:272
accel_t
struct accel_s accel_t
Click ctx object definition.
accel_read_x_axis
int16_t accel_read_x_axis(accel_t *ctx)
Function raw read X axis.
accel_generic_write
void accel_generic_write(accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
accel_s::master_sel
accel_select_t master_sel
Definition: accel.h:259
accel_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel.h:281
accel_cfg_t::sel
accel_select_t sel
Definition: accel.h:286
accel_init
err_t accel_init(accel_t *ctx, accel_cfg_t *cfg)
Initialization function.
accel_cfg_t::cs
pin_name_t cs
Definition: accel.h:274
accel_s::read_f
accel_master_io_t read_f
Definition: accel.h:258
accel_s::spi
spi_master_t spi
Definition: accel.h:252
accel_cfg_t::miso
pin_name_t miso
Definition: accel.h:271
accel_cfg_setup
void accel_cfg_setup(accel_cfg_t *cfg)
Config Object Initialization function.