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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 #include "drv_spi_master.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define ACCEL2_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
66  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
72 
78 #define ACCEL2_MASTER_I2C 0
79 #define ACCEL2_MASTER_SPI 1
80 
86 #define ACCEL2_RETVAL uint8_t
87 
88 #define ACCEL2_OK 0x00
89 #define ACCEL2_INIT_ERROR 0xFF
90 
96 #define ACCEL2_INFO1 0x0D
97 #define ACCEL2_INFO2 0x0E
98 #define ACCEL2_WHO_AM_I 0x0F
99 #define ACCEL2_CTRL_REG3 0x23
100 #define ACCEL2_CTRL_REG4 0x20
101 #define ACCEL2_CTRL_REG5 0x24
102 #define ACCEL2_CTRL_REG6 0x25
103 #define ACCEL2_STATUS 0x27
104 #define ACCEL2_OUT_T 0x0C
105 #define ACCEL2_OFF_X 0x10
106 #define ACCEL2_OFF_Y 0x11
107 #define ACCEL2_OFF_Z 0x12
108 #define ACCEL2_CS_X 0x13
109 #define ACCEL2_CS_Y 0x14
110 #define ACCEL2_CS_Z 0x15
111 #define ACCEL2_LC_L 0x16
112 #define ACCEL2_LC_H 0x17
113 #define ACCEL2_STAT 0x18
114 #define ACCEL2_VFC_1 0x1B
115 #define ACCEL2_VFC_2 0x1C
116 #define ACCEL2_VFC_3 0x1D
117 #define ACCEL2_VFC_4 0x1E
118 #define ACCEL2_THRS3 0x1F
119 #define ACCEL2_OUT_X_L 0x28
120 #define ACCEL2_OUT_X_H 0x29
121 #define ACCEL2_OUT_Y_L 0x2A
122 #define ACCEL2_OUT_Y_H 0x2B
123 #define ACCEL2_OUT_Z_L 0x2C
124 #define ACCEL2_OUT_Z_H 0x2D
125 #define ACCEL2_FIFO_CTRL 0x2E
126 #define ACCEL2_FIFO_SRC 0x2F
127 #define ACCEL2_CTRL_REG1 0x21
128 #define ACCEL2_ST1_1 0x40
129 #define ACCEL2_ST2_1 0x41
130 #define ACCEL2_ST3_1 0x42
131 #define ACCEL2_ST4_1 0x43
132 #define ACCEL2_ST5_1 0x44
133 #define ACCEL2_ST6_1 0x45
134 #define ACCEL2_ST7_1 0x46
135 #define ACCEL2_ST8_1 0x47
136 #define ACCEL2_ST9_1 0x48
137 #define ACCEL2_ST10_1 0x49
138 #define ACCEL2_ST11_1 0x4A
139 #define ACCEL2_ST12_1 0x4B
140 #define ACCEL2_ST13_1 0x4C
141 #define ACCEL2_ST14_1 0x4D
142 #define ACCEL2_ST15_1 0x4E
143 #define ACCEL2_ST16_1 0x4F
144 #define ACCEL2_TIM4_1 0x50
145 #define ACCEL2_TIM3_1 0x51
146 #define ACCEL2_TIM2_1_L 0x52
147 #define ACCEL2_TIM2_1_H 0x53
148 #define ACCEL2_TIM1_1_L 0x54
149 #define ACCEL2_TIM1_1_H 0x55
150 #define ACCEL2_THRS2_1 0x56
151 #define ACCEL2_THRS1_1 0x57
152 #define ACCEL2_MASK1_B 0x59
153 #define ACCEL2_MASK1_A 0x5A
154 #define ACCEL2_SETT1 0x5B
155 #define ACCEL2_PR1 0x5C
156 
162 #define ACCEL2_I2C_ADDRESS = 0x1D
163  // End group macro
166 // --------------------------------------------------------------- PUBLIC TYPES
175 typedef uint8_t accel2_select_t;
176 
180 typedef void ( *accel2_master_io_t )( struct accel2_s*, uint8_t, uint8_t*, uint8_t );
181 
185 typedef struct accel2_s
186 {
187  // Output pins
188  digital_out_t cs;
189 
190  // Input pins
191 
192  digital_in_t int_pin;
193 
194  // Modules
195 
196  i2c_master_t i2c;
197  spi_master_t spi;
198 
199  // ctx variable
200 
201  uint8_t slave_address;
202  pin_name_t chip_select;
206 
208 
212 typedef struct
213 {
214  // Communication gpio pins
215 
216  pin_name_t scl;
217  pin_name_t sda;
218  pin_name_t miso;
219  pin_name_t mosi;
220  pin_name_t sck;
221  pin_name_t cs;
222 
223  // Additional gpio pins
224 
225  pin_name_t int_pin;
226 
227  // static variable
228 
229  uint32_t i2c_speed;
230  uint8_t i2c_address;
231  uint32_t spi_speed;
232  uint8_t spi_mode;
233  spi_master_chip_select_polarity_t cs_polarity;
234 
236 
237 } accel2_cfg_t;
238  // End types group
240 
241 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
247 #ifdef __cplusplus
248 extern "C"{
249 #endif
250 
260 
269 
280 void accel2_generic_write ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
281 
292 void accel2_generic_read ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
293 
301 void accel2_setting ( accel2_t *ctx );
302 
310 uint8_t accel2_check_id ( accel2_t *ctx );
311 
319 int16_t accel2_read_xaxis ( accel2_t *ctx );
320 
328 int16_t accel2_read_yaxis ( accel2_t *ctx );
329 
337 int16_t accel2_read_zaxis ( accel2_t *ctx );
338 
339 #ifdef __cplusplus
340 }
341 #endif
342 #endif // _ACCEL2_H_
343  // End public_function group
346 
347 // ------------------------------------------------------------------------- END
accel2_s::slave_address
uint8_t slave_address
Definition: accel2.h:201
accel2_s::master_sel
accel2_select_t master_sel
Definition: accel2.h:205
accel2_cfg_t
Click configuration structure definition.
Definition: accel2.h:213
accel2_cfg_t::miso
pin_name_t miso
Definition: accel2.h:218
accel2_cfg_t::sda
pin_name_t sda
Definition: accel2.h:217
accel2_s::chip_select
pin_name_t chip_select
Definition: accel2.h:202
accel2_cfg_t::int_pin
pin_name_t int_pin
Definition: accel2.h:225
accel2_cfg_t::sck
pin_name_t sck
Definition: accel2.h:220
accel2_cfg_t::spi_mode
uint8_t spi_mode
Definition: accel2.h:232
accel2_s
Click ctx object definition.
Definition: accel2.h:186
accel2_t
struct accel2_s accel2_t
Click ctx object definition.
accel2_read_zaxis
int16_t accel2_read_zaxis(accel2_t *ctx)
Function read Z axis.
accel2_init
ACCEL2_RETVAL accel2_init(accel2_t *ctx, accel2_cfg_t *cfg)
Initialization function.
accel2_cfg_t::sel
accel2_select_t sel
Definition: accel2.h:235
accel2_generic_write
void accel2_generic_write(accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
accel2_s::cs
digital_out_t cs
Definition: accel2.h:188
accel2_cfg_t::scl
pin_name_t scl
Definition: accel2.h:216
accel2_s::write_f
accel2_master_io_t write_f
Definition: accel2.h:203
ACCEL2_RETVAL
#define ACCEL2_RETVAL
Definition: accel2.h:86
accel2_s::read_f
accel2_master_io_t read_f
Definition: accel2.h:204
accel2_generic_read
void accel2_generic_read(accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
accel2_master_io_t
void(* accel2_master_io_t)(struct accel2_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel2.h:180
accel2_cfg_t::mosi
pin_name_t mosi
Definition: accel2.h:219
accel2_read_yaxis
int16_t accel2_read_yaxis(accel2_t *ctx)
Function read Y axis.
accel2_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel2.h:231
accel2_select_t
uint8_t accel2_select_t
Communication type.
Definition: accel2.h:175
accel2_s::spi
spi_master_t spi
Definition: accel2.h:197
accel2_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel2.h:230
accel2_read_xaxis
int16_t accel2_read_xaxis(accel2_t *ctx)
Function read X axis.
accel2_cfg_t::cs
pin_name_t cs
Definition: accel2.h:221
accel2_setting
void accel2_setting(accel2_t *ctx)
Initializes function.
accel2_cfg_setup
void accel2_cfg_setup(accel2_cfg_t *cfg)
Config Object Initialization function.
accel2_s::int_pin
digital_in_t int_pin
Definition: accel2.h:192
accel2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel2.h:233
accel2_check_id
uint8_t accel2_check_id(accel2_t *ctx)
Check Accel 2 ID.
accel2_s::i2c
i2c_master_t i2c
Definition: accel2.h:196
accel2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel2.h:229