2 * SPI testing utility (using spidev driver)
4 * Copyright (c) 2007 MontaVista Software, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
11 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
17 #include <sys/ioctl.h>
18 #include <linux/types.h>
19 #include <linux/spi/spidev.h>
24 extern int transfer(uint8_t tx[], uint8_t rx[], unsigned int size)
27 struct spi_ioc_transfer tr = {
28 .tx_buf = (unsigned long)tx,
29 .rx_buf = (unsigned long)rx,
31 .speed_hz = _mode.speed,
32 .delay_usecs = _mode.delay,
33 .bits_per_word = _mode.bits};
35 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
37 printf("can't send spi message\n");
40 extern int spi_init(spiMode *modes, const char *device)
44 fd = open(device, O_RDWR);
47 printf("could not open %s\n", device);
50 ret = ioctl(fd, SPI_IOC_WR_MODE, &(modes->mode));
53 printf("can't set spi mode\n");
56 ret = ioctl(fd, SPI_IOC_RD_MODE, &(modes->mode));
59 printf("can't get spi mode\n");
62 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &(modes->bits));
65 printf("can't set bits per word\n");
68 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &(modes->bits));
71 printf("can't get bits per word\n");
74 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &(modes->speed));
77 printf("can't set max speed hz\n");
80 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &(modes->speed));
83 printf("can't get max speed hz\n");
86 printf("spi mode: %d\n", modes->mode);
87 printf("bits per word: %d\n", modes->bits);
88 printf("max speed: %d Hz (%d KHz)\n", modes->speed, modes->speed / 1000);
91 extern void spi_deinit()