annotate hid-usb-to-ir.c @ 0:c6e3b362f9cf v0.2

import
author Peter Meerwald <p.meerwald@bct-electronic.com>
date Thu, 03 Jan 2013 13:08:39 +0100
parents
children 95b8b8a0b2d0
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
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
31 #define SI114X_HWKEY 0x07
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
32 #define SI114x_PART_ID 0x00
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
33 #define SI114x_REV_ID 0x01
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
34 #define SI114x_SEQ_ID 0x02
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
35
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
36 #define CP2112_GETSET_GPIO_CONFIG 0x02
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
37 #define CP2112_GET_GPIO 0x03
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
38 #define CP2112_SET_GPIO 0x04
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
39 #define CP2122_GET_VERSION_INFO 0x05
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
40 #define CP2112_GETSET_SMBUS_CONFIG 0x06
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
41 #define CP2112_DATA_WRITE_READ_REQ 0x11
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
42 #define CP2112_DATA_READ_RESP 0x13
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
43 #define CP2112_DATA_WRITE 0x14
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
44 #define CP2112_TRANSFER_STATUS_REQ 0x15
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
45 #define CP2112_TRANSFER_STATUS_RESP 0x16
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
46
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
47 #define CP2112_LED_DOWN 0x01 /* LED DS2, GPIO 0 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
48 #define CP2112_LED_UP 0x02 /* LED DS3, GPIO 1 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
49 #define CP2112_LED_RIGHT 0x08 /* LED DS8, GPIO 3 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
50 #define CP2112_LED_LEFT 0x80 /* LED DS4, GPIO 7 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
51 #define CP2112_LEDS_MASK (CP2112_LED_DOWN | CP2112_LED_UP | \
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
52 CP2112_LED_RIGHT | CP2112_LED_LEFT)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
53
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
54 /* get cp2112 part number and version */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
55 static int cp2112_get_version(hid_device *hd, unsigned char data[2]) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
56 unsigned char buf[3] = { CP2122_GET_VERSION_INFO, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
57
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
58 int ret = hid_get_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
59 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
60 fprintf(stderr, "hid_get_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
61 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
62 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
63 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
64
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
65 data[0] = buf[1]; /* part number */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
66 data[1] = buf[2]; /* version */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
67
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
68 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
69 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
70
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
71 /* configures cp2112 to automatically send read data, see AN495 section 4.6 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
72 static int cp2112_set_auto_send_read(hid_device *hd, int on_off) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
73 unsigned char buf[14] = { CP2112_GETSET_SMBUS_CONFIG, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
74 int ret = hid_get_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
75 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
76 fprintf(stderr, "hid_get_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
77 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
78 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
79 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
80
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
81 buf[6] = on_off;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
82
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
83 ret = hid_send_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
84 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
85 fprintf(stderr, "hid_send_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
86 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
87 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
88 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
89
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
90 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
91 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
92
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
93 /* see if the cp2112 is idle */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
94 static int cp2112_is_idle(hid_device *hd) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
95 unsigned char status_req[2] = { CP2112_TRANSFER_STATUS_REQ, 0x01, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
96 if (hid_write(hd, status_req, sizeof(status_req)) < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
97 fprintf(stderr, "hid_write() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
98 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
99 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
100 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
101
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
102 unsigned char status_resp[7] = { 0x00, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
103 if (hid_read(hd, status_resp, sizeof(status_resp)) < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
104 fprintf(stderr, "hid_read() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
105 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
106 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
107 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
108
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
109 if (status_resp[0] == CP2112_TRANSFER_STATUS_RESP) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
110 if (status_resp[1] == 0x00) /* is idle? */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
111 return 1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
112 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
113
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
114 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
115 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
116
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
117 /* write a data byte to register reg of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
118 static int cp2112_write_byte(hid_device *hd, unsigned char reg, unsigned char data) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
119 /* 0x5a is the 7-bit SMBus slave address of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
120 unsigned char buf_out[5] = { CP2112_DATA_WRITE, SI114X_SLAVE_ADDR<<1, 0x02, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
121 buf_out[3] = reg;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
122 buf_out[4] = data;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
123 int ret = hid_write(hd, buf_out, sizeof(buf_out));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
124 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
125 fprintf(stderr, "hid_write() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
126 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
127 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
128 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
129
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
130 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
131 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
132
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
133 /* read a data byte from register reg of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
134 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
135 /* 0x5a is the 7-bit SMBus slave address of the si1143 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
136 unsigned char buf_out[6] = { CP2112_DATA_WRITE_READ_REQ,
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
137 SI114X_SLAVE_ADDR<<1, 0x00, 0x01, 0x01, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
138 buf_out[5] = reg;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
139 int ret = hid_write(hd, buf_out, sizeof(buf_out));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
140 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
141 fprintf(stderr, "hid_write() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
142 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
143 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
144 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
145
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
146 /* FIXME: may loop forever, error handling needed */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
147 while (1) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
148
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
149 unsigned char buf_in[8] = { 0x00, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
150 ret = hid_read(hd, buf_in, sizeof(buf_in));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
151 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
152 fprintf(stderr, "hid_read_() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
153 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
154 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
155 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
156
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
157
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
158 if (buf_in[0] == CP2112_TRANSFER_STATUS_REQ)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
159 continue;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
160
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
161 if (buf_in[0] == CP2112_DATA_READ_RESP) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
162 if (buf_in[1] == 0x02 && buf_in[2] == 0x00)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
163 continue; /* no data yet */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
164 if (buf_in[1] == 0x02 && buf_in[2] == 0x01) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
165 data[0] = buf_in[3]; /* data available */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
166 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
167 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
168 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
169 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
170
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
171 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
172 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
173
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
174 /* configure cp2112 GPIO pins */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
175 static int cp2112_config_gpio(hid_device *hd) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
176 unsigned char buf[5] = { CP2112_GETSET_GPIO_CONFIG, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
177 int ret = hid_get_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
178 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
179 fprintf(stderr, "hid_get_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
180 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
181 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
182 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
183
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
184 buf[1] = 0x8b; /* output direction for GPIOs 0, 1, 3, 7 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
185 buf[2] = 0x8b; /* push-pull for GPIOs 0, 1, 3, 7 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
186 buf[3] = 0x00; /* no special functions, i.e. use pins as GPIO */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
187
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
188 ret = hid_send_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
189 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
190 fprintf(stderr, "hid_send_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
191 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
192 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
193 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
194
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
195 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
196 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
197
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
198 /* set cp2112 GPIO pins */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
199 static int cp2112_set_gpio(hid_device *hd, unsigned char mask, unsigned char value) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
200 unsigned char buf[3] = { CP2112_SET_GPIO, };
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
201
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
202 buf[1] = ~value; /* set GPIO pins: 0 .. turn LED on, 1 .. off */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
203 buf[2] = mask; /* mask of pins to set, ignore others */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
204
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
205 int ret = hid_send_feature_report(hd, buf, sizeof(buf));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
206 if (ret < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
207 fprintf(stderr, "hid_send_feature_report() failed: %ls\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
208 hid_error(hd));
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
209 return -1;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
210 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
211
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
212 return 0;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
213 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
214
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
215 int main() {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
216 if (hid_init() < 0) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
217 fprintf(stderr, "hid_init() failed, exit.\n");
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
218 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
219 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
220
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
221 /* open Silabs IR Gesture USB reference design by USB product:vendor id */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
222 hid_device *hd = hid_open(0x10c4, 0xea90, NULL);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
223 if (hd == NULL) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
224 fprintf(stderr, "hid_open() failed\n");
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
225 exit(EXIT_FAILURE);
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 unsigned char version_data[2];
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
229 if (cp2112_get_version(hd, version_data) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
230 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
231 printf("cp2112 part number 0x%02x, device version %d\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
232 version_data[0], version_data[1]);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
233
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
234 if (cp2112_set_auto_send_read(hd, 1) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
235 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
236
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
237 /* write 0x17 to si114x HW_KEY register as per datasheet */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
238 if (cp2112_write_byte(hd, SI114X_HWKEY, 0x17) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
239 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
240
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
241 unsigned char part, rev, seq;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
242 if (cp2112_read_byte(hd, SI114x_PART_ID, &part) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
243 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
244
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
245 if (cp2112_read_byte(hd, SI114x_REV_ID, &rev) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
246 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
247
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
248 if (cp2112_read_byte(hd, SI114x_SEQ_ID, &seq) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
249 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
250 printf("si114x part number 0x%02x, revision 0x%02x, sequencer 0x%02x\n",
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
251 part, rev, seq);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
252
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
253 cp2112_is_idle(hd);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
254
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
255 if (cp2112_config_gpio(hd) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
256 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
257
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
258 while (1) {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
259 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_DOWN) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
260 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
261 usleep(50*1000);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
262 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_RIGHT) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
263 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
264 usleep(50*1000);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
265 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_UP) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
266 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
267 usleep(50*1000);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
268 if (cp2112_set_gpio(hd, CP2112_LEDS_MASK, CP2112_LED_LEFT) < 0)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
269 exit(EXIT_FAILURE);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
270 usleep(50*1000);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
271 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
272
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
273 /* never get here */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
274 printf("done\n");
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
275
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
276 hid_close(hd);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
277 hid_exit();
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
278
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
279 return EXIT_SUCCESS;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
280 }

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