I was talking about importing the ADC data from the CN0540 to the Teensy.
Yes, I understand that. You'll still need to likely send data to the device, writing to registers to set options and such, to configure the device appropriately before you can start reading it from the teensy.
Quick Google didn't show any libraries for this specifically but I wouldn't be surprised if the manufacturer had something, I did see a Linux driver I think which you could look at.
Incidentally, I have also been trying to see how to write to the CN0540 registers using SPI. I haven't had much luck getting answers in other forums on that one. The user guide doesn't give an explanation either.
Again, I haven't used SPI much and when I have there's been library available for the device. I'm sure there's a generic library for SPI communications that works much like your pseudo code in post #2.
So I have 2 questions - how do I use SPI commands to read 24 bit ADC data?
How do I use SPI to read and write registers. The user guide gives register numbers rather than register addresses.
I believe your register numbers are going to be your addresses. While SPI can address more bits, the ADC doesn't have that large of memory address space / many addresses/registers. You can simply pad 0's if needed, for the address.
I'm basing everything here off the data sheet and educated guesses, again, I'm not super familiar with SPI but a data bus is a data bus.
The datasheet says that each SPI access frame is 16 bits.
MSB or Bit 15 of the SPI command is the READ/WRITE# bit, so if it's 1 you're doing a read, if zero, you're doing a write.
Next up, from bits 14-8, we have the address bits. These are going to be the address of the register you want to write to. This leave bits 7-0 for data.
Again, datasheet says:
The SPI control interface uses an off frame protocol. This means that the master (FPGA/DSP) communicates with the AD7768/ AD7768-4 in two frames. The first frame sends a 16-bit instruction (R/W, address, and data) and the second frame is the response where the AD7768/AD7768-4 send 16 bits back to the master.
So, you'll send your frame, then get a 16-bit frame back. Study page 51+ and the timing diagrams. I believe only bits 0-7 will be set in commands you receive from the ADC - and indeed there is a low, mid and high register for each channel, so you'll be doing 3 8-bit reads and shifting.
The ADC will detect illegal commands and respond with 0x0E00 when there's an error.
When the ADC resets, it will set some default settings (page 51) - this is likely how it boots up as well, like I said, you'll likely have to set some registers to get it working properly.
One example is one of the first settings, table 24, 0x01 should set channel mode a, register 0x02 channel mode b, the setting, with bit 3 lets you choose between a wideband filter or a sinc5 filter and set the decimation rate, with bits 2-0, from x32 to x1024. I believe reset in the table says what the value is after a reset.
To do this you'd want to send a transfer kind of like this: (registers usually count down, depends on endianess and platform etc)
0x810D or 0b1000'0001'0000'1101 // write transfer, setting channel mode A filter to Sinc5, decimation rate to x1024.
that's if the register counts down and it gets transferred like that, if not, you might have to reverse the bits, ie:
0xB081 or 0b1011'0000'1000'0001
Good idea to read 51-54 a few times. This will explain the different options, clocking and configurations, which again should ideally be set up for your application.
It actually seems that SPI might only be used for controlling the device. I'm not sure it's used for data output at all - see page 66 for data output options.
There are three options for data output format it seems, table 33.
Each ADC result comprises 32 bits. The first eight bits are the header status bits, which contain status information and the channel number.
The last 24 bits are your 24-bit conversion. DRDY# will go low, then 32 bits will get clocked in on a DOUTx line (assuming you have it set for a channel on each line, again, there are 3 data output configurations)
Also read page 68 from Data Interface: standard conversion operation..
..the datasheet is your friend! study it!