Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/ioctl.h>
- #include <linux/spi/spidev.h>
- #include <errno.h>
- #include <fcntl.h> /* For O_RDWR */
- #include <unistd.h> /* For open(), creat() */
- #include <cstdio> /* For printf() */
- int main(int argc, char *argv[])
- {
- printf("version: %d\n", 7);
- int fd = 0;
- fd = open("/dev/spidev3.0", O_RDWR);
- if (fd < 0)
- {
- printf("SPI : can't open device");
- return -1;
- }
- bool ret = ioctl(fd, SPI_IOC_WR_MODE, SPI_MODE_2);
- if (ret == -1)
- {
- printf("SPI : can't set spi mode");
- close(fd);
- return 1;
- }
- ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, SPI_LSB_FIRST);
- if (ret == -1)
- {
- printf("SPI : can't set spi LSB_FIRST mode");
- close(fd);
- return 1;
- }
- ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, 7000);
- if (ret == -1)
- {
- printf("SPI : can't set spi MAX_SPEED mode");
- close(fd);
- return 1;
- }
- {
- unsigned char spiData[4];
- spiData[0] = 0b00001111; // 0x0F read
- spiData[1] = 0b00000000; // 0x00 msB Address
- spiData[2] = 0b00001000; // 0x08 lsB Address
- spiData[3] = 0b00000000; // 0x00 data byte
- for(int j = 0; j < 500; j++)
- {
- for(int i = 0; i < 6; i++)
- {
- if(spiData[2] == 0xFF)
- {
- spiData[2] = 0;
- }
- spiData[2] = spiData[2] + 1; // first register read is 0x09
- spiData[3] = 0b00000000;
- struct spi_ioc_transfer mesg;
- mesg.tx_buf = (unsigned long)spiData;
- mesg.rx_buf = (unsigned long)spiData;
- mesg.len = 4;
- mesg.bits_per_word = 8;
- ret = ioctl(fd, SPI_IOC_MESSAGE(1), mesg);
- if (ret < 0)
- {
- printf("SPI : failed transaction");
- close(fd);
- return 1;
- }
- printf("MAC %d %d: %02X %02X %02X %02X\n", i, j, spiData[0], spiData[1], spiData[2], spiData[3]);
- }
- }
- }
- close(fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement