accel7  2.0.0.0
accel7.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  */
32 // ----------------------------------------------------------------------------
33 
34 #ifndef ACCEL7_H
35 #define ACCEL7_H
36 
41 #ifdef PREINIT_SUPPORTED
42 #include "preinit.h"
43 #endif
44 
45 #ifdef MikroCCoreVersion
46  #if MikroCCoreVersion >= 1
47  #include "delays.h"
48  #endif
49 #endif
50 
51 #include "drv_digital_out.h"
52 #include "drv_digital_in.h"
53 #include "drv_i2c_master.h"
54 
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
66 #define ACCEL7_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
70 
76 #define ACCEL7_RETVAL uint8_t
77 
78 #define ACCEL7_OK 0x00
79 #define ACCEL7_INIT_ERROR 0xFF
80 
86 #define ACCEL7_AXIS_X 0x06
87 #define ACCEL7_AXIS_Y 0x08
88 #define ACCEL7_AXIS_Z 0x0A
89 #define ACCEL7_DATA_RESP_8bit 0x10
90 #define ACCEL7_DATA_RESP_12bit 0x20
91 #define ACCEL7_DATA_RESP_14bit 0x30
92 #define ACCEL7_RANGE_2g 0x01
93 #define ACCEL7_RANGE_4g 0x02
94 #define ACCEL7_RANGE_8g 0x03
95 #define ACCEL7_RANGE_16g 0x04
96 
102 #define ACCEL7_REG_AXIS_X_LSB 0x06
103 #define ACCEL7_REG_AXIS_X_MSB 0x07
104 #define ACCEL7_REG_AXIS_Y_LSB 0x08
105 #define ACCEL7_REG_AXIS_Y_MSB 0x09
106 #define ACCEL7_REG_AXIS_Z_LSB 0x0A
107 #define ACCEL7_REG_AXIS_Z_MSB 0x0B
108 #define ACCEL7_REG_DCST_RESP 0x0C
109 #define ACCEL7_REG_WHO_AM_I 0x0F
110 #define ACCEL7_REG_INT_SOURCE1 0x16
111 #define ACCEL7_REG_INT_SOURCE2 0x17
112 #define ACCEL7_REG_STATUS 0x18
113 #define ACCEL7_REG_INT_REL 0x1A
114 #define ACCEL7_REG_CTRL_REG1 0x1B
115 #define ACCEL7_REG_CTRL_REG2 0x1D
116 #define ACCEL7_REG_INT_CTRL_REG1 0x1E
117 #define ACCEL7_REG_INT_CTRL_REG2 0x1F
118 #define ACCEL7_REG_DATA_CTRL_REG 0x21
119 #define ACCEL7_REG_WAKEUP_COUNTER 0x29
120 #define ACCEL7_REG_NA_CAUNTER 0x2A
121 #define ACCEL7_REG_SELF_TEST 0x3A
122 #define ACCEL7_REG_WAKEUP_THRESHOLD_MSB 0x6A
123 #define ACCEL7_REG_WAKEUP_THRESHOLD_LSB 0x6B
124 
130 #define ACCEL7_CTRL_REG1_MODE_OPERATING 0x80
131 #define ACCEL7_CTRL_REG1_MODE_STANDBY 0x00
132 #define ACCEL7_CTRL_REG1_RES_LOW_CURRENT 0x00
133 #define ACCEL7_CTRL_REG1_RES_HIGH_RESOLUTION 0x40
134 #define ACCEL7_CTRL_REG1_DRDYE_ENABLE 0x20
135 #define ACCEL7_CTRL_REG1_DRDYE_DISABLE 0x00
136 #define ACCEL7_CTRL_REG1_RANGE_2g 0x00
137 #define ACCEL7_CTRL_REG1_RANGE_4g 0x08
138 #define ACCEL7_CTRL_REG1_RANGE_8g 0x10
139 #define ACCEL7_CTRL_REG1_RANGE_16g 0x04
140 #define ACCEL7_CTRL_REG1_RANGE_HIGH_RES_8g 0x18
141 #define ACCEL7_CTRL_REG1_RANGE_HIGH_RES_16g 0x1C
142 #define ACCEL7_CTRL_REG1_WAKEUP_DISABLE 0x00
143 #define ACCEL7_CTRL_REG1_WAKEUP_ENABLE 0x02
144 
150 #define ACCEL7_CTRL_REG2_START_RAM_REBOOT 0x80
151 #define ACCEL7_CTRL_REG2_DCST_ENABLE 0x10
152 #define ACCEL7_CTRL_REG2_DCST_DISABLE 0x00
153 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_0_781Hz 0x00
154 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_1_563Hz 0x01
155 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_3_125Hz 0x02
156 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_6_25Hz 0x03
157 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_12_5Hz 0x04
158 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_25Hz 0x05
159 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_50Hz 0x06
160 #define ACCEL7_CTRL_REG2_OUT_DATA_RATE_100Hz 0x07
161 
167 #define ACCEL7_INT_CTRL_REG1_IEN_DISABLE 0x00
168 #define ACCEL7_INT_CTRL_REG1_IEN_ENABLE 0x20
169 #define ACCEL7_INT_CTRL_REG1_IEA_DISABLE 0x00
170 #define ACCEL7_INT_CTRL_REG1_IEA_ENABLE 0x10
171 #define ACCEL7_INT_CTRL_REG1_IEL_DISABLE 0x00
172 #define ACCEL7_INT_CTRL_REG1_IEL_ENABLE 0x08
173 #define ACCEL7_INT_CTRL_REG1_SELF_TEST_POL_NEGATIVE 0x00
174 #define ACCEL7_INT_CTRL_REG1_SELF_TEST_POL_POSITIVE 0x02
175 
181 #define ACCEL7_INT_CTRL_REG2_ULMODE_ENABLE 0x80
182 #define ACCEL7_INT_CTRL_REG2_ULMODE_DISABLE 0x00
183 #define ACCEL7_INT_CTRL_REG2_X_NEGATIVE_ENABLE 0x20
184 #define ACCEL7_INT_CTRL_REG2_X_NEGATIVE_DISABLE 0x00
185 #define ACCEL7_INT_CTRL_REG2_Y_NEGATIVE_ENABLE 0x08
186 #define ACCEL7_INT_CTRL_REG2_Y_NEGATIVE_DISABLE 0x00
187 #define ACCEL7_INT_CTRL_REG2_Z_NEGATIVE_ENABLE 0x02
188 #define ACCEL7_INT_CTRL_REG2_Z_NEGATIVE_DISABLE 0x00
189 #define ACCEL7_INT_CTRL_REG2_X_POSITIVE_ENABLE 0x10
190 #define ACCEL7_INT_CTRL_REG2_X_POSITIVE_DISABLE 0x00
191 #define ACCEL7_INT_CTRL_REG2_Y_POSITIVE_ENABLE 0x04
192 #define ACCEL7_INT_CTRL_REG2_Y_POSITIVE_DISABLE 0x00
193 #define ACCEL7_INT_CTRL_REG2_Z_POSITIVE_ENABLE 0x01
194 #define ACCEL7_INT_CTRL_REG2_Z_POSITIVE_DISABLE 0x00
195 
201 #define ACCEL7_DATA_CTRL_REG_ODR_0_781Hz 0x08
202 #define ACCEL7_DATA_CTRL_REG_ODR_1_563Hz 0x09
203 #define ACCEL7_DATA_CTRL_REG_ODR_3_125Hz 0x0A
204 #define ACCEL7_DATA_CTRL_REG_ODR_6_25Hz 0x0B
205 #define ACCEL7_DATA_CTRL_REG_ODR_12_5Hz 0x00
206 #define ACCEL7_DATA_CTRL_REG_ODR_25Hz 0x01
207 #define ACCEL7_DATA_CTRL_REG_ODR_50Hz 0x02
208 #define ACCEL7_DATA_CTRL_REG_ODR_100Hz 0x03
209 #define ACCEL7_DATA_CTRL_REG_ODR_200Hz 0x04
210 #define ACCEL7_DATA_CTRL_REG_ODR_400Hz 0x05
211 #define ACCEL7_DATA_CTRL_REG_ODR_800Hz 0x06
212 #define ACCEL7_DATA_CTRL_REG_ODR_1600Hz 0x07
213 
219 #define ACCEL7_DEVICE_ADDRESS 0x0F
220  // End group macro
223 // --------------------------------------------------------------- PUBLIC TYPES
232 typedef struct
233 {
234  // Input pins
235 
236  digital_in_t int_pin;
237 
238  // Modules
239 
240  i2c_master_t i2c;
241 
242  // ctx variable
243 
244  uint8_t slave_address;
246 
247 } accel7_t;
248 
252 typedef struct
253 {
254  // Communication gpio pins
255 
256  pin_name_t scl;
257  pin_name_t sda;
258 
259  // Additional gpio pins
260 
261  pin_name_t int_pin;
262 
263  // static variable
264 
265  uint32_t i2c_speed;
266  uint8_t i2c_address;
268 
269 } accel7_cfg_t;
270  // End types group
272 
273 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
274 
280 #ifdef __cplusplus
281 extern "C"{
282 #endif
283 
293 
303 
313 void accel7_default_cfg ( accel7_t *ctx, uint8_t resolution, uint8_t range );
314 
325 void accel7_generic_write ( accel7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
326 
337 void accel7_generic_read ( accel7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
338 
348 void accel7_write_byte ( accel7_t *ctx, uint8_t reg, uint8_t input_data );
349 
358 uint8_t accel7_read_byte( accel7_t *ctx, uint8_t reg );
359 
368 int16_t accel7_get_axis ( accel7_t *ctx, uint8_t axis );
369 
380 void accel7_res_range_cfg ( accel7_t *ctx, uint8_t *resolution, uint8_t *range );
381 
390 
391 #ifdef __cplusplus
392 }
393 #endif
394 #endif // _ACCEL7_H_
395  // End public_function group
398 
399 // ------------------------------------------------------------------------- END
accel7_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel7.h:265
accel7_cfg_setup
void accel7_cfg_setup(accel7_cfg_t *cfg)
Config Object Initialization function.
accel7_cfg_t
Click configuration structure definition.
Definition: accel7.h:253
accel7_get_interrupt_state
uint8_t accel7_get_interrupt_state(accel7_t *ctx)
Get interrupt state function.
accel7_generic_read
void accel7_generic_read(accel7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
accel7_cfg_t::sda
pin_name_t sda
Definition: accel7.h:257
accel7_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel7.h:266
accel7_init
ACCEL7_RETVAL accel7_init(accel7_t *ctx, accel7_cfg_t *cfg)
Initialization function.
ACCEL7_RETVAL
#define ACCEL7_RETVAL
Definition: accel7.h:76
accel7_t::slave_address
uint8_t slave_address
Definition: accel7.h:244
accel7_cfg_t::dev_data_resolution
uint8_t dev_data_resolution
Definition: accel7.h:267
accel7_res_range_cfg
void accel7_res_range_cfg(accel7_t *ctx, uint8_t *resolution, uint8_t *range)
Resolution/range configuration function.
accel7_write_byte
void accel7_write_byte(accel7_t *ctx, uint8_t reg, uint8_t input_data)
Write byte function.
accel7_read_byte
uint8_t accel7_read_byte(accel7_t *ctx, uint8_t reg)
Read byte function.
accel7_generic_write
void accel7_generic_write(accel7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
accel7_t::i2c
i2c_master_t i2c
Definition: accel7.h:240
accel7_cfg_t::scl
pin_name_t scl
Definition: accel7.h:256
accel7_t
Click ctx object definition.
Definition: accel7.h:233
accel7_t::data_resolution
uint8_t data_resolution
Definition: accel7.h:245
accel7_get_axis
int16_t accel7_get_axis(accel7_t *ctx, uint8_t axis)
Get axi function.
accel7_t::int_pin
digital_in_t int_pin
Definition: accel7.h:236
accel7_cfg_t::int_pin
pin_name_t int_pin
Definition: accel7.h:261
accel7_default_cfg
void accel7_default_cfg(accel7_t *ctx, uint8_t resolution, uint8_t range)
Click Default Configuration function.