Skip to content

Instantly share code, notes, and snippets.

@kashimAstro
Created December 20, 2017 12:25
Show Gist options
  • Save kashimAstro/afb4b24c17511d9f06e3e29065bfdfa3 to your computer and use it in GitHub Desktop.
Save kashimAstro/afb4b24c17511d9f06e3e29065bfdfa3 to your computer and use it in GitHub Desktop.
spi speed with ioctl ofxGPIO
#include "ofxGPIO.h"
/*
ofxGPIO
speed test SPI
*/
int main(int argc, char ** argv)
{
if(argc < 2)
{
cout << "Param: device-spi" << endl;
exit(0);
}
MCP a2d;
int i,a2dVal = 0;
unsigned char data[3];
struct timespec start, end;
double fq,sec,nsec,msec;
a2d.setup(argv[1], SPI_MODE_0, 1000000, 8);
while(i<500) {
unsigned static int a2dChannel = 0;
data[0] = 1;
data[1] = 0b10000000 |( ((a2dChannel & 7) << 4));
data[2] = 0;
clock_gettime(CLOCK_REALTIME, &start);
a2d.readWrite(data);
a2dVal = 0;
a2dVal = (data[1]<< 8) & 0b1100000000;
a2dVal |= (data[2] & 0xff);
cout << "Result channel["<< a2dChannel <<"]: " << a2dVal << "; ";
clock_gettime(CLOCK_REALTIME, &end);
sec = ((double)end.tv_sec) - ((double)start.tv_sec);
nsec = ((double)end.tv_nsec) - ((double)start.tv_nsec);
msec = round(nsec / 1000);
fq = (1.0e9 / nsec);
cout << "Time s[ " << sec << " ] ms[ " << msec << " ] ns[ " << nsec << " ]; Freq Hz[ " << fq << " ]" << endl;
a2dChannel++;
if(a2dChannel >= 8) a2dChannel = 0;
i++;
}
a2d.xclose();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment