accel2  2.0.0.0
accel2.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 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  // Output pins
178  digital_out_t cs;
179 
180  // Input pins
181 
182  digital_in_t int_pin;
183 
184  // Modules
185 
186  i2c_master_t i2c;
187  spi_master_t spi;
188 
189  // ctx variable
190 
191  uint8_t slave_address;
192  pin_name_t chip_select;
196 
197 } accel2_t;
198 
202 typedef struct
203 {
204  // Communication gpio pins
205 
206  pin_name_t scl;
207  pin_name_t sda;
208  pin_name_t miso;
209  pin_name_t mosi;
210  pin_name_t sck;
211  pin_name_t cs;
212 
213  // Additional gpio pins
214 
215  pin_name_t int_pin;
216 
217  // static variable
218 
219  uint32_t i2c_speed;
220  uint8_t i2c_address;
221  uint32_t spi_speed;
222  uint8_t spi_mode;
223  spi_master_chip_select_polarity_t cs_polarity;
224 
226 
227 } accel2_cfg_t;
228  // End types group
230 
231 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
237 #ifdef __cplusplus
238 extern "C"{
239 #endif
240 
249 void accel2_cfg_setup ( accel2_cfg_t *cfg );
250 
259 
270 void accel2_generic_write ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
271 
282 void accel2_generic_read ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
283 
291 void accel2_setting ( accel2_t *ctx );
292 
300 uint8_t accel2_check_id ( accel2_t *ctx );
301 
309 int16_t accel2_read_xaxis ( accel2_t *ctx );
310 
318 int16_t accel2_read_yaxis ( accel2_t *ctx );
319 
327 int16_t accel2_read_zaxis ( accel2_t *ctx );
328 
329 #ifdef __cplusplus
330 }
331 #endif
332 #endif // _ACCEL2_H_
333  // End public_function group
336 
337 // ------------------------------------------------------------------------- END
pin_name_t sck
Definition: accel2.h:210
uint8_t slave_address
Definition: accel2.h:191
pin_name_t chip_select
Definition: accel2.h:192
#define ACCEL2_RETVAL
Definition: accel2.h:76
Click ctx object definition.
Definition: accel2.h:175
accel2_select_t sel
Definition: accel2.h:225
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:211
Click configuration structure definition.
Definition: accel2.h:202
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:215
int16_t accel2_read_yaxis(accel2_t *ctx)
Function read Y axis.
accel2_select_t master_sel
Definition: accel2.h:195
digital_out_t cs
Definition: accel2.h:178
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:206
uint8_t spi_mode
Definition: accel2.h:222
int16_t accel2_read_xaxis(accel2_t *ctx)
Function read X axis.
accel2_master_io_t write_f
Definition: accel2.h:193
accel2_master_io_t read_f
Definition: accel2.h:194
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:207
void accel2_generic_read(accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
uint8_t i2c_address
Definition: accel2.h:220
int16_t accel2_read_zaxis(accel2_t *ctx)
Function read Z axis.
pin_name_t mosi
Definition: accel2.h:209
struct accel2_s accel2_t
Click ctx object definition.
spi_master_t spi
Definition: accel2.h:187
i2c_master_t i2c
Definition: accel2.h:186
uint32_t spi_speed
Definition: accel2.h:221
pin_name_t miso
Definition: accel2.h:208
digital_in_t int_pin
Definition: accel2.h:182
spi_master_chip_select_polarity_t cs_polarity
Definition: accel2.h:223
uint32_t i2c_speed
Definition: accel2.h:219