Lines Matching +full:ch3 +full:- +full:0

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
44 #define DEFAULT_DEVICE_NAME "/dev/spigen0.0"
48 #define DIR_READ 0
51 #define DIR_NONE -1
54 int mode; /* mode (0,1,2,3, -1 == use default) */
55 int speed; /* speed (in Hz, -1 == use default) */
56 int count; /* count (0 through 'n' bytes, negative for
58 int binary; /* non-zero for binary output or zero for
59 * ASCII output when ASCII != 0 */
61 * non-zero for ASCII input, 'binary'
63 int lsb; /* non-zero for LSB order (default order is
65 int verbose; /* non-zero for verbosity */
85 * LSB array - reversebits[n] is the LSB value of n as an MSB. Use this array
90 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
91 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
92 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
93 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
94 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
95 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
96 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
97 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
98 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
99 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
100 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
101 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
102 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
103 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
104 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
105 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
106 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
107 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
108 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
109 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
110 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
111 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
112 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
113 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
114 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
115 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
116 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
117 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
118 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
119 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
120 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
121 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
129 fputs(" - communicate on SPI bus with slave devices\n" in usage()
131 " spi [-f device] [-d r|w|rw] [-m mode] [-s max-speed] [-c count]\n" in usage()
132 " [-C \"command bytes\"] [-A] [-b] [-L] [-v]\n" in usage()
133 " spi -i [-f device] [-v]\n" in usage()
134 " spi -h\n" in usage()
136 " -f specifies the device (default is spigen0.0)\n" in usage()
137 " -d specifies the operation (r, w, or rw; default is rw)\n" in usage()
138 " -m specifies the mode (0, 1, 2, or 3)\n" in usage()
139 " -s specifies the maximum speed (default is 0, device default)\n" in usage()
140 " -c specifies the number of data bytes to transfer (default 0, i.e. none)\n" in usage()
142 " -C specifies 'command bytes' to be sent, as 2 byte hexadecimal values\n" in usage()
144 " -L specifies 'LSB' order on the SPI bus (default is MSB)\n" in usage()
145 " -i query information about the device\n" in usage()
146 " -A uses ASCII for input/output as 2-digit hex values\n" in usage()
147 " -b Override output format as binary (only valid with '-A')\n" in usage()
148 " -v verbose output\n" in usage()
149 " -h prints this message\n" in usage()
165 finfo = 0; in main()
168 hdev = -1; in main()
169 err = 0; in main()
171 dev_name[0] = 0; in main()
173 opt.mode = -1; in main()
174 opt.speed = -1; in main()
175 opt.count = 0; in main()
176 opt.ASCII = 0; in main()
177 opt.binary = 0; in main()
178 opt.lsb = 0; in main()
179 opt.verbose = 0; in main()
180 opt.ncmd = 0; in main()
183 while (!err && (ch = getopt(argc, argv, "f:d:m:s:c:C:AbLvih")) != -1) { in main()
186 if (optarg[0] == 'r') { in main()
187 if (optarg[1] == 'w' && optarg[2] == 0) { in main()
190 else if (optarg[1] == 0) { in main()
194 else if (optarg[0] == 'w' && optarg[1] == 0) { in main()
203 if (!optarg[0]) { /* unlikely */ in main()
204 fputs("error - missing device name\n", stderr); in main()
208 if (optarg[0] == '/') in main()
220 if (!pstr || *pstr || opt.mode < 0 || opt.mode > 3) { in main()
230 if (!pstr || *pstr || opt.speed < 0) { in main()
287 argc -= optind; in main()
291 (fdir == DIR_NONE && !finfo && opt.mode == -1 && opt.speed == -1 && opt.count == 0)) { in main()
301 if ((opt.count != 0 || opt.ncmd != 0) && fdir == DIR_NONE) { in main()
310 if (opt.count == 0) in main()
316 if (opt.count < 0 && fdir != DIR_READWRITE && fdir != DIR_WRITE) { in main()
326 if (!dev_name[0]) /* no device name specified */ in main()
331 if (hdev == -1) { in main()
332 fprintf(stderr, "Error - unable to open '%s', errno=%d\n", in main()
345 if (opt.mode != -1) { in main()
352 if (opt.speed != -1) { in main()
373 if (hdev != -1) in main()
389 err = 0; in interpret_command_bytes()
391 popt->pcmd = (uint8_t *)malloc(cbcmd); in interpret_command_bytes()
393 if (!popt->pcmd) in interpret_command_bytes()
396 pcur = popt->pcmd; in interpret_command_bytes()
398 ctr = 0; in interpret_command_bytes()
410 if (ch < 0 || !*ppos) { /* must be valid pair of hex characters */ in interpret_command_bytes()
416 if (ch2 < 0) { in interpret_command_bytes()
421 ch = (ch * 16 + ch2) & 0xff; /* convert to byte */ in interpret_command_bytes()
423 if (ctr >= cbcmd) { /* need re-alloc buffer? (unlikely) */ in interpret_command_bytes()
425 ptemp = realloc(popt->pcmd, cbcmd); in interpret_command_bytes()
435 popt->pcmd = (uint8_t *)ptemp; in interpret_command_bytes()
436 pcur = popt->pcmd + ctr; in interpret_command_bytes()
439 if (popt->lsb) in interpret_command_bytes()
448 popt->ncmd = ctr; /* record num bytes in '-C' argument */ in interpret_command_bytes()
452 /* at this point popt->pcmd is NULL or a valid pointer */ in interpret_command_bytes()
469 if (err == 0) in get_info()
472 if (err == 0) { in get_info()
489 uint32_t fmode = popt->mode; in set_mode()
491 if (popt->mode < 0) /* use default? */ in set_mode()
492 return 0; in set_mode()
500 uint32_t clock_speed = popt->speed; in set_speed()
502 if (popt->speed < 0) in set_speed()
503 return 0; in set_speed()
511 if (c >= '0' && c <= '9') { in hexval()
512 return c - '0'; in hexval()
514 return c - 'A' + 10; in hexval()
516 return c - 'a' + 10; in hexval()
518 return -1; in hexval()
524 int ch, ch2, ch3, ncmd, lsb, err; in prep_write_buffer() local
529 ncmd = popt->ncmd; /* num command bytes (can be zero) */ in prep_write_buffer()
531 if (ncmd == 0 && popt->count == 0) in prep_write_buffer()
535 if (popt->count < 0) { in prep_write_buffer()
539 cbdata = popt->count; in prep_write_buffer()
542 lsb = popt->lsb; /* non-zero if LSB order; else MSB */ in prep_write_buffer()
545 cbread = 0; in prep_write_buffer()
547 err = 0; in prep_write_buffer()
552 if (popt->pcmd && ncmd > 0) { in prep_write_buffer()
553 memcpy(pdata, popt->pcmd, ncmd); /* copy command bytes */ in prep_write_buffer()
568 if (popt->ASCII) { in prep_write_buffer()
579 if (ch2 < 0) { in prep_write_buffer()
590 ch3 = hexval(ch); in prep_write_buffer()
592 if (ch3 < 0) in prep_write_buffer()
595 ch = ch2 * 16 + ch3; in prep_write_buffer()
603 /* for LSB, flip the bits - otherwise, just copy the value */ in prep_write_buffer()
619 if (popt->verbose > 0) { in prep_write_buffer()
627 if (popt->ASCII) in prep_write_buffer()
639 if (popt->count < 0) { in prep_write_buffer()
640 popt->count = cbread; in prep_write_buffer()
643 * for everything else, fill the rest of the read buffer with '0' in prep_write_buffer()
648 pdat2[cbread++] = 0; in prep_write_buffer()
652 * popt->count bytes will be sent and read from the SPI, preceded by the in prep_write_buffer()
653 * 'popt->ncmd' command bytes (if any). in prep_write_buffer()
654 * So we must use 'popt->count' and 'popt->ncmd' from this point on in in prep_write_buffer()
658 if (popt->verbose > 0 && popt->count + popt->ncmd) { in prep_write_buffer()
659 if ((popt->count + popt->ncmd) == 1) in prep_write_buffer()
665 popt->count + popt->ncmd, szbytes); in prep_write_buffer()
667 verbose_dump_buffer(pdata, popt->count + popt->ncmd, lsb); in prep_write_buffer()
680 return 0; in _read_write()
696 * The remaining members for spi.st_data are zero - all bytes are in _read_write()
702 err = ioctl(hdev, SPIGENIOC_TRANSFER, &spi) < 0 ? -1 : 0; in _read_write()
706 for (ctr = 0; ctr < cbrw; ctr++) { in _read_write()
726 pbuf = (uint8_t *)pr + popt->ncmd; /* only the data we want */ in _do_data_output()
727 icount = popt->count; in _do_data_output()
728 err = 0; in _do_data_output()
730 if (icount <= 0) { in _do_data_output()
731 return -1; /* should not but could happen */ in _do_data_output()
739 if (popt->ncmd != 1) in _do_data_output()
745 if (popt->binary || !popt->ASCII) { in _do_data_output()
746 if (popt->verbose > 0) in _do_data_output()
752 else if (icount > 0) { in _do_data_output()
753 if (popt->verbose > 0) in _do_data_output()
758 for (idx = 0; !err && idx < icount; idx++) { in _do_data_output()
767 err = fprintf(stdout, "%02hhx", pbuf[idx]) < 0; in _do_data_output()
778 else if (popt->verbose > 0 && icount) { in _do_data_output()
781 popt->ncmd, sz_byte2, icount, sz_bytes); in _do_data_output()
784 verbose_dump_buffer(pr, icount + popt->ncmd, popt->lsb); in _do_data_output()
797 icount = popt->count + popt->ncmd; in perform_read()
799 /* prep write buffer filled with 0 bytes */ in perform_read()
803 err = -1; in perform_read()
810 if (popt->pcmd && popt->ncmd > 0) in perform_read()
811 memcpy(pw, popt->pcmd, popt->ncmd); in perform_read()
816 err = -2; in perform_read()
822 err = _read_write(hdev, pw, pr, icount, popt->lsb); in perform_read()
824 if (!err && popt->count > 0) in perform_read()
846 err = -1; in perform_write()
850 err = _read_write(hdev, pw, NULL, popt->count + popt->ncmd, popt->lsb); in perform_write()
868 icount = popt->count + popt->ncmd; /* assign after fn call */ in perform_readwrite()
871 err = -1; in perform_readwrite()
878 err = -2; in perform_readwrite()
884 err = _read_write(hdev, pw, pr, icount, popt->lsb); in perform_readwrite()
904 fputs(" | 0 1 2 3 4 5 6 7 8 9 A B C D E F " in verbose_dump_buffer()
907 for (ictr = 0; ictr < icount; ictr += 16) { in verbose_dump_buffer()
908 fprintf(stderr, " %6x | ", ictr & 0xfffff0); in verbose_dump_buffer()
910 for (ictr2 = 0; ictr2 < 16; ictr2++) { in verbose_dump_buffer()
928 for (ictr2 = 0; ictr2 < 16; ictr2++) { in verbose_dump_buffer()