accel3 2.0.0.0
accel3.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 ACCEL3_H
36#define ACCEL3_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_out.h"
53#include "drv_digital_in.h"
54#include "drv_i2c_master.h"
55#include "drv_spi_master.h"
56
57// -------------------------------------------------------------- PUBLIC MACROS
67#define ACCEL3_MAP_MIKROBUS( cfg, mikrobus ) \
68 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
69 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
70 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
71 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
72 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
73 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
74 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
81#define ACCEL3_MASTER_I2C 0
82#define ACCEL3_MASTER_SPI 1
89#define ACCEL3_RETVAL uint8_t
90
91#define ACCEL3_OK 0x00
92#define ACCEL3_INIT_ERROR 0xFF
99#define ACCEL3_I2C_ADDRESS 0x18
106#define ACCEL3_REG_WHO_AM_I 0x0F
107#define ACCEL3_REG_HP_FILTER_RESET 0x25
108#define ACCEL3_REG_REFERENCE 0x26
109#define ACCEL3_REG_STATUS 0x27
116#define ACCEL3_CTRL_REG1 0x20
117#define ACCEL3_CTRL_REG2 0x21
118#define ACCEL3_CTRL_REG3 0x22
119#define ACCEL3_CTRL_REG4 0x23
120#define ACCEL3_CTRL_REG5 0x24
127#define ACCEL3_OUT_X_L 0x28
128#define ACCEL3_OUT_X_H 0x29
129#define ACCEL3_OUT_Y_L 0x2A
130#define ACCEL3_OUT_Y_H 0x2B
131#define ACCEL3_OUT_Z_L 0x2C
132#define ACCEL3_OUT_Z_H 0x2D
139#define ACCEL3_ENABLE_AXIS 0x27
140#define ACCEL3_FULL_SCALE 0x00
141#define ACCEL3_DATA_MASK 0x80
142#define ACCEL3_DEVICE_IDENTIFIER 0x32
143#define ACCEL3_TRUE 0x00
144#define ACCEL3_FALSE 0x01
145#define ACCEL3_START_VALUE 0x00
148 // End group macro
149
150// --------------------------------------------------------------- PUBLIC TYPES
159typedef uint8_t accel3_select_t;
160
164typedef void ( *accel3_master_io_t )( struct accel3_s*, uint8_t, uint8_t*, uint8_t );
165
169typedef struct accel3_s
170{
171 // Output pins
172
173 digital_out_t int_pin;
174 digital_out_t cs;
175
176 // Modules
177
178 i2c_master_t i2c;
179 spi_master_t spi;
180
181 // ctx variable
182
184 pin_name_t chip_select;
188
190
194typedef struct
195{
196 // Communication gpio pins
197
198 pin_name_t scl;
199 pin_name_t sda;
200 pin_name_t miso;
201 pin_name_t mosi;
202 pin_name_t sck;
203 pin_name_t cs;
204
205 // Additional gpio pins
206
207 pin_name_t int_pin;
208
209 // static variable
210
211 uint32_t i2c_speed;
212 uint8_t i2c_address;
213 uint32_t spi_speed;
214 uint8_t spi_mode;
215 spi_master_chip_select_polarity_t cs_polarity;
216
218
220
221 // End types group
222
231typedef struct
232{
233 int16_t x;
234 int16_t y;
235 int16_t z;
236
238
239 // Measurement data
240
241// ------------------------------------------------------------------ CONSTANTS
248 // End constants group
249
250// ------------------------------------------------------------------ VARIABLES
257 // End variable group
258
259// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
265#ifdef __cplusplus
266extern "C"{
267#endif
268
278
288
297
308void accel3_generic_write ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
309
320void accel3_generic_read ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
321
329uint8_t accel3_check_id ( accel3_t *ctx );
330
341
342#ifdef __cplusplus
343}
344#endif
345#endif // _ACCEL3_H_
346
347 // End public_function group
349
350// ------------------------------------------------------------------------- END
#define ACCEL3_RETVAL
Definition accel3.h:89
void accel3_default_cfg(accel3_t *ctx, accel3_cfg_t *cfg)
Click Default Configuration function.
uint8_t accel3_check_id(accel3_t *ctx)
Check Accel 3 ID function.
void accel3_cfg_setup(accel3_cfg_t *cfg)
Config Object Initialization function.
void accel3_generic_write(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void accel3_read_data(accel3_t *ctx, accel3_data_t *accel3_data)
Get accelerometer data function.
ACCEL3_RETVAL accel3_init(accel3_t *ctx, accel3_cfg_t *cfg)
Initialization function.
void accel3_generic_read(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
void(* accel3_master_io_t)(struct accel3_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition accel3.h:164
uint8_t accel3_select_t
Communication type.
Definition accel3.h:159
struct accel3_s accel3_t
Click ctx object definition.
accel3_data_t accel3_data
Definition main.c:32
Click configuration structure definition.
Definition accel3.h:195
uint8_t spi_mode
Definition accel3.h:214
uint32_t i2c_speed
Definition accel3.h:211
spi_master_chip_select_polarity_t cs_polarity
Definition accel3.h:215
accel3_select_t sel
Definition accel3.h:217
pin_name_t sck
Definition accel3.h:202
pin_name_t mosi
Definition accel3.h:201
uint32_t spi_speed
Definition accel3.h:213
pin_name_t scl
Definition accel3.h:198
pin_name_t int_pin
Definition accel3.h:207
pin_name_t miso
Definition accel3.h:200
pin_name_t sda
Definition accel3.h:199
pin_name_t cs
Definition accel3.h:203
uint8_t i2c_address
Definition accel3.h:212
Accel data object definition.
Definition accel3.h:232
int16_t z
Definition accel3.h:235
int16_t x
Definition accel3.h:233
int16_t y
Definition accel3.h:234
Click ctx object definition.
Definition accel3.h:170
digital_out_t cs
Definition accel3.h:174
spi_master_t spi
Definition accel3.h:179
i2c_master_t i2c
Definition accel3.h:178
accel3_master_io_t read_f
Definition accel3.h:186
digital_out_t int_pin
Definition accel3.h:173
accel3_select_t master_sel
Definition accel3.h:187
uint8_t slave_address
Definition accel3.h:183
accel3_master_io_t write_f
Definition accel3.h:185
pin_name_t chip_select
Definition accel3.h:184