accel11  2.0.0.0
accel11.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 ACCEL11_H
36 #define ACCEL11_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 ACCEL11_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.it1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
72  cfg.it2 = MIKROBUS( mikrobus, MIKROBUS_INT );
73 
79 #define ACCEL11_MASTER_I2C 0
80 #define ACCEL11_MASTER_SPI 1
81 
87 #define ACCEL11_RETVAL uint8_t
88 
89 #define ACCEL11_OK 0x00
90 #define ACCEL11_INIT_ERROR 0xFF
91 
97 #define ACCEL11_CHIP_ID 0x16
98 
104 #define ACCEL11_REG_CHIP_ID 0x00
105 #define ACCEL11_REG_ERROR 0x02
106 #define ACCEL11_REG_STATUS 0x03
107 #define ACCEL11_REG_AUX_X_LSB 0x0A
108 #define ACCEL11_REG_AUX_X_MSB 0x0B
109 #define ACCEL11_REG_AUX_Y_LSB 0x0C
110 #define ACCEL11_REG_AUX_Y_MSB 0x0D
111 #define ACCEL11_REG_AUX_Z_LSB 0x0E
112 #define ACCEL11_REG_AUX_Z_MSB 0x0F
113 #define ACCEL11_REG_AUX_R_LSB 0x10
114 #define ACCEL11_REG_AUX_R_MSB 0x11
115 #define ACCEL11_REG_ACCEL_X_LSB 0x12
116 #define ACCEL11_REG_ACCEL_X_MSB 0x13
117 #define ACCEL11_REG_ACCEL_Y_LSB 0x14
118 #define ACCEL11_REG_ACCEL_Y_MSB 0x15
119 #define ACCEL11_REG_ACCEL_Z_LSB 0x16
120 #define ACCEL11_REG_ACCEL_Z_MSB 0x17
121 #define ACCEL11_REG_SENSOR_TIME_0 0x18
122 #define ACCEL11_REG_SENSOR_TIME_1 0x19
123 #define ACCEL11_REG_SENSOR_TIME_2 0x1A
124 #define ACCEL11_REG_EVENT 0x1B
125 #define ACCEL11_REG_STATUS_INT_0 0x1C
126 #define ACCEL11_REG_STATUS_INT_1 0x1D
127 #define ACCEL11_REG_STEP_COUNTER_0 0x1E
128 #define ACCEL11_REG_STEP_COUNTER_1 0x1F
129 #define ACCEL11_REG_STEP_COUNTER_2 0x20
130 #define ACCEL11_REG_STEP_COUNTER_3 0x21
131 #define ACCEL11_REG_TEMPERATURE 0x22
132 #define ACCEL11_REG_FIFO_LENGTH_LSB 0x24
133 #define ACCEL11_REG_FIFO_LENGTH_MSB 0x25
134 #define ACCEL11_REG_FIFO_DATA 0x26
135 #define ACCEL11_REG_ACTIVITY_TYPE 0x27
136 #define ACCEL11_REG_INTERNAL_STATUS 0x2A
137 #define ACCEL11_REG_ACCEL_CONFIG 0x40
138 #define ACCEL11_REG_ACCEL_RANGE 0x41
139 #define ACCEL11_REG_AUX_CONFIG 0x44
140 #define ACCEL11_REG_FIFO_DOWNS 0x45
141 #define ACCEL11_REG_FIFO_WTM_LSB 0x46
142 #define ACCEL11_REG_FIFO_WTM_MSB 0x47
143 #define ACCEL11_REG_FIFO_CONFIG_0 0x48
144 #define ACCEL11_REG_FIFO_CONFIG_1 0x49
145 #define ACCEL11_AUX_DEV_ID 0x4B
146 #define ACCEL11_REG_AUX_IF_CONFIG 0x4C
147 #define ACCEL11_REG_AUX_RD_ADDRESS 0x4D
148 #define ACCEL11_REG_AUX_WR_ADDRESS 0x4E
149 #define ACCEL11_REG_AUX_WR_DATA 0x4F
150 #define ACCEL11_REG_INT1_IO_CTRL 0x53
151 #define ACCEL11_REG_INT2_IO_CTRL 0x54
152 #define ACCEL11_REG_INT_LATCH 0x55
153 #define ACCEL11_REG_INT1_MAP 0x56
154 #define ACCEL11_REG_INT2_MAP 0x57
155 #define ACCEL11_REG_INT_MAP_DATA 0x58
156 #define ACCEL11_REG_INT_CTRL 0x59
157 #define ACCEL11_REG_FEATURES_IN 0x5E
158 #define ACCEL11_REG_INTERNAL_ERROR 0x5F
159 #define ACCEL11_REG_NVM_CONF 0x6A
160 #define ACCEL11_REG_SPI_IF_CONF 0x6B
161 #define ACCEL11_REG_ACCEL_SELF_TEST 0x6D
162 #define ACCEL11_REG_NV_CONF 0x70
163 #define ACCEL11_REG_OFFSET_X_AXIS 0x71
164 #define ACCEL11_REG_OFFSET_Y_AXIS 0x72
165 #define ACCEL11_REG_OFFSET_Z_AXIS 0x73
166 #define ACCEL11_REG_POWER_CONFIG 0x7C
167 #define ACCEL11_REG_POWER_CTRL 0x7D
168 #define ACCEL11_REG_COMMAND 0x7E
169 
175 #define ACCEL11_ERR_AUX 0x80
176 #define ACCEL11_ERR_FIFO 0x80
177 #define ACCEL11_ERR_ACCEL 0x04
178 #define ACCEL11_ERR_CMD 0x02
179 #define ACCEL11_ERR_FATAL 0x01
180 
186 #define ACCEL11_STATUS_DRDY_ACCEL 0x80
187 #define ACCEL11_STATUS_DRDY_AUX 0x20
188 #define ACCEL11_STATUS_RDY_CMD 0x10
189 #define ACCEL11_STATUS_AUX_MAN_OP 0x04
190 
196 #define ACCEL11_EVENT_POWER_UP 0x00
197 #define ACCEL11_EVENT_SOFTWARE_RESET 0x01
198 
204 #define ACCEL11_INTS0_ERR_INT_OUT 0x80
205 #define ACCEL11_INTS0_NO_MOTION_OUT 0x40
206 #define ACCEL11_INTS0_WAKEUP_OUT 0x20
207 #define ACCEL11_INTS0_WRIST_TILT_OUT 0x08
208 #define ACCEL11_INTS0_ACTIVITY_TYPE_OUT 0x04
209 #define ACCEL11_INTS0_STEP_COUNTER_OUT 0x02
210 
216 #define ACCEL11_INTS1_ACCEL_DRDY_INT 0x80
217 #define ACCEL11_INTS1_AUX_DRDY_INT 0x20
218 #define ACCEL11_INTS1_FWM_INT 0x02
219 #define ACCEL11_INTS1_FFULL_INT 0x01
220 
226 #define ACCEL11_ATYPE_USER_NOT_MOVING 0x00
227 #define ACCEL11_ATYPE_USER_WALKING 0x01
228 #define ACCEL11_ATYPE_USER_RUNNING 0x02
229 
235 #define ACCEL11_INTERS_ODR_HIGH_ERR 0x80
236 #define ACCEL11_INTERS_ODR_50HZ_ERR 0x40
237 #define ACCEL11_INTERS_AXES_REMAP_ERR 0x20
238 #define ACCEL11_INTERS_NOT_INIT 0x00
239 #define ACCEL11_INTERS_INIT_OK 0x01
240 #define ACCEL11_INTERS_INIT_ERROR 0x02
241 #define ACCEL11_INTERS_INVALID_DRIVER 0x03
242 #define ACCEL11_INTERS_SENSOR_STOPPED 0x04
243 
249 #define ACCEL11_ACFG_AVERAGING_MODE 0x00
250 #define ACCEL11_ACFG_CONTINUOUS_MODE 0x80
251 #define ACCEL11_ACFG_BWP_NO_AVERAGING 0x00
252 #define ACCEL11_ACFG_BWP_AVERAGING_2_SAMPLES 0x10
253 #define ACCEL11_ACFG_BWP_AVERAGING_4_SAMPLES 0x20
254 #define ACCEL11_ACFG_BWP_AVERAGING_8_SAMPLES 0x30
255 #define ACCEL11_ACFG_BWP_AVERAGING_16_SAMPLES 0x40
256 #define ACCEL11_ACFG_BWP_AVERAGING_32_SAMPLES 0x50
257 #define ACCEL11_ACFG_BWP_AVERAGING_64_SAMPLES 0x60
258 #define ACCEL11_ACFG_BWP_AVERAGING_128_SAMPLES 0x70
259 
265 #define ACCEL11_RANGE_2g 0x00
266 #define ACCEL11_RANGE_4g 0x01
267 #define ACCEL11_RANGE_8g 0x02
268 #define ACCEL11_RANGE_16g 0x03
269 
275 #define ACCEL11_CFG_ODR_0p78Hz 0x01
276 #define ACCEL11_CFG_ODR_1p5Hz 0x02
277 #define ACCEL11_CFG_ODR_3p1Hz 0x03
278 #define ACCEL11_CFG_ODR_6p25Hz 0x04
279 #define ACCEL11_CFG_ODR_12p5Hz 0x05
280 #define ACCEL11_CFG_ODR_25Hz 0x06
281 #define ACCEL11_CFG_ODR_50Hz 0x07
282 #define ACCEL11_CFG_ODR_100Hz 0x08
283 #define ACCEL11_CFG_ODR_200Hz 0x09
284 #define ACCEL11_CFG_ODR_400Hz 0x0A
285 #define ACCEL11_CFG_ODR_800Hz 0x0B
286 #define ACCEL11_CFG_ODR_1k6Hz 0x0C
287 #define ACCEL11_CFG_ODR_3k2Hz 0x0D
288 #define ACCEL11_CFG_ODR_6k4Hz 0x0E
289 #define ACCEL11_CFG_ODR_12k8Hz 0x0F
290 
296 #define ACCEL11_FCFG0_FIFO_TIME_ENABLE 0x02
297 #define ACCEL11_FCFG0_FIFO_TIME_DISABLE 0x00
298 #define ACCEL11_FCFG0_FIFO_STOP_ON_FULL_ENABLE 0x01
299 #define ACCEL11_FCFG0_FIFO_STOP_ON_FULL_DISABLE 0x00
300 
306 #define ACCEL11_FCGF1_FIFO_ACCEL_ENABLE 0x40
307 #define ACCEL11_FCGF1_FIFO_ACCEL_DISABLE 0x00
308 #define ACCEL11_FCGF1_FIFO_AUX_ENABLE 0x20
309 #define ACCEL11_FCGF1_FIFO_AUX_DISABLE 0x00
310 #define ACCEL11_FCGF1_FIFO_HEADER_ENABLE 0x10
311 #define ACCEL11_FCGF1_FIFO_HEADER_DISABLE 0x00
312 #define ACCEL11_FCGF1_FIFO_TAG_INT1_ENABLE 0x08
313 #define ACCEL11_FCGF1_FIFO_TAG_INT1_DISABLE 0x00
314 #define ACCEL11_FCGF1_FIFO_TAG_INT2_ENABLE 0x04
315 #define ACCEL11_FCGF1_FIFO_TAG_INT2_DISABLE 0x00
316 
322 #define ACCEL11_IFCFG_DATA_MODE 0x00
323 #define ACCEL11_IFCFG_SETUP_MODE 0x80
324 #define ACCEL11_IFCFG_BURST_LENGTH_1 0x00
325 #define ACCEL11_IFCFG_BURST_LENGTH_2 0x01
326 #define ACCEL11_IFCFG_BURST_LENGTH_6 0x02
327 #define ACCEL11_IFCFG_BURST_LENGTH_8 0x03
328 
334 #define ACCEL11_INTIO_INPUT_ENABLE 0x10
335 #define ACCEL11_INTIO_INPUT_DISABLE 0x00
336 #define ACCEL11_INTIO_OUTPUT_ENABLE 0x08
337 #define ACCEL11_INTIO_OUTPUT_DISABLE 0x00
338 #define ACCEL11_INTIO_PUSH_PULL 0x00
339 #define ACCEL11_INTIO_OPEN_DRAIN 0x04
340 #define ACCEL11_INTIO_LVL_ACTIVE_LOW 0x00
341 #define ACCEL11_INTIO_LVL_ACTIVE_HIGH 0x02
342 #define ACCEL11_INTIO_TRIGGER_LEVEL 0x00
343 #define ACCEL11_INTIO_TRIGGER_EDGE 0x01
344 
350 #define ACCEL11_INTLAT_NON_LATCHED 0x00
351 #define ACCEL11_INTLAT_LATCHED 0x01
352 
358 #define ACCEL11_IMAPD_INT1_FFULL 0x01
359 #define ACCEL11_IMAPD_INT1_FWM 0x03
360 #define ACCEL11_IMAPD_INT1_DRDY 0x04
361 #define ACCEL11_IMAPD_INT2_FFULL 0x10
362 #define ACCEL11_IMAPD_INT2_FWM 0x20
363 #define ACCEL11_IMAPD_INT2_DRDY 0x40
364 
370 #define ACCEL11_SPI_4_WIRE 0x00
371 #define ACCEL11_SPI_3_WIRE 0x01
372 #define ACCEL11_AUX_INTERFACE_OFF 0x00
373 #define ACCEL11_AUX_INTERFACE_MAG 0x10
374 
380 #define ACCEL_NV_ACCEL_OFFSET_ENABLE 0x08
381 #define ACCEL_NV_ACCEL_OFFSET_DISABLE 0x00
382 #define ACCEL_NV_I2C_WATCHDOG_ENABLE 0x04
383 #define ACCEL_NV_I2C_WATCHDOG_DISABLE 0x00
384 #define ACCEL_NV_I2C_WDT_TIMEOUT_SHORT 0x00
385 #define ACCEL_NV_I2C_WDT_TIMEOUT_LONG 0x02
386 #define ACCEL_NV_I2C_INTERFACE_ENABLE 0x00
387 #define ACCEL_NV_I2C_INTERFACE_DISABLE 0x01
388 
394 #define ACCEL11_PWRCFG_APS_ON 0x01
395 #define ACCEL11_PWRCFG_APS_OFF 0x00
396 #define ACCEL11_PWRCFG_FSW_ON 0x02
397 #define ACCEL11_PWRCFG_FSW_OFF 0x00
398 
404 #define ACCEL11_PWRCTRL_ACCEL_ENABLE 0x04
405 #define ACCEL11_PWRCTRL_ACCEL_DISABLE 0x00
406 
412 #define ACCEL11_CMD_NVM_PROG 0xA0
413 #define ACCEL11_CMD_FIFO_FLUSH 0xB0
414 #define ACCEL11_CMD_SW_RESET 0xB6
415 
421 #define ACCEL11_ACCEL_X_AXIS 0x12
422 #define ACCEL11_ACCEL_Y_AXIS 0x14
423 #define ACCEL11_ACCEL_Z_AXIS 0x16
424 
430 #define ACCEL11_SLAVE_ADDRESS_GND 0x18
431 #define ACCEL11_SLAVE_ADDRESS_VCC 0x19
432 
438 #define ACCEL11_TEST_COMUNICATION_ERROR 0x01
439 #define ACCEL11_TEST_COMUNICATION_OK 0x00
440  // End group macro
443 // --------------------------------------------------------------- PUBLIC TYPES
452 typedef uint8_t accel11_select_t;
453 
457 typedef void ( *accel11_master_io_t )( struct accel11_s*, uint8_t, uint8_t*, uint8_t );
458 
462 typedef struct accel11_s
463 {
464  // Output pins
465  digital_out_t cs;
466 
467  // Input pins
468 
469  digital_in_t it1;
470  digital_in_t it2;
471 
472  // Modules
473 
474  i2c_master_t i2c;
475  spi_master_t spi;
476 
477  // ctx variable
478 
479  uint8_t slave_address;
480  pin_name_t chip_select;
484 
486 
490 typedef struct
491 {
492  // Communication gpio pins
493 
494  pin_name_t scl;
495  pin_name_t sda;
496  pin_name_t miso;
497  pin_name_t mosi;
498  pin_name_t sck;
499  pin_name_t cs;
500 
501  // Additional gpio pins
502 
503  pin_name_t it1;
504  pin_name_t it2;
505 
506  // static variable
507 
508  uint32_t i2c_speed;
509  uint8_t i2c_address;
510  uint32_t spi_speed;
511  uint8_t spi_mode;
512  spi_master_chip_select_polarity_t cs_polarity;
513 
515 
516 } accel11_cfg_t;
517  // End types group
519 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
525 #ifdef __cplusplus
526 extern "C"{
527 #endif
528 
538 
547 
556 
567 void accel11_generic_write ( accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
568 
579 void accel11_generic_read ( accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
580 
590 void accel11_write_byte ( accel11_t *ctx, uint8_t addr, uint8_t data_in );
591 
600 uint8_t accel11_read_byte ( accel11_t *ctx, uint8_t addr );
601 
610 
619 
628 int16_t accel11_get_axis_data ( accel11_t *ctx, uint8_t axis );
629 
638 
647 
648 #ifdef __cplusplus
649 }
650 #endif
651 #endif // _ACCEL11_H_
652  // End public_function group
655 
656 // ------------------------------------------------------------------------- END
accel11_cfg_t::mosi
pin_name_t mosi
Definition: accel11.h:497
accel11_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel11.h:508
accel11_init
ACCEL11_RETVAL accel11_init(accel11_t *ctx, accel11_cfg_t *cfg)
Initialization function.
accel11_s::master_sel
accel11_select_t master_sel
Definition: accel11.h:483
accel11_cfg_t::spi_mode
uint8_t spi_mode
Definition: accel11.h:511
accel11_cfg_t::it2
pin_name_t it2
Definition: accel11.h:504
accel11_cfg_t::it1
pin_name_t it1
Definition: accel11.h:503
accel11_cfg_t::sck
pin_name_t sck
Definition: accel11.h:498
accel11_s::slave_address
uint8_t slave_address
Definition: accel11.h:479
accel11_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel11.h:509
accel11_software_reset
void accel11_software_reset(accel11_t *ctx)
Software reset function.
accel11_default_cfg
void accel11_default_cfg(accel11_t *ctx)
Click Default Configuration function.
accel11_cfg_t::sda
pin_name_t sda
Definition: accel11.h:495
accel11_s::read_f
accel11_master_io_t read_f
Definition: accel11.h:482
accel11_s::write_f
accel11_master_io_t write_f
Definition: accel11.h:481
accel11_cfg_t
Click configuration structure definition.
Definition: accel11.h:491
ACCEL11_RETVAL
#define ACCEL11_RETVAL
Definition: accel11.h:87
accel11_get_temperature
int8_t accel11_get_temperature(accel11_t *ctx)
Function get temperature.
accel11_generic_read
void accel11_generic_read(accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
accel11_s
Click ctx object definition.
Definition: accel11.h:463
accel11_read_byte
uint8_t accel11_read_byte(accel11_t *ctx, uint8_t addr)
Functions for read byte from register.
accel11_s::chip_select
pin_name_t chip_select
Definition: accel11.h:480
accel11_s::spi
spi_master_t spi
Definition: accel11.h:475
accel11_s::i2c
i2c_master_t i2c
Definition: accel11.h:474
accel11_select_t
uint8_t accel11_select_t
Communication type.
Definition: accel11.h:452
accel11_s::it2
digital_in_t it2
Definition: accel11.h:470
accel11_cfg_t::miso
pin_name_t miso
Definition: accel11.h:496
accel11_get_axis_data
int16_t accel11_get_axis_data(accel11_t *ctx, uint8_t axis)
Accel Axis data function.
accel11_write_byte
void accel11_write_byte(accel11_t *ctx, uint8_t addr, uint8_t data_in)
Functions for write one byte in register.
accel11_s::it1
digital_in_t it1
Definition: accel11.h:469
accel11_cfg_t::cs
pin_name_t cs
Definition: accel11.h:499
accel11_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel11.h:512
accel11_cfg_setup
void accel11_cfg_setup(accel11_cfg_t *cfg)
Config Object Initialization function.
accel11_s::cs
digital_out_t cs
Definition: accel11.h:465
accel11_t
struct accel11_s accel11_t
Click ctx object definition.
accel11_master_io_t
void(* accel11_master_io_t)(struct accel11_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel11.h:457
accel11_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel11.h:510
accel11_test_comunication
uint8_t accel11_test_comunication(accel11_t *ctx)
Test comunication function.
accel11_cfg_t::sel
accel11_select_t sel
Definition: accel11.h:514
accel11_power_on_procedure
void accel11_power_on_procedure(accel11_t *ctx)
Function for power on chip.
accel11_cfg_t::scl
pin_name_t scl
Definition: accel11.h:494
accel11_generic_write
void accel11_generic_write(accel11_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.