I've been fighting with this for a while now and couldn't get anywhere. I am using the spidev and C to communicate with a camera and receive a jpeg picture.
To receive the picture: simply send 0x3C and the camera will send the jpeg picture starting with 0xFF, 0xD8 and ending with 0xFF, 0xD9.
Here is my code which I adapted from spidev_fdx.c for full duplex and running on RPi:
int ret;
struct spi_ioc_transfer xfer[2];
unsigned char buf[300000], *bp;
int status;
memset(xfer, 0, sizeof xfer);
memset(buf, 0, sizeof buf);
buf[0] = BURST_FIFO_READ;//This is 0x3C = 0011 1100
xfer[0].tx_buf = (unsigned long)buf;
xfer[0].len = 1;
xfer[0].cs_change = 0;//Keep CS low after sending 0x3C
xfer[1].rx_buf = (unsigned long) buf;
xfer[1].len = 300000;
printf("Starting fourth transfer...\n");
delay_ms(2000);
status = ioctl(spi_fd, SPI_IOC_MESSAGE(2), xfer);
if (status < 0)
perror("SPI_IOC_MESSAGE");
printf("Image received...\n");
for(int bufcnt = 0;bufcnt<100;bufcnt++){
printf("%02x",buf[bufcnt]);
}
Now here is the output of the last for loop which are the first 100 bytes:
00010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
You can see that no where in here there is 0xFF,0xD8. However, here is a picture from the scope:
The signals are from top to bottom: CS, Clock, MOSI, MISO.
The SPI mode is 0 so the clock idles at low and data is sampled at the rising edge.
You can see the 0x3C = 0011 1100 coming out on the blue line, and the camera responds with 0xFF, 0xD8 on the purple line. for reference, 0xFF = 1111 1111, 0xD8 = 1101 1000
That does not agree with the first 100 elements of buf printed above.I tried to read the last 100 elements and it was still garbage. I don't know what I'm doing wrong. Any help is appreciated!
Note: I am aware that the default buffer size for spi is 4096. I have already changed this to 300000 and cat /sys/module/spidev/parameters/bufsiz returns 300000
