upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
kxtj3.h
1 /*
2 * The MIT License (MIT)
3 *
4 * Author: Assam Boudjelthia
5 * Copyright (c) 2018 Rohm Semiconductor.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 
25 #pragma once
26 
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31 
32 #include <assert.h>
33 #include <unistd.h>
34 
35 #include <mraa/i2c.h>
36 #include <mraa/gpio.h>
37 #include <upm.h>
38 
39 #include "kxtj3_registers.h"
40 
62 #define SECOND_IN_MICRO_S 1000000
63 
67  typedef enum
68  {
69  LOW_RES,
70  HIGH_RES
71  } KXTJ3_RESOLUTION_T;
72 
76  typedef enum
77  {
78  ACTIVE_LOW,
79  ACTIVE_HIGH
80 
81  } KXTJ3_INTERRUPT_POLARITY_T;
82 
86  typedef enum
87  {
88  LATCH_UNTIL_CLEARED,
89  TRANSMIT_ONE_PULSE
90  } KXTJ3_INTERRUPT_RESPONSE_T;
91 
95  typedef enum
96  {
97  NO_INTERRUPT,
98  KXTJ3_DATA_READY_INTERRUPT = KXTJ3_INT_SOURCE1_DRDY,
99  KXTJ3_WAKEUP_INTERRUPT = KXTJ3_INT_SOURCE1_WUFS,
100  KXTJ3_DATA_READY_AND_WAKEUP_INT = KXTJ3_INT_SOURCE1_DRDY | KXTJ3_INT_SOURCE1_WUFS
101  } KXTJ3_INTERRUPT_SOURCE_T;
102 
106  typedef enum
107  {
108  KXTJ3_RANGE_2G = KXTJ3_CTRL_REG1_GSEL_2G,
109  KXTJ3_RANGE_4G = KXTJ3_CTRL_REG1_GSEL_4G,
110  KXTJ3_RANGE_8G = KXTJ3_CTRL_REG1_GSEL_8G,
111  KXTJ3_RANGE_8G_14 = KXTJ3_CTRL_REG1_GSEL_8G_14,
112  KXTJ3_RANGE_16G = KXTJ3_CTRL_REG1_GSEL_16G,
113  KXTJ3_RANGE_16G_2 = KXTJ3_CTRL_REG1_GSEL_16G2,
114  KXTJ3_RANGE_16G_3 = KXTJ3_CTRL_REG1_GSEL_16G3,
115  KXTJ3_RANGE_16G_14 = KXTJ3_CTRL_REG1_GSEL_16G_14
116  } KXTJ3_G_RANGE_T;
117 
121  typedef enum
122  {
123  KXTJ3_ODR_0P781 = KXTJ3_DATA_CTRL_REG_OSA_0P781,
124  KXTJ3_ODR_1P563 = KXTJ3_DATA_CTRL_REG_OSA_1P563,
125  KXTJ3_ODR_3P125 = KXTJ3_DATA_CTRL_REG_OSA_3P125,
126  KXTJ3_ODR_6P25 = KXTJ3_DATA_CTRL_REG_OSA_6P25,
127  KXTJ3_ODR_12P5 = KXTJ3_DATA_CTRL_REG_OSA_12P5,
128  KXTJ3_ODR_25 = KXTJ3_DATA_CTRL_REG_OSA_25,
129  KXTJ3_ODR_50 = KXTJ3_DATA_CTRL_REG_OSA_50,
130  KXTJ3_ODR_100 = KXTJ3_DATA_CTRL_REG_OSA_100,
131  KXTJ3_ODR_200 = KXTJ3_DATA_CTRL_REG_OSA_200,
132  KXTJ3_ODR_400 = KXTJ3_DATA_CTRL_REG_OSA_400,
133  KXTJ3_ODR_800 = KXTJ3_DATA_CTRL_REG_OSA_800,
134  KXTJ3_ODR_1600 = KXTJ3_DATA_CTRL_REG_OSA_1600
135  } KXTJ3_ODR_T;
136 
140  typedef enum
141  {
142  KXTJ3_ODR_WAKEUP_0P781 = KXTJ3_CTRL_REG2_OWUF_0P781,
143  KXTJ3_ODR_WAKEUP_1P563 = KXTJ3_CTRL_REG2_OWUF_1P563,
144  KXTJ3_ODR_WAKEUP_3P125 = KXTJ3_CTRL_REG2_OWUF_3P125,
145  KXTJ3_ODR_WAKEUP_6P25 = KXTJ3_CTRL_REG2_OWUF_6P25,
146  KXTJ3_ODR_WAKEUP_12P5 = KXTJ3_CTRL_REG2_OWUF_12P5,
147  KXTJ3_ODR_WAKEUP_25 = KXTJ3_CTRL_REG2_OWUF_25,
148  KXTJ3_ODR_WAKEUP_50 = KXTJ3_CTRL_REG2_OWUF_50,
149  KXTJ3_ODR_WAKEUP_100 = KXTJ3_CTRL_REG2_OWUF_100
150  } KXTJ3_ODR_WAKEUP_T;
151 
155  typedef enum
156  {
157  X_NEGATIVE = KXTJ3_INT_CTRL_REG2_XNWU,
158  X_POSITIVE = KXTJ3_INT_CTRL_REG2_XPWU,
159  Y_NEGATIVE = KXTJ3_INT_CTRL_REG2_YNWU,
160  Y_POSITIVE = KXTJ3_INT_CTRL_REG2_YPWU,
161  Z_NEGATIVE = KXTJ3_INT_CTRL_REG2_ZNWU,
162  Z_POSITIVE = KXTJ3_INT_CTRL_REG2_ZPWU
163  } KXTJ3_WAKEUP_SOURCE_T;
164 
168  typedef struct
169  {
170  bool X_NEGATIVE,
171  X_POSITIVE,
172  Y_NEGATIVE,
173  Y_POSITIVE,
174  Z_NEGATIVE,
175  Z_POSITIVE;
177 
181  typedef struct _kxtj3_context
182  {
183  mraa_i2c_context i2c;
184  KXTJ3_RESOLUTION_T res_mode;
185  KXTJ3_G_RANGE_T g_range_mode;
186  float acceleration_scale;
187  KXTJ3_ODR_T odr;
188  float odr_in_sec;
189  KXTJ3_ODR_WAKEUP_T odr_wakeup;
190  float odr_in_sec_wakeup;
191  mraa_gpio_context interrupt_pin;
192  } * kxtj3_context;
193 
201  kxtj3_context kxtj3_init(int bus, uint8_t addr);
202 
219  upm_result_t kxtj3_sensor_init(const kxtj3_context dev, KXTJ3_ODR_T odr, KXTJ3_RESOLUTION_T res, KXTJ3_G_RANGE_T g_range);
220 
228  upm_result_t kxtj3_get_who_am_i(const kxtj3_context dev, uint8_t *data);
229 
237  void kxtj3_close(kxtj3_context dev);
238 
245  upm_result_t kxtj3_set_sensor_active(const kxtj3_context dev);
246 
253  upm_result_t kxtj3_set_sensor_standby(const kxtj3_context dev);
254 
268  upm_result_t kxtj3_set_g_range(const kxtj3_context dev, KXTJ3_G_RANGE_T g_range);
269 
285  upm_result_t kxtj3_set_resolution(const kxtj3_context dev, KXTJ3_RESOLUTION_T resolution);
286 
296  upm_result_t kxtj3_set_odr(const kxtj3_context dev, KXTJ3_ODR_T odr);
297 
307  upm_result_t kxtj3_set_odr_wakeup_function(const kxtj3_context dev, KXTJ3_ODR_WAKEUP_T odr);
308 
321  upm_result_t kxtj3_self_test_digital_communication(kxtj3_context dev);
322 
336  upm_result_t kxtj3_sensor_self_test(const kxtj3_context dev);
337 
349  upm_result_t kxtj3_sensor_software_reset(const kxtj3_context dev);
350 
360  upm_result_t kxtj3_get_acceleration_data_raw(const kxtj3_context dev, float *x, float *y, float *z);
361 
370  upm_result_t kxtj3_get_acceleration_data(const kxtj3_context dev, float *x, float *y, float *z);
371 
379  float kxtj3_get_acceleration_sampling_period(kxtj3_context dev);
380 
388  float kxtj3_get_wakeup_sampling_period(kxtj3_context dev);
389 
399  upm_result_t kxtj3_enable_data_ready_interrupt(const kxtj3_context dev);
400 
409  upm_result_t kxtj3_disable_data_ready_interrupt(const kxtj3_context dev);
410 
419  upm_result_t kxtj3_enable_wakeup_interrupt(const kxtj3_context dev);
420 
429  upm_result_t kxtj3_disable_wakeup_interrupt(const kxtj3_context dev);
430 
450  upm_result_t kxtj3_enable_interrupt_pin(const kxtj3_context dev, KXTJ3_INTERRUPT_POLARITY_T polarity, KXTJ3_INTERRUPT_RESPONSE_T response_type);
451 
460  upm_result_t kxtj3_disable_interrupt_pin(const kxtj3_context dev);
461 
473  upm_result_t kxtj3_set_interrupt_polarity(const kxtj3_context dev, KXTJ3_INTERRUPT_POLARITY_T polarity);
474 
491  upm_result_t kxtj3_set_interrupt_response(const kxtj3_context dev, KXTJ3_INTERRUPT_RESPONSE_T response_type);
492 
501  bool kxtj3_get_interrupt_status(const kxtj3_context dev);
502 
513  upm_result_t kxtj3_read_interrupt_source1_reg(const kxtj3_context dev, uint8_t *reg_value);
514 
523  KXTJ3_INTERRUPT_SOURCE_T kxtj3_get_interrupt_source(const kxtj3_context dev);
524 
535  upm_result_t kxtj3_install_isr(const kxtj3_context dev, mraa_gpio_edge_t edge, int pin, void (*isr)(void *), void *isr_args);
536 
543  void kxtj3_uninstall_isr(const kxtj3_context dev);
544 
553  upm_result_t kxtj3_clear_interrupt_information(kxtj3_context dev);
554 
564  upm_result_t kxtj3_enable_wakeup_single_axis_direction(kxtj3_context dev, KXTJ3_WAKEUP_SOURCE_T axis);
565 
575  upm_result_t kxtj3_disable_wakeup_single_axis_direction(kxtj3_context dev, KXTJ3_WAKEUP_SOURCE_T axis);
576 
585  kxtj3_wakeup_axes kxtj3_get_wakeup_axis_and_direction(kxtj3_context dev);
586 
598  upm_result_t kxtj3_enable_wakeup_latch(kxtj3_context dev);
599 
616  upm_result_t kxtj3_disable_wakeup_latch(kxtj3_context dev);
617 
634  upm_result_t kxtj3_set_wakeup_motion_counter(kxtj3_context dev, uint8_t count);
635 
649  upm_result_t kxtj3_set_wakeup_motion_time(kxtj3_context dev, float desired_time);
650 
658  upm_result_t kxtj3_get_wakeup_motion_time(kxtj3_context dev, float *out_time);
659 
676  upm_result_t kxtj3_set_wakeup_non_activity_counter(kxtj3_context dev, uint8_t count);
677 
691  upm_result_t kxtj3_set_wakeup_non_activity_time(kxtj3_context dev, float desired_time);
692 
700  upm_result_t kxtj3_get_wakeup_non_activity_time(kxtj3_context dev, float *out_time);
701 
717  upm_result_t kxtj3_set_wakeup_threshold_counter(kxtj3_context dev, uint16_t count);
718 
733  upm_result_t kxtj3_set_wakeup_threshold_g_value(kxtj3_context dev, float g_threshold);
734 
742  upm_result_t kxtj3_get_wakeup_threshold(kxtj3_context dev, float *out_threshold);
743 
744 #ifdef __cplusplus
745 }
746 #endif
Use it to get axis and direction for wake-up function.
Definition: kxtj3.h:168
Sensor context.
Definition: kxtj3.h:181