#include
# include & lt; Unistd. H>
#include
#include
#include
#include
#include
#include
# include "spidev. H"
#include
# include "spi. H"
# define ARRAY_SIZE (a) (sizeof (a)/sizeof ([0]) (a))
Void pabort (const char * s)
{
Perror (s);
Abort ();
}
Const char * device="/dev/spidev4.0";
Uint32_t mode;
Uint8_t bits=8;
Unsigned int speed=500000;
Unsigned short int delay;
Unsigned short int transfer (int fd, unsigned short int channel, unsigned short int delay)
{
Int ret.
int i=0;
Uint8_t tx [2]={0};
Unsigned short int c=0;
//a=1 & lt; <5 | a;
The channel=channel<4.
Printf (" channel=% x ", channel);
Tx [0]=channel> 8;
Tx [1]=channel& 0 x00ff;
Uint8_t rx [ARRAY_SIZE (tx)]={0};
Struct spi_ioc_transfer tr={
Tx. Tx_buf=(unsigned long),
Rx. Rx_buf=(unsigned long),
Len=ARRAY_SIZE (tx),
Delay_usecs=delay,
Speed_hz=speed,
Bits_per_word=bits,
};
If (mode & amp; SPI_TX_QUAD)
Tr. Tx_nbits=4;
Else if (mode & amp; SPI_TX_DUAL)
Tr. Tx_nbits=2;
If (mode & amp; SPI_RX_QUAD)
Tr. Rx_nbits=4;
Else if (mode & amp; SPI_RX_DUAL)
Tr. Rx_nbits=2;
if (! (mode & amp; SPI_LOOP)) {
If (mode & amp; (SPI_TX_QUAD | SPI_TX_DUAL))
Tr. Rx_buf=0;
Else if (mode & amp; (SPI_RX_QUAD | SPI_RX_DUAL))
Tr. Tx_buf=0;
}
Ret=ioctl (fd, SPI_IOC_MESSAGE (1), & amp; Tr);
for(i=0; i<1000; I++);
Ret=ioctl (fd, SPI_IOC_MESSAGE (1), & amp; Tr);
If (ret & lt; 1)
Pabort (" can 't send spi message ");
For (ret=0; Ret & lt; ARRAY_SIZE (tx); Ret++) {
if (! 6) (ret %)
Puts (" ");
Printf (" % 2 "x, rx (ret));
//printf (" % x, ");
}
C=rx [0] <8 | rx [1];
Printf (" % x ", c);
Puts (" ");
return c;
}
Void parse_opts ()
{
Mode |=SPI_CPOL;
If (mode & amp; SPI_LOOP) {
If (mode & amp; SPI_TX_DUAL)
Mode |=SPI_RX_DUAL;
If (mode & amp; SPI_TX_QUAD)
Mode |=SPI_RX_QUAD;
}
}
Int ADcollect (unsigned short int channel, unsigned int ADspeed, unsigned short int delay)
{
Int ret=0;
Int fd.
Unsigned short int d;
Parse_opts ();
If (ADspeed!=0)
{
Speed=ADspeed;
}
Fd=open (device, O_RDWR);
If (fd & lt; 0)
Pabort (" can 't open device ");
/*
* spi mode
*/
Ret=ioctl (fd, SPI_IOC_WR_MODE32, & amp; Mode);
If (ret==1)
Pabort (" can 't set spi mode ");
Ret=ioctl (fd, SPI_IOC_RD_MODE32, & amp; Mode);
If (ret==1)
Pabort (" can 't get spi mode ");
/*
* bits per word
*/
Ret=ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, & amp; Bits);
If (ret==1)
Pabort (" can 't set bits per word ");
Ret=ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, & amp; Bits);
If (ret==1)
Pabort (" can 't get bits per word ");
/*
* Max speed hz
*/
Ret=ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, & amp; Speed);
If (ret==1)
Pabort (" can 't set the Max speed hz ");
Ret=ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, & amp; Speed);
If (ret==1)
Pabort (" can 't get Max speed hz ");
Printf (" spi mode: 0 x % x \ n ", mode).
Printf (" \ n bits per word: % d ", bits);
Max speed: printf (" % d Hz (% d KHz) \ n ", speed, speed/1000);
D=transfer (fd, channel, delay);
Printf (" % x ", d);
close(fd);
Return d;
}
This is my. H file code
# # ifndef SPI_H
# define SPI_H
Extern "C" {
Void pabort (const char * s);
Unsigned short int transfer (int fd, unsigned short int channel, unsigned short int delay).
Void parse_opts ();
Int ADcollect (unsigned short int channel, unsigned int ADspeed, unsigned short int delay).
}
# endif
Just don't know why I am a compilation is done according to the online they say, "out of the question
Really good ask you how to solve this
CodePudding user response:
Extern "C" this instruction is used in C + + code, C language does not support, if your code suffix is. C, it will not be able to use this keyword, directly cut off the line, if you this on. May give h. C may also give. CPP contains, then write like this:# ifdef __cplusplus
Extern "C" {
# endif
.
# ifdef __cplusplus
}
# endif