annotate hid-usb-to-ir.c @ 4:4f66576cd77a default tip

add README
author Peter Meerwald <p.meerwald@bct-electronic.com>
date Thu, 03 Jan 2013 13:12:08 +0100
parents 95b8b8a0b2d0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
1 /*
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
2 * Demo program for the Silabs IR Gesture USB Reference Design,
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
3 * http://www.silabs.com/products/sensors/Pages/HID-USB-to-IR-Reference-Design.aspx
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
4 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
5 * Reads the part number of the cp2112 bridge,
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
6 * http://www.silabs.com/products/interface/usbtouart/Pages/HID-USB-to-SMBus-Bridge.aspx,
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
7 * and the part/revision/sequencer version of the si1143 via SMBus/I2C,
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
8 * finally lets the four blue LEDs dance :).
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
9 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
10 * Copyright (c) 2012 Peter Meerwald, <pmeerw@pmeerw.net>
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
11 * Released under GPLv3 license, see http://www.gnu.org/licenses/gpl-3.0.html.
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
12 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
13 * Depends on the HID API multi-platform library, available from
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
14 * http://www.signal11.us/oss/hidapi/.
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
15 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
16 * Build with
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
17 * gcc -Wall -g -o hid-usb-to-ir hid-usb-to-ir.c `pkg-config hidapi-hidraw --libs`
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
18 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
19 * May need root permission to access USB device (or appropriate udev rule).
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
20 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
21 * Based upon information from Silabs AN495 v0.2.
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
22 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
23
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
24 #include <stdio.h>
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
25 #include <stdlib.h>
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
26 #include <string.h>
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
27 #include <unistd.h>
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
28 #include "hidapi/hidapi.h"
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
29
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
30 #define SI114X_SLAVE_ADDR 0x5a
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
31
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
32 #define SI114x_REG_PART_ID 0x00
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
33 #define SI114x_REG_REV_ID 0x01
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
34 #define SI114x_REG_SEQ_ID 0x02
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
35 #define SI114X_REG_HWKEY 0x07
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
36 #define SI114X_REG_MEAS_RATE 0x08
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
37 #define SI114X_REG_ALS_RATE 0x09
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
38 #define SI114X_REG_PS_RATE 0x0a
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
39 #define SI114X_REG_PS_LED21 0x0f
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
40 #define SI114X_REG_PS_LED3 0x10
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
41 #define SI114X_REG_PARAM_WR 0x17
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
42 #define SI114X_REG_COMMAND 0x18
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
43 #define SI114X_REG_ALSVIS_DATA0 0x22
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
44 #define SI114X_REG_ALSVIS_DATA1 0x23
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
45 #define SI114X_REG_ALSIR_DATA0 0x24
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
46 #define SI114X_REG_ALSIR_DATA1 0x25
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
47 #define SI114X_REG_PS1_DATA0 0x26
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
48 #define SI114X_REG_PS1_DATA1 0x27
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
49 #define SI114X_REG_PS2_DATA0 0x28
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
50 #define SI114X_REG_PS2_DATA1 0x29
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
51 #define SI114X_REG_PS3_DATA0 0x2a
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
52 #define SI114X_REG_PS3_DATA1 0x2b
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
53 #define SI114X_REG_AUX_DATA0 0x2c
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
54 #define SI114X_REG_AUX_DATA1 0x2d
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
55
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
56 /* Commands for COMMAND */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
57 #define SI114X_CMD_NOP 0x00
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
58 #define SI114X_CMD_RESET 0x01
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
59 #define SI114X_CMD_BUSADDR 0x02
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
60 #define SI114X_CMD_PS_FORCE 0x05
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
61 #define SI114X_CMD_ALS_FORCE 0x06
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
62 #define SI114X_CMD_PSALS_FORCE 0x07
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
63 #define SI114X_CMD_PS_PAUSE 0x09
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
64 #define SI114X_CMD_ALS_PAUSE 0x0a
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
65 #define SI114X_CMD_PSALS_PAUSE 0x0b
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
66 #define SI114X_CMD_PS_AUTO 0x0d
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
67 #define SI114X_CMD_ALS_AUTO 0x0e
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
68 #define SI114X_CMD_PSALS_AUTO 0x0f
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
69 #define SI114X_CMD_PARAM_QUERY 0x80
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
70 #define SI114X_CMD_PARAM_SET 0xa0
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
71 #define SI114X_CMD_PARAM_AND 0xc0
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
72 #define SI114X_CMD_PARAM_OR 0xe0
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
73
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
74 /* Parameter offsets */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
75 #define SI114X_PARAM_CHLIST 0x01
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
76 #define SI114X_PARAM_PSLED12_SELECT 0x02
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
77 #define SI114X_PARAM_PSLED3_SELECT 0x03
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
78 #define SI114X_PARAM_PS_ADC_COUNTER 0x0a
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
79 #define SI114X_PARAM_PS_ADC_GAIN 0x0b
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
80 #define SI114X_PARAM_PS_ADC_MISC 0x0c
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
81
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
82 /* Channel enable masks for CHLIST parameter */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
83 #define SI114X_CHLIST_EN_PS1 0x01
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
84 #define SI114X_CHLIST_EN_PS2 0x02
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
85 #define SI114X_CHLIST_EN_PS3 0x04
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
86 #define SI114X_CHLIST_EN_ALSVIS 0x10
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
87 #define SI114X_CHLIST_EN_ALSIR 0x20
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
88 #define SI114X_CHLIST_EN_AUX 0x40
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
89
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
90 /* Measurement rate settings */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
91 #define SI114X_MEAS_RATE_FORCED 0x00
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
92 #define SI114X_MEAS_RATE_10MS 0x84
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
93 #define SI114X_MEAS_RATE_20MS 0x94
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
94 #define SI114X_MEAS_RATE_100MS 0xb9
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
95 #define SI114X_MEAS_RATE_496MS 0xdf
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
96 #define SI114X_MEAS_RATE_1984MS 0xff
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
97
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
98 /* ALS rate settings relative to measurement rate */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
99 #define SI114X_ALS_RATE_OFF 0x00
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
100 #define SI114X_ALS_RATE_1X 0x08
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
101 #define SI114X_ALS_RATE_10X 0x32
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
102 #define SI114X_ALS_RATE_100X 0x69
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
103
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
104 /* PS rate settings relative to measurement rate */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
105 #define SI114X_PS_RATE_OFF 0x00
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
106 #define SI114X_PS_RATE_1X 0x08
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
107 #define SI114X_PS_RATE_10X 0x32
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
108 #define SI114X_PS_RATE_100X 0x69
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
109
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
110 #define CP2112_GETSET_GPIO_CONFIG 0x02
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
111 #define CP2112_GET_GPIO 0x03
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
112 #define CP2112_SET_GPIO 0x04
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
113 #define CP2122_GET_VERSION_INFO 0x05
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
114 #define CP2112_GETSET_SMBUS_CONFIG 0x06
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
115 #define CP2112_DATA_WRITE_READ_REQ 0x11
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
116 #define CP2112_DATA_READ_RESP 0x13
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
117 #define CP2112_DATA_WRITE 0x14
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
118 #define CP2112_TRANSFER_STATUS_REQ 0x15
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
119 #define CP2112_TRANSFER_STATUS_RESP 0x16
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
120
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
121 #define CP2112_LED_DOWN 0x01 /* LED DS2, GPIO 0 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
122 #define CP2112_LED_UP 0x02 /* LED DS3, GPIO 1 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
123 #define CP2112_LED_RIGHT 0x08 /* LED DS8, GPIO 3 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
124 #define CP2112_LED_LEFT 0x80 /* LED DS4, GPIO 7 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
125 #define CP2112_LEDS_MASK (CP2112_LED_DOWN | CP2112_LED_UP | \
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
126 CP2112_LED_RIGHT | CP2112_LED_LEFT)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
127
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
128 /* get cp2112 part number and version */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
129 static int cp2112_get_version(hid_device *hd, unsigned char data[2]) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
130 unsigned char buf[3] = { CP2122_GET_VERSION_INFO, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
131
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
132 int ret = hid_get_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
133 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
134 fprintf(stderr, "hid_get_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
135 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
136 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
137 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
138
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
139 data[0] = buf[1]; /* part number */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
140 data[1] = buf[2]; /* version */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
141
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
142 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
143 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
144
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
145 /* configures cp2112 to automatically send read data, see AN495 section 4.6 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
146 static int cp2112_set_auto_send_read(hid_device *hd, int on_off) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
147 unsigned char buf[14] = { CP2112_GETSET_SMBUS_CONFIG, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
148 int ret = hid_get_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
149 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
150 fprintf(stderr, "hid_get_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
151 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
152 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
153 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
154
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
155 buf[6] = on_off;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
156
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
157 ret = hid_send_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
158 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
159 fprintf(stderr, "hid_send_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
160 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
161 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
162 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
163
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
164 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
165 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
166
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
167 /* see if the cp2112 is idle */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
168 static int cp2112_is_idle(hid_device *hd) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
169 unsigned char status_req[2] = { CP2112_TRANSFER_STATUS_REQ, 0x01, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
170 if (hid_write(hd, status_req, sizeof(status_req)) < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
171 fprintf(stderr, "hid_write() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
172 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
173 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
174 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
175
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
176 unsigned char status_resp[7] = { 0x00, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
177 if (hid_read(hd, status_resp, sizeof(status_resp)) < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
178 fprintf(stderr, "hid_read() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
179 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
180 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
181 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
182
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
183 if (status_resp[0] == CP2112_TRANSFER_STATUS_RESP) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
184 if (status_resp[1] == 0x00) /* is idle? */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
185 return 1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
186 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
187
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
188 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
189 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
190
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
191 /* write a data byte to register reg of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
192 static int cp2112_write_byte(hid_device *hd, unsigned char reg, unsigned char data) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
193 /* 0x5a is the 7-bit SMBus slave address of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
194 unsigned char buf_out[5] = { CP2112_DATA_WRITE, SI114X_SLAVE_ADDR<<1, 0x02, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
195 buf_out[3] = reg;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
196 buf_out[4] = data;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
197 int ret = hid_write(hd, buf_out, sizeof(buf_out));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
198 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
199 fprintf(stderr, "hid_write() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
200 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
201 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
202 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
203
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
204 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
205 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
206
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
207 static int cp2112_write_param_byte(hid_device *hd, unsigned char param, unsigned char data) {
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
208 if (cp2112_write_byte(hd, SI114X_REG_PARAM_WR, data) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
209 return -1;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
210 if (cp2112_write_byte(hd, SI114X_REG_COMMAND, param | SI114X_CMD_PARAM_SET) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
211 return -1;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
212 return 0;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
213 }
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
214
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
215 /* read a data byte from register reg of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
216 static int cp2112_read_byte(hid_device *hd, unsigned char reg, unsigned char data[1]) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
217 /* 0x5a is the 7-bit SMBus slave address of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
218 unsigned char buf_out[6] = { CP2112_DATA_WRITE_READ_REQ,
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
219 SI114X_SLAVE_ADDR<<1, 0x00, 0x01, 0x01, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
220 buf_out[5] = reg;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
221 int ret = hid_write(hd, buf_out, sizeof(buf_out));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
222 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
223 fprintf(stderr, "hid_write() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
224 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
225 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
226 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
227
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
228 /* FIXME: may loop forever, error handling needed */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
229 while (1) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
230
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
231 unsigned char buf_in[8] = { 0x00, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
232 ret = hid_read(hd, buf_in, sizeof(buf_in));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
233 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
234 fprintf(stderr, "hid_read_() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
235 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
236 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
237 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
238
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
239
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
240 if (buf_in[0] == CP2112_TRANSFER_STATUS_REQ)
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
241 continue;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
242
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
243 if (buf_in[0] == CP2112_DATA_READ_RESP) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
244 if (buf_in[1] == 0x02 && buf_in[2] == 0x00)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
245 continue; /* no data yet */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
246 if (buf_in[1] == 0x02 && buf_in[2] == 0x01) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
247 data[0] = buf_in[3]; /* data available */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
248 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
249 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
250 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
251 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
252
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
253 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
254 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
255
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
256 static int cp2112_read_word(hid_device *hd, unsigned char reg, unsigned short data[1]) {
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
257 unsigned char lsb, msb;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
258
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
259 if (cp2112_read_byte(hd, reg, &lsb) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
260 return -1;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
261 if (cp2112_read_byte(hd, reg+1, &msb) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
262 return -1;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
263
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
264 data[0] = (msb << 8) | lsb;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
265 return 0;
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
266 }
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
267
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
268 /* configure cp2112 GPIO pins */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
269 static int cp2112_config_gpio(hid_device *hd) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
270 unsigned char buf[5] = { CP2112_GETSET_GPIO_CONFIG, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
271 int ret = hid_get_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
272 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
273 fprintf(stderr, "hid_get_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
274 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
275 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
276 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
277
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
278 buf[1] = 0x8b; /* output direction for GPIOs 0, 1, 3, 7 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
279 buf[2] = 0x8b; /* push-pull for GPIOs 0, 1, 3, 7 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
280 buf[3] = 0x00; /* no special functions, i.e. use pins as GPIO */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
281
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
282 ret = hid_send_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
283 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
284 fprintf(stderr, "hid_send_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
285 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
286 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
287 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
288
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
289 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
290 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
291
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
292 /* set cp2112 GPIO pins */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
293 static int cp2112_set_gpio(hid_device *hd, unsigned char mask, unsigned char value) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
294 unsigned char buf[3] = { CP2112_SET_GPIO, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
295
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
296 buf[1] = ~value; /* set GPIO pins: 0 .. turn LED on, 1 .. off */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
297 buf[2] = mask; /* mask of pins to set, ignore others */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
298
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
299 int ret = hid_send_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
300 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
301 fprintf(stderr, "hid_send_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
302 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
303 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
304 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
305
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
306 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
307 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
308
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
309 int main() {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
310 if (hid_init() < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
311 fprintf(stderr, "hid_init() failed, exit.\n");
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
312 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
313 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
314
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
315 /* open Silabs IR Gesture USB reference design by USB product:vendor id */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
316 hid_device *hd = hid_open(0x10c4, 0xea90, NULL);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
317 if (hd == NULL) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
318 fprintf(stderr, "hid_open() failed\n");
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
319 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
320 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
321
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
322 unsigned char version_data[2];
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
323 if (cp2112_get_version(hd, version_data) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
324 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
325 printf("cp2112 part number 0x%02x, device version %d\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
326 version_data[0], version_data[1]);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
327
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
328 if (cp2112_set_auto_send_read(hd, 1) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
329 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
330
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
331 /* write 0x17 to si114x HW_KEY register as per datasheet */
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
332 if (cp2112_write_byte(hd, SI114X_REG_HWKEY, 0x17) < 0)
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
333 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
334
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
335 unsigned char part, rev, seq;
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
336 if (cp2112_read_byte(hd, SI114x_REG_PART_ID, &part) < 0)
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
337 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
338
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
339 if (cp2112_read_byte(hd, SI114x_REG_REV_ID, &rev) < 0)
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
340 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
341
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
342 if (cp2112_read_byte(hd, SI114x_REG_SEQ_ID, &seq) < 0)
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
343 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
344 printf("si114x part number 0x%02x, revision 0x%02x, sequencer 0x%02x\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
345 part, rev, seq);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
346
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
347 if (cp2112_write_byte(hd, SI114X_REG_COMMAND, SI114X_CMD_RESET) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
348 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
349 usleep(20000);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
350
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
351 if (cp2112_write_byte(hd, SI114X_REG_HWKEY, 0x17) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
352 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
353 usleep(20000);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
354
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
355 cp2112_is_idle(hd);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
356
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
357 /* set LED currents to maximum */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
358 if (cp2112_write_byte(hd, SI114X_REG_PS_LED3, 0x04))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
359 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
360 if (cp2112_write_byte(hd, SI114X_REG_PS_LED21, 0x32))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
361 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
362
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
363 if (cp2112_write_param_byte(hd, SI114X_PARAM_PS_ADC_MISC, 0x00 | 0x04))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
364 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
365
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
366 if (cp2112_write_param_byte(hd, SI114X_PARAM_PSLED12_SELECT, 0x21))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
367 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
368
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
369 if (cp2112_write_param_byte(hd, SI114X_PARAM_PSLED3_SELECT, 0x04))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
370 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
371
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
372 if (cp2112_write_param_byte(hd, SI114X_PARAM_CHLIST,
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
373 SI114X_CHLIST_EN_ALSVIS |
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
374 SI114X_CHLIST_EN_PS3 |
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
375 SI114X_CHLIST_EN_PS2 |
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
376 SI114X_CHLIST_EN_PS1))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
377 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
378
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
379 if (cp2112_write_param_byte(hd, SI114X_PARAM_PS_ADC_GAIN, 0x01))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
380 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
381
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
382 if (cp2112_write_param_byte(hd, SI114X_PARAM_PS_ADC_COUNTER, 0x06 << 4))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
383 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
384
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
385 /* in autonomous mode, wakeup every 100 ms */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
386 if (cp2112_write_byte(hd, SI114X_REG_MEAS_RATE, SI114X_MEAS_RATE_100MS))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
387 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
388
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
389 /* measure ALS every time device wakes up */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
390 if (cp2112_write_byte(hd, SI114X_REG_ALS_RATE, SI114X_ALS_RATE_1X))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
391 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
392
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
393 /* measure proximity every time device wakes up */
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
394 if (cp2112_write_byte(hd, SI114X_REG_PS_RATE, SI114X_PS_RATE_1X))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
395 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
396
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
397 if (cp2112_write_byte(hd, SI114X_REG_COMMAND, SI114X_CMD_PSALS_AUTO))
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
398 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
399
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
400 cp2112_is_idle(hd);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
401
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
402 if (cp2112_config_gpio(hd) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
403 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
404
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
405 while (1) {
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
406 unsigned short als, ps1, ps2, ps3;
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
407 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_DOWN) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
408 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
409 usleep(50*1000);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
410 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_RIGHT) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
411 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
412 usleep(50*1000);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
413 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_UP) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
414 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
415 usleep(50*1000);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
416 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_LEFT) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
417 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
418 usleep(50*1000);
2
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
419
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
420 if (cp2112_read_word(hd, SI114X_REG_ALSVIS_DATA0, &als) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
421 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
422 if (cp2112_read_word(hd, SI114X_REG_PS1_DATA0, &ps1) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
423 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
424 if (cp2112_read_word(hd, SI114X_REG_PS2_DATA0, &ps2) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
425 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
426 if (cp2112_read_word(hd, SI114X_REG_PS3_DATA0, &ps3) < 0)
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
427 exit(EXIT_FAILURE);
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
428
95b8b8a0b2d0 add proximity measurement
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
429 printf("%d %d %d %d\n", ps1, ps2, ps3, als);
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
430 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
431
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
432 /* never get here */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
433 printf("done\n");
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
434
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
435 hid_close(hd);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
436 hid_exit();
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
437
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
438 return EXIT_SUCCESS;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
439 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.