Le contrôleur SPI

publication: 10 mars 2022 / mis à jour 30 mars 2022

Read this page in english

 

Appel à collaboration

Vous développez des montages, simples ou complexes avec ESP32 et ESP32forth.

Partagez-les ici sur ce site.

ESP32forth ne pourra se développer qu'avec la collaboration active de toutes les bonnes volontés.

Vos montages peuvent aider d'autres développeurs.

Les montages des autres développeurs peuvent vous aider.

Pour proposer un article ou un montage, cliquez ici



Aperçu

fig. 1

Comme le montre la figure 1, ESP32 intègre quatre contrôleurs SPI qui peuvent être utilisés pour communiquer avec des périphériques utilisant le protocole SPI. Le contrôleur SPI0 est utilisé comme tampon pour accéder à la mémoire externe. Le contrôleur SPI1 peut être utilisé comme maître. Les contrôleurs SPI2 et SPI3 peuvent être configurés en tant que maître ou esclave. Quand il est utilisé comme maître, chaque contrôleur SPI peut piloter plusieurs signaux CS (CS0 ~ CS2) pour activer plusieurs esclaves. Les contrôleurs SPI1 ~ SPI3 partagent deux canaux DMA.

Les bus de signaux SPI sont constitués de signaux D, Q, CS0-CS2, CLK, WP et HD, comme le montre le tableau 1. Les contrôleurs SPI0 et SPI1 partagent un bus de signal via un arbitre: les signaux du bus partagé commencent par "SPI". Les contrôleurs SPI2 et SPI3 utilisent des bus de signal commençant respectivement par "HSPI" et "VSPI". Les lignes d'E/S incluses dans les bus de signaux peuvent être mappés sur des broches via le module IO_MUX ou la matrice GPIO.

Le contrôleur SPI prend en charge la communication en duplex intégral/semi-duplex à quatre lignes (lignes MOSI, MISO, CS et CLK) et communication semi-duplex à trois lignes uniquement (lignes DATA, CS et CLK) en mode GP-SPI. En mode QSPI, un SPI le contrôleur accède au flash ou à la SRAM en utilisant les bus de signal D, Q, CS0 ~ CS2, CLK, WP et HD en tant que quatre bits bus SPI parallèle. Le mappage entre les signaux de bus SPI et les signaux de fonction de broche sous différentes communications mode est indiqué dans le tableau 1.

Four-line GP-SPI
Full-duplex/half- duplex signal bus
Three-line GP-SPI
Half-duplex
signal bus
QSPI Pin Signal bus Pin function signals
SPI signal bus HSPI signal bus VSPI signal bus
MOSIDATADSPIDHSPIDVSPID
MISO-QSPIQHSPIQVSPIQ
CSCSCSSPICS0HSPICS0VSPICS0
CLKCLKCLKSPICLKHSPICLKVSPICLK
--WPSPIWPHSPIWPVSPIWP
--HDSPIHDHSPIHDVSPIHD

Table ­1. Mapping Between SPI Bus Signals and Pin Function Signals

Fonctionnalités SPI

SPI à usage général (GPSPI)

QSPI parallèle

Prise en charge SPI DMA

Matériel d'interruption SPI

GP-­SPI

Le mode maître SPI prend en charge la communication en duplex intégral/semi-duplex à quatre lignes et la communication à trois lignes communication semi-duplex. La figure 2 décrit les connexions nécessaires pour quatre lignes communications en duplex intégral/semi-duplex.

fig. 2

The SPI1 ~ SPI3 controllers can communicate with other slaves as a standard SPI master. SPI2 and SPI3 can be configured as either a master or a slave. Every SPI master can be connected to three slaves at most by default. When not using DMA, the maximum length of data received/sent in one burst is 64 bytes. The data length is in multiples of one byte.

CommandDescription
0x1Received by slave; writes data sent by the master into the slave status register via MOSI.
0x2Received by slave; writes data sent by the master into the slave data buffer via MOSI.
0x3Sent by slave; sends data in the slave buffer to master via MISO.
0x4Sent by slave; sends data in the slave status register to master via MISO.
0x6Writes master data on MOSI into data buffer and then sends the date in the slave data buffer to MISO.

Table ­2. Command Definitions Supported by GP­SPI Slave in Half­duplex Mode

GP­SPI Four­line Full­duplex Communication

When configured to four-line full-duplex mode, the ESP32 SPI can act as either a master or a slave. The length of received and sent data needs to be set by configuring the SPI_MISO_DLEN_REG, SPI_MOSI_DLEN_REG registers for master mode as well as SPI_SLV_RDBUF_DLEN_REG, SPI_SLV_WRBUF_DLEN_REG registers for slave mode. The SPI_DOUTDIN bit and SPI_USR_MOSI bit in register SPI_USER_REG should be configured to enable this communication mode. The SPI_USR bit in register SPI_CMD_REG needs to be configured to initialize a data transfer.

GP­SPI Four­line Half­duplex Communication

When configured to four-line half-duplex mode, the ESP32 SPI can act as either a master or a slave. In this mode, the SPI communication supports flexible communication format as: command + address + dummy phase + received and/or sent data. The format is specified as follows:

  1. command: length of 0 ~ 16 bits; Master Out Slave In (MOSI)
  2. address: length of 0 ~ 32/64 bits; Master Out Slave In (MOSI)
  3. dummy phase: length of 0 ~ 256 SPI clocks
  4. received and/or sent data: length of 0 ~ 512 bits (64 bytes); Master Out Slave In (MOSI) or Master In Slave Out (MISO)

The address length is up to 32 bits in GP-SPI master mode and 64 bits in QSPI master mode. The command phase, address phase, dummy phase and received/sent data phase are controlled by bits SPI_USR_COMMAND, SPI_USR_ADDR, SPI_USR_DUMMY and SPI_USR_MISO/SPI_USR_MOSI respectively in register SPI_USER_REG. A certain phase is enabled only when its corresponding control bit is set to 1. Details can be found in register description. When SPI works as a master, the register can be configured by software as required to determine whether or not to enable a certain phase.

When SPI works as a slave, the communication format must contain command, address, received and/or sent data, among which the command has several options listed in Table 7-2. During data transmission or reception, the CS signal should keep logic level low. If the CS signal is pulled up during transmission, the internal state of the slave will be reset.

The master can write the slave status register SPI_SLV_WR_STATUS_REG, and decide whether to read data from register SPI_SLV_WR_STATUS_REG or register SPI_RD_STATUS_REG via the SPI_SLV_STATUS_READBACK bit in register SPI_SLAVE1_REG. The SPI master can maintain communication with the slave by reading and writing slave status register, thus realizing complex communication with ease.

The length of received and sent data is controlled by SPI_MISO_DLEN_REG and SPI_MOSI_DLEN_REG in master mode, as well as SPI_SLV_RDBUF_DLEN_REG and SPI_SLV_WRBUF_DLEN_REG in slave mode. A reception or transmission of data is controlled by bit SPI_USR_MOSI or SPI_USR_MISO in SPI_USER_REG. The SPI_USR bit in register SPI_CMD_REG needs to be configured to initialize a data transfer.

GP-­SPI Three­line Half­duplex Communication

The three-line half-duplex communication differs from four-line half-duplex communication in that the reception and transmission shares one signal bus and that the communication format must contain command, address, received and/or sent data. Software can enable three-line half-duplex communication by configuring SPI_SIO bit in SPI_USER_REG register.

Note:

GP-­SPI Data Buffer

fig. 3

ESP32 SPI has 16 × 32 bits of data buffer to buffer data-send and data-receive operations. As is shown in Figure 7-3, received data is written from the low byte of SPI_W0_REG by default and the writing ends with SPI_W15_REG. If the data length is over 64 bytes, the extra part will be written from SPI_W0_REG.

Data buffer blocks SPI_W0_REG ~ SPI_W7_REG and SPI_W8_REG ~ SPI_W15_REG data correspond to the lower part and the higher part respectively. They can be used separately, and are controlled by the SPI_USR_MOSI_HIGHPART bit and the SPI_USR_MISO_HIGHPART bit in register SPI_USER_REG. For example, if SPI is configured as a master, when SPI_USR_MOSI_HIGHPART = 1, SPI_W8_REG ~ SPI_W15_REG are used as buffer for sending data; when SPI_USR_MISO_HIGHPART = 1, SPI_W8_REG ~ SPI_W15_REG are used as buffer for receiving data. If SPI acts as a slave, when SPI_USR_MOSI_HIGHPART = 1, SPI_W8_REG ~ SPI_W15_REG are used as buffer for receiving data; when SPI_USR_MISO_HIGHPART = 1, SPI_W8_REG ~ SPI_W15_REG are used as buffer for sending data.

Table ­3. Clock Polarity and Phase, and Corresponding SPI Register Values for SPI Master

Registersmode0mode1mode2mode3
SPI_CK_IDLE_EDGE0011
SPI_CK_OUT_EDGE0110
SPI_MISO_DELAY_MODE2(0)1(0)1(0)2(0)
SPI_MISO_DELAY_NUM0000
SPI_MOSI_DELAY_MODE0000
SPI_MOSI_DELAY_NUM0000

GP-­SPI Clock Control

The maximum output clock frequency of ESP32 GP-SPI master is fapb/2, and the maximum input clock frequency of the ESP32 GP-SPI slave is fapb/8. The master can derive other clock frequencies via frequency division.

SPI_CLKCNT_N and SPI_CLKDIV_PRE are two bits of register SPI_CLOCK_REG (Please refer to 7.7 Register Description for details). SPI_CLKCNT_H = ⌊ SPI_CLKCNT_N+1 –1⌋, SPI_CLKCNT_N=SPI_CLKCNT_L. When the SPI_CLK_EQU_SYSCLK bit in register SPI_CLOCK_REG is set to 1, and the other bits are set to 0, SPI output clock frequency is fapb. For other clock frequencies, SPI_CLK_EQU_SYSCLK needs to be 0. In slave mode, SPI_CLKCNT_N, SPI_CLKCNT_L, SPI_CLKCNT_H and SPI_CLKDIV_PRE should all be 0.

Polarité d'horloge GPSPI (CPOL) et phase d'horloge (CPHA)

La polarité d'horloge et la phase d'horloge de ESP32 SPI sont contrôlées par le bit SPI_CK_IDLE_EDGE dans le registre SPI_PIN_REG, Bit SPI_CK_OUT_EDGE et bit SPI_CK_I_EDGE dans le registre SPI_USER_REG, ainsi que SPI_MISO_DELAY_MODE[1:0] bit, SPI_MISO_DELAY_NUM[2:0] bit, SPI_MOSI_DELAY_MODE[1:0] bit, SPI_MOSI_DELAY_MUM[2:0] bit dans le registre SPI_CTRL2_REG. Le Tableau 7-3 et le Tableau 7-4 montrent la polarité et la phase de l'horloge ainsi que les enregistrer les valeurs pour ESP32 SPI maître et esclave, respectivement. Notez que pour mode0 et mode2 dans le Tableau 7-4, le les registres sont configurés différemment en mode non-DMA et en mode DMA, et que les données de l'esclave SPI sont sorties en avancer en mode DMA.

Table 4: Polarité et phase d'horloge et valeurs de registre SPI correspondantes pour l'esclave SPI

Registersmode0mode1 mode2mode3
Non-DMADMANon-DMADMA
SPI_CK_IDLE_EDGE101010
SPI_CK_I_EDGE011100
SPI_MISO_DELAY_MODE002001
SPI_MISO_DELAY_NUM020020
SPI_MOSI_DELAY_MODE200100
SPI_MOSI_DELAY_NUM230230
  1. mode0 signifie CPOL=0, CPHA=0. Lorsque SPI est inactif, la sortie d'horloge est au niveau logique bas ; changements de données sur le front descendant de l'horloge SPI et est échantillonné sur le front montant ;
  2. mode1 signifie CPOL=0, CPHA=1. Lorsque SPI est inactif, la sortie d'horloge est au niveau logique bas ; changements de données à la hausse front de l'horloge SPI et est échantillonné sur le front descendant ;
  3. mode2 signifie quand CPOL=1, CPHA=0. Lorsque SPI est inactif, la sortie d'horloge est au niveau logique haut ; changements de données sur le front montant de l'horloge SPI et est échantillonné sur le front descendant ;
  4. mode3 signifie quand CPOL=1, CPHA=1. Lorsque SPI est inactif, la sortie d'horloge est au niveau logique haut ; changements de données sur le front descendant de l'horloge SPI et est échantillonné sur le front montant.

GP-­SPI Timing

The data signals of ESP32 GP-SPI can be mapped to physical pins either via IO_MUX or via IO_MUX and GPIO matrix. Input signals will be delayed by two clkapb clock cycles when they pass through the matrix. Output signals will not be delayed.

When GP-SPI is used as master and the data signals are not received by the SPI controller via GPIO matrix, if GP-SPI output clock frequency is clkapb/2, register SPI_MISO_DELAY_MODE should be set to 0 when configuring the clock polarity. If GP-SPI output clock frequency is not higher than clkapb/4, register SPI_MISO_DELAY_MODE can be set to the corresponding value in Table 7-3 when configuring the clock polarity.

When GP-SPI is used in master mode and the data signals enter the SPI controller via the GPIO matrix:

  1. If GP-SPI output clock frequency is clkapb/2, register SPI_MISO_DELAY_MODE should be set to 0 and the dummy phase should be enabled (SPI_USR_DUMMY = 1) for one clkspi clock cycle (SPI_USR_DUMMY_CYCLELEN = 0) when configuring the clock polarity;
  2. If GP-SPI output clock frequency is clkapb/4, register SPI_MISO_DELAY_MODE should be set to 0 when configuring the clock polarity;
  3. If GP-SPI output clock frequency is not higher than clkapb/8, register SPI_MISO_DELAY_MODE can be set to the corresponding value in Table 7-3 when configuring the clock polarity

When GP-SPI is used in slave mode, the clock signal and the data signals should be routed to the SPI controller via the same path, i.e., neither the clock signal nor the data signals passes through GPIO matrix, or both of them pass through GPIO matrix. This is important in ensuring that the signals are not delayed by different time periods before they reach the SPI hardware.

fig. 4

To conclude, if signals do not pass through GPIO matrix, the SPI slave clock frequency is up to fapb/8; if signals pass through GPIO matrix, the SPI slave clock frequency is up to fapb/12. Note that (tspi/2–tpre) represents data output hold time for SPI slave in mode0 and mode2.

Register Summary

NameDescription SPI0SPI1SPI2SPI3 Acc
Control and configuration registers
SPI_CTRL_REGBit order and
QIO/DIO/QOUT/DOUT
mode settings
3FF430083FF420083FF640083FF65008R/W
SPI_CTRL2_REGTiming configuration3FF430143FF420143FF640143FF65014R/W
SPI_CLOCK_REGClock configuration3FF430183FF420183FF640183FF65018R/W
SPI_PIN_REGPolarity and CS configuration3FF430343FF420343FF640343FF65034R/W
Slave mode configuration registers
SPI_SLAVE_REGSlave mode configuration and interrupt status 3FF430383FF420383FF640383FF65038R/W
SPI_SLAVE1_REGSlave data bit lengths3FF4303C3FF4203C3FF6403C3FF6503CR/W
SPI_SLAVE2_REGDummy cycle length configuration 3FF430403FF420403FF640403FF65040R/W
SPI_SLV_WR_STATUS_REGSlave status/Part of lower master address 3FF430303FF420303FF640303FF65030R/W
SPI_SLV_WRBUF_DLEN_REGWrite-buffer operation length 3FF430483FF420483FF640483FF65048R/W
SPI_SLV_RDBUF_DLEN_REGRead-buffer operation length 3FF4304C3FF4204C3FF6404C3FF6504CR/W
SPI_SLV_RD_BIT_REGRead data operation length 3FF430643FF420643FF640643FF65064R/W
User ­defined command mode registers
SPI_CMD_REGStart user-defined command 3FF430003FF420003FF640003FF65000R/W
SPI_ADDR_REGAddress data3FF430043FF420043FF640043FF65004R/W
SPI_USER_REGUser defined command configuration 3FF4301C3FF4201C3FF6401C3FF6501CR/W
SPI_USER1_REGAddress and dummy cycle configuration 3FF430203FF420203FF640203FF65020R/W
SPI_USER2_REGCommand length and value configuration 3FF430243FF420243FF640243FF65024R/W
SPI_MOSI_DLEN_REGMOSI length3FF430283FF420283FF640283FF65028R/W
SPI_W0_REGSPI data register 03FF430803FF420803FF640803FF65080R/W
SPI_W1_REGSPI data register 13FF430843FF420843FF640843FF65084R/W
SPI_W2_REGSPI data register 23FF430883FF420883FF640883FF65088R/W
SPI_W3_REGSPI data register 33FF4308C3FF4208C3FF6408C3FF6508CR/W
SPI_W4_REGSPI data register 43FF430903FF420903FF640903FF65090R/W
SPI_W5_REGSPI data register 53FF430943FF420943FF640943FF65094R/W
SPI_W6_REGSPI data register 63FF430983FF420983FF640983FF65098R/W
SPI_W7_REGSPI data register 73FF4309C3FF4209C3FF6409C3FF6509CR/W
SPI_W8_REGSPI data register 83FF430A03FF420A03FF640A03FF650A0R/W
SPI_W9_REGSPI data register 93FF430A43FF420A43FF640A43FF650A4R/W
SPI_W10_REG SPIdata register 103FF430A83FF420A83FF640A83FF650A8R/W
SPI_W11_REG SPIdata register 113FF430AC3FF420AC3FF640AC3FF650ACR/W
SPI_W12_REG SPIdata register 123FF430B03FF420B03FF640B03FF650B0R/W
SPI_W13_REG SPIdata register 133FF430B43FF420B43FF640B43FF650B4R/W
SPI_W14_REG SPIdata register 143FF430B83FF420B83FF640B83FF650B8R/W
SPI_W15_REG SPIdata register 153FF430BC3FF420BC3FF640BC3FF650BCR/W
DMA configuration registers
SPI_DMA_CONF_REGDMA configuration register 3FF431003FF421003FF641003FF65100R/W
SPI_DMA_OUT_LINK_REGDMA outlink address and configuration 3FF431043FF421043FF641043FF65104R/W
SPI_DMA_IN_LINK_REGDMA inlink address and configuration 3FF431083FF421083FF641083FF65108R/W
SPI_DMA_STATUS_REGDMA status3FF4310C3FF4210C3FF6410C3FF6510CRO
SPI_IN_ERR_EOF_DES_ADDR_REGDescriptor address where an error occurs 3FF431203FF421203FF641203FF65120RO
SPI_IN_SUC_EOF_DES_ADDR_REGDescriptor address where EOF occurs 3FF431243FF421243FF641243FF65124RO
SPI_INLINK_DSCR_REGCurrent descriptor pointer 3FF431283FF421283FF641283FF65128RO
SPI_INLINK_DSCR_BF0_REGNext descriptor data pointer 3FF4312C3FF4212C3FF6412C3FF6512CRO
SPI_INLINK_DSCR_BF1_REGCurrent descriptor data pointer 3FF431303FF421303FF641303FF65130RO
SPI_OUT_EOF_BFR_DES_ADDR_REGRelative buffer address where EOF occurs 3FF431343FF421343FF641343FF65134RO
SPI_OUT_EOF_DES_ADDR_REGDescriptor address where EOF occurs 3FF431383FF421383FF641383FF65138RO
SPI_OUTLINK_DSCR_REGCurrent descriptor pointer 3FF4313C3FF4213C3FF6413C3FF6513CRO
SPI_OUTLINK_DSCR_BF0_REGNext descriptor data pointer 3FF431403FF421403FF641403FF65140RO
SPI_OUTLINK_DSCR_BF1_REGCurrent descriptor data pointer 3FF431443FF421443FF641443FF65144RO
SPI_DMA_RSTATUS_REGDMA memory read status 3FF431483FF421483FF641483FF65148RO
SPI_DMA_TSTATUS_REGDMA memory write status 3FF4314C3FF4214C3FF6414C3FF6514CRO
DMA interrupt registers
SPI_DMA_INT_RAW_REG Raw interrupt status3FF431143FF421143FF641143FF65114RO
SPI_DMA_INT_ST_REGMasked interrupt status 3FF431183FF421183FF641183FF65118RO
SPI_DMA_INT_ENA_REGInterrupt enable bits3FF431103FF421103FF641103FF65110R/W
SPI_DMA_INT_CLR_REGInterrupt clear bits3FF4311C3FF4211C3FF6411C3FF6511CR/W

Legal: site web personnel sans commerce / personal site without seling