accel3  2.0.0.0
accel3.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 ACCEL3_H
36 #define ACCEL3_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 #include "drv_spi_master.h"
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define ACCEL3_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 ACCEL3_MASTER_I2C 0
68 #define ACCEL3_MASTER_SPI 1
69 
75 #define ACCEL3_RETVAL uint8_t
76 
77 #define ACCEL3_OK 0x00
78 #define ACCEL3_INIT_ERROR 0xFF
79 
85 #define ACCEL3_I2C_ADDRESS 0x18
86 
92 #define ACCEL3_REG_WHO_AM_I 0x0F
93 #define ACCEL3_REG_HP_FILTER_RESET 0x25
94 #define ACCEL3_REG_REFERENCE 0x26
95 #define ACCEL3_REG_STATUS 0x27
96 
102 #define ACCEL3_CTRL_REG1 0x20
103 #define ACCEL3_CTRL_REG2 0x21
104 #define ACCEL3_CTRL_REG3 0x22
105 #define ACCEL3_CTRL_REG4 0x23
106 #define ACCEL3_CTRL_REG5 0x24
107 
113 #define ACCEL3_OUT_X_L 0x28
114 #define ACCEL3_OUT_X_H 0x29
115 #define ACCEL3_OUT_Y_L 0x2A
116 #define ACCEL3_OUT_Y_H 0x2B
117 #define ACCEL3_OUT_Z_L 0x2C
118 #define ACCEL3_OUT_Z_H 0x2D
119 
125 #define ACCEL3_ENABLE_AXIS 0x27
126 #define ACCEL3_FULL_SCALE 0x00
127 #define ACCEL3_DATA_MASK 0x80
128 #define ACCEL3_DEVICE_IDENTIFIER 0x32
129 #define ACCEL3_TRUE 0x00
130 #define ACCEL3_FALSE 0x01
131 #define ACCEL3_START_VALUE 0x00
132  // End group macro
135 
136 // --------------------------------------------------------------- PUBLIC TYPES
145 typedef uint8_t accel3_select_t;
146 
150 typedef void ( *accel3_master_io_t )( struct accel3_s*, uint8_t, uint8_t*, uint8_t );
151 
155 typedef struct accel3_s
156 {
157  // Output pins
158 
159  digital_out_t int_pin;
160 
161  // Modules
162 
163  i2c_master_t i2c;
164  spi_master_t spi;
165 
166  // ctx variable
167 
168  hal_i2c_address_t slave_address;
172 
173 } accel3_t;
174 
178 typedef struct
179 {
180  // Communication gpio pins
181 
182  pin_name_t scl;
183  pin_name_t sda;
184  pin_name_t miso;
185  pin_name_t mosi;
186  pin_name_t sck;
187  pin_name_t cs;
188 
189  // Additional gpio pins
190 
191  pin_name_t int_pin;
192 
193  // static variable
194 
195  hal_i2c_speed_t i2c_speed;
196  hal_i2c_address_t i2c_address;
197  hal_spi_speed_t spi_speed;
198  hal_spi_mode_t spi_mode;
199 
201 
202 } accel3_cfg_t;
203  // End types group
205 
214 typedef struct
215 {
216  int16_t x;
217  int16_t y;
218  int16_t z;
219 
220 } accel3_data_t;
221  // Measurement data
223 
224 // ------------------------------------------------------------------ CONSTANTS // End constants group
232 
233 // ------------------------------------------------------------------ VARIABLES // End variable group
241 
242 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
248 #ifdef __cplusplus
249 extern "C"{
250 #endif
251 
260 void accel3_cfg_setup ( accel3_cfg_t *cfg );
261 
271 
279 void accel3_default_cfg ( accel3_t *ctx, accel3_cfg_t *cfg );
280 
291 void accel3_generic_write ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
292 
303 void accel3_generic_read ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
304 
312 uint8_t accel3_check_id ( accel3_t *ctx );
313 
324 
325 #ifdef __cplusplus
326 }
327 #endif
328 #endif // _ACCEL3_H_
329  // End public_function group
332 
333 // ------------------------------------------------------------------------- END
struct accel3_s accel3_t
Click ctx object definition.
hal_i2c_speed_t i2c_speed
Definition: accel3.h:195
pin_name_t mosi
Definition: accel3.h:185
accel3_data_t accel3_data
Definition: main.c:32
void accel3_generic_read(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
void accel3_cfg_setup(accel3_cfg_t *cfg)
Config Object Initialization function.
Accel data object definition.
Definition: accel3.h:214
hal_spi_speed_t spi_speed
Definition: accel3.h:197
accel3_select_t master_sel
Definition: accel3.h:171
hal_spi_mode_t spi_mode
Definition: accel3.h:198
Click configuration structure definition.
Definition: accel3.h:178
pin_name_t cs
Definition: accel3.h:187
ACCEL3_RETVAL accel3_init(accel3_t *ctx, accel3_cfg_t *cfg)
Initialization function.
void(* accel3_master_io_t)(struct accel3_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel3.h:150
void accel3_generic_write(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
Click ctx object definition.
Definition: accel3.h:155
accel3_select_t sel
Definition: accel3.h:200
#define ACCEL3_RETVAL
Definition: accel3.h:75
int16_t x
Definition: accel3.h:216
spi_master_t spi
Definition: accel3.h:164
hal_i2c_address_t i2c_address
Definition: accel3.h:196
int16_t z
Definition: accel3.h:218
pin_name_t int_pin
Definition: accel3.h:191
pin_name_t sck
Definition: accel3.h:186
accel3_master_io_t read_f
Definition: accel3.h:170
int16_t y
Definition: accel3.h:217
pin_name_t miso
Definition: accel3.h:184
void accel3_default_cfg(accel3_t *ctx, accel3_cfg_t *cfg)
Click Default Configuration function.
pin_name_t sda
Definition: accel3.h:183
uint8_t accel3_check_id(accel3_t *ctx)
Check Accel 3 ID function.
uint8_t accel3_select_t
Communication type.
Definition: accel3.h:145
accel3_master_io_t write_f
Definition: accel3.h:169
pin_name_t scl
Definition: accel3.h:182
i2c_master_t i2c
Definition: accel3.h:163
hal_i2c_address_t slave_address
Definition: accel3.h:168
void accel3_read_data(accel3_t *ctx, accel3_data_t *accel3_data)
Get accelerometer data function.
digital_out_t int_pin
Definition: accel3.h:159