Lines Matching +full:num +full:- +full:transfer +full:- +full:bits
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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) */
58 int binary; /* non-zero for binary output or zero for
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
86 * to obtain a reversed bit pattern of the index value when bits must
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()
168 hdev = -1; in main()
173 opt.mode = -1; in main()
174 opt.speed = -1; in main()
183 while (!err && (ch = getopt(argc, argv, "f:d:m:s:c:C:AbLvih")) != -1) { in main()
204 fputs("error - missing device name\n", stderr); in main()
287 argc -= optind; in main()
291 (fdir == DIR_NONE && !finfo && opt.mode == -1 && opt.speed == -1 && opt.count == 0)) { 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()
359 /* do data transfer */ in main()
373 if (hdev != -1) in main()
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()
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()
489 uint32_t fmode = popt->mode; in set_mode()
491 if (popt->mode < 0) /* use default? */ in set_mode()
500 uint32_t clock_speed = popt->speed; in set_speed()
502 if (popt->speed < 0) in set_speed()
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()
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()
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()
603 /* for LSB, flip the bits - otherwise, just copy the value */ in prep_write_buffer()
609 cbread++; /* increment num bytes read so far */ 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()
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()
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()
705 /* flip the bits for 'lsb' mode */ 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()
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()
753 if (popt->verbose > 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()
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()