accel11  2.0.0.0
accel11.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 ACCEL11_H
36 #define ACCEL11_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 ACCEL11_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.it1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
62  cfg.it2 = MIKROBUS( mikrobus, MIKROBUS_INT );
63 
69 #define ACCEL11_MASTER_I2C 0
70 #define ACCEL11_MASTER_SPI 1
71 
77 #define ACCEL11_RETVAL uint8_t
78 
79 #define ACCEL11_OK 0x00
80 #define ACCEL11_INIT_ERROR 0xFF
81 
87 #define ACCEL11_CHIP_ID 0x16
88 
94 #define ACCEL11_REG_CHIP_ID 0x00
95 #define ACCEL11_REG_ERROR 0x02
96 #define ACCEL11_REG_STATUS 0x03
97 #define ACCEL11_REG_AUX_X_LSB 0x0A
98 #define ACCEL11_REG_AUX_X_MSB 0x0B
99 #define ACCEL11_REG_AUX_Y_LSB 0x0C
100 #define ACCEL11_REG_AUX_Y_MSB 0x0D
101 #define ACCEL11_REG_AUX_Z_LSB 0x0E
102 #define ACCEL11_REG_AUX_Z_MSB 0x0F
103 #define ACCEL11_REG_AUX_R_LSB 0x10
104 #define ACCEL11_REG_AUX_R_MSB 0x11
105 #define ACCEL11_REG_ACCEL_X_LSB 0x12
106 #define ACCEL11_REG_ACCEL_X_MSB 0x13
107 #define ACCEL11_REG_ACCEL_Y_LSB 0x14
108 #define ACCEL11_REG_ACCEL_Y_MSB 0x15
109 #define ACCEL11_REG_ACCEL_Z_LSB 0x16
110 #define ACCEL11_REG_ACCEL_Z_MSB 0x17
111 #define ACCEL11_REG_SENSOR_TIME_0 0x18
112 #define ACCEL11_REG_SENSOR_TIME_1 0x19
113 #define ACCEL11_REG_SENSOR_TIME_2 0x1A
114 #define ACCEL11_REG_EVENT 0x1B
115 #define ACCEL11_REG_STATUS_INT_0 0x1C
116 #define ACCEL11_REG_STATUS_INT_1 0x1D
117 #define ACCEL11_REG_STEP_COUNTER_0 0x1E
118 #define ACCEL11_REG_STEP_COUNTER_1 0x1F
119 #define ACCEL11_REG_STEP_COUNTER_2 0x20
120 #define ACCEL11_REG_STEP_COUNTER_3 0x21
121 #define ACCEL11_REG_TEMPERATURE 0x22
122 #define ACCEL11_REG_FIFO_LENGTH_LSB 0x24
123 #define ACCEL11_REG_FIFO_LENGTH_MSB 0x25
124 #define ACCEL11_REG_FIFO_DATA 0x26
125 #define ACCEL11_REG_ACTIVITY_TYPE 0x27
126 #define ACCEL11_REG_INTERNAL_STATUS 0x2A
127 #define ACCEL11_REG_ACCEL_CONFIG 0x40
128 #define ACCEL11_REG_ACCEL_RANGE 0x41
129 #define ACCEL11_REG_AUX_CONFIG 0x44
130 #define ACCEL11_REG_FIFO_DOWNS 0x45
131 #define ACCEL11_REG_FIFO_WTM_LSB 0x46
132 #define ACCEL11_REG_FIFO_WTM_MSB 0x47
133 #define ACCEL11_REG_FIFO_CONFIG_0 0x48
134 #define ACCEL11_REG_FIFO_CONFIG_1 0x49
135 #define ACCEL11_AUX_DEV_ID 0x4B
136 #define ACCEL11_REG_AUX_IF_CONFIG 0x4C
137 #define ACCEL11_REG_AUX_RD_ADDRESS 0x4D
138 #define ACCEL11_REG_AUX_WR_ADDRESS 0x4E
139 #define ACCEL11_REG_AUX_WR_DATA 0x4F
140 #define ACCEL11_REG_INT1_IO_CTRL 0x53
141 #define ACCEL11_REG_INT2_IO_CTRL 0x54
142 #define ACCEL11_REG_INT_LATCH 0x55
143 #define ACCEL11_REG_INT1_MAP 0x56
144 #define ACCEL11_REG_INT2_MAP 0x57
145 #define ACCEL11_REG_INT_MAP_DATA 0x58
146 #define ACCEL11_REG_INT_CTRL 0x59
147 #define ACCEL11_REG_FEATURES_IN 0x5E
148 #define ACCEL11_REG_INTERNAL_ERROR 0x5F
149 #define ACCEL11_REG_NVM_CONF 0x6A
150 #define ACCEL11_REG_SPI_IF_CONF 0x6B
151 #define ACCEL11_REG_ACCEL_SELF_TEST 0x6D
152 #define ACCEL11_REG_NV_CONF 0x70
153 #define ACCEL11_REG_OFFSET_X_AXIS 0x71
154 #define ACCEL11_REG_OFFSET_Y_AXIS 0x72
155 #define ACCEL11_REG_OFFSET_Z_AXIS 0x73
156 #define ACCEL11_REG_POWER_CONFIG 0x7C
157 #define ACCEL11_REG_POWER_CTRL 0x7D
158 #define ACCEL11_REG_COMMAND 0x7E
159 
165 #define ACCEL11_ERR_AUX 0x80
166 #define ACCEL11_ERR_FIFO 0x80
167 #define ACCEL11_ERR_ACCEL 0x04
168 #define ACCEL11_ERR_CMD 0x02
169 #define ACCEL11_ERR_FATAL 0x01
170 
176 #define ACCEL11_STATUS_DRDY_ACCEL 0x80
177 #define ACCEL11_STATUS_DRDY_AUX 0x20
178 #define ACCEL11_STATUS_RDY_CMD 0x10
179 #define ACCEL11_STATUS_AUX_MAN_OP 0x04
180 
186 #define ACCEL11_EVENT_POWER_UP 0x00
187 #define ACCEL11_EVENT_SOFTWARE_RESET 0x01
188 
194 #define ACCEL11_INTS0_ERR_INT_OUT 0x80
195 #define ACCEL11_INTS0_NO_MOTION_OUT 0x40
196 #define ACCEL11_INTS0_WAKEUP_OUT 0x20
197 #define ACCEL11_INTS0_WRIST_TILT_OUT 0x08
198 #define ACCEL11_INTS0_ACTIVITY_TYPE_OUT 0x04
199 #define ACCEL11_INTS0_STEP_COUNTER_OUT 0x02
200 
206 #define ACCEL11_INTS1_ACCEL_DRDY_INT 0x80
207 #define ACCEL11_INTS1_AUX_DRDY_INT 0x20
208 #define ACCEL11_INTS1_FWM_INT 0x02
209 #define ACCEL11_INTS1_FFULL_INT 0x01
210 
216 #define ACCEL11_ATYPE_USER_NOT_MOVING 0x00
217 #define ACCEL11_ATYPE_USER_WALKING 0x01
218 #define ACCEL11_ATYPE_USER_RUNNING 0x02
219 
225 #define ACCEL11_INTERS_ODR_HIGH_ERR 0x80
226 #define ACCEL11_INTERS_ODR_50HZ_ERR 0x40
227 #define ACCEL11_INTERS_AXES_REMAP_ERR 0x20
228 #define ACCEL11_INTERS_NOT_INIT 0x00
229 #define ACCEL11_INTERS_INIT_OK 0x01
230 #define ACCEL11_INTERS_INIT_ERROR 0x02
231 #define ACCEL11_INTERS_INVALID_DRIVER 0x03
232 #define ACCEL11_INTERS_SENSOR_STOPPED 0x04
233 
239 #define ACCEL11_ACFG_AVERAGING_MODE 0x00
240 #define ACCEL11_ACFG_CONTINUOUS_MODE 0x80
241 #define ACCEL11_ACFG_BWP_NO_AVERAGING 0x00
242 #define ACCEL11_ACFG_BWP_AVERAGING_2_SAMPLES 0x10
243 #define ACCEL11_ACFG_BWP_AVERAGING_4_SAMPLES 0x20
244 #define ACCEL11_ACFG_BWP_AVERAGING_8_SAMPLES 0x30
245 #define ACCEL11_ACFG_BWP_AVERAGING_16_SAMPLES 0x40
246 #define ACCEL11_ACFG_BWP_AVERAGING_32_SAMPLES 0x50
247 #define ACCEL11_ACFG_BWP_AVERAGING_64_SAMPLES 0x60
248 #define ACCEL11_ACFG_BWP_AVERAGING_128_SAMPLES 0x70
249 
255 #define ACCEL11_RANGE_2g 0x00
256 #define ACCEL11_RANGE_4g 0x01
257 #define ACCEL11_RANGE_8g 0x02
258 #define ACCEL11_RANGE_16g 0x03
259 
265 #define ACCEL11_CFG_ODR_0p78Hz 0x01
266 #define ACCEL11_CFG_ODR_1p5Hz 0x02
267 #define ACCEL11_CFG_ODR_3p1Hz 0x03
268 #define ACCEL11_CFG_ODR_6p25Hz 0x04
269 #define ACCEL11_CFG_ODR_12p5Hz 0x05
270 #define ACCEL11_CFG_ODR_25Hz 0x06
271 #define ACCEL11_CFG_ODR_50Hz 0x07
272 #define ACCEL11_CFG_ODR_100Hz 0x08
273 #define ACCEL11_CFG_ODR_200Hz 0x09
274 #define ACCEL11_CFG_ODR_400Hz 0x0A
275 #define ACCEL11_CFG_ODR_800Hz 0x0B
276 #define ACCEL11_CFG_ODR_1k6Hz 0x0C
277 #define ACCEL11_CFG_ODR_3k2Hz 0x0D
278 #define ACCEL11_CFG_ODR_6k4Hz 0x0E
279 #define ACCEL11_CFG_ODR_12k8Hz 0x0F
280 
286 #define ACCEL11_FCFG0_FIFO_TIME_ENABLE 0x02
287 #define ACCEL11_FCFG0_FIFO_TIME_DISABLE 0x00
288 #define ACCEL11_FCFG0_FIFO_STOP_ON_FULL_ENABLE 0x01
289 #define ACCEL11_FCFG0_FIFO_STOP_ON_FULL_DISABLE 0x00
290 
296 #define ACCEL11_FCGF1_FIFO_ACCEL_ENABLE 0x40
297 #define ACCEL11_FCGF1_FIFO_ACCEL_DISABLE 0x00
298 #define ACCEL11_FCGF1_FIFO_AUX_ENABLE 0x20
299 #define ACCEL11_FCGF1_FIFO_AUX_DISABLE 0x00
300 #define ACCEL11_FCGF1_FIFO_HEADER_ENABLE 0x10
301 #define ACCEL11_FCGF1_FIFO_HEADER_DISABLE 0x00
302 #define ACCEL11_FCGF1_FIFO_TAG_INT1_ENABLE 0x08
303 #define ACCEL11_FCGF1_FIFO_TAG_INT1_DISABLE 0x00
304 #define ACCEL11_FCGF1_FIFO_TAG_INT2_ENABLE 0x04
305 #define ACCEL11_FCGF1_FIFO_TAG_INT2_DISABLE 0x00
306 
312 #define ACCEL11_IFCFG_DATA_MODE 0x00
313 #define ACCEL11_IFCFG_SETUP_MODE 0x80
314 #define ACCEL11_IFCFG_BURST_LENGTH_1 0x00
315 #define ACCEL11_IFCFG_BURST_LENGTH_2 0x01
316 #define ACCEL11_IFCFG_BURST_LENGTH_6 0x02
317 #define ACCEL11_IFCFG_BURST_LENGTH_8 0x03
318 
324 #define ACCEL11_INTIO_INPUT_ENABLE 0x10
325 #define ACCEL11_INTIO_INPUT_DISABLE 0x00
326 #define ACCEL11_INTIO_OUTPUT_ENABLE 0x08
327 #define ACCEL11_INTIO_OUTPUT_DISABLE 0x00
328 #define ACCEL11_INTIO_PUSH_PULL 0x00
329 #define ACCEL11_INTIO_OPEN_DRAIN 0x04
330 #define ACCEL11_INTIO_LVL_ACTIVE_LOW 0x00
331 #define ACCEL11_INTIO_LVL_ACTIVE_HIGH 0x02
332 #define ACCEL11_INTIO_TRIGGER_LEVEL 0x00
333 #define ACCEL11_INTIO_TRIGGER_EDGE 0x01
334 
340 #define ACCEL11_INTLAT_NON_LATCHED 0x00
341 #define ACCEL11_INTLAT_LATCHED 0x01
342 
348 #define ACCEL11_IMAPD_INT1_FFULL 0x01
349 #define ACCEL11_IMAPD_INT1_FWM 0x03
350 #define ACCEL11_IMAPD_INT1_DRDY 0x04
351 #define ACCEL11_IMAPD_INT2_FFULL 0x10
352 #define ACCEL11_IMAPD_INT2_FWM 0x20
353 #define ACCEL11_IMAPD_INT2_DRDY 0x40
354 
360 #define ACCEL11_SPI_4_WIRE 0x00
361 #define ACCEL11_SPI_3_WIRE 0x01
362 #define ACCEL11_AUX_INTERFACE_OFF 0x00
363 #define ACCEL11_AUX_INTERFACE_MAG 0x10
364 
370 #define ACCEL_NV_ACCEL_OFFSET_ENABLE 0x08
371 #define ACCEL_NV_ACCEL_OFFSET_DISABLE 0x00
372 #define ACCEL_NV_I2C_WATCHDOG_ENABLE 0x04
373 #define ACCEL_NV_I2C_WATCHDOG_DISABLE 0x00
374 #define ACCEL_NV_I2C_WDT_TIMEOUT_SHORT 0x00
375 #define ACCEL_NV_I2C_WDT_TIMEOUT_LONG 0x02
376 #define ACCEL_NV_I2C_INTERFACE_ENABLE 0x00
377 #define ACCEL_NV_I2C_INTERFACE_DISABLE 0x01
378 
384 #define ACCEL11_PWRCFG_APS_ON 0x01
385 #define ACCEL11_PWRCFG_APS_OFF 0x00
386 #define ACCEL11_PWRCFG_FSW_ON 0x02
387 #define ACCEL11_PWRCFG_FSW_OFF 0x00
388 
394 #define ACCEL11_PWRCTRL_ACCEL_ENABLE 0x04
395 #define ACCEL11_PWRCTRL_ACCEL_DISABLE 0x00
396 
402 #define ACCEL11_CMD_NVM_PROG 0xA0
403 #define ACCEL11_CMD_FIFO_FLUSH 0xB0
404 #define ACCEL11_CMD_SW_RESET 0xB6
405 
411 #define ACCEL11_ACCEL_X_AXIS 0x12
412 #define ACCEL11_ACCEL_Y_AXIS 0x14
413 #define ACCEL11_ACCEL_Z_AXIS 0x16
414 
420 #define ACCEL11_SLAVE_ADDRESS_GND 0x18
421 #define ACCEL11_SLAVE_ADDRESS_VCC 0x19
422 
428 #define ACCEL11_TEST_COMUNICATION_ERROR 0x01
429 #define ACCEL11_TEST_COMUNICATION_OK 0x00
430  // End group macro
433 // --------------------------------------------------------------- PUBLIC TYPES
442 typedef uint8_t accel11_select_t;
443 
447 typedef void ( *accel11_master_io_t )( struct accel11_s*, uint8_t, uint8_t*, uint8_t );
448 
452 typedef struct accel11_s
453 {
454  // Input pins
455 
456  digital_in_t it1;
457  digital_in_t it2;
458 
459  // Modules
460 
461  i2c_master_t i2c;
462  spi_master_t spi;
463 
464  // ctx variable
465 
466  hal_i2c_address_t slave_address;
470 
471 } accel11_t;
472 
476 typedef struct
477 {
478  // Communication gpio pins
479 
480  pin_name_t scl;
481  pin_name_t sda;
482  pin_name_t miso;
483  pin_name_t mosi;
484  pin_name_t sck;
485  pin_name_t cs;
486 
487  // Additional gpio pins
488 
489  pin_name_t it1;
490  pin_name_t it2;
491 
492  // static variable
493 
494  hal_i2c_speed_t i2c_speed;
495  hal_i2c_address_t i2c_address;
496  hal_spi_speed_t spi_speed;
497  hal_spi_mode_t spi_mode;
498 
500 
501 } accel11_cfg_t;
502  // End types group
504 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
510 #ifdef __cplusplus
511 extern "C"{
512 #endif
513 
522 void accel11_cfg_setup ( accel11_cfg_t *cfg );
523 
532 
540 void accel11_default_cfg ( accel11_t *ctx );
541 
552 void accel11_generic_write ( accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
553 
564 void accel11_generic_read ( accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
565 
575 void accel11_write_byte ( accel11_t *ctx, uint8_t addr, uint8_t data_in );
576 
585 uint8_t accel11_read_byte ( accel11_t *ctx, uint8_t addr );
586 
594 uint8_t accel11_test_comunication ( accel11_t *ctx );
595 
603 void accel11_software_reset ( accel11_t *ctx );
604 
613 int16_t accel11_get_axis_data ( accel11_t *ctx, uint8_t axis );
614 
623 
631 int8_t accel11_get_temperature ( accel11_t *ctx );
632 
633 #ifdef __cplusplus
634 }
635 #endif
636 #endif // _ACCEL11_H_
637  // End public_function group
640 
641 // ------------------------------------------------------------------------- END
void accel11_default_cfg(accel11_t *ctx)
Click Default Configuration function.
uint8_t accel11_read_byte(accel11_t *ctx, uint8_t addr)
Functions for read byte from register.
pin_name_t mosi
Definition: accel11.h:483
accel11_select_t sel
Definition: accel11.h:499
accel11_select_t master_sel
Definition: accel11.h:469
Click configuration structure definition.
Definition: accel11.h:476
int8_t accel11_get_temperature(accel11_t *ctx)
Function get temperature.
void accel11_power_on_procedure(accel11_t *ctx)
Function for power on chip.
accel11_master_io_t write_f
Definition: accel11.h:467
hal_i2c_address_t slave_address
Definition: accel11.h:466
hal_i2c_address_t i2c_address
Definition: accel11.h:495
void accel11_cfg_setup(accel11_cfg_t *cfg)
Config Object Initialization function.
accel11_master_io_t read_f
Definition: accel11.h:468
void accel11_generic_read(accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
pin_name_t it1
Definition: accel11.h:489
uint8_t accel11_test_comunication(accel11_t *ctx)
Test comunication function.
hal_spi_mode_t spi_mode
Definition: accel11.h:497
pin_name_t miso
Definition: accel11.h:482
hal_spi_speed_t spi_speed
Definition: accel11.h:496
i2c_master_t i2c
Definition: accel11.h:461
void accel11_write_byte(accel11_t *ctx, uint8_t addr, uint8_t data_in)
Functions for write one byte in register.
pin_name_t sda
Definition: accel11.h:481
digital_in_t it2
Definition: accel11.h:457
void accel11_generic_write(accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
hal_i2c_speed_t i2c_speed
Definition: accel11.h:494
spi_master_t spi
Definition: accel11.h:462
void accel11_software_reset(accel11_t *ctx)
Software reset function.
void(* accel11_master_io_t)(struct accel11_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel11.h:447
digital_in_t it1
Definition: accel11.h:456
pin_name_t sck
Definition: accel11.h:484
uint8_t accel11_select_t
Communication type.
Definition: accel11.h:442
int16_t accel11_get_axis_data(accel11_t *ctx, uint8_t axis)
Accel Axis data function.
pin_name_t cs
Definition: accel11.h:485
pin_name_t it2
Definition: accel11.h:490
pin_name_t scl
Definition: accel11.h:480
struct accel11_s accel11_t
Click ctx object definition.
ACCEL11_RETVAL accel11_init(accel11_t *ctx, accel11_cfg_t *cfg)
Initialization function.
#define ACCEL11_RETVAL
Definition: accel11.h:77
Click ctx object definition.
Definition: accel11.h:452