Click here to Skip to main content
15,936,395 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionReturn a local 2d Array Pin
kinderu18-May-18 8:59
kinderu18-May-18 8:59 
AnswerRe: Return a local 2d Array Pin
Richard MacCutchan18-May-18 9:13
mveRichard MacCutchan18-May-18 9:13 
GeneralRe: Return a local 2d Array Pin
kinderu18-May-18 9:45
kinderu18-May-18 9:45 
GeneralRe: Return a local 2d Array Pin
leon de boer18-May-18 16:34
leon de boer18-May-18 16:34 
GeneralRe: Return a local 2d Array Pin
Richard MacCutchan18-May-18 20:46
mveRichard MacCutchan18-May-18 20:46 
AnswerRe: Return a local 2d Array Pin
jfbode102930-May-18 4:46
jfbode102930-May-18 4:46 
QuestionCan someone show me an example of including a DLL or function in a C program ? Pin
m3mb3r17-May-18 10:41
m3mb3r17-May-18 10:41 
SuggestionRe: Can someone show me an example of including a DLL or function in a C program ? Pin
Randor 17-May-18 11:52
professional Randor 17-May-18 11:52 
AnswerRe: Can someone show me an example of including a DLL or function in a C program ? Pin
Richard MacCutchan17-May-18 21:40
mveRichard MacCutchan17-May-18 21:40 
AnswerRe: Can someone show me an example of including a DLL or function in a C program ? Pin
leon de boer18-May-18 8:58
leon de boer18-May-18 8:58 
Questionvolatile misbehaves Pin
Vaclav_17-May-18 3:19
Vaclav_17-May-18 3:19 
AnswerRe: volatile misbehaves Pin
Richard MacCutchan17-May-18 4:04
mveRichard MacCutchan17-May-18 4:04 
GeneralRe: volatile misbehaves Pin
Vaclav_17-May-18 4:26
Vaclav_17-May-18 4:26 
GeneralRe: volatile misbehaves Pin
Richard MacCutchan17-May-18 4:42
mveRichard MacCutchan17-May-18 4:42 
GeneralRe: volatile misbehaves Pin
Vaclav_17-May-18 5:39
Vaclav_17-May-18 5:39 
GeneralRe: volatile misbehaves Pin
supercat930-May-18 12:20
supercat930-May-18 12:20 
AnswerRe: volatile misbehaves Pin
Joe Woodbury17-May-18 5:48
professionalJoe Woodbury17-May-18 5:48 
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

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.