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 
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  digital_out_t cs;
218 
219  // Input pins
220 
221  digital_in_t int_pin;
222 
223  // Modules
224 
225  i2c_master_t i2c;
226  spi_master_t spi;
227 
228  // ctx variable
229 
230  uint8_t slave_address;
231  pin_name_t chip_select;
235 
237 
241 typedef struct
242 {
243  // Communication gpio pins
244 
245  pin_name_t scl;
246  pin_name_t sda;
247  pin_name_t miso;
248  pin_name_t mosi;
249  pin_name_t sck;
250  pin_name_t cs;
251 
252  // Additional gpio pins
253 
254  pin_name_t int_pin;
255 
256  // static variable
257 
258  uint32_t i2c_speed;
259  uint8_t i2c_address;
260  uint32_t spi_speed;
261  uint8_t spi_mode;
262  spi_master_chip_select_polarity_t cs_polarity;
263 
265 
266 } accel_cfg_t;
267  // End types group
269 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
275 #ifdef __cplusplus
276 extern "C"{
277 #endif
278 
288 
297 
336 
347 void accel_generic_write ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
348 
359 void accel_generic_read ( accel_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
360 
370 int16_t accel_read_x_axis ( accel_t *ctx );
371 
381 int16_t accel_read_y_axis ( accel_t *ctx );
382 
392 int16_t accel_read_z_axis ( accel_t *ctx );
393 
404 uint8_t accel_check_int_pin ( accel_t *ctx );
405 
406 #ifdef __cplusplus
407 }
408 #endif
409 #endif // _ACCEL_H_
410  // End public_function group
413 
414 // ------------------------------------------------------------------------- END
accel_cfg_t::sda
pin_name_t sda
Definition: accel.h:246
accel_cfg_t::spi_mode
uint8_t spi_mode
Definition: accel.h:261
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:230
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:258
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:210
accel_cfg_t
Click configuration structure definition.
Definition: accel.h:242
accel_s::int_pin
digital_in_t int_pin
Definition: accel.h:221
accel_select_t
uint8_t accel_select_t
Communication type.
Definition: accel.h:205
accel_s::chip_select
pin_name_t chip_select
Definition: accel.h:231
accel_s
Click ctx object definition.
Definition: accel.h:216
accel_s::i2c
i2c_master_t i2c
Definition: accel.h:225
accel_s::write_f
accel_master_io_t write_f
Definition: accel.h:232
accel_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel.h:262
accel_s::cs
digital_out_t cs
Definition: accel.h:217
accel_cfg_t::int_pin
pin_name_t int_pin
Definition: accel.h:254
accel_cfg_t::scl
pin_name_t scl
Definition: accel.h:245
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:249
accel_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel.h:260
accel_init
ACCEL_RETVAL accel_init(accel_t *ctx, accel_cfg_t *cfg)
Initialization function.
accel_cfg_t::mosi
pin_name_t mosi
Definition: accel.h:248
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:234
accel_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel.h:259
accel_cfg_t::sel
accel_select_t sel
Definition: accel.h:264
accel_cfg_t::cs
pin_name_t cs
Definition: accel.h:250
ACCEL_RETVAL
#define ACCEL_RETVAL
Definition: accel.h:75
accel_s::read_f
accel_master_io_t read_f
Definition: accel.h:233
accel_s::spi
spi_master_t spi
Definition: accel.h:226
accel_cfg_t::miso
pin_name_t miso
Definition: accel.h:247
accel_cfg_setup
void accel_cfg_setup(accel_cfg_t *cfg)
Config Object Initialization function.