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 
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
68 #define ACCEL2_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
70  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
71  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
72  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
73  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
74  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
75  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
76 
82 #define ACCEL2_MASTER_I2C 0
83 #define ACCEL2_MASTER_SPI 1
84 
90 #define ACCEL2_RETVAL uint8_t
91 
92 #define ACCEL2_OK 0x00
93 #define ACCEL2_INIT_ERROR 0xFF
94 
100 #define ACCEL2_INFO1 0x0D
101 #define ACCEL2_INFO2 0x0E
102 #define ACCEL2_WHO_AM_I 0x0F
103 #define ACCEL2_CTRL_REG3 0x23
104 #define ACCEL2_CTRL_REG4 0x20
105 #define ACCEL2_CTRL_REG5 0x24
106 #define ACCEL2_CTRL_REG6 0x25
107 #define ACCEL2_STATUS 0x27
108 #define ACCEL2_OUT_T 0x0C
109 #define ACCEL2_OFF_X 0x10
110 #define ACCEL2_OFF_Y 0x11
111 #define ACCEL2_OFF_Z 0x12
112 #define ACCEL2_CS_X 0x13
113 #define ACCEL2_CS_Y 0x14
114 #define ACCEL2_CS_Z 0x15
115 #define ACCEL2_LC_L 0x16
116 #define ACCEL2_LC_H 0x17
117 #define ACCEL2_STAT 0x18
118 #define ACCEL2_VFC_1 0x1B
119 #define ACCEL2_VFC_2 0x1C
120 #define ACCEL2_VFC_3 0x1D
121 #define ACCEL2_VFC_4 0x1E
122 #define ACCEL2_THRS3 0x1F
123 #define ACCEL2_OUT_X_L 0x28
124 #define ACCEL2_OUT_X_H 0x29
125 #define ACCEL2_OUT_Y_L 0x2A
126 #define ACCEL2_OUT_Y_H 0x2B
127 #define ACCEL2_OUT_Z_L 0x2C
128 #define ACCEL2_OUT_Z_H 0x2D
129 #define ACCEL2_FIFO_CTRL 0x2E
130 #define ACCEL2_FIFO_SRC 0x2F
131 #define ACCEL2_CTRL_REG1 0x21
132 #define ACCEL2_ST1_1 0x40
133 #define ACCEL2_ST2_1 0x41
134 #define ACCEL2_ST3_1 0x42
135 #define ACCEL2_ST4_1 0x43
136 #define ACCEL2_ST5_1 0x44
137 #define ACCEL2_ST6_1 0x45
138 #define ACCEL2_ST7_1 0x46
139 #define ACCEL2_ST8_1 0x47
140 #define ACCEL2_ST9_1 0x48
141 #define ACCEL2_ST10_1 0x49
142 #define ACCEL2_ST11_1 0x4A
143 #define ACCEL2_ST12_1 0x4B
144 #define ACCEL2_ST13_1 0x4C
145 #define ACCEL2_ST14_1 0x4D
146 #define ACCEL2_ST15_1 0x4E
147 #define ACCEL2_ST16_1 0x4F
148 #define ACCEL2_TIM4_1 0x50
149 #define ACCEL2_TIM3_1 0x51
150 #define ACCEL2_TIM2_1_L 0x52
151 #define ACCEL2_TIM2_1_H 0x53
152 #define ACCEL2_TIM1_1_L 0x54
153 #define ACCEL2_TIM1_1_H 0x55
154 #define ACCEL2_THRS2_1 0x56
155 #define ACCEL2_THRS1_1 0x57
156 #define ACCEL2_MASK1_B 0x59
157 #define ACCEL2_MASK1_A 0x5A
158 #define ACCEL2_SETT1 0x5B
159 #define ACCEL2_PR1 0x5C
160 
166 #define ACCEL2_I2C_ADDRESS = 0x1D
167  // End group macro
170 // --------------------------------------------------------------- PUBLIC TYPES
179 typedef uint8_t accel2_select_t;
180 
184 typedef void ( *accel2_master_io_t )( struct accel2_s*, uint8_t, uint8_t*, uint8_t );
185 
189 typedef struct accel2_s
190 {
191  // Output pins
192  digital_out_t cs;
193 
194  // Input pins
195 
196  digital_in_t int_pin;
197 
198  // Modules
199 
200  i2c_master_t i2c;
201  spi_master_t spi;
202 
203  // ctx variable
204 
205  uint8_t slave_address;
206  pin_name_t chip_select;
210 
212 
216 typedef struct
217 {
218  // Communication gpio pins
219 
220  pin_name_t scl;
221  pin_name_t sda;
222  pin_name_t miso;
223  pin_name_t mosi;
224  pin_name_t sck;
225  pin_name_t cs;
226 
227  // Additional gpio pins
228 
229  pin_name_t int_pin;
230 
231  // static variable
232 
233  uint32_t i2c_speed;
234  uint8_t i2c_address;
235  uint32_t spi_speed;
236  uint8_t spi_mode;
237  spi_master_chip_select_polarity_t cs_polarity;
238 
240 
241 } accel2_cfg_t;
242  // End types group
244 
245 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
251 #ifdef __cplusplus
252 extern "C"{
253 #endif
254 
264 
273 
284 void accel2_generic_write ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
285 
296 void accel2_generic_read ( accel2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
297 
305 void accel2_setting ( accel2_t *ctx );
306 
314 uint8_t accel2_check_id ( accel2_t *ctx );
315 
323 int16_t accel2_read_xaxis ( accel2_t *ctx );
324 
332 int16_t accel2_read_yaxis ( accel2_t *ctx );
333 
341 int16_t accel2_read_zaxis ( accel2_t *ctx );
342 
343 #ifdef __cplusplus
344 }
345 #endif
346 #endif // _ACCEL2_H_
347  // End public_function group
350 
351 // ------------------------------------------------------------------------- END
accel2_s::slave_address
uint8_t slave_address
Definition: accel2.h:205
accel2_s::master_sel
accel2_select_t master_sel
Definition: accel2.h:209
accel2_cfg_t
Click configuration structure definition.
Definition: accel2.h:217
accel2_cfg_t::miso
pin_name_t miso
Definition: accel2.h:222
accel2_cfg_t::sda
pin_name_t sda
Definition: accel2.h:221
accel2_s::chip_select
pin_name_t chip_select
Definition: accel2.h:206
accel2_cfg_t::int_pin
pin_name_t int_pin
Definition: accel2.h:229
accel2_cfg_t::sck
pin_name_t sck
Definition: accel2.h:224
accel2_cfg_t::spi_mode
uint8_t spi_mode
Definition: accel2.h:236
accel2_s
Click ctx object definition.
Definition: accel2.h:190
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:239
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:192
accel2_cfg_t::scl
pin_name_t scl
Definition: accel2.h:220
accel2_s::write_f
accel2_master_io_t write_f
Definition: accel2.h:207
ACCEL2_RETVAL
#define ACCEL2_RETVAL
Definition: accel2.h:90
accel2_s::read_f
accel2_master_io_t read_f
Definition: accel2.h:208
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:184
accel2_cfg_t::mosi
pin_name_t mosi
Definition: accel2.h:223
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:235
accel2_select_t
uint8_t accel2_select_t
Communication type.
Definition: accel2.h:179
accel2_s::spi
spi_master_t spi
Definition: accel2.h:201
accel2_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel2.h:234
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:225
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:196
accel2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel2.h:237
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:200
accel2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel2.h:233