accel3
2.0.0.0
cmake
accel3
lib_accel3
include
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 );
75
81
#define ACCEL3_MASTER_I2C 0
82
#define ACCEL3_MASTER_SPI 1
83
89
#define ACCEL3_RETVAL uint8_t
90
91
#define ACCEL3_OK 0x00
92
#define ACCEL3_INIT_ERROR 0xFF
93
99
#define ACCEL3_I2C_ADDRESS 0x18
100
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
110
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
121
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
133
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
146
// End group macro
149
150
// --------------------------------------------------------------- PUBLIC TYPES
159
typedef
uint8_t
accel3_select_t
;
160
164
typedef
void ( *
accel3_master_io_t
)(
struct
accel3_s
*, uint8_t, uint8_t*, uint8_t );
165
169
typedef
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
183
uint8_t
slave_address
;
184
pin_name_t
chip_select
;
185
accel3_master_io_t
write_f
;
186
accel3_master_io_t
read_f
;
187
accel3_select_t
master_sel
;
188
189
}
accel3_t
;
190
194
typedef
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
217
accel3_select_t
sel
;
218
219
}
accel3_cfg_t
;
220
// End types group
222
231
typedef
struct
232
{
233
int16_t
x
;
234
int16_t
y
;
235
int16_t
z
;
236
237
}
accel3_data_t
;
238
// Measurement data
240
241
// ------------------------------------------------------------------ CONSTANTS // End constants group
249
250
// ------------------------------------------------------------------ VARIABLES // End variable group
258
259
// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
265
#ifdef __cplusplus
266
extern
"C"
{
267
#endif
268
277
void
accel3_cfg_setup
(
accel3_cfg_t
*cfg );
278
287
ACCEL3_RETVAL
accel3_init
(
accel3_t
*ctx,
accel3_cfg_t
*cfg );
288
296
void
accel3_default_cfg
(
accel3_t
*ctx,
accel3_cfg_t
*cfg );
297
308
void
accel3_generic_write
(
accel3_t
*ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
309
320
void
accel3_generic_read
(
accel3_t
*ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
321
329
uint8_t
accel3_check_id
(
accel3_t
*ctx );
330
340
void
accel3_read_data
(
accel3_t
*ctx,
accel3_data_t
*
accel3_data
);
341
342
#ifdef __cplusplus
343
}
344
#endif
345
#endif // _ACCEL3_H_
346
// End public_function group
349
350
// ------------------------------------------------------------------------- END
accel3_check_id
uint8_t accel3_check_id(accel3_t *ctx)
Check Accel 3 ID function.
accel3_s::slave_address
uint8_t slave_address
Definition:
accel3.h:183
accel3_s::chip_select
pin_name_t chip_select
Definition:
accel3.h:184
accel3_cfg_t
Click configuration structure definition.
Definition:
accel3.h:195
accel3_generic_write
void accel3_generic_write(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
accel3_cfg_t::spi_mode
uint8_t spi_mode
Definition:
accel3.h:214
accel3_cfg_t::miso
pin_name_t miso
Definition:
accel3.h:200
accel3_generic_read
void accel3_generic_read(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
accel3_read_data
void accel3_read_data(accel3_t *ctx, accel3_data_t *accel3_data)
Get accelerometer data function.
accel3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition:
accel3.h:215
accel3_cfg_t::mosi
pin_name_t mosi
Definition:
accel3.h:201
accel3_cfg_t::sda
pin_name_t sda
Definition:
accel3.h:199
accel3_cfg_t::i2c_address
uint8_t i2c_address
Definition:
accel3.h:212
accel3_data
accel3_data_t accel3_data
Definition:
main.c:32
accel3_s::cs
digital_out_t cs
Definition:
accel3.h:174
ACCEL3_RETVAL
#define ACCEL3_RETVAL
Definition:
accel3.h:89
accel3_s::master_sel
accel3_select_t master_sel
Definition:
accel3.h:187
accel3_data_t
Accel data object definition.
Definition:
accel3.h:232
accel3_t
struct accel3_s accel3_t
Click ctx object definition.
accel3_s::write_f
accel3_master_io_t write_f
Definition:
accel3.h:185
accel3_select_t
uint8_t accel3_select_t
Communication type.
Definition:
accel3.h:159
accel3_master_io_t
void(* accel3_master_io_t)(struct accel3_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition:
accel3.h:164
accel3_s::read_f
accel3_master_io_t read_f
Definition:
accel3.h:186
accel3_data_t::y
int16_t y
Definition:
accel3.h:234
accel3_data_t::z
int16_t z
Definition:
accel3.h:235
accel3_cfg_t::sel
accel3_select_t sel
Definition:
accel3.h:217
accel3_cfg_t::cs
pin_name_t cs
Definition:
accel3.h:203
accel3_cfg_t::spi_speed
uint32_t spi_speed
Definition:
accel3.h:213
accel3_data_t::x
int16_t x
Definition:
accel3.h:233
accel3_cfg_t::int_pin
pin_name_t int_pin
Definition:
accel3.h:207
accel3_s::spi
spi_master_t spi
Definition:
accel3.h:179
accel3_s::i2c
i2c_master_t i2c
Definition:
accel3.h:178
accel3_default_cfg
void accel3_default_cfg(accel3_t *ctx, accel3_cfg_t *cfg)
Click Default Configuration function.
accel3_init
ACCEL3_RETVAL accel3_init(accel3_t *ctx, accel3_cfg_t *cfg)
Initialization function.
accel3_s::int_pin
digital_out_t int_pin
Definition:
accel3.h:173
accel3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition:
accel3.h:211
accel3_cfg_t::sck
pin_name_t sck
Definition:
accel3.h:202
accel3_cfg_t::scl
pin_name_t scl
Definition:
accel3.h:198
accel3_s
Click ctx object definition.
Definition:
accel3.h:170
accel3_cfg_setup
void accel3_cfg_setup(accel3_cfg_t *cfg)
Config Object Initialization function.
Generated by
1.8.20