summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Li2017-07-05 19:35:32 -0500
committerPeter Li2017-07-05 19:35:32 -0500
commit95e4ac43427e108c5b6110b66d64b515c99430ef (patch)
tree219b8a288744add95464273bd55eb9e3a309ae82
downloaddrv260x-util-95e4ac43427e108c5b6110b66d64b515c99430ef.tar.gz
drv260x-util-95e4ac43427e108c5b6110b66d64b515c99430ef.tar.xz
drv260x-util-95e4ac43427e108c5b6110b66d64b515c99430ef.zip
first commit
-rwxr-xr-xhaptics/Android.mk19
-rwxr-xr-xhaptics/CleanSpec.mk49
-rwxr-xr-xhaptics/haptics.h55
-rwxr-xr-xhaptics/main.c1119
4 files changed, 1242 insertions, 0 deletions
diff --git a/haptics/Android.mk b/haptics/Android.mk
new file mode 100755
index 0000000..cf19f9d
--- /dev/null
+++ b/haptics/Android.mk
@@ -0,0 +1,19 @@
1ifneq ($(TARGET_SIMULATOR), true)
2
3LOCAL_PATH := $(call my-dir)
4
5include $(CLEAR_VARS)
6
7LOCAL_SRC_FILES := main.c
8
9LOCAL_C_INCLUDES := external/haptics/
10
11LOCAL_CFLAGS := -O2 -g -W -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
12
13LOCAL_MODULE := haptics
14LOCAL_MODULE_TAGS := debug
15LOCAL_SYSTEM_SHARED_LIBRARIES := libc
16
17include $(BUILD_EXECUTABLE)
18
19endif
diff --git a/haptics/CleanSpec.mk b/haptics/CleanSpec.mk
new file mode 100755
index 0000000..b84e1b6
--- /dev/null
+++ b/haptics/CleanSpec.mk
@@ -0,0 +1,49 @@
1# Copyright (C) 2007 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16# If you don't need to do a full clean build but would like to touch
17# a file or delete some intermediate files, add a clean step to the end
18# of the list. These steps will only be run once, if they haven't been
19# run before.
20#
21# E.g.:
22# $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
23# $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
24#
25# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
26# files that are missing or have been moved.
27#
28# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
29# Use $(OUT_DIR) to refer to the "out" directory.
30#
31# If you need to re-do something that's already mentioned, just copy
32# the command and add it to the bottom of the list. E.g., if a change
33# that you made last week required touching a file and a change you
34# made today requires touching the same file, just copy the old
35# touch step and add it to the end of the list.
36#
37# ************************************************
38# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
39# ************************************************
40
41# For example:
42#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
43#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
44#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
45#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
46
47# ************************************************
48# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
49# ************************************************
diff --git a/haptics/haptics.h b/haptics/haptics.h
new file mode 100755
index 0000000..afccb1d
--- /dev/null
+++ b/haptics/haptics.h
@@ -0,0 +1,55 @@
1/*
2 * 2013 @ Texas Intruments, Inc.
3 *
4*/
5
6#define HAPTICS_VERSION "Version : 2.02 (30th, May, 2014)"
7
8enum Haptic_Target{
9 HAPTIC_DRVXXXX,
10 HAPTIC_DRV2604,
11 HAPTIC_DRV2604L,
12 HAPTIC_DRV2605,
13 HAPTIC_DRV2605L,
14 HAPTIC_DRV2667,
15 HAPTIC_DRV2669
16};
17
18#define FIFO_PBK_BYTES_LEN 100
19#define DRV2604_RAM_SIZE (2*1024)
20#define MAX_READ_BYTES 0xff
21
22/* Commands */
23#define HAPTIC_CMDID_PLAY_SINGLE_EFFECT 0x01
24#define HAPTIC_CMDID_PLAY_EFFECT_SEQUENCE 0x02
25#define HAPTIC_CMDID_PLAY_TIMED_EFFECT 0x03
26#define HAPTIC_CMDID_GET_DEV_ID 0x04
27#define HAPTIC_CMDID_RUN_DIAG 0x05
28#define HAPTIC_CMDID_AUDIOHAPTIC_ENABLE 0x06
29#define HAPTIC_CMDID_AUDIOHAPTIC_DISABLE 0x07
30#define HAPTIC_CMDID_AUDIOHAPTIC_GETSTATUS 0x08
31
32#define HAPTIC_CMDID_REG_WRITE 0x09
33#define HAPTIC_CMDID_REG_READ 0x0a
34#define HAPTIC_CMDID_REG_SETBIT 0x0b
35#define HAPTIC_CMDID_PATTERN_RTP 0x0c
36#define HAPTIC_CMDID_RTP_SEQUENCE 0x0d
37#define HAPTIC_CMDID_PLAY_FIFO 0x0e
38#define HAPTIC_CMDID_ANALOG_INPUT 0x0f
39#define HAPTIC_CMDID_GET_EFFECT_COUNT 0x10
40#define HAPTIC_CMDID_UPDATE_FIRMWARE 0x11
41#define HAPTIC_CMDID_READ_FIRMWARE 0x12
42#define HAPTIC_CMDID_STOP 0xFF
43
44/* Command size */
45#define HAPTIC_CMDSZ_SINGLE_EFFECT 2
46#define HAPTIC_CMDSZ_EFFECT_SEQUENCE 9
47#define HAPTIC_CMDSZ_TIMED_EFFECT 3
48#define HAPTIC_CMDSZ_STOP 1
49
50#define MAX_TIMEOUT 10000 /* 10s */
51#define REGS_TOTAL 0x23
52
53#define RTP_DURATION_MAX 10000 //ms
54
55#define ARRAY_LEN(x) ((int)(sizeof(x)/sizeof((x)[0])))
diff --git a/haptics/main.c b/haptics/main.c
new file mode 100755
index 0000000..40c3765
--- /dev/null
+++ b/haptics/main.c
@@ -0,0 +1,1119 @@
1/*
2 * main.c
3 *
4 * device driver test tool for Haptics devices
5 *
6 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
7 * ALL RIGHTS RESERVED
8 *
9*/
10
11#include <sys/cdefs.h>
12#include <sys/stat.h>
13#include <stdlib.h>
14#include <string.h>
15#include <ctype.h>
16#include <stdio.h>
17#include <unistd.h>
18#include <errno.h>
19#include <stdarg.h>
20#include <fcntl.h>
21#include "haptics.h"
22
23const char *haptic_devices[] = {
24 "/dev/drvxxxx",
25 "/dev/drv2604",
26 "/dev/drv2604L",
27 "/dev/drv2605",
28 "/dev/drv2605L",
29 "/dev/drv2667",
30 "/dev/drv2669"
31 };
32
33const char *target_str[] = {
34 "DRVXXXX",
35 "DRV2604",
36 "DRV2604L",
37 "DRV2605",
38 "DRV2605L",
39 "DRV2667",
40 "DRV2669"
41 };
42
43static void usage(enum Haptic_Target target){
44 if(target == HAPTIC_DRV2667){
45 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
46 "usage: haptics -r Reg_Addr1 [count] (read)",
47 " haptics -w Reg_Addr Value1 Value2 ... (write)",
48 " haptics -s Reg_Addr1 Mask1 Value1 Reg_Addr2 Mask2 Value2 ... (set bit)",
49 " haptics -e effect1 effect2... (play effect sequence)",
50 " haptics -v OnTime (vibrate for OnTime ms, maximum 0x2710(10000ms))",
51 " haptics -f fifo.bin (play binary from fifo.bin)",
52 " haptics -a (analog input mode enable)",
53 " haptics -p (stop any vibration)",
54 " haptics -d (dump all the registers)",
55 " haptics -t (get target, DRVXXXX/DRV2604/DRV2605/DRV2667/DRV2669)",
56 "NOTE: 1. all numbers are Hexadecimal numbers without prefix 0x .",
57 "NOTE: 2. Reg_Addr/OnTime is UInt16, others are all UInt8 .",
58 HAPTICS_VERSION);
59 }else if(target == HAPTIC_DRV2669){
60 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\t%s\n",
61 "usage: haptics -r Reg_Addr1 [count] (read)",
62 " haptics -w Reg_Addr Value1 Value2 ... (write)",
63 " haptics -s Reg_Addr1 Mask1 Value1 Reg_Addr2 Mask2 Value2 ... (set bit)",
64 " haptics -e effect1 effect2... (play effect sequence)",
65 " haptics -v OnTime (vibrate for OnTime ms, maximum 0x2710(10000ms))",
66 " haptics -f fifo.bin (play binary from fifo.bin)",
67 " haptics -p (stop any vibration)",
68 " haptics -d (dump all the registers)",
69 " haptics -t (get target, DRVXXXX/DRV2604/DRV2605/DRV2667/DRV2669)",
70 "NOTE: 1. all numbers are Hexadecimal numbers without prefix 0x .",
71 "NOTE: 2. Reg_Addr/OnTime is UInt16, others are all UInt8 .",
72 HAPTICS_VERSION);
73 }else if((target == HAPTIC_DRV2605)||(target == HAPTIC_DRV2605L)){
74 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\t%s\n\t%s\n",
75 "usage: haptics -r Reg_Addr [count] (read)",
76 " haptics -w Reg_Addr Value1 [Value2 ...] (write)",
77 " haptics -s Reg_Addr1 Mask1 Value1 [Reg_Addr2 Mask2 Value2 ...](set bit)",
78 " haptics -e effect1 [effect2...] (play effect sequence)",
79 " haptics -v OnTime (vibrate for OnTime ms, maximum 0x2710(10000ms))",
80 " haptics -p (stop any vibration)",
81 " haptics -q Amp1 Time1 [Amp2 Time2 ...] (RTP seq playback)",
82 " haptics -n OnTime OffTime Strength Repeat (Pattern RTP playback)",
83 " haptics -a enable (enable/disable audio2haptics)",
84 " haptics -d (dump all the registers)",
85 " haptics -t (target, DRVXXXX/DRV2604(L)/DRV2605(L)/DRV2667/DRV2669)",
86 "NOTE: 1. all numbers are Hex(UInt8) without prefix 0x(except OnTime) .",
87 HAPTICS_VERSION);
88 }else if((target == HAPTIC_DRV2604)||(target == HAPTIC_DRV2604L)){
89 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\t%s\n\t%s\n",
90 "usage: haptics -r Reg_Addr [count] (read)",
91 " haptics -w Reg_Addr Value1 [Value2 ...] (write)",
92 " haptics -s Reg_Addr1 Mask1 Value1 [Reg_Addr2 Mask2 Value2 ...](set bit)",
93 " haptics -e effect1 [effect2...] (play effect sequence)",
94 " haptics -v OnTime (vibrate for OnTime ms, maximum 0x2710(10000ms))",
95 " haptics -p (stop any vibration)",
96 " haptics -q Amp1 Time1 [Amp2 Time2 ...] (RTP seq playback)",
97 " haptics -n OnTime OffTime Strength Repeat (Pattern RTP playback)",
98 " haptics -f fw.bin (update firmware)",
99 " haptics -b addr_uint16 [count] (read back firmware)",
100 " haptics -d (dump all the registers)",
101 " haptics -t (get target, DRVXXXX/DRV2604/DRV2605/DRV2667/DRV2669)",
102 "NOTE: 1. all numbers are Hex(UInt8) without prefix 0x(except OnTime) .",
103 HAPTICS_VERSION);
104 }else{
105 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\t%s\n",
106 "usage: haptics -r Reg_Addr [count] (read)",
107 " haptics -w Reg_Addr Value1 [Value2 ...] (write)",
108 " haptics -s Reg_Addr1 Mask1 Value1 [Reg_Addr2 Mask2 Value2 ...](set bit)",
109 " haptics -e effect1 [effect2...] (play effect sequence)",
110 " haptics -v OnTime (vibrate for OnTime ms, maximum 0x2710(10000ms))",
111 " haptics -p (stop any vibration)",
112 " haptics -q Amp1 Time1 [Amp2 Time2 ...] (RTP seq playback)",
113 " haptics -n OnTime OffTime Strength Repeat (Pattern RTP playback)",
114 " haptics -t (get target, DRVXXXX/DRV2604/DRV2605/DRV2667/DRV2669)",
115 "NOTE: 1. all numbers are Hex(UInt8) without prefix 0x(except OnTime) .",
116 HAPTICS_VERSION);
117 }
118}
119
120static int str2char(char *argv, unsigned char *pUInt8_Val){
121 int str_len = strlen(argv), i, result = -1, j;
122 unsigned char val[2] = {0};
123
124 if(str_len > 2){
125 fprintf(stderr, "invalid parameters\n");
126 goto err;
127 }
128
129 for(i = (str_len-1), j=0; i >= 0; i--, j++){
130 if((argv[i] <= '9')&&(argv[i] >= '0')){
131 val[j] = argv[i] - 0x30;
132 }else if((argv[i] <='f')&&(argv[i]>= 'a')){
133 val[j] = argv[i] - 0x57;
134 }else if((argv[i] <='F')&&(argv[i]>= 'A')){
135 val[j] = argv[i] - 0x37;
136 }else{
137 fprintf(stderr, "reg/data out of range\n");
138 goto err;
139 }
140 }
141
142 *pUInt8_Val = (unsigned char)(val[0]|(val[1]<<4));
143 result = 0;
144
145err:
146 return result;
147}
148
149static int str2short(char *argv, unsigned short *pUInt16_Val){
150 int str_len = strlen(argv), i, j, result = -1;
151 unsigned char val[4] = {0};
152
153 if(str_len > 4){
154 fprintf(stderr, "invalid parameters\n");
155 goto err;
156 }
157
158 for(i = (str_len-1), j=0; i >= 0; i--, j++){
159 if((argv[i] <= '9')&&(argv[i] >= '0')){
160 val[j] = argv[i] - 0x30;
161 }else if((argv[i] <='f')&&(argv[i]>= 'a')){
162 val[j] = argv[i] - 0x57;
163 }else if((argv[i] <='F')&&(argv[i]>= 'A')){
164 val[j] = argv[i] - 0x37;
165 }else{
166 fprintf(stderr, "reg/data out of range\n");
167 goto err;
168 }
169 }
170
171 *pUInt16_Val = (unsigned short)(val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12));
172 result = 0;
173
174err:
175 return result;
176}
177
178static int DRV266x_Reg_Write(int fileHandle, int argc, char **argv){
179 int err = -1;
180 unsigned short regaddr_UInt16;
181 unsigned char *pBuff = NULL, regval_UInt8;
182 int i=0, reg_count = 0;
183
184 if(argc < 4){
185 fprintf(stderr, "invalid para numbers\n");
186 goto err;
187 }
188
189 reg_count = (argc - 3);
190
191 pBuff = (unsigned char *)malloc(sizeof(unsigned char)*reg_count+3);
192 if(pBuff == NULL){
193 fprintf(stderr, "not enough mem\n");
194 goto err;
195 }
196
197 pBuff[0] = HAPTIC_CMDID_REG_WRITE;
198 err = str2short(argv[2], &regaddr_UInt16);
199 if(err < 0){
200 goto err;
201 }
202
203 pBuff[1] = (unsigned char)(regaddr_UInt16&0x00ff);
204 pBuff[2] = (unsigned char)((regaddr_UInt16&0xff00)>>8);
205
206 for(i=0 ; i< reg_count; i++){
207 err = str2char(argv[i+3], &regval_UInt8);
208 if(err < 0){
209 goto err;
210 }
211 pBuff[3 + i] = regval_UInt8;
212 }
213
214 err = write(fileHandle, pBuff, reg_count+3);
215 if(err != (reg_count+3)){
216 fprintf(stderr, "write err=%d\n", err);
217 }else{
218 fprintf(stderr, "WRegs(0x%x)=", regaddr_UInt16);
219 for(i=0; i< reg_count; i++){
220 fprintf(stderr, "0x%x ", pBuff[3+i]);
221 }
222 fprintf(stderr, "\n");
223 }
224
225err:
226 if(pBuff != NULL)
227 free(pBuff);
228
229 return err;
230}
231
232static int DRV260x_Reg_Write(int fileHandle, int argc, char **argv){
233 int err = -1;
234 unsigned char *pBuff = NULL, regaddr_UInt8, regval_UInt8;
235 int i=0, reg_count = 0;
236
237 if(argc < 4){
238 fprintf(stderr, "invalid para numbers\n");
239 goto err;
240 }
241
242 reg_count = argc - 3;
243
244 pBuff = (unsigned char *)malloc(reg_count+2);
245 if(pBuff == NULL){
246 fprintf(stderr, "not enough mem\n");
247 goto err;
248 }
249
250 pBuff[0] = HAPTIC_CMDID_REG_WRITE;
251
252 err = str2char(argv[2], &regaddr_UInt8);
253 if(err < 0){
254 goto err;
255 }
256 pBuff[1] = regaddr_UInt8;
257
258 for(i=0; i< reg_count; i++){
259 err = str2char(argv[i+3], &regval_UInt8);
260 if(err < 0){
261 goto err;
262 }
263 pBuff[i + 2] = regval_UInt8;
264 }
265
266 err = write(fileHandle, pBuff, reg_count+2);
267 if(err != (reg_count+2)){
268 fprintf(stderr, "write err=%d\n", err);
269 }else{
270 for(i=0; i< reg_count; i++){
271 fprintf(stderr, "WReg(0x%x)=0x%x\n", regaddr_UInt8+i, pBuff[2+i]);
272 }
273 }
274
275err:
276 if(pBuff != NULL)
277 free(pBuff);
278
279 return err;
280}
281
282static int DRV266x_Reg_Read(int fileHandle, int argc, char **argv){
283 int err = 0;
284 unsigned char Buff[4], RegCount_UInt8;
285 unsigned char *pRead_Buff = NULL;
286 unsigned short regAddr_UInt16;
287 int i=0;
288
289 if((argc < 3)||(argc > 4)){
290 fprintf(stderr, "invalid para numbers\n");
291 goto err;
292 }
293
294 Buff[0] = HAPTIC_CMDID_REG_READ;
295 err = str2short(argv[2], &regAddr_UInt16);
296 if(err < 0){
297 fprintf(stderr, "addr err=%d\n", err);
298 goto err;
299 }
300
301 Buff[1] = (unsigned char)(regAddr_UInt16&0x00ff);
302 Buff[2] = (unsigned char)((regAddr_UInt16&0xff00)>>8);
303
304 if(argc == 4){
305 err = str2char(argv[3], &RegCount_UInt8);
306 if(err < 0){
307 fprintf(stderr, "count err=%d\n", err);
308 goto err;
309 }
310 }else{
311 RegCount_UInt8 = 1;
312 }
313
314 Buff[3] = RegCount_UInt8;
315
316 err = write(fileHandle, Buff, 4);
317 if(err != 4){
318 fprintf(stderr, "send read err=%d\n", err);
319 goto err;
320 }
321
322 pRead_Buff = (unsigned char *)malloc(RegCount_UInt8);
323 if(pRead_Buff == NULL){
324 fprintf(stderr, "not enough mem\n");
325 goto err;
326 }
327
328 err = read(fileHandle, pRead_Buff, RegCount_UInt8);
329 if(err != (RegCount_UInt8)){
330 fprintf(stderr, "read back err=%d\n", err);
331 }else{
332 for(i=0; i< RegCount_UInt8; i++){
333 fprintf(stderr, "RReg[0x%x]=0x%x\n", regAddr_UInt16+i, pRead_Buff[i]);
334 }
335 }
336
337err:
338 if(pRead_Buff != NULL)
339 free(pRead_Buff);
340
341 return err;
342}
343
344static int DRV260x_Reg_Read(int fileHandle, int argc, char **argv){
345 int err = 0;
346 unsigned char pBuff[3], RegAddr_UInt8=0, RegCount_UInt8 = 1;
347 int i=0;
348 unsigned char *pRead_Buff = NULL;
349
350 if(argc < 3){
351 fprintf(stderr, "invalid para numbers\n");
352 goto err;
353 }
354
355 pBuff[0] = HAPTIC_CMDID_REG_READ;
356
357 err = str2char(argv[2], &RegAddr_UInt8);
358 if(err < 0){
359 goto err;
360 }
361 pBuff[1] = RegAddr_UInt8;
362
363 if(argc > 3){
364 err = str2char(argv[3], &RegCount_UInt8);
365 if(err < 0){
366 goto err;
367 }
368 }
369
370 pBuff[2] = RegCount_UInt8;
371
372 pRead_Buff = (unsigned char *)malloc(RegCount_UInt8);
373 if(pRead_Buff == NULL){
374 fprintf(stderr, "not enough mem\n");
375 goto err;
376 }
377
378 err = write(fileHandle, pBuff, 3);
379 if(err != 3){
380 fprintf(stderr, "send read err=%d\n", err);
381 goto err;
382 }
383
384 err = read(fileHandle, pRead_Buff, RegCount_UInt8 );
385 if(err != RegCount_UInt8){
386 fprintf(stderr, "read back err=%d\n", err);
387 }else{
388 for(i=0; i< RegCount_UInt8; i++){
389 fprintf(stderr, "RReg[0x%x] = 0x%x\n", RegAddr_UInt8+i, pRead_Buff[i]);
390 }
391 }
392
393err:
394 if(pRead_Buff != NULL)
395 free(pRead_Buff);
396
397 return err;
398}
399
400static int DRV266x_Reg_SetBit(int fileHandle, int argc, char **argv){
401 int err = 0;
402 unsigned char *pBuff = NULL, regMask_UInt8, regVal_UInt8;
403 int i=0, j, reg_count;
404 unsigned short regAddr_UInt16;
405
406 if((argc < 5)||((argc-2)%3 != 0)){
407 fprintf(stderr, "invalid para numbers\n");
408 goto err;
409 }
410
411 reg_count = (argc - 2)/3;
412
413 pBuff = (unsigned char *)malloc(reg_count*4 + 1);
414 if(pBuff == NULL){
415 fprintf(stderr, "not enough mem\n");
416 goto err;
417 }
418
419 pBuff[0] = HAPTIC_CMDID_REG_SETBIT;
420
421 for(i=0, j =0; i< reg_count; i++ , j++){
422 err = str2short(argv[j*3 + 2], &regAddr_UInt16);
423 if(err < 0){
424 goto err;
425 }
426
427 err = str2char(argv[j*3 + 3], &regMask_UInt8);
428 if(err < 0){
429 goto err;
430 }
431
432 err = str2char(argv[j*3 + 4], &regVal_UInt8);
433 if(err < 0){
434 goto err;
435 }
436
437 pBuff[i*4+1] = (unsigned char)(regAddr_UInt16&0x00ff);
438 pBuff[i*4+2] = (unsigned char)((regAddr_UInt16&0xff00)>>8);
439 pBuff[i*4+3] = regMask_UInt8;
440 pBuff[i*4+4] = regVal_UInt8;
441 }
442
443 err = write(fileHandle, pBuff, reg_count*4 + 1);
444 if(err != (reg_count*4 + 1)){
445 fprintf(stderr, "setbit err=%d\n", err);
446 }else{
447 for(i=0; i<reg_count; i++){
448 regAddr_UInt16 = (unsigned short)((pBuff[i*4+2]<<8)|pBuff[i*4+1] );
449 fprintf(stderr, "SReg(0x%x) Msk(0x%x) Val=0x%x\n", regAddr_UInt16, pBuff[i*4+3], pBuff[i*4+4]);
450 }
451 }
452
453err:
454 if(pBuff != NULL)
455 free(pBuff);
456
457 return err;
458}
459
460static int DRV260x_Reg_SetBit(int fileHandle, int argc, char **argv){
461 int err = 0;
462 unsigned char *pBuff = NULL, regAddr_UInt8, regMask_UInt8, regVal_UInt8;
463 int i=0, reg_count, j;
464
465 if((argc < 5)||((argc-2)%3 != 0)){
466 fprintf(stderr, "invalid para numbers\n");
467 goto err;
468 }
469
470 reg_count = (argc-2)/3;
471
472 pBuff = (unsigned char *)malloc(reg_count*3 + 1);
473 if(pBuff == NULL){
474 fprintf(stderr, "not enough mem\n");
475 goto err;
476 }
477
478 pBuff[0] = HAPTIC_CMDID_REG_SETBIT;
479
480 for(i=0, j=0; i< reg_count; i++, j++){
481 err = str2char(argv[j*3 + 2], &regAddr_UInt8);
482 if(err < 0){
483 goto err;
484 }
485 err = str2char(argv[j*3 + 3], &regMask_UInt8);
486 if(err < 0){
487 goto err;
488 }
489 err = str2char(argv[j*3 + 4], &regVal_UInt8);
490 if(err < 0){
491 goto err;
492 }
493
494 pBuff[i*3 + 1] = regAddr_UInt8;
495 pBuff[i*3 + 2] = regMask_UInt8;
496 pBuff[i*3 + 3] = regVal_UInt8;
497 }
498
499 err = write(fileHandle, pBuff, argc-1);
500 if(err != (argc-1)){
501 fprintf(stderr, "setbit err=%d\n", err);
502 }else{
503 for(i=0 ; i< reg_count; i++){
504 fprintf(stderr, "SReg(0x%x) Msk(0x%x) Val=0x%x\n", pBuff[i*3 + 1], pBuff[i*3+2], pBuff[i*3+3]);
505 }
506 }
507
508err:
509 if(pBuff != NULL)
510 free(pBuff);
511
512 return err;
513}
514
515static int DRV266x_Reg_Dump(int fileHandle){
516 char *argv_266x[] = {"haptics","-r", "0", "b"};
517 return DRV266x_Reg_Read(fileHandle, 4, argv_266x);
518}
519
520static int DRV260x_Reg_Dump(int fileHandle){
521 char *argv_260x[] = {"haptics","-r", "0", "23"};
522 return DRV260x_Reg_Read(fileHandle, 4, argv_260x);
523}
524
525static int DRV260x_Reg_RTPSeqPlay(int fileHandle, int argc, char **argv){
526 int err = 0;
527 unsigned char *pBuff = NULL, amp_UInt8, time_UInt8, count;
528 int i=0;
529
530 if((argc < 4)||( (argc%2) != 0)){
531 fprintf(stderr, "invalid para numbers\n");
532 goto err;
533 }
534
535 pBuff = (unsigned char *)malloc(argc-1);
536 if(pBuff == NULL){
537 fprintf(stderr, "not enough mem\n");
538 goto err;
539 }
540
541 pBuff[0] = HAPTIC_CMDID_RTP_SEQUENCE;
542 count = (argc-2)/2;
543
544 for(i=0; i< count; i++){
545 err = str2char(argv[i*2+2], &amp_UInt8);
546 if(err < 0){
547 goto err;
548 }
549 err = str2char(argv[i*2+3], &time_UInt8);
550 if(err < 0){
551 goto err;
552 }
553 pBuff[i*2+1] = amp_UInt8;
554 pBuff[i*2+2] = time_UInt8;
555 fprintf(stderr, "RTP[%d]:amp=0x%x,time=0x%x\n", i, amp_UInt8, time_UInt8 );
556 }
557
558 err = write(fileHandle, pBuff, count*2 +1);
559 if(err != (count*2 +1)){
560 fprintf(stderr, "write err=%d\n", err);
561 }else{
562
563 }
564
565err:
566 if(pBuff != NULL)
567 free(pBuff);
568
569 return err;
570
571}
572
573static int DRV26xx_SeqPlay(int fileHandle, int argc, char **argv){
574 int err = 0;
575 unsigned char *pBuff = NULL, seq_UInt8;
576 int i=0, seq_count;
577
578 if(argc < 3){
579 fprintf(stderr, "invalid para numbers\n");
580 goto err;
581 }
582
583 seq_count = argc - 2;
584
585 pBuff = (unsigned char *)malloc(seq_count + 1);
586 if(pBuff == NULL){
587 fprintf(stderr, "not enough mem\n");
588 goto err;
589 }
590
591 pBuff[0] = HAPTIC_CMDID_PLAY_EFFECT_SEQUENCE;
592
593 for(i=0; i< seq_count; i++){
594 err = str2char(argv[i+2], &seq_UInt8);
595 if(err < 0){
596 goto err;
597 }
598
599 pBuff[i+1] = seq_UInt8;
600 }
601
602 fprintf(stderr, "SeqPlay[%d]:", seq_count );
603 for(i=0; i< seq_count; i++)
604 fprintf(stderr, "%d ", pBuff[i+1]);
605 fprintf(stderr, "\n");
606
607 err = write(fileHandle, pBuff, seq_count + 1);
608 if(err != (seq_count + 1)){
609 fprintf(stderr, "write err=%d\n", err);
610 }
611
612err:
613 if(pBuff != NULL)
614 free(pBuff);
615
616 return err;
617}
618
619static int DRV26xx_VIBPlay(int fileHandle, int argc, char **argv){
620 int err = 0;
621 unsigned char Buff[3];
622 unsigned short OnTime;
623
624 if(argc < 3){
625 fprintf(stderr, "invalid para numbers\n");
626 goto err;
627 }
628
629 Buff[0] = HAPTIC_CMDID_PLAY_TIMED_EFFECT;
630 err = str2short(argv[2], &OnTime);
631 if(err < 0){
632 goto err;
633 }
634
635 if(OnTime > RTP_DURATION_MAX){
636 OnTime = RTP_DURATION_MAX;
637 }
638
639 Buff[1] = (unsigned char)(OnTime&0x00ff);
640 Buff[2] = (unsigned char)((OnTime&0xff00)>>8);
641
642 err = write(fileHandle, Buff, 3);
643 if(err != 3){
644 fprintf(stderr, "write err=%d\n", err);
645 }else{
646 fprintf(stderr, "VIBPlay %d ms\n", OnTime);
647 }
648
649err:
650
651 return err;
652}
653
654static int DRV260x_PatternRTPPlay(int fileHandle, int argc, char **argv){
655 int err = 0;
656 unsigned char Buff[7], strength=0, repeat=0;
657 unsigned short OnTime = 0, OffTime = 0;
658
659 if(argc < 6){
660 fprintf(stderr, "invalid para numbers\n");
661 goto err;
662 }
663
664 Buff[0] = HAPTIC_CMDID_PATTERN_RTP;
665 err = str2short(argv[2], &OnTime);
666 if(err < 0){
667 goto err;
668 }
669 if(OnTime > RTP_DURATION_MAX){
670 OnTime = RTP_DURATION_MAX;
671 }
672 Buff[1] = (unsigned char)(OnTime&0x00ff);
673 Buff[2] = (unsigned char)((OnTime&0xff00)>>8);
674
675 err = str2short(argv[3], &OffTime);
676 if(err < 0){
677 goto err;
678 }
679 Buff[3] = (unsigned char)(OffTime&0x00ff);
680 Buff[4] = (unsigned char)((OffTime&0xff00)>>8);
681
682 err = str2char(argv[4], &strength);
683 if(err < 0){
684 goto err;
685 }
686 Buff[5] = strength;
687
688 err = str2char(argv[5], &repeat);
689 if(err < 0){
690 goto err;
691 }
692 Buff[6] = repeat;
693
694 err = write(fileHandle, Buff, 7);
695 if(err != 7){
696 fprintf(stderr, "write err=%d\n", err);
697 }else{
698 fprintf(stderr, "Pattern Strength(0x%x), On(%d ms)-Off(%d ms), repeat %d\n", strength, OnTime, OffTime, repeat);
699 }
700
701err:
702 return err;
703}
704
705static int DRV266x_FIFOPlay(int fileHandle, int argc, char **argv){
706 int err = 0;
707 unsigned char *pBuff = NULL;
708 char *filename;
709 int fifoHandle = -1;
710 size_t filesize, readsize;
711 struct stat st;
712
713 if(argc < 3){
714 fprintf(stderr, "invalid para numbers\n");
715 goto err;
716 }
717
718 filename = argv[2];
719 err = stat(filename, &st);
720 if ( err < 0) {
721 fprintf(stderr, "file error\n");
722 goto err;
723 }
724
725 filesize = (st.st_size > FIFO_PBK_BYTES_LEN)?FIFO_PBK_BYTES_LEN:st.st_size;
726
727 pBuff = (unsigned char *)malloc(filesize + 1);
728 if (pBuff == NULL) {
729 fprintf(stderr, "not enough memory\n");
730 goto err;
731 }
732
733 pBuff[0] = HAPTIC_CMDID_PLAY_FIFO;
734
735 fifoHandle = open(filename, O_RDONLY);
736 if (fifoHandle < 0) {
737 fprintf(stderr, "fifo open error(%d)\n", fifoHandle);
738 goto err;
739 }
740
741 readsize = read(fifoHandle, &pBuff[1], filesize);
742 if ( readsize != filesize) {
743 fprintf(stderr, "fifo read error\n");
744 goto err;
745 }
746
747 err = write(fileHandle, pBuff, filesize + 1);
748 if(err != ((int)filesize + 1)){
749 fprintf(stderr, "write err=%d\n", err);
750 }else{
751 fprintf(stderr, "FIFOPlay OK\n");
752 }
753
754err:
755 if(pBuff != NULL)
756 free(pBuff);
757
758 if(fifoHandle > 0){
759 close(fifoHandle);
760 }
761
762 return err;
763}
764
765static int DRV2604_FirmwareUpdate(int fileHandle, int argc, char **argv){
766 int err = 0;
767 unsigned char *pBuff = NULL;
768 char *filename;
769 int fwhandle = -1;
770 size_t filesize, readsize;
771 struct stat st;
772
773 if(argc < 3){
774 fprintf(stderr, "invalid para numbers\n");
775 goto err;
776 }
777
778 filename = argv[2];
779 err = stat(filename, &st);
780 if ( err < 0) {
781 fprintf(stderr, "file error\n");
782 goto err;
783 }
784
785 if(st.st_size > DRV2604_RAM_SIZE){
786 filesize = DRV2604_RAM_SIZE;
787 }else{
788 filesize = st.st_size;
789 }
790
791 pBuff = (unsigned char *)malloc(filesize + 1);
792 if (pBuff == NULL) {
793 fprintf(stderr, "not enough memory\n");
794 goto err;
795 }
796
797 pBuff[0] = HAPTIC_CMDID_UPDATE_FIRMWARE;
798
799 fwhandle = open(filename, O_RDONLY);
800 if (fwhandle < 0) {
801 fprintf(stderr, "fw open error(%d)\n", fwhandle);
802 goto err;
803 }
804
805 readsize = read(fwhandle, &pBuff[1], filesize);
806 if ( readsize != filesize) {
807 fprintf(stderr, "fifo read error\n");
808 goto err;
809 }
810
811 err = write(fileHandle, pBuff, filesize + 1);
812 if(err != ((int)filesize + 1)){
813 fprintf(stderr, "write err=%d\n", err);
814 }else{
815 fprintf(stderr, "Firmware Update OK\n");
816 }
817
818err:
819 if(pBuff != NULL)
820 free(pBuff);
821
822 if(fwhandle > 0){
823 close(fwhandle);
824 }
825
826 return err;
827}
828
829static int DRV2604_ReadRam(int fileHandle, int argc, char **argv){
830 int err = 0, i;
831 unsigned char Buff[4];
832 unsigned char data[MAX_READ_BYTES];
833 unsigned char count = 0, send_count, read_count;
834 unsigned short addr;
835
836 if((argc != 3)&&(argc != 4)){
837 fprintf(stderr, "invalid para numbers\n");
838 goto err;
839 }
840
841 err = str2short(argv[2], &addr);
842 if ( err < 0) {
843 fprintf(stderr, "addr error\n");
844 goto err;
845 }
846
847 if(argc == 4){
848 err = str2char(argv[3], &count);
849 if ( err < 0) {
850 fprintf(stderr, "addr error\n");
851 goto err;
852 }
853 Buff[3] = count;
854 }
855
856 Buff[0] = HAPTIC_CMDID_READ_FIRMWARE;
857 Buff[1] = (unsigned char)addr;
858 Buff[2] = (unsigned char)(addr>>8);
859
860 send_count = (argc==4)?4:3;
861
862 err = write(fileHandle, Buff, send_count);
863 if(err != send_count){
864 fprintf(stderr, "read ram send err=%d\n", err);
865 goto err;
866 }
867
868 read_count = (argc==4)?count:1;
869 err = read(fileHandle, data, read_count);
870 if(err != read_count){
871 fprintf(stderr, "read ram get err=%d\n", err);
872 goto err;
873 }
874
875 for(i=0; i < read_count; i++){
876 fprintf(stderr, "DRV2604 RAM[0x%x]=0x%x\n", addr + i, data[i]);
877 }
878
879err:
880 return err;
881}
882
883static int DRV26xx_StopPlay(int fileHandle){
884 int err = 0;
885 unsigned char Buff;
886
887 Buff = HAPTIC_CMDID_STOP;
888
889 err = write(fileHandle, &Buff, 1);
890 if(err != 1){
891 fprintf(stderr, "write err=%d\n", err);
892 }else{
893 fprintf(stderr, "Stop Vibration\n");
894 }
895
896 return err;
897}
898
899static int DRV2667_AnalogInput(int fileHandle){
900 int err = 0;
901 unsigned char Buff;
902
903 Buff = HAPTIC_CMDID_ANALOG_INPUT;
904
905 err = write(fileHandle, &Buff, 1);
906 if(err != 1){
907 fprintf(stderr, "write err=%d\n", err);
908 }else{
909 fprintf(stderr, "DRV2667 AnalogInput Enable\n");
910 }
911
912 return err;
913}
914
915static enum Haptic_Target getDevTarget(int index){
916 enum Haptic_Target target;
917 switch(index){
918 case 1:
919 target = HAPTIC_DRV2604;
920 break;
921 case 2:
922 target = HAPTIC_DRV2604L;
923 break;
924 case 3:
925 target = HAPTIC_DRV2605;
926 break;
927 case 4:
928 target = HAPTIC_DRV2605L;
929 break;
930 case 5:
931 target = HAPTIC_DRV2667;
932 break;
933 case 6:
934 target = HAPTIC_DRV2669;
935 break;
936 default:
937 target = HAPTIC_DRVXXXX;
938 break;
939 }
940 return target;
941}
942
943static int getDevHandle(enum Haptic_Target *pTarget){
944 int fileHandle = -1, i;
945 struct stat sbuf;
946 int dev_count = ARRAY_LEN(haptic_devices);
947
948 for(i=0; i< dev_count; i++){
949 if (stat(haptic_devices[i], &sbuf) == 0){
950 *pTarget = getDevTarget(i);
951 break;
952 }
953 }
954
955 if(i == dev_count){
956 fprintf(stderr, "[ERROR]dev nodes not found\n");
957 }else{
958 fileHandle = open(haptic_devices[i], O_RDWR);
959 if(fileHandle < 0 ){
960 fprintf(stderr, "[ERROR]file(%s) open_RDWR error\n", haptic_devices[i]);
961 }
962 }
963
964 return fileHandle;
965}
966
967static int DRV2605_Audio2Haptics(int fileHandle, int argc, char **argv){
968 int err = -1;
969 unsigned char Buff;
970 unsigned char enable;
971
972 if(argc != 3){
973 fprintf(stderr, "invalid argc=%d\n", argc);
974 goto err;
975 }
976
977 err = str2char(argv[2], &enable);
978 if(err < 0){
979 fprintf(stderr, "invalid argv[2]=%d\n", argc);
980 goto err;
981 }
982
983 if(enable != 0){
984 Buff = HAPTIC_CMDID_AUDIOHAPTIC_ENABLE;
985 }else{
986 Buff = HAPTIC_CMDID_AUDIOHAPTIC_DISABLE;
987 }
988
989 err = write(fileHandle, &Buff, 1);
990 if(err != 1){
991 fprintf(stderr, "write err=%d\n", err);
992 }else{
993 fprintf(stderr, "DRV2605 A2H %s\n", (Buff==HAPTIC_CMDID_AUDIOHAPTIC_ENABLE)?"enabled":"disabled");
994 }
995
996err:
997 return err;
998}
999
1000static int getDeviceTarget(enum Haptic_Target target){
1001 int result = 0;;
1002
1003 fprintf(stderr, "Haptic Target=%s\n", target_str[target]);
1004
1005 return result;
1006}
1007
1008int main(int argc, char **argv)
1009{
1010 int ret = 0;
1011 int ch;
1012 int fileHandle = -1;
1013 enum Haptic_Target target;
1014
1015 fileHandle = getDevHandle(&target);
1016 if(fileHandle < 0 ){
1017 fprintf(stderr, " file handle err=%d\n", fileHandle);
1018 return ret;
1019 }
1020
1021 if(argc == 1){
1022 usage(target);
1023 return 0;
1024 }
1025
1026 while ((ch = getopt(argc, argv, "wrsdptevfaqnb")) != -1) {
1027 switch (ch) {
1028 case 'w':
1029 if((target == HAPTIC_DRV2667)||(target == HAPTIC_DRV2669)){
1030 ret = DRV266x_Reg_Write(fileHandle, argc, argv);
1031 }else{
1032 ret = DRV260x_Reg_Write(fileHandle, argc, argv);
1033 }
1034 break;
1035 case 'r':
1036 if((target == HAPTIC_DRV2667)||(target == HAPTIC_DRV2669)){
1037 ret = DRV266x_Reg_Read( fileHandle, argc, argv);
1038 }else{
1039 ret = DRV260x_Reg_Read( fileHandle, argc, argv);
1040 }
1041 break;
1042 case 's':
1043 if((target == HAPTIC_DRV2667)||(target == HAPTIC_DRV2669)){
1044 ret = DRV266x_Reg_SetBit( fileHandle, argc, argv);
1045 }else{
1046 ret = DRV260x_Reg_SetBit( fileHandle, argc, argv);
1047 }
1048 break;
1049 case 'e':
1050 ret = DRV26xx_SeqPlay( fileHandle, argc, argv);
1051 break;
1052 case 'd':
1053 if((target == HAPTIC_DRV2667)||(target == HAPTIC_DRV2669)){
1054 ret = DRV266x_Reg_Dump( fileHandle );
1055 }else if((target == HAPTIC_DRV2604)||(target == HAPTIC_DRV2605)||(target == HAPTIC_DRV2604L)||(target == HAPTIC_DRV2605L)){
1056 ret = DRV260x_Reg_Dump( fileHandle);
1057 }else{
1058 fprintf(stderr, " target not support=%d\n", target);
1059 }
1060 break;
1061 case 'p':
1062 ret = DRV26xx_StopPlay(fileHandle);
1063 break;
1064 case 'q':
1065 if((target == HAPTIC_DRV2604)||(target == HAPTIC_DRV2605)||(target == HAPTIC_DRV2604L)||(target == HAPTIC_DRV2605L))
1066 ret = DRV260x_Reg_RTPSeqPlay(fileHandle, argc, argv);
1067 else{
1068 fprintf(stderr, " target not support=%d\n", target);
1069 }
1070 break;
1071 case 'n':
1072 if((target == HAPTIC_DRV2604)||(target == HAPTIC_DRV2605)||(target == HAPTIC_DRV2604L)||(target == HAPTIC_DRV2605L))
1073 ret = DRV260x_PatternRTPPlay(fileHandle, argc, argv);
1074 else{
1075 fprintf(stderr, " target not support=%d\n", target);
1076 }
1077 break;
1078 case 'v':
1079 ret = DRV26xx_VIBPlay(fileHandle, argc, argv);
1080 break;
1081 case 'f':
1082 if((target == HAPTIC_DRV2667)||(target == HAPTIC_DRV2669)){
1083 ret = DRV266x_FIFOPlay(fileHandle, argc, argv);
1084 }else if((target == HAPTIC_DRV2604)||(target == HAPTIC_DRV2604L)){
1085 ret = DRV2604_FirmwareUpdate(fileHandle, argc, argv);
1086 }else{
1087 fprintf(stderr, " target not support=%d\n", target);
1088 }
1089 break;
1090 case 'a':
1091 if(target == HAPTIC_DRV2667){
1092 ret = DRV2667_AnalogInput(fileHandle);
1093 }else if(target == HAPTIC_DRV2605){
1094 ret = DRV2605_Audio2Haptics(fileHandle, argc, argv);
1095 }else{
1096 fprintf(stderr, " target not support=%d\n", target);
1097 }
1098 break;
1099 case 'b':
1100 if((target == HAPTIC_DRV2604)||(target == HAPTIC_DRV2604L)){
1101 ret = DRV2604_ReadRam(fileHandle, argc, argv);
1102 }else{
1103 fprintf(stderr, " target not support=%d\n", target);
1104 }
1105 break;
1106 case 't':
1107 ret = getDeviceTarget(target);
1108 break;
1109 default:
1110 usage(target);
1111 break;
1112 }
1113 }
1114
1115 if(fileHandle > 0 )
1116 close(fileHandle);
1117
1118 return ret;
1119}