3 * Code to measure the SPI speed/latency.
4 * Copyright (c) 2014 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with wiringPi.
21 * If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
33 //#include <sys/ioctl.h>
34 //#include <linux/spi/spidev.h>
37 #include <wiringPiSPI.h>
44 #define MAX_SIZE (1024*1024)
49 void spiSetup (int speed)
51 if ((myFd = wiringPiSPISetup (SPI_CHAN, speed)) < 0)
53 fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ;
61 int speed, times, size ;
62 unsigned int start, end ;
64 unsigned char *myData ;
65 double timePerTransaction, perfectTimePerTransaction, dataSpeed ;
67 if ((myData = malloc (MAX_SIZE)) == NULL)
69 fprintf (stderr, "Unable to allocate buffer: %s\n", strerror (errno)) ;
75 for (speed = 1 ; speed <= 32 ; speed *= 2)
77 printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
78 printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ;
79 printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
82 spiSetup (speed * 1000000) ;
83 for (size = 1 ; size <= MAX_SIZE ; size *= 2)
85 printf ("| %5d | %6d ", speed, size) ;
88 for (times = 0 ; times < NUM_TIMES ; ++times)
89 if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1)
91 printf ("SPI failure: %s\n", strerror (errno)) ;
100 timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ;
101 dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ;
102 perfectTimePerTransaction = ((double)(size * 8)) / ((double)(speed * 1000000)) ;
104 printf ("| %8.3f ", timePerTransaction * 1000.0) ;
105 printf ("| %8.1f ", 1.0 / timePerTransaction) ;
106 printf ("| %9.5f ", dataSpeed) ;
107 printf ("| %8.5f ", (timePerTransaction - perfectTimePerTransaction) * 1000.0) ;
113 printf ("+-------+--------+----------+----------+-----------+------------+\n") ;