Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: volatile misbehaves Pin
Vaclav_17-May-18 6:04
Vaclav_17-May-18 6:04 
GeneralRe: volatile misbehaves Pin
Vaclav_17-May-18 7:24
Vaclav_17-May-18 7:24 
AnswerRe: volatile misbehaves Pin
leon de boer18-May-18 8:16
leon de boer18-May-18 8:16 
GeneralRe: volatile misbehaves Pin
Vaclav_19-May-18 14:02
Vaclav_19-May-18 14:02 
GeneralRe: volatile misbehaves Pin
Vaclav_19-May-18 14:36
Vaclav_19-May-18 14:36 
GeneralRe: volatile misbehaves Pin
leon de boer20-May-18 4:41
leon de boer20-May-18 4:41 
GeneralRe: volatile misbehaves Pin
Vaclav_20-May-18 5:44
Vaclav_20-May-18 5:44 
GeneralRe: volatile misbehaves Pin
leon de boer20-May-18 20:35
leon de boer20-May-18 20:35 
Each device has it's own name it's basically the name in the process device (look at the /dev directory you can see them all)

Now the SPI is a proper block device and you can write to it like a file unlike the GPIO

so for SPI it's either
/dev/spidev0.0
/dev/spidev0.1
Depending if you want SPI0 or 1

Lets do SPI0
#include <stdbool.h>
#include <fcntl.h>				//Needed for SPI port
#include <sys/ioctl.h>			//Needed for SPI port
#include <linux/spi/spidev.h>	//Needed for SPI port
#include <unistd.h>			    //Needed for SPI port

/* This function will open the SPI port (either 0 or 1) with the given parameters */
/* If it fails it will return -1 otherwise it will return handle to opened device */

int OpenSPI( int SPI_Port, int* SPI_WriteMode, int* SPI_ReadMode, int* SPI_WriteLength, int* SPI_ReadLength, int* SPI_WriteSpeed, int* SPI_ReadSpeed)
{
   int status;
   int spi_handle = -1;       // This will be our handle to the SPI

   /* OPEN A HANDLE TO THE SPI DEVICE */
   if (SPI_Port) == 0){
      spi_handle = open ("/dev/spidev0.0", O_RDWR);
   } else if (SPI_Port == 1) {
      spi_handle = open ("/dev/spidev0.1", O_RDWR)
   } else {
      printf("Invalid SPI port number specified\n");
      return -1;
   }

   if (spi_handle < 0) {
      printf("Unable to open physical memory handle: %s\n", strerror(errno));      
      return -1;
   }


   /* SET SPI WRITE MODE */
   status = ioctl(spi_handle, SPI_IOC_WR_MODE, SPI_WriteMode);
   if(status < 0) {
      printf("Could not set SPIMode (WR)...ioctl fail");
      return -1;
   }

   /* SET SPI READ MODE */
   status = ioctl(spi_handle, SPI_IOC_RD_MODE, SPI_ReadMode);
   if(status < 0) {
      printf("Could not set SPIMode (RD)...ioctl fail");
      return(-1);
   }

   /* SET SPI WRITE BITS PER WORD */
   status = ioctl(spi_handle, SPI_IOC_WR_BITS_PER_WORD, SPI_WriteLength);
   if (status < 0) {
      printf("Could not set SPI bitsPerWord (WR)...ioctl fail");
      return -1;
   }

   /* SET SPI READ BITS PER WORD LETS MAKE IT 8 */
   status = ioctl(spi_handle, SPI_IOC_RD_BITS_PER_WORD, SPI_ReadLength);
   if (status < 0) {
      printf("Could not set SPI bitsPerWord(RD)...ioctl fail");
      return -1;
   }


   /* SET SPI WRITE BITS SPEED */
   status = ioctl(spi_handle, SPI_IOC_WR_MAX_SPEED_HZ, SPI_WriteSpeed);
   if (status < 0) {
      printf("Could not set SPI speed (WR)...ioctl fail");
      return -1;
   }

   /* SET SPI READ BITS SPEED */
   status = ioctl(spi_handle, SPI_IOC_RD_MAX_SPEED_HZ, SPI_ReadSpeed);
   if (status < 0) {
      printf("Could not set SPI speed (RD)...ioctl fail");
      return -1;
   }

   /* Everytthing set successfully return the handle */
   return (spi_handle);
}

/* Okay we need to write data to the SPI lets make that procedure */
int SpiWrite (int spi_device, int SPI_WriteSpeed, int SPI_WriteLength, unsigned char *data, int length)
{
    struct spi_ioc_transfer spi[length];
    int i = 0;
    int retVal = -1;

   //one spi transfer for each byte
   for (i = 0 ; i < length ; i++)
   {
	memset(&spi[i], 0, sizeof (spi[i]));
	spi[i].tx_buf  = (unsigned long)(data + i); // transmit from "data"
	spi[i].rx_buf  = 0;
	spi[i].len     = length;
        spi[i].delay_usecs   = 0 ;
        spi[i].speed_hz      = SPI_WriteSpeed;
        spi[i].bits_per_word = SPI_WriteLength;
        spi[i].cs_change = 0;
   }
   retVal = ioctl(spi_device, SPI_IOC_MESSAGE(length), &spi);
   if (retVal < 0){
      printf("Error - Problem transmitting spi data..ioctl");
      return(-1);
   }
   return retVal;
}


/* Okay we need to read data to the SPI lets make that procedure */
int SpiRead (int spi_device, int SPI_ReadSpeed, int SPI_ReadLength, unsigned char *data, int length)
{
   struct spi_ioc_transfer spi[length];
   int i = 0;
   int retVal = -1;

   // one spi transfer for each byte
   for (i = 0 ; i < length ; i++) 
   {
      memset(&spi[i], 0, sizeof (spi[i]));
      spi[i].tx_buf = 0;
      spi[i].rx_buf = (unsigned long)(data + i) ; // receive into "data"
      spi[i].len  = length;  // Amount of data to recieve
      spi[i].delay_usecs = 0 ;
      spi[i].speed_hz = SPI_ReadSpeed;
      spi[i].bits_per_word = SPI_ReadLength;
      spi[i].cs_change = 0;
   }

   retVal = ioctl(spi_device, SPI_IOC_MESSAGE(length), &spi) ;
   if(retVal < 0) {
      printf("Error - Problem transmitting spi data..ioctl");
      return -1;
    }
   return retVal;
}


Okay finally thats is all done lets use everything

int main (void){
   static int WriteSpeed = 1000000;  // lets set write speed to 1Mhz
   static int ReadSpeed  = 1000000;  // Lets set read speed to 1Mhz
   static int WriteLength = 8;       // Lets set write length to 8 bits
   static int ReadLength  = 8;       // Lets set read length to 8 bits

//----- There are a number of SPI MODES that are available -----
//SPI_MODE_0 (0,0) 	CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge
//SPI_MODE_1 (0,1) 	CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge
//SPI_MODE_2 (1,0) 	CPOL = 1, CPHA = 0, Clock idle high, data is clocked in on falling edge, output data (change) on rising edge
//SPI_MODE_3 (1,1) 	CPOL = 1, CPHA = 1, Clock idle high, data is clocked in on rising, edge output data (change) on falling edge

   static int WriteMode = SPI_MODE_0 ;       // Lets write MODE 0
   static int ReadMode  = SPI_MODE_0;        // Lets read MODDE 0

   static int SPI_handle;                    // This is our SPI handle that will be return and we hold

    /* Lets assume we want SPI port 0 lets open it */
    SPI_handle = OpenSPI(0, &WriteMode, &ReadMode, &WriteLength, &ReadLength, &WriteSpeed, &ReadSpeed);

    /* Check it opened */
   if (SPI_handle >= 0){
        
        /* let write something A,B,C,D */
        unsigned char Data[4] = { 0x41, 0x42, 0x43, 0x44};
        SpiWrite(SPI_handle, WriteSpeed, WriteLength, &Data[0], 4);


        /* we are finished so close the handle */
        close(SPI_handle); 

   }
}

Now the read/write routines are less than optimal I would bring the struct out to the interface but it will get you started.
In vino veritas

GeneralRe: volatile misbehaves Pin
Vaclav_21-May-18 3:18
Vaclav_21-May-18 3:18 
GeneralRe: volatile misbehaves Pin
leon de boer21-May-18 4:33
leon de boer21-May-18 4:33 
Question__sync_synchronize stops processor ? Pin
Vaclav_16-May-18 10:31
Vaclav_16-May-18 10:31 
AnswerRe: __sync_synchronize stops processor ? Pin
Randor 16-May-18 12:15
professional Randor 16-May-18 12:15 
GeneralRe: __sync_synchronize stops processor ? Pin
Vaclav_16-May-18 13:15
Vaclav_16-May-18 13:15 
GeneralRe: __sync_synchronize stops processor ? Pin
Randor 16-May-18 14:10
professional Randor 16-May-18 14:10 
GeneralRe: __sync_synchronize stops processor ? Pin
Vaclav_17-May-18 3:22
Vaclav_17-May-18 3:22 
QuestionTo install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu14-May-18 19:30
manoharbalu14-May-18 19:30 
AnswerRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov14-May-18 20:24
Victor Nijegorodov14-May-18 20:24 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu14-May-18 20:50
manoharbalu14-May-18 20:50 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov14-May-18 22:56
Victor Nijegorodov14-May-18 22:56 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu15-May-18 3:10
manoharbalu15-May-18 3:10 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov15-May-18 3:40
Victor Nijegorodov15-May-18 3:40 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
manoharbalu15-May-18 3:48
manoharbalu15-May-18 3:48 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
leon de boer15-May-18 5:50
leon de boer15-May-18 5:50 
GeneralRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Victor Nijegorodov15-May-18 9:57
Victor Nijegorodov15-May-18 9:57 
AnswerRe: To install Visual Studio(2008 Pro,2010 Express and 2013 Express) in the same machine Pin
Joe Woodbury17-May-18 5:55
professionalJoe Woodbury17-May-18 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.