SPI mode 0, data is transmitted at SCK falling edge, and ideally sampled at SCK rising edge. Will we design to have data sampled at SCK falling edge? - embedded

For SPI mode 0, functional operation is always data transmitted at SCK falling edge, then data is to be sampled at SCK rising edge.
But if you were to refer to https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT800.pdf Figure 6-1 with Tod, the input to SPI slave is sampled at SCK rising edge. SPI slave output then seems it can be sampled at SCK falling or rising at the SPI master side.
And if you refer to https://datasheet.lcsc.com/lcsc/2101051104_Goodix-Technology-GR5515IGND_C2680494.pdf Figure 10-21 with tHMO/tHMO, this is similar that the data seems can be safely sampled at both SPIM SCK rising and falling edge.
Especially when SPIM SCK freq is high, then at SPIM side using SCK falling edge to sample input data can relax the setup time for SPIS output to SPIM input path. Is my understanding correct? And is people doing this way?

Related

Transferring and receiving more than 32-bits over Teensy 4.1 SPI at same time

I want to read and transfer more than 32 bits over SPI using teensy 4.1 on Arduino IDE.
Hi! I want to read binary data comprising of 100 bits. The data is synced at 1.8Mhz clock and I am able to sync Teensy 4.1 with it. What I want to do is, I want to read that data through SPI with no CS pin by making the SPI clock 1.8Mhz. I achieved this but the microcontroller SPI is running in non-continuous mode i.e., by observing the signals on Oscilloscope,
> CS goes LOW
> 32 Clock pulses
> CS Goes High
> CS Goes low
> again 32 clock pulses.
> CS goes low
again, I am reading data over SPI just using the MOSI and MISO pins. Clock and CS pins are for observation only.

How to understand the SPI clock modes?

There are many links on the web describing the SPI timing/clock modes. E.g., the following picture from here indicates 4 combinations of CPOL/CPHA determines when to sampling/transmitting data wrt clock rising/failing edges:
And for SPI to work correctly, it's required that both the controller(or master) and the device (or slave) should work in the same clock mode.
Few days ago, I encounter a datasheet which describes a QSPI controller, saying that it only supports mode 1 (CPOL=0, CPHA=1). It also containing a AC timing requirement for the SPI interface, as below:
The timing diagram confirms that the controller works in mode 1: sclk at low when idle, and data sampled at falling edge of sclk. So far so good.
What puzzled me is another statement in the user guide: "The SPI only supports SPI mode 1. but, if the SPI device follows the Soc AC timing, it is usable regardless of the mode." How it's possible? Apparently it violates the "same mode" rule. For example, how it works when the controller is in mode 1 (sampling on falling edge) and the device is in mode 0 or 3 (sampling on the rising edge)?
Btw, a working system shows that the controller actually works with a QSPI flash devices whose datasheet tells that it only support mode 0 and 3. This implies that I must miss some points in understanding the SPI clock modes...
Any explanation is appreciated.
Your "mode 1" qspi controller is not behaving like a normal mode 1 spi controller.
Look at the timing diagram: It outputs new data on MOSI on the falling edge of CLK, AND it samples MISO on the falling edge of CLK. That is not equivalent of any of the four SPI modes.
It is done to allow much higher performance.
(Q)SPI flash memories sample MOSI on the rising edge of CLK, and output new data on MISO on the falling edge of CLK.
Regular SPI controllers will sample MISO on the rising edge of CLK. This means the entire round-trip of CLK from controller to flash, flash access, and MISO back to the controller, must happen in just half a clock cycle.
This will typically limit you to 25MHz or less.
By having the controller sample MISO on the falling edge, the round-trip gets a whole clock period worth of time, which means you can reach 50MHz or more.
In general:
In SPI there is only one clock edge that matters to the receiver. In modes 0 and 3 it is the rising edge, in modes 1 and 2 it is the falling edge.
The receiver requires the data that it is going to read to be valid for some short period immediately before the edge that matters (called the "setup time") and requires that it remains valid for some short period after the edge that matters (called the "hold time").
Outside this small window (the setup time plus the hold time) the value of the data lines does not matter at all. The receiver does not do anything at the other clock edge to that the one where it samples the data, and the line may have any value then, including an intermediate voltage which is between high and low.
The only responsibility of each transmitter is to meet the requirements of the receiver. That is, the master must control MOSI in such a way that that it meets the requirements of the slave, and the slave must control MISO so that it meets the requirements of the master.
At slow speeds the easiest way for each transmitter to meet the requirements of the other device is to change the data at the moment of the opposite clock edge to the one the receiver cares about. This is the implies using the same clock mode at both ends as shown in the diagrams in your question.
At higher speeds this is not always ideal and the timing method described in your datasheet extract is widespread and perfectly normal.
For your particular device:
Your slave device samples MOSI on the falling edge of CLK. It requires the master to have driven MOSI to the correct level at least tIS (input sample time) before this and to hold it for at least tIH (input hold time) after this.
In the other direction, your slave device holds the previous MISO value for at least tCLQX (clock low output hold time) after the edge where it assumes the master would have sampled it.
Your slave device outputs the next MISO value no later than tCLQV (clock low to output valid time) after the same falling edge.
In the case of the naive timing diagrams in your question, you would assume that tCLQX would have the same value as tCLQV and would also coincide with the next rising edge of the clock, but the datasheet is saying that this does not have to be the case.

what is the best way to design a shift register with stm32

I am using a STM32F031K6, clocked at 40MHz, and I want to design a program which acts as a looping shift register - an external trigger is used to clock it, the values in the shift register left shift every time a rising/falling edge is received. the output is one pin either high or low.
I need to make the time between the clocking edge and the output less than 0.5uS, or failing that as quick as possible. The values of the shift register can be changed and the length can also be changed, but for now I'm just starting with a byte like 11000010 .
I initially thought to implement this with an external interrupt but it was suggested there may be a better way to implement it
any help much appreciated
You might use the SPI peripheral of the STM32F0 for your task. When configured in slave mode, each time an external clock edge is detected on the SCK signal, the MISO will be set to the next bit of a value loaded into an internal shift register via the SPI data register.
Check out the chapter on the Serial peripheral interface (SPI) in STM32F0 reference manual.
Especially have a look at the sections addressing the following keywords:
General description: SPI block diagram
Slave Mode (Master selection: Slave configuration)
Simplex communication: Transmit-only mode (RXONLY=0)
Slave select (NSS) pin management: Software NSS management (SSM = 1)
Data frame format (data size can be set from 4-bit up to 16-bit length)
Configuration of SPI
The SPI unit is highly configurable, e.g. regarding the polarity of clock signal. Since it is an independent hardware unit, it should be able to handle your 0.5us reaction time requirement. The MCU firmware needs to set up the SPI unit and then provide new data to the SPI unit, each time the Tx buffer empty flag (TXE) is set. This can also be done by interrupt (TXEIE) or even using a DMA channel (TXDMAEN) with a circular buffer. In the latter case the "shift register functionality" runs completely independent of the MCU core (after setup).

Where is the SPI multiplexer of my dreams?

Consider you have an SPI bus with only a single chip select.
Is there a chip such that I can connect 8 or more devices to that SPI bus?
To simplify things, you may assume that all devices agree on the SPI mode (data needs to be valid on a rising edge). Also, all devices are of the time, where chip selects stays low for the whole transfer, and is not toggled after each word.
The SPI multiplexer could have 4 inputs:
MISO, MOSI, input clock, master chip select
and 9 outputs:
output clock, 8 slave chip selects
MISO and MOSI are connected directly to the slaves. The slaves have their SPI clock connected to the output clock and their chips selects are connected to one of the 8 slave chip selects.
The SPI multiplexer would take the two bytes of each SPI transfer as its own input. The first byte could indicate which slave is to be selected. For configuration of the multiplexer, a ninth address could be allowed.
If one of the 8 slaves is selected, the multiplexer would then activate the slave's chip select after the first byte (or even after the first few bits of the first byte). The output clock would activate with the start of the second byte and it would be synchronous to the input clock. Leaving the clock inactive during the first byte makes sure that the slaves never take notice of the first byte.
Such a chip does not seem to exist. I found solutions with two chip selects, but that's not an option to upgrade old hardware designs with just a single chip select.
Does such a thing exist?
It does not exist because there is no need for it. Often CS is only controlled from software as a regular gpio before initiating an SPI transfer, then you could just wire the different slaves to different GPIO and use them as CS. If the CS is generated and needed by the hardware block you gate this signal with the external chip select to choose the wanted slave.
Your proposed one byte to select bus would also break all software which relay on writing from and reading to the same buffer, and it would be decrease the available bandwidth with control signals.
As a side note with recent development with secure enclaves and Trustzone the trend is not to share spi buses but rather hard wire them so only code running in the trusted part of the SoC will be able to access the connected slave.

Frequency components less than centre frequency of USRP

I have two USRP N210s connected together as a receiver and transmitter and I'm trying to send multi tone signals across the channel. However, I am finding extra frequency spikes mirrored about the centre frequency for when I send a signal with more than 2 tones.
I am using a low pass filter at the output with a cutoff frequency of 200kHz and the signal I am sending is constrained to 0-200kHz. I have an out of tree module that creates the multi tone signals that are evenly distributed within this bandwidth.
As I increase the number of tones the reflected frequency components become more and more prominent to the point at which I can barely correlate the input and output signals.
This is what the transmitted multi tone signals looks like
And here is the flowchart at the receiver end
The center frequency of the USRP source (receiver) is given by
uhd.tune_request(center_freq, rf_freq=(center_freq + lo_offset),rf_freq_policy=uhd.tune_request.POLICY_MANUAL)
which evaluates to 2.48GHz for which is the baseband frequency for the transmitting USRP
It may be something to do with the down conversion in the USRP or when GNURadio is actually sampling from the receiver with respect to this process.
Removing the LPF and connecting the FFT sink to the USRP source doesn't fix anything. The extra frequency spikes are still there (assuming the number of tones > 2)
You're not specifying how you transmit your tones into the RX port of your USRP, but the most likely thing to have happened here is that you didn't use sufficient attenuation between TX and RX – and you're saturating the RX amplifier chain (hopefully not damaging it).
By saturating, you drive them into nonlinear operation, which would lead to a broadband intermodulation.