upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
kx122.h
Go to the documentation of this file.
1 /*
2 * The MIT License (MIT)
3 *
4 * Author: Samuli Rissanen <samuli.rissanen@hotmail.com>
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 #endif
30 
31 #include <assert.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <sys/types.h>
35 
36 #include <mraa/i2c.h>
37 #include <mraa/spi.h>
38 #include <mraa/gpio.h>
39 
40 #include <upm.h>
41 
42 #include "kx122_registers.h"
43 
52 //Frequency of the SPI connection
53 #define DEFAULT_SPI_FREQUENCY 10000
54 
55 //Default slave addresses for the sensor
56 #define KX122_DEFAULT_SLAVE_ADDR_1 0x1F
57 #define KX122_DEFAULT_SLAVE_ADDR_2 0x1E
58 
59 //Microseconds in a second
60 #define MICRO_S 1000000
61 
62 //Sensor ODR values
63 typedef enum{
64  KX122_ODR_12P5 = KX122_ODCNTL_OSA_12P5,
65  KX122_ODR_25 = KX122_ODCNTL_OSA_25,
66  KX122_ODR_50 = KX122_ODCNTL_OSA_50,
67  KX122_ODR_100 = KX122_ODCNTL_OSA_100,
68  KX122_ODR_200 = KX122_ODCNTL_OSA_200,
69  KX122_ODR_400 = KX122_ODCNTL_OSA_400,
70  KX122_ODR_800 = KX122_ODCNTL_OSA_800,
71  KX122_ODR_1600 = KX122_ODCNTL_OSA_1600,
72  KX122_ODR_0P781 = KX122_ODCNTL_OSA_0P781,
73  KX122_ODR_1P563 = KX122_ODCNTL_OSA_1P563,
74  KX122_ODR_3P125 = KX122_ODCNTL_OSA_3P125,
75  KX122_ODR_6P25 = KX122_ODCNTL_OSA_6P25,
76  KX122_ODR_3200 = KX122_ODCNTL_OSA_3200,
77  KX122_ODR_6400 = KX122_ODCNTL_OSA_6400,
78  KX122_ODR_12800 = KX122_ODCNTL_OSA_12800,
79  KX122_ODR_25600 = KX122_ODCNTL_OSA_25600
80 } KX122_ODR_T;
81 
82 //Sensor average values
83 typedef enum{
84  KX122_NO_AVG = KX122_LP_CNTL_AVC_NO_AVG,
85  KX122_2_SAMPLE_AVG = KX122_LP_CNTL_AVC_2_SAMPLE_AVG,
86  KX122_4_SAMPLE_AVG = KX122_LP_CNTL_AVC_4_SAMPLE_AVG,
87  KX122_8_SAMPLE_AVG = KX122_LP_CNTL_AVC_8_SAMPLE_AVG,
88  KX122_16_SAMPLE_AVG = KX122_LP_CNTL_AVC_16_SAMPLE_AVG,
89  KX122_32_SAMPLE_AVG = KX122_LP_CNTL_AVC_32_SAMPLE_AVG,
90  KX122_64_SAMPLE_AVG = KX122_LP_CNTL_AVC_64_SAMPLE_AVG,
91  KX122_128_SAMPLE_AVG = KX122_LP_CNTL_AVC_128_SAMPLE_AVG
92 } KX122_AVG_T;
93 
94 //Sensor range values
95 typedef enum{
96  KX122_RANGE_2G = KX122_CNTL1_GSEL_2G,
97  KX122_RANGE_4G = KX122_CNTL1_GSEL_4G,
98  KX122_RANGE_8G = KX122_CNTL1_GSEL_8G
99 } KX122_RANGE_T;
100 
101 //Sensor buffer modes
102 typedef enum{
103  KX122_FIFO_MODE = KX122_BUF_CNTL2_BUF_M_FIFO,
104  KX122_FILO_MODE = KX122_BUF_CNTL2_BUF_M_FILO,
105  KX122_STREAM_MODE = KX122_BUF_CNTL2_BUF_M_STREAM,
106 } KX122_BUFFER_MODE_T;
107 
108 //Sensor interrupt types
109 typedef enum{
110  KX122_BUF_FULL_INT = KX122_INC4_BFI1,
111  KX122_WATERMARK_INT = KX122_INC4_WMI1,
112  KX122_DATA_READY_INT = KX122_INC4_DRDYI1
113 } KX122_INTERRUPT_T;
114 
115 //High and low resolution modes
116 typedef enum{
117  HIGH_RES,
118  LOW_RES
119 } KX122_RES_T;
120 
121 //Sensor low-pass filter roll of values
122 typedef enum{
123  ODR_9,
124  ODR_2
125 } LPRO_STATE_T;
126 
127 //Interrupt pins
128 typedef enum{
129  INT1,
130  INT2
131 }KX122_INTERRUPT_PIN_T;
132 
133 //Interrupt polarity
134 typedef enum{
135  ACTIVE_LOW,
136  ACTIVE_HIGH
137 } KX122_INTERRUPT_POLARITY_T;
138 
139 //Device context
140 typedef struct _kx122_context {
141  mraa_i2c_context i2c;
142  mraa_spi_context spi;
143  mraa_gpio_context gpio1; //Interrupt pin 1
144  mraa_gpio_context gpio2; //Interrupt pin 2
145  mraa_gpio_context chip_select; //Chip select pin (SPI)
146 
147  float accel_scale; //Acceleration scaling
148  KX122_RES_T res_mode; //Sensor resolution
149  KX122_RANGE_T grange_mode; //Sensor range
150 
151  float buffer_accel_scale; //Buffer acceleration scaling
152  KX122_BUFFER_MODE_T buffer_mode; //Buffer mode
153  KX122_RES_T buffer_res; //Buffer resolution
154 
155  bool using_spi;
156 
157 } *kx122_context;
158 
159 //Struct for ODR values and their decimal counterparts.
160 typedef struct _odr_item{
161  uint8_t odr_value;
162  float odr_decimal;
163 } odr_item;
164 
179 kx122_context kx122_init(int bus, int addr, int chip_select_pin, int spi_bus_frequency);
180 
188 void kx122_close(kx122_context dev);
189 
190 
205 upm_result_t kx122_device_init(const kx122_context dev, KX122_ODR_T odr, KX122_RES_T res, KX122_RANGE_T grange);
206 
213 float kx122_get_sample_period(const kx122_context dev);
214 
222 upm_result_t kx122_get_who_am_i(const kx122_context dev, uint8_t *data);
223 
233 upm_result_t kx122_get_acceleration_data_raw(const kx122_context dev, float *x, float *y, float *z);
234 
243 upm_result_t kx122_get_acceleration_data(const kx122_context dev, float *x, float *y, float *z);
244 
256 upm_result_t kx122_sensor_software_reset(const kx122_context dev);
257 
266 upm_result_t kx122_enable_iir(const kx122_context dev);
267 
276 upm_result_t kx122_disable_iir(const kx122_context dev);
277 
292 upm_result_t kx122_self_test(const kx122_context dev);
293 
300 upm_result_t kx122_set_sensor_standby(const kx122_context dev);
301 
308 upm_result_t kx122_set_sensor_active(const kx122_context dev);
309 
319 upm_result_t kx122_set_odr(const kx122_context dev, KX122_ODR_T odr);
320 
330 upm_result_t kx122_set_grange(const kx122_context dev, KX122_RANGE_T grange);
331 
344 upm_result_t kx122_set_resolution(const kx122_context dev, KX122_RES_T res);
345 
355 upm_result_t kx122_set_bw(const kx122_context dev, LPRO_STATE_T lpro);
356 
366 upm_result_t kx122_set_average(const kx122_context dev, KX122_AVG_T avg);
367 
379 upm_result_t kx122_install_isr(const kx122_context dev, mraa_gpio_edge_t edge,KX122_INTERRUPT_PIN_T intp, int pin,void (*isr)(void *), void *arg);
380 
387 void kx122_uninstall_isr(const kx122_context dev, KX122_INTERRUPT_PIN_T intp);
388 
403 upm_result_t kx122_enable_interrupt1(const kx122_context dev, KX122_INTERRUPT_POLARITY_T polarity);
404 
419 upm_result_t kx122_enable_interrupt2(const kx122_context dev, KX122_INTERRUPT_POLARITY_T polarity);
420 
429 upm_result_t kx122_disable_interrupt1(const kx122_context dev);
430 
439 upm_result_t kx122_disable_interrupt2(const kx122_context dev);
440 
452 upm_result_t kx122_route_interrupt1(const kx122_context dev, uint8_t bits);
453 
465 upm_result_t kx122_route_interrupt2(const kx122_context dev, uint8_t bits);
466 
476 
492 upm_result_t kx122_get_interrupt_source(const kx122_context dev, uint8_t *data);
493 
502 upm_result_t kx122_clear_interrupt(const kx122_context dev);
503 
513 upm_result_t kx122_enable_data_ready_interrupt(const kx122_context dev);
514 
523 upm_result_t kx122_disable_data_ready_interrupt(const kx122_context dev);
524 
535 upm_result_t kx122_enable_buffer_full_interrupt(const kx122_context dev);
536 
545 upm_result_t kx122_disable_buffer_full_interrupt(const kx122_context dev);
546 
555 upm_result_t kx122_enable_buffer(const kx122_context dev);
556 
565 upm_result_t kx122_disable_buffer(const kx122_context dev);
566 
582 upm_result_t kx122_buffer_init(const kx122_context dev, uint samples, KX122_RES_T res, KX122_BUFFER_MODE_T mode);
583 
593 upm_result_t kx122_set_buffer_resolution(const kx122_context dev, KX122_RES_T res);
594 
610 upm_result_t kx122_set_buffer_threshold(const kx122_context dev, uint samples);
611 
626 upm_result_t kx122_set_buffer_mode(const kx122_context dev, KX122_BUFFER_MODE_T mode);
627 
635 upm_result_t kx122_get_buffer_status(const kx122_context dev, uint *samples);
636 
649 upm_result_t kx122_read_buffer_samples_raw(const kx122_context dev, uint len, float *x_array, float *y_array, float *z_array);
650 
663 upm_result_t kx122_read_buffer_samples(const kx122_context dev, uint len, float *x_array, float *y_array, float *z_array);
664 
671 upm_result_t kx122_clear_buffer(const kx122_context dev);
672 
673 #ifdef __cplusplus
674 }
675 #endif
upm_result_t kx122_get_acceleration_data(const kx122_context dev, float *x, float *y, float *z)
Definition: kx122.c:557
upm_result_t kx122_read_buffer_samples_raw(const kx122_context dev, uint len, float *x_array, float *y_array, float *z_array)
Definition: kx122.c:1001
upm_result_t kx122_set_buffer_mode(const kx122_context dev, KX122_BUFFER_MODE_T mode)
Definition: kx122.c:957
upm_result_t kx122_disable_interrupt1(const kx122_context dev)
Definition: kx122.c:811
upm_result_t kx122_clear_buffer(const kx122_context dev)
Definition: kx122.c:965
upm_result_t kx122_disable_data_ready_interrupt(const kx122_context dev)
Definition: kx122.c:841
bool kx122_get_interrupt_status(const kx122_context dev)
Definition: kx122.c:873
upm_result_t kx122_disable_interrupt2(const kx122_context dev)
Definition: kx122.c:817
upm_result_t kx122_disable_iir(const kx122_context dev)
Definition: kx122.c:613
upm_result_t kx122_set_odr(const kx122_context dev, KX122_ODR_T odr)
Definition: kx122.c:698
upm_result_t kx122_enable_interrupt1(const kx122_context dev, KX122_INTERRUPT_POLARITY_T polarity)
Definition: kx122.c:787
upm_result_t kx122_enable_data_ready_interrupt(const kx122_context dev)
Definition: kx122.c:835
upm_result_t kx122_get_acceleration_data_raw(const kx122_context dev, float *x, float *y, float *z)
Definition: kx122.c:520
void kx122_uninstall_isr(const kx122_context dev, KX122_INTERRUPT_PIN_T intp)
Definition: kx122.c:772
upm_result_t kx122_set_buffer_threshold(const kx122_context dev, uint samples)
Definition: kx122.c:934
upm_result_t kx122_set_sensor_standby(const kx122_context dev)
Definition: kx122.c:504
upm_result_t kx122_buffer_init(const kx122_context dev, uint samples, KX122_RES_T res, KX122_BUFFER_MODE_T mode)
Definition: kx122.c:901
upm_result_t kx122_route_interrupt2(const kx122_context dev, uint8_t bits)
Definition: kx122.c:829
upm_result_t kx122_sensor_software_reset(const kx122_context dev)
Definition: kx122.c:578
upm_result_t kx122_clear_interrupt(const kx122_context dev)
Definition: kx122.c:865
kx122_context kx122_init(int bus, int addr, int chip_select_pin, int spi_bus_frequency)
Definition: kx122.c:164
upm_result_t kx122_set_sensor_active(const kx122_context dev)
Definition: kx122.c:510
Definition: kx122.h:160
upm_result_t kx122_get_who_am_i(const kx122_context dev, uint8_t *data)
Definition: kx122.c:498
upm_result_t kx122_enable_buffer_full_interrupt(const kx122_context dev)
Definition: kx122.c:847
upm_result_t kx122_get_buffer_status(const kx122_context dev, uint *samples)
Definition: kx122.c:971
upm_result_t kx122_enable_interrupt2(const kx122_context dev, KX122_INTERRUPT_POLARITY_T polarity)
Definition: kx122.c:799
upm_result_t kx122_enable_buffer(const kx122_context dev)
Definition: kx122.c:889
upm_result_t kx122_disable_buffer_full_interrupt(const kx122_context dev)
Definition: kx122.c:853
upm_result_t kx122_route_interrupt1(const kx122_context dev, uint8_t bits)
Definition: kx122.c:823
upm_result_t kx122_self_test(const kx122_context dev)
Definition: kx122.c:619
upm_result_t kx122_device_init(const kx122_context dev, KX122_ODR_T odr, KX122_RES_T res, KX122_RANGE_T grange)
Definition: kx122.c:479
upm_result_t kx122_set_buffer_resolution(const kx122_context dev, KX122_RES_T res)
Definition: kx122.c:922
upm_result_t kx122_set_grange(const kx122_context dev, KX122_RANGE_T grange)
Definition: kx122.c:704
void kx122_close(kx122_context dev)
Definition: kx122.c:249
upm_result_t kx122_get_interrupt_source(const kx122_context dev, uint8_t *data)
Definition: kx122.c:859
upm_result_t kx122_read_buffer_samples(const kx122_context dev, uint len, float *x_array, float *y_array, float *z_array)
Definition: kx122.c:1073
upm_result_t kx122_set_bw(const kx122_context dev, LPRO_STATE_T lpro)
Definition: kx122.c:727
upm_result_t kx122_disable_buffer(const kx122_context dev)
Definition: kx122.c:895
Definition: kx122.h:140
upm_result_t kx122_set_resolution(const kx122_context dev, KX122_RES_T res)
Definition: kx122.c:712
upm_result_t kx122_install_isr(const kx122_context dev, mraa_gpio_edge_t edge, KX122_INTERRUPT_PIN_T intp, int pin, void(*isr)(void *), void *arg)
Definition: kx122.c:744
float kx122_get_sample_period(const kx122_context dev)
Definition: kx122.c:455
upm_result_t kx122_enable_iir(const kx122_context dev)
Definition: kx122.c:607
upm_result_t kx122_set_average(const kx122_context dev, KX122_AVG_T avg)
Definition: kx122.c:738