OK. I haven’t gotten any responses, so I’ll try again. This is the description of SPI ioctl transfer struct. It says “Holds pointer to userspace buffer with transmit data, or null. If no data is provided, zeroes are shifted out. : Holds pointer to userspace buffer for receive data, or null.”
Detailed Description
struct spi_ioc_transfer - describes a single SPI transfer : Holds pointer to userspace buffer with transmit data, or null. If no data is provided, zeroes are shifted out. : Holds pointer to userspace buffer for receive data, or null. : Length of tx and rx buffers, in bytes. : Temporary override of the device’s bitrate. : Temporary override of the device’s wordsize. : If nonzero, how long to delay after the last bit transfer before optionally deselecting the device before the next transfer. : True to deselect device before starting the next transfer.
In the C++ program, the author gets the addresses of the arrays passed into the function and puts them in the struct for transmit and receive:
int SPIDevice::transfer(unsigned char send[], unsigned char receive[], int length){
struct spi_ioc_transfer transfer;
transfer.tx_buf = (unsigned long) send;
transfer.rx_buf = (unsigned long) receive;
transfer.len = length;
transfer.speed_hz = this->speed;
transfer.bits_per_word = this->bits;
transfer.delay_usecs = this->delay;
transfer.pad = 0;
int status = ioctl(this->file, SPI_IOC_MESSAGE(1), &transfer);
if (status < 0) {
perror("SPI: Transfer SPI_IOC_MESSAGE Failed");
return -1;
}
return status;
}
I don’t know if I’m doing this correctly. I am using an NTuple instead of an Array.
send = (0b00000001, 0b10000000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
receive = (0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
function spi_transfer(spi::SPIDevice, send::NTuple{8,UInt8}, receive::NTuple{8, UInt8}, len::Int32)
transfer = spi_ioc_transfer()
#see <linux/spi/spidev.h> for details!
transfer.tx_buf = pointer_from_objref(send)
transfer.rx_buf = pointer_from_objref(receive)
transfer.len = len #number of bytes in vector
transfer.speed_hz = spi.speed
transfer.delay_usecs = spi.delay
transfer.bits_per_word = spi.bits
transfer.pad = 0
ret = ccall((:ioctl, "libc"), Cint, (Cint, Clong, Ref{spi_ioc_transfer}...), spi.file, SPI_IOC_MESSAGE(1), transfer)
if (ret < 0)
error("SPI: Transfer SPI_IOC_MESSAGE Failed")
return -1
end
end
I’m still getting a -1 on the return. The other parameter values all match what are in the C++ code. I am just not sure of getting the address of the NTuple or whether I should just use an Array. AND YES I HAVE BEEN THROUGH Calling C and Fortran Code section a dozen times.