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 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_in.h"
53 #include "drv_i2c_master.h"
54 #include "drv_spi_master.h"
55 #include "spi_specifics.h"
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
68 #define ACCEL_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
70  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
71  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
72  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
73  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
74  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
75  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
76 
82 #define ACCEL_MASTER_I2C 0
83 #define ACCEL_MASTER_SPI 1
84 
90 #define ACCEL_OK 0
91 #define ACCEL_ERROR -1
92 
98 #define ACCEL_I2C_ADR_VCC 0x1D
99 #define ACCEL_I2C_ADR_GND 0x53
100 
106 #define ACCEL_REG_DEVID 0x00
107 #define ACCEL_REG_THRESH_TAP 0x1D
108 #define ACCEL_REG_OFSX 0x1E
109 #define ACCEL_REG_OFSY 0x1F
110 #define ACCEL_REG_OFSZ 0x20
111 #define ACCEL_REG_DUR 0x21
112 #define ACCEL_REG_LATENT 0x22
113 #define ACCEL_REG_WINDOW 0x23
114 #define ACCEL_REG_THRESH_ACT 0x24
115 #define ACCEL_REG_THRESH_INACT 0x25
116 #define ACCEL_REG_TIME_INACT 0x26
117 #define ACCEL_REG_ACT_INACT_CTL 0x27
118 #define ACCEL_REG_THRESH_FF 0x28
119 #define ACCEL_REG_TIME_FF 0x29
120 #define ACCEL_REG_TAP_AXES 0x2A
121 #define ACCEL_REG_ACT_TAP_STATUS 0x2B
122 #define ACCEL_REG_BW_RATE 0x2C
123 #define ACCEL_REG_POWER_CTL 0x2D
124 #define ACCEL_REG_INT_ENABLE 0x2E
125 #define ACCEL_REG_INT_MAP 0x2F
126 #define ACCEL_REG_INT_SOURCE 0x30
127 #define ACCEL_REG_DATA_FORMAT 0x31
128 #define ACCEL_REG_DATA_X_LSB 0x32
129 #define ACCEL_REG_DATA_X_MSB 0x33
130 #define ACCEL_REG_DATA_Y_LSB 0x34
131 #define ACCEL_REG_DATA_Y_MSB 0x35
132 #define ACCEL_REG_DATA_Z_LSB 0x36
133 #define ACCEL_REG_DATA_Z_MSB 0x37
134 #define ACCEL_REG_FIFO_CTL 0x38
135 #define ACCEL_REG_FIFO_STATUS 0x39
136 
142 #define ACCEL_DEVID 0xE5
143 
149 #define ACCEL_POWER_CTL_LINK 0x20
150 #define ACCEL_POWER_CTL_AUTO_SLEEP 0x10
151 #define ACCEL_POWER_CTL_MEASURE 0x08
152 #define ACCEL_POWER_CTL_SLEEP 0x04
153 #define ACCEL_POWER_CTL_WAKEUP_8 0x03
154 #define ACCEL_POWER_CTL_WAKEUP_4 0x02
155 #define ACCEL_POWER_CTL_WAKEUP_2 0x01
156 #define ACCEL_POWER_CTL_WAKEUP_1 0x00
157 
163 #define ACCEL_DATA_FORMAT_SELF_TEST 0x80
164 #define ACCEL_DATA_FORMAT_SPI 0x40
165 #define ACCEL_DATA_FORMAT_INT_INVERT 0x20
166 #define ACCEL_DATA_FORMAT_FULL_RES 0x08
167 #define ACCEL_DATA_FORMAT_JUSTIFY 0x04
168 #define ACCEL_DATA_FORMAT_RANGE_16 0x03
169 #define ACCEL_DATA_FORMAT_RANGE_8 0x02
170 #define ACCEL_DATA_FORMAT_RANGE_4 0x01
171 #define ACCEL_DATA_FORMAT_RANGE_2 0x00
172 
178 #define ACCEL_BW_RATE_LOW_POWER 0x10
179 #define ACCEL_BW_RATE_1600 0x0F
180 #define ACCEL_BW_RATE_800 0x0E
181 #define ACCEL_BW_RATE_400 0x0D
182 #define ACCEL_BW_RATE_200 0x0C
183 #define ACCEL_BW_RATE_100 0x0B
184 #define ACCEL_BW_RATE_50 0x0A
185 #define ACCEL_BW_RATE_25 0x09
186 #define ACCEL_BW_RATE_12_5 0x08
187 #define ACCEL_BW_RATE_6_25 0x07
188 #define ACCEL_BW_RATE_3_13 0x06
189 #define ACCEL_BW_RATE_1_56 0x05
190 #define ACCEL_BW_RATE_0_78 0x04
191 #define ACCEL_BW_RATE_0_39 0x03
192 #define ACCEL_BW_RATE_0_2 0x02
193 #define ACCEL_BW_RATE_0_1 0x01
194 #define ACCEL_BW_RATE_0_05 0x00
195 
201 #define ACCEL_FIFO_CTL_FIFO_MODE_BYPASS 0x00
202 #define ACCEL_FIFO_CTL_FIFO_MODE_ENABLE 0x40
203 #define ACCEL_FIFO_CTL_FIFO_MODE_STREAM 0x80
204 #define ACCEL_FIFO_CTL_FIFO_MODE_TRIGGER 0x60
205 #define ACCEL_FIFO_CTL_TRIGGER_INT2 0x20
206 
212 #define ACCEL_DATA_RES_LSB_PER_G 256.0f
213 
223 #define ACCEL_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
224 #define ACCEL_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
225 
226  // End group macro
228 // --------------------------------------------------------------- PUBLIC TYPES
237 typedef uint8_t accel_select_t;
238 
242 typedef void ( *accel_master_io_t )( struct accel_s*, uint8_t, uint8_t*, uint8_t );
243 
247 typedef struct accel_s
248 {
249  digital_out_t cs;
250 
251  // Input pins
252  digital_in_t int_pin;
253 
254  // Modules
255  i2c_master_t i2c;
256  spi_master_t spi;
257 
258  // ctx variable
259  uint8_t slave_address;
260  pin_name_t chip_select;
264 
266 
270 typedef struct
271 {
272  // Communication gpio pins
273  pin_name_t scl;
274  pin_name_t sda;
275  pin_name_t miso;
276  pin_name_t mosi;
277  pin_name_t sck;
278  pin_name_t cs;
279 
280  // Additional gpio pins
281  pin_name_t int_pin;
282 
283  // static variable
284  uint32_t i2c_speed;
285  uint8_t i2c_address;
286  uint32_t spi_speed;
287  spi_master_mode_t spi_mode;
288  spi_master_chip_select_polarity_t cs_polarity;
289 
291 
292 } accel_cfg_t;
293  // End types group
295 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
301 #ifdef __cplusplus
302 extern "C"{
303 #endif
304 
314 
322 err_t accel_init ( accel_t *ctx, accel_cfg_t *cfg );
323 
362 
373 void accel_generic_write ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
374 
385 void accel_generic_read ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
386 
396 int16_t accel_read_x_axis ( accel_t *ctx );
397 
407 int16_t accel_read_y_axis ( accel_t *ctx );
408 
418 int16_t accel_read_z_axis ( accel_t *ctx );
419 
430 uint8_t accel_check_int_pin ( accel_t *ctx );
431 
432 #ifdef __cplusplus
433 }
434 #endif
435 #endif // _ACCEL_H_
436  // End public_function group
439 
440 // ------------------------------------------------------------------------- END
accel_cfg_t::sda
pin_name_t sda
Definition: accel.h:274
accel_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel.h:287
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:259
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:284
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:242
accel_cfg_t
Click configuration structure definition.
Definition: accel.h:271
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel_s::int_pin
digital_in_t int_pin
Definition: accel.h:252
accel_select_t
uint8_t accel_select_t
Communication type.
Definition: accel.h:237
accel_s::chip_select
pin_name_t chip_select
Definition: accel.h:260
accel_s
Click ctx object definition.
Definition: accel.h:248
accel_s::i2c
i2c_master_t i2c
Definition: accel.h:255
accel_s::write_f
accel_master_io_t write_f
Definition: accel.h:261
accel_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel.h:288
accel_s::cs
digital_out_t cs
Definition: accel.h:249
accel_cfg_t::int_pin
pin_name_t int_pin
Definition: accel.h:281
accel_cfg_t::scl
pin_name_t scl
Definition: accel.h:273
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:277
accel_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel.h:286
accel_cfg_t::mosi
pin_name_t mosi
Definition: accel.h:276
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:263
accel_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel.h:285
accel_cfg_t::sel
accel_select_t sel
Definition: accel.h:290
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:278
accel_s::read_f
accel_master_io_t read_f
Definition: accel.h:262
accel_s::spi
spi_master_t spi
Definition: accel.h:256
accel_cfg_t::miso
pin_name_t miso
Definition: accel.h:275
accel_cfg_setup
void accel_cfg_setup(accel_cfg_t *cfg)
Config Object Initialization function.