I am using the following code to read the ADC on an OnLogic Fr202. It always returns…
- User
I am using the following code to read the ADC on an OnLogic Fr202. It always returns 0xff,0xff,0xff,0xff. Can you help me find the problem? void sysonlogicmachineio::readallanaloginputs() { for (int x=0;x<MAXINOUT;++x) { if (analogchannelflags[x]!=-1) { // see https://www.analog.com/media/en/technical-documentation/data-sheets/2444589fc.pdf for more info // we will use two analog input pins for each input (differential inputs) int en = 1; // enable (use the following bits to select channel and speed, etc.) int single = 0; // use two pins for each input (differential) int odd = 0; // odd also acts a sign. Setting to 1 would reverse polarity of the differential input int osr = 3; // corresponds to an oversample rate of 256 (the default value) int twox=0; //don't run at 2 times the speed
int addr=x*2; uint8_t tx_buf[4] = { 0x80 | (en << 5) | (single << 4) | (odd << 3) | (addr), (osr << 4) | (twox << 3), 0x00, 0x00 };// int fd; int ret; uint8_t rx_buf[4]; struct spi_ioc_transfer tr; memset(&tr,0,sizeof(tr)); memset(rx_buf,0,4);
tr.tx_buf = (unsigned long)tx_buf; tr.rx_buf = (unsigned long)rx_buf; tr.len = 4; tr.delay_usecs = 0; tr.speed_hz = 4000000; tr.bits_per_word = 8;//printf("analogiospifile=%d\n",analogiospifile); ret = ioctl(analogiospifile, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) { perror("Can't send SPI message 1\n"); }
printf("Sent: "); for (int i = 0; i < 4; i++) printf("0x%02x ", tx_buf[i]); printf("\nReceived: "); for (int i = 0; i < 4; i++) printf("0x%02x ", rx_buf[i]); long maxlongvalue = 0x0fffffff; int eoc = (rx_buf[0]) >> 7 & 0x1;// out['rsvd'] = rx_buf[0] >> 6 & 0x1 int sign=(rx_buf[0] >> 5) & 0x1; // out['msb'] = rx_buf[0] >> 4 & 0x1 long longvalue = ((rx_buf[0] & 0x0f) << 24) + (rx_buf[1] << 16) + (rx_buf[2] << 8) + rx_buf[3];
if (!sign) longvalue=-longvalue;// out['normal'] = (out['data'] / (maxval* 1.0)) * 1.3 printf("eoc %d long value=%ld\n",eoc,longvalue); analoginputvalues[x]=(float)longvalue/maxlongvalue; //while(1) {}; // hang } } }
- Assistant
Thanks for sharing your code! The fact that you always get
0xff, 0xff, 0xff, 0xffback from the SPI read usually indicates one of the following issues:- SPI device not properly opened or configured
- Incorrect SPI mode, speed, or bits per word
- Wrong chip select or SPI device node
- Incorrect command format or addressing
- Hardware wiring or connection issues
Step-by-step debugging and suggestions:
1. Check SPI device file descriptor (
analogiospifile)Make sure
analogiospifileis a valid file descriptor opened with something like:analogiospifile = open("/dev/spidevX.Y", O_RDWR); if (analogiospifile < 0) { perror("Failed to open SPI device"); return; }If this is invalid, all SPI transfers will fail and return 0xFF bytes.
2. Verify SPI mode, speed, and bits per word
Before your transfer, set SPI mode and bits per word explicitly:
uint8_t mode = SPI_MODE_0; // or SPI_MODE_1, 2, 3 depending on your ADC datasheet uint8_t bits = 8; uint32_t speed = 4000000; // 4 MHz ioctl(analogiospifile, SPI_IOC_WR_MODE, &mode); ioctl(analogiospifile, SPI_IOC_WR_BITS_PER_WORD, &bits); ioctl(analogiospifile, SPI_IOC_WR_MAX_SPEED_HZ, &speed);Make sure these match the ADC requirements exactly.
3. Validate your command bytes
Your first byte is:
0x80 | (en << 5) | (single << 4) | (odd << 3) | (addr)enis 1, so bit 5 is set.singleis 0 (differential mode).oddis 0.addrisx*2.
Check the datasheet carefully to confirm the bit layout and that
addris correct for your channel.
4. Check the SPI transfer struct
Your
spi_ioc_transferstruct looks mostly correct, but the pointers should be cast to(uintptr_t)or(unsigned long)carefully:tr.tx_buf = (unsigned long)tx_buf; tr.rx_buf = (unsigned long)rx_buf;This is usually fine, but on some platforms, you might want to cast to
(uintptr_t)or use:tr.tx_buf = (uintptr_t)tx_buf; tr.rx_buf = (uintptr_t)rx_buf;
5. Hardware and wiring
- Make sure the SPI lines (MOSI, MISO, SCLK, CS) are connected correctly.
- Check if the chip select line is controlled by the SPI driver or manually.
- If manual CS is needed, you must toggle it yourself before and after the transfer.
6. Test with a simple SPI loopback or known device
Try sending a simple known command or test with a loopback (connect MOSI to MISO) to verify SPI communication.
Summary checklist:
-
analogiospifileis valid and opened correctly. - SPI mode, speed, bits per word are set correctly via
ioctl. - Command bytes match the ADC datasheet exactly.
- SPI transfer struct pointers cast correctly.
- Hardware wiring and chip select are correct.
- Try a simple SPI test to verify communication.
If you want, I can help you write a minimal SPI test program or help verify the command bytes if you provide the exact ADC model or datasheet link.
Example minimal SPI setup snippet:
int fd = open("/dev/spidev0.0", O_RDWR); if (fd < 0) { perror("open"); return; } uint8_t mode = SPI_MODE_0; uint8_t bits = 8; uint32_t speed = 4000000; ioctl(fd, SPI_IOC_WR_MODE, &mode); ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); // Then do your transfer
Let me know if you want me to review the command format or help with a test program!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.