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 "drv_digital_in.h"
39 #include "drv_i2c_master.h"
40 #include "drv_spi_master.h"
41 #include "spi_specifics.h"
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define ACCEL_MAP_MIKROBUS( cfg, mikrobus ) \
55  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
56  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
57  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
58  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
59  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
60  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
61  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
62 
68 #define ACCEL_MASTER_I2C 0
69 #define ACCEL_MASTER_SPI 1
70 
76 #define ACCEL_OK 0
77 #define ACCEL_ERROR -1
78 
84 #define ACCEL_I2C_ADR_VCC 0x1D
85 #define ACCEL_I2C_ADR_GND 0x53
86 
92 #define ACCEL_REG_DEVID 0x00
93 #define ACCEL_REG_THRESH_TAP 0x1D
94 #define ACCEL_REG_OFSX 0x1E
95 #define ACCEL_REG_OFSY 0x1F
96 #define ACCEL_REG_OFSZ 0x20
97 #define ACCEL_REG_DUR 0x21
98 #define ACCEL_REG_LATENT 0x22
99 #define ACCEL_REG_WINDOW 0x23
100 #define ACCEL_REG_THRESH_ACT 0x24
101 #define ACCEL_REG_THRESH_INACT 0x25
102 #define ACCEL_REG_TIME_INACT 0x26
103 #define ACCEL_REG_ACT_INACT_CTL 0x27
104 #define ACCEL_REG_THRESH_FF 0x28
105 #define ACCEL_REG_TIME_FF 0x29
106 #define ACCEL_REG_TAP_AXES 0x2A
107 #define ACCEL_REG_ACT_TAP_STATUS 0x2B
108 #define ACCEL_REG_BW_RATE 0x2C
109 #define ACCEL_REG_POWER_CTL 0x2D
110 #define ACCEL_REG_INT_ENABLE 0x2E
111 #define ACCEL_REG_INT_MAP 0x2F
112 #define ACCEL_REG_INT_SOURCE 0x30
113 #define ACCEL_REG_DATA_FORMAT 0x31
114 #define ACCEL_REG_DATA_X_LSB 0x32
115 #define ACCEL_REG_DATA_X_MSB 0x33
116 #define ACCEL_REG_DATA_Y_LSB 0x34
117 #define ACCEL_REG_DATA_Y_MSB 0x35
118 #define ACCEL_REG_DATA_Z_LSB 0x36
119 #define ACCEL_REG_DATA_Z_MSB 0x37
120 #define ACCEL_REG_FIFO_CTL 0x38
121 #define ACCEL_REG_FIFO_STATUS 0x39
122 
128 #define ACCEL_DEVID 0xE5
129 
135 #define ACCEL_POWER_CTL_LINK 0x20
136 #define ACCEL_POWER_CTL_AUTO_SLEEP 0x10
137 #define ACCEL_POWER_CTL_MEASURE 0x08
138 #define ACCEL_POWER_CTL_SLEEP 0x04
139 #define ACCEL_POWER_CTL_WAKEUP_8 0x03
140 #define ACCEL_POWER_CTL_WAKEUP_4 0x02
141 #define ACCEL_POWER_CTL_WAKEUP_2 0x01
142 #define ACCEL_POWER_CTL_WAKEUP_1 0x00
143 
149 #define ACCEL_DATA_FORMAT_SELF_TEST 0x80
150 #define ACCEL_DATA_FORMAT_SPI 0x40
151 #define ACCEL_DATA_FORMAT_INT_INVERT 0x20
152 #define ACCEL_DATA_FORMAT_FULL_RES 0x08
153 #define ACCEL_DATA_FORMAT_JUSTIFY 0x04
154 #define ACCEL_DATA_FORMAT_RANGE_16 0x03
155 #define ACCEL_DATA_FORMAT_RANGE_8 0x02
156 #define ACCEL_DATA_FORMAT_RANGE_4 0x01
157 #define ACCEL_DATA_FORMAT_RANGE_2 0x00
158 
164 #define ACCEL_BW_RATE_LOW_POWER 0x10
165 #define ACCEL_BW_RATE_1600 0x0F
166 #define ACCEL_BW_RATE_800 0x0E
167 #define ACCEL_BW_RATE_400 0x0D
168 #define ACCEL_BW_RATE_200 0x0C
169 #define ACCEL_BW_RATE_100 0x0B
170 #define ACCEL_BW_RATE_50 0x0A
171 #define ACCEL_BW_RATE_25 0x09
172 #define ACCEL_BW_RATE_12_5 0x08
173 #define ACCEL_BW_RATE_6_25 0x07
174 #define ACCEL_BW_RATE_3_13 0x06
175 #define ACCEL_BW_RATE_1_56 0x05
176 #define ACCEL_BW_RATE_0_78 0x04
177 #define ACCEL_BW_RATE_0_39 0x03
178 #define ACCEL_BW_RATE_0_2 0x02
179 #define ACCEL_BW_RATE_0_1 0x01
180 #define ACCEL_BW_RATE_0_05 0x00
181 
187 #define ACCEL_FIFO_CTL_FIFO_MODE_BYPASS 0x00
188 #define ACCEL_FIFO_CTL_FIFO_MODE_ENABLE 0x40
189 #define ACCEL_FIFO_CTL_FIFO_MODE_STREAM 0x80
190 #define ACCEL_FIFO_CTL_FIFO_MODE_TRIGGER 0x60
191 #define ACCEL_FIFO_CTL_TRIGGER_INT2 0x20
192 
198 #define ACCEL_DATA_RES_LSB_PER_G 256.0f
199 
209 #define ACCEL_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
210 #define ACCEL_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
211 
212  // End group macro
214 // --------------------------------------------------------------- PUBLIC TYPES
223 typedef uint8_t accel_select_t;
224 
228 typedef void ( *accel_master_io_t )( struct accel_s*, uint8_t, uint8_t*, uint8_t );
229 
233 typedef struct accel_s
234 {
235  digital_out_t cs;
236 
237  // Input pins
238  digital_in_t int_pin;
239 
240  // Modules
241  i2c_master_t i2c;
242  spi_master_t spi;
243 
244  // ctx variable
245  uint8_t slave_address;
246  pin_name_t chip_select;
250 
252 
256 typedef struct
257 {
258  // Communication gpio pins
259  pin_name_t scl;
260  pin_name_t sda;
261  pin_name_t miso;
262  pin_name_t mosi;
263  pin_name_t sck;
264  pin_name_t cs;
265 
266  // Additional gpio pins
267  pin_name_t int_pin;
268 
269  // static variable
270  uint32_t i2c_speed;
271  uint8_t i2c_address;
272  uint32_t spi_speed;
273  spi_master_mode_t spi_mode;
274  spi_master_chip_select_polarity_t cs_polarity;
275 
277 
278 } accel_cfg_t;
279  // End types group
281 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
287 #ifdef __cplusplus
288 extern "C"{
289 #endif
290 
300 
308 err_t accel_init ( accel_t *ctx, accel_cfg_t *cfg );
309 
348 
359 void accel_generic_write ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
360 
371 void accel_generic_read ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
372 
382 int16_t accel_read_x_axis ( accel_t *ctx );
383 
393 int16_t accel_read_y_axis ( accel_t *ctx );
394 
404 int16_t accel_read_z_axis ( accel_t *ctx );
405 
416 uint8_t accel_check_int_pin ( accel_t *ctx );
417 
418 #ifdef __cplusplus
419 }
420 #endif
421 #endif // _ACCEL_H_
422  // End public_function group
425 
426 // ------------------------------------------------------------------------- END
accel_cfg_t::sda
pin_name_t sda
Definition: accel.h:260
accel_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accel.h:273
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:245
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:270
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:228
accel_cfg_t
Click configuration structure definition.
Definition: accel.h:257
spi_specifics.h
This file contains SPI specific macros, functions, etc.
accel_s::int_pin
digital_in_t int_pin
Definition: accel.h:238
accel_select_t
uint8_t accel_select_t
Communication type.
Definition: accel.h:223
accel_s::chip_select
pin_name_t chip_select
Definition: accel.h:246
accel_s
Click ctx object definition.
Definition: accel.h:234
accel_s::i2c
i2c_master_t i2c
Definition: accel.h:241
accel_s::write_f
accel_master_io_t write_f
Definition: accel.h:247
accel_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel.h:274
accel_s::cs
digital_out_t cs
Definition: accel.h:235
accel_cfg_t::int_pin
pin_name_t int_pin
Definition: accel.h:267
accel_cfg_t::scl
pin_name_t scl
Definition: accel.h:259
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:263
accel_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel.h:272
accel_cfg_t::mosi
pin_name_t mosi
Definition: accel.h:262
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:249
accel_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel.h:271
accel_cfg_t::sel
accel_select_t sel
Definition: accel.h:276
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:264
accel_s::read_f
accel_master_io_t read_f
Definition: accel.h:248
accel_s::spi
spi_master_t spi
Definition: accel.h:242
accel_cfg_t::miso
pin_name_t miso
Definition: accel.h:261
accel_cfg_setup
void accel_cfg_setup(accel_cfg_t *cfg)
Config Object Initialization function.