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>
23 extern int transfer(uint8_t tx[], uint8_t rx[],unsigned int size)
26 struct spi_ioc_transfer tr = {
27 .tx_buf = (unsigned long)tx,
28 .rx_buf = (unsigned long)rx,
30 .speed_hz = _mode.speed,
31 .delay_usecs = _mode.delay,
32 .bits_per_word = _mode.bits
35 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
37 printf("can't send spi message");
40 extern int spi_init(spiMode* modes,const char *device )
44 fd = open(device, O_RDWR);
46 printf("could not open %s\n",device);
49 ret = ioctl(fd, SPI_IOC_WR_MODE, &(modes->mode));
51 printf("can't set spi mode\n");
54 ret = ioctl(fd, SPI_IOC_RD_MODE, &(modes->mode));
56 printf("can't get spi mode\n");
59 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &(modes->bits));
61 printf("can't set bits per word\n");
64 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &(modes->bits));
66 printf("can't get bits per word\n");
69 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &(modes->speed));
71 printf("can't set max speed hz\n");
74 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &(modes->speed));
76 printf("can't get max speed hz\n");
79 printf("spi mode: %d\n", modes->mode);
80 printf("bits per word: %d\n", modes->bits);
81 printf("max speed: %d Hz (%d KHz)\n", modes->speed, modes->speed/1000);
84 extern void spi_deinit()