mail:bookworepeng@Hotmail.com
qq:196568501
author:DriverMonkey
承接各類嵌入式外包項目(phone:13410905075)
1/*2 * SPI testing utility (using spidev driver)3 *4 * Copyright (c) 2007 MontaVista Software, Inc.5 * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>6 *7 * This program is free software; you can redistribute it and/or modify8 * it under the terms of the GNU General Public License as published by9 * the Free Software Foundation; either version 2 of the License.10 *11 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include12 */1314#include <stdint.h>15#include <unistd.h>16#include <stdio.h>17#include <stdlib.h>18#include <getopt.h>19#include <fcntl.h>20#include <sys/ioctl.h>21#include <linux/types.h>22#include <linux/spi/spidev.h>2324#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))2526static void pabort(const char *s)27{28perror(s);29abort();30}3132static const char *device = "/dev/spidev1.1";33static uint8_t mode;34static uint8_t bits = 8;35static uint32_t speed = 500000;36static uint16_t delay;3738static void transfer(int fd)39{40int ret;41uint8_t tx[] = {420xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,430x40, 0x00, 0x00, 0x00, 0x00, 0x95,440xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,450xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,460xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,470xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,480xF0, 0x0D,49};50uint8_t rx[ARRAY_SIZE(tx)] = {0, };51struct spi_ioc_transfer tr = {52.tx_buf = (unsigned long)tx,53.rx_buf = (unsigned long)rx,54.len = ARRAY_SIZE(tx),55.delay_usecs = delay,56.speed_hz = speed,57.bits_per_word = bits,58};5960ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);61if (ret < 1)62pabort("can't send spi message");6364for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {65if (!(ret % 6))66puts("");67printf("%.2X ", rx[ret]);68}69puts("");70}7172static void print_usage(const char *prog)73{74printf("Usage: %s [-DsbdlHOLC3]\n", prog);75puts(" -D --device device to use (default /dev/spidev1.1)\n"76 " -s --speed max speed (Hz)\n"77 " -d --delay delay (usec)\n"78 " -b --bpw bits per word \n"79 " -l --loop loopback\n"80 " -H --cpha clock phase\n"81 " -O --cpol clock polarity\n"82 " -L --lsb least significant bit first\n"83 " -C --cs-high chip select active high\n"84 " -3 --3wire SI/SO signals shared\n");85exit(1);86}8788static void parse_opts(int argc, char *argv[])89{90while (1) {91static const struct option lopts[] = {92{ "device", 1, 0, 'D' },93{ "speed", 1, 0, 's' },94{ "delay", 1, 0, 'd' },95{ "bpw", 1, 0, 'b' },96{ "loop", 0, 0, 'l' },97{ "cpha", 0, 0, 'H' },98{ "cpol", 0, 0, 'O' },99{ "lsb", 0, 0, 'L' },100{ "cs-high", 0, 0, 'C' },101{ "3wire", 0, 0, '3' },102{ "no-cs", 0, 0, 'N' },103{ "ready", 0, 0, 'R' },104{ NULL, 0, 0, 0 },105};106int c;107108c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);109110if (c == -1)111break;112113switch (c) {114case 'D':115device = optarg;116break;117case 's':118speed = atoi(optarg);119break;120case 'd':121delay = atoi(optarg);122break;123case 'b':124bits = atoi(optarg);125break;126case 'l':127mode |= SPI_LOOP;128break;129case 'H':130mode |= SPI_CPHA;131break;132case 'O':133mode |= SPI_CPOL;134break;135case 'L':136mode |= SPI_LSB_FIRST;137break;138case 'C':139mode |= SPI_CS_HIGH;140break;141case '3':142mode |= SPI_3WIRE;143break;144case 'N':145mode |= SPI_NO_CS;146break;147case 'R':148mode |= SPI_READY;149break;150default:151print_usage(argv[0]);152break;153}154}155}156157int main(int argc, char *argv[])158{159int ret = 0;160int fd;161162parse_opts(argc, argv);163164fd = open(device, O_RDWR);165if (fd < 0)166pabort("can't open device");167168/*169 * spi mode170 */171ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);172if (ret == -1)173pabort("can't set spi mode");174175ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);176if (ret == -1)177pabort("can't get spi mode");178179/*180 * bits per word181 */182ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);183if (ret == -1)184pabort("can't set bits per word");185186ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);187if (ret == -1)188pabort("can't get bits per word");189190/*191 * max speed hz192 */193ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);194if (ret == -1)195pabort("can't set max speed hz");196197ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);198if (ret == -1)199pabort("can't get max speed hz");200201printf("spi mode: %d\n", mode);202printf("bits per word: %d\n", bits);203printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);204205transfer(fd);206207close(fd);208209return ret;210}