Home > OS >  How to use the SPI communication between raspberries pie Linux? For a great god
How to use the SPI communication between raspberries pie Linux? For a great god

Time:11-11

Spontaneous since there is no problem, but the two raspberries spi between sending and receiving data cannot succeed, I doubt whether the Linux the default device, two main equipment can't communication??? But from the principle analysis should be able to receive data, namely a, a, should be received, but still not, the code is as follows:
 
#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

# define ARRAY_SIZE (a) (sizeof (a)/sizeof ([0]) (a))



The static void transfer (int fd);

The static void pabort (const char * s);



The static const char * device="/dev/spidev0.0";

The static uint8_t mode;

The static uint8_t bits=8;

The static uint32_t speed=80000;

The static uint16_t delay;

The static uint8_t cs=1;

//buffer

Char tx [10]="1234";

//uint8_t tx [1800]={100101102103104};

Char rx [ARRAY_SIZE (tx)]={};



Int spi_init ()

{

int ret=0;

Int fd.

//mode=mode | SPI_MODE_0 | SPI_CS_HIGH | SPI_LSB_FIRST | SPI_LOOP;

//parse_opts (arg c, argv);

//step 1 open

Fd=open (device, O_RDWR);

If (fd & lt; 0)

Pabort (" can 't open device ");

//step 2 ioctl CMD

/*

* spi mode

*/

Ret=ioctl (fd, SPI_IOC_WR_MODE, & amp; Mode);//write mode

If (ret==1)

Pabort (" can 't set spi mode ");

Ret=ioctl (fd, SPI_IOC_RD_MODE, & amp; Mode);//read mode

If (ret==1)

Pabort (" can 't get spi mode ");

/* set or obtain SPI to read and write data digits

* bits per word

*/

Ret=ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, & amp; Bits); How much//write each word a

If (ret==1)

Pabort (" can 't set bits per word ");

Ret=ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, & amp; Bits); How much//to read every word a

If (ret==1)

Pabort (" can 't get bits per word ");

/*

* Max speed hz

*/

Ret=ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, & amp; Speed);//write maximum rate

If (ret==1)

Pabort (" can 't set the Max speed hz ");

Ret=ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, & amp; Speed);//read maximum rate

If (ret==1)

Pabort (" can 't get Max speed hz ");



Return fd.

}

Int main (int arg c, char * argv [])

{

Int fd=spi_init ();

Int flag=0;

Printf (" spi fd: % d \ n ", fd);

Printf (" spi mode: % d \ n ", mode).

Printf (" \ n bits per word: % d ", bits);

Max speed: printf (" % d Hz (% d KHz) \ n ", speed, speed/1000);

//step 3 the read and write

While (1) {

If (getchar ()=='1') {

flag=1; Strcpy (tx, "1234");

}

Else if (getchar ()=='2') {

flag=1; Strcpy (tx, "2345");

}

If (flag)

Transfer (fd);

If (strlen (rx) & gt; 0)

{

Len Recv: printf (" % s, % d \ n ", rx, strlen (rx));

//printf (" recv: % d, % d, % d, % d, % d ", rx [0], rx [1], the rx [2], the rx [3], rx [4]);

}

Printf (" -- ");

//printf (" recv: % d, % d, % d, % d, % d ", rx [0], rx [1], the rx [2], the rx [3], rx [4]);

}

//step 4 close

Close (fd);

return 0;

}

//transfer function

The static void transfer (int fd)

{

Int ret.



//full-duplex data transmission

Struct spi_ioc_transfer tr={

Tx. Tx_buf=(unsigned long),

Rx. Rx_buf=(unsigned long),

//. Len=ARRAY_SIZE (tx),

Len=5,

Delay_usecs=delay,

Speed_hz=speed,

Bits_per_word=bits,

. Cs_change=cs

};

Ret=ioctl (fd, SPI_IOC_MESSAGE (1), & amp; Tr);//SPI_IOC_MESSAGE n (n) said transmission packet

If (ret & lt; 1)

Pabort (" can 't send spi message ");

}

The static void pabort (const char * s)

{

Perror (s);

Abort ();

}


CodePudding user response:

Can't take the initiative to send from the device, from a family only one do, code set on distinguishing

CodePudding user response:

The default general family, spi

CodePudding user response:

SPI is a master-slave oh, can be more than a master from, but not both
  • Related