accel2  2.0.0.0
accel2.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 ACCEL2_H
36 #define ACCEL2_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
54 #define ACCEL2_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 ACCEL2_MASTER_I2C 0
69 #define ACCEL2_MASTER_SPI 1
70 
76 #define ACCEL2_RETVAL uint8_t
77 
78 #define ACCEL2_OK 0x00
79 #define ACCEL2_INIT_ERROR 0xFF
80 
86 #define ACCEL2_INFO1 0x0D
87 #define ACCEL2_INFO2 0x0E
88 #define ACCEL2_WHO_AM_I 0x0F
89 #define ACCEL2_CTRL_REG3 0x23
90 #define ACCEL2_CTRL_REG4 0x20
91 #define ACCEL2_CTRL_REG5 0x24
92 #define ACCEL2_CTRL_REG6 0x25
93 #define ACCEL2_STATUS 0x27
94 #define ACCEL2_OUT_T 0x0C
95 #define ACCEL2_OFF_X 0x10
96 #define ACCEL2_OFF_Y 0x11
97 #define ACCEL2_OFF_Z 0x12
98 #define ACCEL2_CS_X 0x13
99 #define ACCEL2_CS_Y 0x14
100 #define ACCEL2_CS_Z 0x15
101 #define ACCEL2_LC_L 0x16
102 #define ACCEL2_LC_H 0x17
103 #define ACCEL2_STAT 0x18
104 #define ACCEL2_VFC_1 0x1B
105 #define ACCEL2_VFC_2 0x1C
106 #define ACCEL2_VFC_3 0x1D
107 #define ACCEL2_VFC_4 0x1E
108 #define ACCEL2_THRS3 0x1F
109 #define ACCEL2_OUT_X_L 0x28
110 #define ACCEL2_OUT_X_H 0x29
111 #define ACCEL2_OUT_Y_L 0x2A
112 #define ACCEL2_OUT_Y_H 0x2B
113 #define ACCEL2_OUT_Z_L 0x2C
114 #define ACCEL2_OUT_Z_H 0x2D
115 #define ACCEL2_FIFO_CTRL 0x2E
116 #define ACCEL2_FIFO_SRC 0x2F
117 #define ACCEL2_CTRL_REG1 0x21
118 #define ACCEL2_ST1_1 0x40
119 #define ACCEL2_ST2_1 0x41
120 #define ACCEL2_ST3_1 0x42
121 #define ACCEL2_ST4_1 0x43
122 #define ACCEL2_ST5_1 0x44
123 #define ACCEL2_ST6_1 0x45
124 #define ACCEL2_ST7_1 0x46
125 #define ACCEL2_ST8_1 0x47
126 #define ACCEL2_ST9_1 0x48
127 #define ACCEL2_ST10_1 0x49
128 #define ACCEL2_ST11_1 0x4A
129 #define ACCEL2_ST12_1 0x4B
130 #define ACCEL2_ST13_1 0x4C
131 #define ACCEL2_ST14_1 0x4D
132 #define ACCEL2_ST15_1 0x4E
133 #define ACCEL2_ST16_1 0x4F
134 #define ACCEL2_TIM4_1 0x50
135 #define ACCEL2_TIM3_1 0x51
136 #define ACCEL2_TIM2_1_L 0x52
137 #define ACCEL2_TIM2_1_H 0x53
138 #define ACCEL2_TIM1_1_L 0x54
139 #define ACCEL2_TIM1_1_H 0x55
140 #define ACCEL2_THRS2_1 0x56
141 #define ACCEL2_THRS1_1 0x57
142 #define ACCEL2_MASK1_B 0x59
143 #define ACCEL2_MASK1_A 0x5A
144 #define ACCEL2_SETT1 0x5B
145 #define ACCEL2_PR1 0x5C
146 
152 #define ACCEL2_I2C_ADDRESS = 0x1D
153  // End group macro
156 // --------------------------------------------------------------- PUBLIC TYPES
165 typedef uint8_t accel2_select_t;
166 
170 typedef void ( *accel2_master_io_t )( struct accel2_s*, uint8_t, uint8_t*, uint8_t );
171 
175 typedef struct accel2_s
176 {
177 
178  // Input pins
179 
180  digital_in_t int_pin;
181 
182  // Modules
183 
184  i2c_master_t i2c;
185  spi_master_t spi;
186 
187  // ctx variable
188 
189  hal_i2c_address_t slave_address;
193 
194 } accel2_t;
195 
199 typedef struct
200 {
201  // Communication gpio pins
202 
203  pin_name_t scl;
204  pin_name_t sda;
205  pin_name_t miso;
206  pin_name_t mosi;
207  pin_name_t sck;
208  pin_name_t cs;
209 
210  // Additional gpio pins
211 
212  pin_name_t int_pin;
213 
214  // static variable
215 
216  hal_i2c_speed_t i2c_speed;
217  hal_i2c_address_t i2c_address;
218  hal_spi_speed_t spi_speed;
219  hal_spi_mode_t spi_mode;
220 
222 
223 } accel2_cfg_t;
224  // End types group
226 
227 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
233 #ifdef __cplusplus
234 extern "C"{
235 #endif
236 
245 void accel2_cfg_setup ( accel2_cfg_t *cfg );
246 
255 
266 void accel2_generic_write ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
267 
278 void accel2_generic_read ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
279 
287 void accel2_setting ( accel2_t *ctx );
288 
296 uint8_t accel2_check_id ( accel2_t *ctx );
297 
305 uint16_t accel2_read_xaxis ( accel2_t *ctx );
306 
314 uint16_t accel2_read_yaxis ( accel2_t *ctx );
315 
323 uint16_t accel2_read_zaxis ( accel2_t *ctx );
324 
325 #ifdef __cplusplus
326 }
327 #endif
328 #endif // _ACCEL2_H_
329  // End public_function group
332 
333 // ------------------------------------------------------------------------- END
pin_name_t sck
Definition: accel2.h:207
#define ACCEL2_RETVAL
Definition: accel2.h:76
uint16_t accel2_read_yaxis(accel2_t *ctx)
Function read Y axis.
hal_spi_mode_t spi_mode
Definition: accel2.h:219
uint16_t accel2_read_zaxis(accel2_t *ctx)
Function read Z axis.
Click ctx object definition.
Definition: accel2.h:175
accel2_select_t sel
Definition: accel2.h:221
hal_i2c_address_t slave_address
Definition: accel2.h:189
uint8_t accel2_check_id(accel2_t *ctx)
Check Accel 2 ID.
void accel2_setting(accel2_t *ctx)
Initializes function.
pin_name_t cs
Definition: accel2.h:208
Click configuration structure definition.
Definition: accel2.h:199
void accel2_generic_write(accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
pin_name_t int_pin
Definition: accel2.h:212
hal_i2c_speed_t i2c_speed
Definition: accel2.h:216
accel2_select_t master_sel
Definition: accel2.h:192
hal_i2c_address_t i2c_address
Definition: accel2.h:217
void(* accel2_master_io_t)(struct accel2_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel2.h:170
ACCEL2_RETVAL accel2_init(accel2_t *ctx, accel2_cfg_t *cfg)
Initialization function.
pin_name_t scl
Definition: accel2.h:203
accel2_master_io_t write_f
Definition: accel2.h:190
accel2_master_io_t read_f
Definition: accel2.h:191
uint8_t accel2_select_t
Communication type.
Definition: accel2.h:165
void accel2_cfg_setup(accel2_cfg_t *cfg)
Config Object Initialization function.
pin_name_t sda
Definition: accel2.h:204
void accel2_generic_read(accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
hal_spi_speed_t spi_speed
Definition: accel2.h:218
pin_name_t mosi
Definition: accel2.h:206
struct accel2_s accel2_t
Click ctx object definition.
spi_master_t spi
Definition: accel2.h:185
i2c_master_t i2c
Definition: accel2.h:184
uint16_t accel2_read_xaxis(accel2_t *ctx)
Function read X axis.
pin_name_t miso
Definition: accel2.h:205
digital_in_t int_pin
Definition: accel2.h:180