generated from github/welcome-to-github
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghifp_main.c
505 lines (480 loc) · 14.9 KB
/
ghifp_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/****************************************************************************
* ghifp/ghifp_main.c
*
* Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/arch.h>
#include <sdk/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <errno.h>
#include "gacrux_cmd.h"
#include "host_if_fctry.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Test command format */
#define CMD_KEY_INIT "INIT"
#define CMD_KEY_DEINIT "DEINIT"
#define CMD_KEY_CHANGE_SYS_STATUS "CHGSTAT"
#define CMD_KEY_TRANSMIT_FW "TXFW"
#define CMD_KEY_EXECUTE_FW "EXECFW"
#define CMD_KEY_UARTCONF "UARTCONF"
#define CMD_KEY_I2CCONF "I2CCONF"
#define CMD_KEY_I2CWRITE "I2CWRITE"
#define CMD_KEY_TEST "CMD"
#define CMD_KEY_SPICONF "SPICONF"
#define CMD_KEY_DEBUG_SEND "DBGSEND"
#define CMD_KEY_DEBUG_RECV "DBGRECV"
#define CMD_KEY_DEBUG_FILE_SEND "DBGFSEND"
#define CMD_KEY_CHANGE_IF "CHGIF"
#define CMD_KEY_SET_DIVISION_SZ "SETDIVSZ"
#define CMD_KEY_SETADDR "SETADDR"
#define CMD_KEY_SETDFS "SETDFS"
#define CMD_KEY_SETSPICLK "SETSPICLK"
#define CMD_KEY_DEBUG_TRANSMIT_FW "DBGTXFW"
#define CMD_KEY_BINARY_INPUT "BININ"
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
static void help(void)
{
printf("ghifp [arg1] ([arg2] [arg3] ...)\n");
printf("arg list\n");
printf("\t- %s\n", CMD_KEY_INIT);
printf("\t- %s\n", CMD_KEY_DEINIT);
printf("\t- %s [stat]\n", CMD_KEY_CHANGE_SYS_STATUS);
printf("\t\te.g. \"ghifp chgstat 1\"\n");
printf("\t- %s [fw_path]\n", CMD_KEY_TRANSMIT_FW);
printf("\t\te.g. \"ghifp txfw /mnt/spif/test.bin\"\n");
printf("\t- %s\n", CMD_KEY_EXECUTE_FW);
printf("\t- %s [baudrate No] [flow control No]\n", CMD_KEY_UARTCONF);
printf("\t\te.g. \"ghifp uartconf 6 0\"\n");
printf("\t- %s [speed No]\n", CMD_KEY_I2CCONF);
printf("\t- %s [binary cpmmand No]\n", CMD_KEY_I2CWRITE);
printf("\t\te.g. \"ghifp i2cwrite 1 2 3 4 5\"\n");
printf("\t- %s [binary cpmmand No]\n", CMD_KEY_TEST);
printf("\t\te.g. \"ghifp cmd 1 2 3 4 5\"\n");
printf("\t- %s [data frame size No]\n", CMD_KEY_SPICONF);
printf("\t\te.g. \"ghifp spiconf 1\"\n");
printf("\t- %s [len] [hex binary(text)]\n", CMD_KEY_DEBUG_SEND);
printf("\t\te.g. \"ghifp dbgsend 4 001188FF\"\n");
printf("\t- %s [0 or 1]\n", CMD_KEY_DEBUG_RECV);
printf("\t\te.g. \"ghifp dbgrecv 1\"\n");
printf("\t- %s [path]\n", CMD_KEY_DEBUG_FILE_SEND);
printf("\t\te.g. \"ghifp dbgfsend /mnt/spif/test.bin\"\n");
printf("\t- %s [interface num]\n", CMD_KEY_CHANGE_IF);
printf("\t\te.g. \"ghifp chgif 1\"\n");
printf("\t- %s [one packet size]\n", CMD_KEY_SET_DIVISION_SZ);
printf("\t\te.g. \"ghifp setdivsz 4086\"\n");
printf("\t- %s [hex target address]\n", CMD_KEY_SETADDR);
printf("\t\tIt is only effective when interface type is I2C.\n");
printf("\t\te.g. \"ghifp setaddr 24\"\n");
printf("\t- %s [data frame size]\n", CMD_KEY_SETDFS);
printf("\t\tIt is only effective when interface type is SPI.\n");
printf("\t\te.g. \"ghifp setdfs 16\"\n");
printf("\t- %s [clock No]\n", CMD_KEY_SETSPICLK);
printf("\t\tIt is only effective when interface type is SPI.\n");
printf("\t\te.g. \"ghifp setspiclk 2\"\n");
printf("\t- %s [virtual_file_sz] [div_sz] [total_pkt_type] [pkt_no_type]\n",
CMD_KEY_DEBUG_TRANSMIT_FW);
printf("\t\te.g. \"ghifp dbgtxfw 20480 4086 0 0\n");
printf("\t- %s [input][path]\n", CMD_KEY_BINARY_INPUT);
printf("\t\te.g. \"ghifp binin 0 test.bin\"\n");
return;
}
static int dump_arg(int argc, FAR char *argv[])
{
int i;
for (i=0; i<argc; i++)
{
printf("[%d]:%s\n", i, argv[i]);
}
return 0;
}
int ghifp_cmd_entry(int argc, FAR char *argv[])
{
int ret = 0;
if (argc <= 0)
{
help();
return -EINVAL;
}
if (0 == strcasecmp(argv[0], CMD_KEY_INIT))
{
/* Initialize */
if (argc == 1)
{
ret = gacrux_cmd_init();
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_DEINIT))
{
/* De-Initialize */
if (argc == 1)
{
ret = gacrux_cmd_deinit();
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_CHANGE_SYS_STATUS))
{
/* Change system status */
if (argc == 2)
{
ret = gacrux_cmd_change_sys_status(atoi(argv[1]));
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_TRANSMIT_FW))
{
/* Change system status */
if (argc == 2)
{
ret = gacrux_cmd_tx_fw(argv[1]);
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_EXECUTE_FW))
{
/* Execute FW */
if (argc == 1)
{
ret = gacrux_cmd_execute_fw();
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_UARTCONF))
{
/* Change UART configuration */
if (argc == 3)
{
ret = gacrux_cmd_uartconf((uint8_t)atoi(argv[1]),
(uint8_t)atoi(argv[2]));
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_I2CCONF))
{
/* Change I2C configuration */
if (argc == 2)
{
ret = gacrux_cmd_i2cconf((uint8_t)atoi(argv[1]));
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_I2CWRITE))
{
printf("argc = %d.\n", argc);
/* I2C write */
if (argc >= 2)
{
uint8_t opr[argc - 2];
for (int i = 0; i < (argc - 2); i++)
{
opr[i] = (uint8_t)atoi(argv[2 + i]);
}
ret = gacrux_cmd_i2cwrite((uint8_t)atoi(argv[1]), opr,
(uint16_t)(argc - 2));
gacrux_cmd_i2cread();
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_TEST))
{
/* I2C write */
if (argc >= 2)
{
uint8_t opr[argc - 2];
for (int i = 0; i < (argc - 2); i++)
{
opr[i] = (uint8_t)atoi(argv[2 + i]);
}
switch (gacrux_cmd_get_if_type())
{
case HOST_IF_FCTRY_TYPE_UART:
gacrux_cmd_debug_send((char *)opr, (uint16_t)(argc - 2));
break;
case HOST_IF_FCTRY_TYPE_I2C:
ret = gacrux_cmd_i2cwrite((uint8_t)atoi(argv[1]), opr,
(uint16_t)(argc - 2));
gacrux_cmd_i2cread();
break;
case HOST_IF_FCTRY_TYPE_SPI: {
uint8_t bin_cmd = (uint8_t)atoi(argv[1]);
ret = gacrux_cmd_spiwrite(bin_cmd, opr, (uint16_t)(argc - 2));
int res_len = gacrux_cmd_spiread();
up_mdelay(5);
if (res_len < 0 && bin_cmd == 0 && (opr[0] == 2))
{
int retry = 1;
while (res_len < 0 && retry < 10)
{
ret = gacrux_cmd_spiwrite(bin_cmd, opr, (uint16_t)(argc - 2));
res_len = gacrux_cmd_spiread();
printf("RETRY: %d\n", retry);
retry++;
}
}
break;
}
default:
break;
}
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_SPICONF))
{
/* Change SPI configuration */
if (argc == 2)
{
ret = gacrux_cmd_spiconf((uint8_t)atoi(argv[1]));
}
else
{
printf("No need arguments.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_DEBUG_SEND))
{
/* Send arbitrary binary */
if (argc == 3)
{
ret = gacrux_cmd_debug_send(argv[2], atoi(argv[1]));
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_DEBUG_RECV))
{
/* Enable debug receive mode */
if (argc == 2)
{
ret = gacrux_cmd_set_config(HOST_IF_SET_CONFIG_REQ_DBGRECV,
(void *)argv[1]);
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_DEBUG_FILE_SEND))
{
/* Send arbitrary file */
if (argc == 2)
{
ret = gacrux_cmd_debug_file_send(argv[1]);
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_CHANGE_IF))
{
/* Change serial I/F */
if (argc == 2)
{
ret = gacrux_cmd_set_if_type(atoi(argv[1]));
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_SET_DIVISION_SZ))
{
/* Change division size when xfer program */
if (argc == 2)
{
ret = gacrux_cmd_set_division_size((uint16_t)atoi(argv[1]));
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_SETADDR))
{
/* Change I2C target address */
if (argc == 2)
{
ret = gacrux_cmd_set_config(HOST_IF_SET_CONFIG_REQ_SETADDR,
(void *)argv[1]);
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_SETDFS))
{
printf("Deprecated command. Recommend to use \"SPICONF\"\n");
/* Change SPI data frame size */
if (argc == 2)
{
ret = gacrux_cmd_set_config(HOST_IF_SET_CONFIG_REQ_SETDFS,
(void *)argv[1]);
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_SETSPICLK))
{
/* Change SPI clock */
if (argc == 2)
{
ret = gacrux_cmd_set_config(HOST_IF_SET_CONFIG_REQ_SETSPICLK,
(void *)argv[1]);
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_DEBUG_TRANSMIT_FW))
{
/* Transmit FW for test */
if (argc == 5)
{
ret = gacrux_cmd_debug_tx_fw(
(uint32_t)atoi(argv[1]), (uint16_t)atoi(argv[2]),
(uint8_t)atoi(argv[3]), (uint8_t)atoi(argv[4]));
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else if (0 == strcasecmp(argv[0], CMD_KEY_BINARY_INPUT))
{
/* Change system status */
if (argc == 3)
{
printf("argv[1] : %d.\n", (uint8_t)atoi(argv[1]));
ret = gacrux_cmd_bin_input((uint8_t)atoi(argv[1]), argv[2]);
}
else
{
printf("The number of arguments is incorrect.\n");
ret = -EINVAL;
}
}
else
{
help();
ret = -EINVAL;
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* ghifp_main
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int ghifp_main(int argc, char *argv[])
#endif
{
int ret;
// dump_arg(argc, argv);
ret = ghifp_cmd_entry(argc-1, &argv[1]); /* Except "ghifp". */
printf("ghifp command ret=%d\n", ret);
return 0;
}