Skip to content

Commit 5fdc978

Browse files
committed
Merge branch 'doc/spi_speed' into 'master'
doc(spi_master): add descriptions about the SPI master speed See merge request idf/esp-idf!2145
2 parents 393f3da + e0b05c2 commit 5fdc978

File tree

1 file changed

+158
-14
lines changed

1 file changed

+158
-14
lines changed

docs/en/api-reference/peripherals/spi_master.rst

Lines changed: 158 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,13 @@ A transaction on the SPI bus consists of five phases, any of which may be skippe
5757
* The dummy phase. The phase is configurable, used to meet the timing requirements.
5858
* The read phase. The slave sends data to the master.
5959

60-
In full duplex, the read and write phases are combined, causing the SPI host to read and
61-
write data simultaneously. The total transaction length is decided by
60+
In full duplex mode, the read and write phases are combined, and the SPI host reads and
61+
writes data simultaneously. The total transaction length is decided by
6262
``command_bits + address_bits + trans_conf.length``, while the ``trans_conf.rx_length``
6363
only determins length of data received into the buffer.
6464

65-
In half duplex, the length of write phase and read phase are decided by ``trans_conf.length`` and
66-
``trans_conf.rx_length`` respectively. ** Note that a half duplex transaction with both a read and
67-
write phase is not supported when using DMA. ** If such transaction is needed, you have to use one
68-
of the alternative solutions:
69-
70-
1. use full-duplex mode instead.
71-
2. disable the DMA by set the last parameter to 0 in bus initialization function just as belows:
72-
``ret=spi_bus_initialize(VSPI_HOST, &buscfg, 0);``
73-
74-
this may prohibit you from transmitting and receiving data longer than 32 bytes.
75-
3. try to use command and address field to replace the write phase.
65+
While in half duplex mode, the host have independent write and read phases. The length of write phase and read phase are
66+
decided by ``trans_conf.length`` and ``trans_conf.rx_length`` respectively.
7667

7768
The command and address phase are optional in that not every SPI device will need to be sent a command
7869
and/or address. This is reflected in the device configuration: when the ``command_bits`` or ``address_bits``
@@ -82,6 +73,39 @@ Something similar is true for the read and write phase: not every transaction ne
8273
as well as data to be read. When ``rx_buffer`` is NULL (and SPI_USE_RXDATA) is not set) the read phase
8374
is skipped. When ``tx_buffer`` is NULL (and SPI_USE_TXDATA) is not set) the write phase is skipped.
8475

76+
GPIO matrix and native pins
77+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
79+
Most peripheral pins in ESP32 can directly connect to a GPIO, which is called *native pin*. When the peripherals are
80+
required to work with other pins than the native pins, ESP32 use a *GPIO matrix* to realize this. If one of the pins is
81+
not native, the driver automatically routes all the signals to the GPIO matrix, which works under 80MHz. The signals are
82+
sampled and sent to peripherals or the GPIOs.
83+
84+
When the GPIO matrix is used, signals cannot propogate to the peripherals over 40MHz, and the setup time of MISO is very
85+
likely violated. Hence the clock frequency limitation is a little lower than the case without GPIO matrix.
86+
87+
Native pins for SPI controllers are as below:
88+
89+
+----------+------+------+
90+
| Pin Name | HSPI | VSPI |
91+
+ +------+------+
92+
| | GPIO Number |
93+
+==========+======+======+
94+
| CS0* | 15 | 5 |
95+
+----------+------+------+
96+
| SCLK | 14 | 18 |
97+
+----------+------+------+
98+
| MISO | 12 | 19 |
99+
+----------+------+------+
100+
| MOSI | 13 | 23 |
101+
+----------+------+------+
102+
| QUADWP | 2 | 22 |
103+
+----------+------+------+
104+
| QUADHD | 4 | 21 |
105+
+----------+------+------+
106+
107+
note * Only the first device attaching to the bus can use CS0 pin.
108+
85109
Using the spi_master driver
86110
^^^^^^^^^^^^^^^^^^^^^^^^^^^
87111

@@ -125,21 +149,141 @@ Write and read phases
125149

126150
Normally, data to be transferred to or from a device will be read from or written to a chunk of memory
127151
indicated by the ``rx_buffer`` and ``tx_buffer`` members of the transaction structure.
128-
When DMA is enabled for transfers, these buffers are highly recommended to meet the requirements as belows:
152+
When DMA is enabled for transfers, these buffers are highly recommended to meet the requirements as below:
129153

130154
1. allocated in DMA-capable memory using ``pvPortMallocCaps(size, MALLOC_CAP_DMA)``;
131155
2. 32-bit aligned (start from the boundary and have length of multiples of 4 bytes).
132156

133157
If these requirements are not satisfied, efficiency of the transaction will suffer due to the allocation and
134158
memcpy of temporary buffers.
135159

160+
.. note:: Half duplex transactions with both read and write phases are not supported when using DMA. See
161+
:ref:`spi_known_issues` for details and workarounds.
162+
136163
Sometimes, the amount of data is very small making it less than optimal allocating a separate buffer
137164
for it. If the data to be transferred is 32 bits or less, it can be stored in the transaction struct
138165
itself. For transmitted data, use the ``tx_data`` member for this and set the ``SPI_USE_TXDATA`` flag
139166
on the transmission. For received data, use ``rx_data`` and set ``SPI_USE_RXDATA``. In both cases, do
140167
not touch the ``tx_buffer`` or ``rx_buffer`` members, because they use the same memory locations
141168
as ``tx_data`` and ``rx_data``.
142169

170+
Speed and Timing Considerations
171+
-------------------------------
172+
173+
Transferring speed
174+
^^^^^^^^^^^^^^^^^^
175+
176+
There're two factors limiting the transferring speed: (1) The transaction interval, (2) The SPI clock frequency used.
177+
When large transactions are used, the clock frequency determines the transferring speed; while the interval effects the
178+
speed a lot if small transactions are used.
179+
180+
1. Transaction interval: The interval mainly comes from the cost of FreeRTOS queues and the time switching between
181+
tasks and the ISR. It also takes time for the software to setup spi peripheral registers as well as copy data to
182+
FIFOs, or setup DMA links. Depending on whether the DMA is used, the interval of an one-byte transaction is around
183+
25us typically.
184+
185+
1. The CPU is blocked and switched to other tasks when the
186+
transaction is in flight. This save the cpu time but increase the interval.
187+
2. When the DMA is enabled, it needs about 2us per transaction to setup the linked list. When the master is
188+
transferring, it automatically read data from the linked list. If the DMA is not enabled,
189+
CPU has to write/read each byte to/from the FIFO by itself. Usually this is faster than 2us, but the
190+
transaction length is limited to 32 bytes for both write and read.
191+
192+
Typical transaction interval with one byte data is as below:
193+
194+
+-----------------------+---------+
195+
| Transaction Time (us) | Typical |
196+
+=======================+=========+
197+
| DMA | 24 |
198+
+-----------------------+---------+
199+
| No DMA | 22 |
200+
+-----------------------+---------+
201+
202+
2. SPI clock frequency: Each byte transferred takes 8 times of the clock period *8/fspi*. If the clock frequency is
203+
too high, some functions may be limited to use. See :ref:`timing_considerations`.
204+
205+
For a normal transaction, the overall cost is *20+8n/Fspi[MHz]* [us] for n bytes tranferred
206+
in one transaction. Hence the transferring speed is : *n/(20+8n/Fspi)*. Example of transferring speed under 8MHz
207+
clock speed:
208+
209+
+-----------+----------------------+--------------------+------------+-------------+
210+
| Frequency | Transaction Interval | Transaction Length | Total Time | Total Speed |
211+
| | | | | |
212+
| [MHz] | [us] | [bytes] | [us] | [kBps] |
213+
+===========+======================+====================+============+=============+
214+
| 8 | 25 | 1 | 26 | 38.5 |
215+
+-----------+----------------------+--------------------+------------+-------------+
216+
| 8 | 25 | 8 | 33 | 242.4 |
217+
+-----------+----------------------+--------------------+------------+-------------+
218+
| 8 | 25 | 16 | 41 | 490.2 |
219+
+-----------+----------------------+--------------------+------------+-------------+
220+
| 8 | 25 | 64 | 89 | 719.1 |
221+
+-----------+----------------------+--------------------+------------+-------------+
222+
| 8 | 25 | 128 | 153 | 836.6 |
223+
+-----------+----------------------+--------------------+------------+-------------+
224+
225+
When the length of transaction is short, the cost of transaction interval is really high. Please try to squash data
226+
into one transaction if possible to get higher transfer speed.
227+
228+
.. _timing_considerations:
229+
230+
Timing considerations
231+
^^^^^^^^^^^^^^^^^^^^^
232+
Due to the input delay of MISO pin, ESP32 SPI master cannot read data at very high speed. The frequency allowed is
233+
rather low when the GPIO matrix is used. Currently only frequency not greater than 8.8MHz is fully supported. When the
234+
frequency is higher, you have to use the native pins or the *dummy bit workaround*.
235+
236+
.. _dummy_bit_workaround:
237+
238+
**Dummy bit workaround:** We can insert dummy clocks (during which the host does not read data) before the read phase
239+
actually begins. The slave still sees the dummy clocks and gives out data, but the host does not read until the read
240+
phase. This compensates the lack of setup time of MISO required by the host, allowing the host reading at higher
241+
frequency.
242+
243+
The maximum frequency (in MHz) host can read (or read and write) under different conditions is as below:
244+
245+
+-------------+-------------+-----------+-----------------------------+
246+
| Frequency Limit | Dummy Bits| Comments |
247+
+-------------+-------------+ Used + +
248+
| GPIO matrix | Native pins | By Driver | |
249+
+=============+=============+===========+=============================+
250+
| 8.8 | N.M. | 0 | |
251+
+-------------+-------------+-----------+-----------------------------+
252+
| N.M. | N.M. | 1 | Half Duplex, no DMA allowed |
253+
+-------------+-------------+-----------+ +
254+
| N.M. | N.M. | 2 | |
255+
+-------------+-------------+-----------+-----------------------------+
256+
257+
N.M.: Not Measured Yet.
258+
259+
And if the host only writes, the *dummy bit workaround* is not used and the frequency limit is as below:
260+
261+
+-------------+----------------------+
262+
| GPIO matrix | Native pins |
263+
+=============+======================+
264+
| 40 | 80 |
265+
+-------------+----------------------+
266+
267+
.. _spi_known_issues:
268+
269+
Known Issues
270+
------------
271+
272+
1. Half duplex mode is not compatible with DMA when both writing and reading phases exist.
273+
274+
If such transactions are required, you have to use one of the alternative solutions:
275+
276+
1. use full-duplex mode instead.
277+
2. disable the DMA by setting the last parameter to 0 in bus initialization function just as below:
278+
``ret=spi_bus_initialize(VSPI_HOST, &buscfg, 0);``
279+
280+
this may prohibit you from transmitting and receiving data longer than 32 bytes.
281+
3. try to use command and address field to replace the write phase.
282+
283+
2. Full duplex mode is not compatible with the *dummy bit workaround*, hence the frequency is limited. See :ref:`dummy
284+
bit speed-up workaround <dummy_bit_workaround>`.
285+
286+
143287
Application Example
144288
-------------------
145289

0 commit comments

Comments
 (0)