Skip to content

Instantly share code, notes, and snippets.

@Jotschi
Created September 6, 2012 19:16
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Jotschi/3659646 to your computer and use it in GitHub Desktop.
Example program that write 1,2,3,4,5 to the 25LC010A eeprom chip.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <wiringPiSPI.h>
int main (void) {
int i ;
printf ("Raspberry Pi SPI 25LC010A program\n") ;
// Create a spi connection with 10MHz (This is the maximum of the 25LC010A)
if (wiringPiSPISetup (0, 10000000) < 0)
{
fprintf (stderr, "Unable to open SPI device 0: %s\n", strerror (errno)) ;
exit (1) ;
}
// We want to use the first SPI channel
int ce = 0;
uint8_t writeCommand[7];
// Enable writing
writeCommand[0] = 0b110;
wiringPiSPIDataRW (ce, writeCommand, 1);
// Write data
writeCommand[0] = 0b10;
writeCommand[1] = 9;
writeCommand[2] = 1;
writeCommand[3] = 2;
writeCommand[4] = 3;
writeCommand[5] = 4;
writeCommand[6] = 5;
wiringPiSPIDataRW (ce, writeCommand, 7);
// Wait a moment after the data has been written.
usleep(20000);
int e = 0;
for(e = 0; e < 20000; e++) {
// Clear the buffer
for(i = 0; i < 7; i++) {
writeCommand[i] = 0xFF;
}
// Read data. We need to write 7 bytes. The read data will be set to the given buffer array.
// For each byte we write a byte will be read
writeCommand[0] = 0b11;
writeCommand[1] = 9;
wiringPiSPIDataRW(ce, writeCommand, 7);
for(i = 2; i < 7; i++) {
printf ("Buffer at %d: %02X\n", i, writeCommand[i]) ;
}
}
return 0 ;
}
@deepaklav
Copy link

hii, in rasperry pi 3 there are 2 spi bus 0 and 1 ,i want to use spi1 if wiringPiSPISetup() function has access to spi1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment