accel  2.0.0.0
accel.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright (c) 2019, MikroElektronika - www.mikroe.com
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef ACCEL_H
36 #define ACCEL_H
37 
38 #include "drv_digital_in.h"
39 #include "drv_i2c_master.h"
40 #include "drv_spi_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
53 #define ACCEL_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
57  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
58  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
59  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
60  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
61 
67 #define ACCEL_MASTER_I2C 0
68 #define ACCEL_MASTER_SPI 1
69 
75 #define ACCEL_RETVAL uint8_t
76 
77 #define ACCEL_OK 0x00
78 #define ACCEL_INIT_ERROR 0xFF
79 
85 #define ACCEL_I2C_ADR_VCC 0x1D
86 #define ACCEL_I2C_ADR_GND 0x53
87 
93 #define ACCEL_REG_DEVID 0x00
94 #define ACCEL_REG_THRESH_TAP 0x1D
95 #define ACCEL_REG_OFSX 0x1E
96 #define ACCEL_REG_OFSY 0x1F
97 #define ACCEL_REG_OFSZ 0x20
98 #define ACCEL_REG_DUR 0x21
99 #define ACCEL_REG_LATENT 0x22
100 #define ACCEL_REG_WINDOW 0x23
101 #define ACCEL_REG_THRESH_ACT 0x24
102 #define ACCEL_REG_THRESH_INACT 0x25
103 #define ACCEL_REG_TIME_INACT 0x26
104 #define ACCEL_REG_ACT_INACT_CTL 0x27
105 #define ACCEL_REG_THRESH_FF 0x28
106 #define ACCEL_REG_TIME_FF 0x29
107 #define ACCEL_REG_TAP_AXES 0x2A
108 #define ACCEL_REG_ACT_TAP_STATUS 0x2B
109 #define ACCEL_REG_BW_RATE 0x2C
110 #define ACCEL_REG_POWER_CTL 0x2D
111 #define ACCEL_REG_INT_ENABLE 0x2E
112 #define ACCEL_REG_INT_MAP 0x2F
113 #define ACCEL_REG_INT_SOURCE 0x30
114 #define ACCEL_REG_DATA_FORMAT 0x31
115 #define ACCEL_REG_DATA_X_LSB 0x32
116 #define ACCEL_REG_DATA_X_MSB 0x33
117 #define ACCEL_REG_DATA_Y_LSB 0x34
118 #define ACCEL_REG_DATA_Y_MSB 0x35
119 #define ACCEL_REG_DATA_Z_LSB 0x36
120 #define ACCEL_REG_DATA_Z_MSB 0x37
121 #define ACCEL_REG_FIFO_CTL 0x38
122 #define ACCEL_REG_FIFO_STATUS 0x39
123 
129 #define ACCEL_DEVID 0xE5
130 
136 #define ACCEL_POWER_CTL_LINK 0x20
137 #define ACCEL_POWER_CTL_AUTO_SLEEP 0x10
138 #define ACCEL_POWER_CTL_MEASURE 0x08
139 #define ACCEL_POWER_CTL_SLEEP 0x04
140 #define ACCEL_POWER_CTL_WAKEUP_8 0x03
141 #define ACCEL_POWER_CTL_WAKEUP_4 0x02
142 #define ACCEL_POWER_CTL_WAKEUP_2 0x01
143 #define ACCEL_POWER_CTL_WAKEUP_1 0x00
144 
150 #define ACCEL_DATA_FORMAT_SELF_TEST 0x80
151 #define ACCEL_DATA_FORMAT_SPI 0x40
152 #define ACCEL_DATA_FORMAT_INT_INVERT 0x20
153 #define ACCEL_DATA_FORMAT_FULL_RES 0x08
154 #define ACCEL_DATA_FORMAT_JUSTIFY 0x04
155 #define ACCEL_DATA_FORMAT_RANGE_16 0x03
156 #define ACCEL_DATA_FORMAT_RANGE_8 0x02
157 #define ACCEL_DATA_FORMAT_RANGE_4 0x01
158 #define ACCEL_DATA_FORMAT_RANGE_2 0x00
159 
165 #define ACCEL_BW_RATE_LOW_POWER 0x10
166 #define ACCEL_BW_RATE_1600 0x0F
167 #define ACCEL_BW_RATE_800 0x0E
168 #define ACCEL_BW_RATE_400 0x0D
169 #define ACCEL_BW_RATE_200 0x0C
170 #define ACCEL_BW_RATE_100 0x0B
171 #define ACCEL_BW_RATE_50 0x0A
172 #define ACCEL_BW_RATE_25 0x09
173 #define ACCEL_BW_RATE_12_5 0x08
174 #define ACCEL_BW_RATE_6_25 0x07
175 #define ACCEL_BW_RATE_3_13 0x06
176 #define ACCEL_BW_RATE_1_56 0x05
177 #define ACCEL_BW_RATE_0_78 0x04
178 #define ACCEL_BW_RATE_0_39 0x03
179 #define ACCEL_BW_RATE_0_2 0x02
180 #define ACCEL_BW_RATE_0_1 0x01
181 #define ACCEL_BW_RATE_0_05 0x00
182 
188 #define ACCEL_FIFO_CTL_FIFO_MODE_BYPASS 0x00
189 #define ACCEL_FIFO_CTL_FIFO_MODE_ENABLE 0x40
190 #define ACCEL_FIFO_CTL_FIFO_MODE_STREAM 0x80
191 #define ACCEL_FIFO_CTL_FIFO_MODE_TRIGGER 0x60
192 #define ACCEL_FIFO_CTL_TRIGGER_INT2 0x20
193  // End group macro
196 // --------------------------------------------------------------- PUBLIC TYPES
205 typedef uint8_t accel_select_t;
206 
210 typedef void ( *accel_master_io_t )( struct accel_s*, uint8_t, uint8_t*, uint8_t );
211 
215 typedef struct accel_s
216 {
217  // Input pins
218 
219  digital_in_t int_pin;
220 
221  // Modules
222 
223  i2c_master_t i2c;
224  spi_master_t spi;
225 
226  // ctx variable
227 
228  hal_i2c_address_t slave_address;
232 
233 } accel_t;
234 
238 typedef struct
239 {
240  // Communication gpio pins
241 
242  pin_name_t scl;
243  pin_name_t sda;
244  pin_name_t miso;
245  pin_name_t mosi;
246  pin_name_t sck;
247  pin_name_t cs;
248 
249  // Additional gpio pins
250 
251  pin_name_t int_pin;
252 
253  // static variable
254 
255  hal_i2c_speed_t i2c_speed;
256  hal_i2c_address_t i2c_address;
257  hal_spi_speed_t spi_speed;
258  hal_spi_mode_t spi_mode;
259 
261 
262 } accel_cfg_t;
263  // End types group
265 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
271 #ifdef __cplusplus
272 extern "C"{
273 #endif
274 
283 void accel_cfg_setup ( accel_cfg_t *cfg );
284 
293 
331 void accel_default_cfg ( accel_t *ctx );
332 
343 void accel_generic_write ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
344 
355 void accel_generic_read ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
356 
366 int16_t accel_read_x_axis ( accel_t *ctx );
367 
377 int16_t accel_read_y_axis ( accel_t *ctx );
378 
388 int16_t accel_read_z_axis ( accel_t *ctx );
389 
400 uint8_t accel_check_int_pin ( accel_t *ctx );
401 
402 #ifdef __cplusplus
403 }
404 #endif
405 #endif // _ACCEL_H_
406  // End public_function group
409 
410 // ------------------------------------------------------------------------- END
accel_select_t master_sel
Definition: accel.h:231
void accel_generic_read(accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
pin_name_t scl
Definition: accel.h:242
hal_i2c_speed_t i2c_speed
Definition: accel.h:255
hal_spi_mode_t spi_mode
Definition: accel.h:258
#define ACCEL_RETVAL
Definition: accel.h:75
ACCEL_RETVAL accel_init(accel_t *ctx, accel_cfg_t *cfg)
Initialization function.
Click configuration structure definition.
Definition: accel.h:238
void accel_cfg_setup(accel_cfg_t *cfg)
Config Object Initialization function.
uint8_t accel_select_t
Communication type.
Definition: accel.h:205
struct accel_s accel_t
Click ctx object definition.
accel_master_io_t read_f
Definition: accel.h:230
pin_name_t mosi
Definition: accel.h:245
Click ctx object definition.
Definition: accel.h:215
pin_name_t int_pin
Definition: accel.h:251
pin_name_t sck
Definition: accel.h:246
hal_spi_speed_t spi_speed
Definition: accel.h:257
pin_name_t cs
Definition: accel.h:247
uint8_t accel_check_int_pin(accel_t *ctx)
INT Pin Get function.
pin_name_t miso
Definition: accel.h:244
int16_t accel_read_x_axis(accel_t *ctx)
Function raw read X axis.
hal_i2c_address_t i2c_address
Definition: accel.h:256
spi_master_t spi
Definition: accel.h:224
void accel_generic_write(accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void accel_default_cfg(accel_t *ctx)
Click Default Configuration function.
hal_i2c_address_t slave_address
Definition: accel.h:228
pin_name_t sda
Definition: accel.h:243
accel_select_t sel
Definition: accel.h:260
digital_in_t int_pin
Definition: accel.h:219
void(* accel_master_io_t)(struct accel_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel.h:210
accel_master_io_t write_f
Definition: accel.h:229
int16_t accel_read_y_axis(accel_t *ctx)
Function raw read Y axis.
i2c_master_t i2c
Definition: accel.h:223
int16_t accel_read_z_axis(accel_t *ctx)
Function raw read Z axis.