ESP-IDF Programming Guide Logo
ESP32-S2
v4.3-beta1
  • Get Started
  • API Reference
    • Networking
    • Peripherals
    • Protocols
      • ASIO
      • ESP-MQTT
      • ESP-TLS
      • HTTP Client
      • HTTP Server
      • HTTPS Server
      • ICMP Echo
      • Local Control
      • mDNS
      • Modbus
      • Websocket Client
      • ESP Serial Slave Link
        • Overview
        • Espressif Device protocols
        • Terminology
        • Services provided by ESP slave
        • Initialization of ESP Serial Slave Link
        • APIs
        • Application Example
        • API Reference
      • Certificate Bundle
      • IP Network Layer
    • Provisioning
    • Storage
    • System
    • Configuration Options
    • Error Codes Reference
  • H/W Reference
  • API Guides
  • Libraries and Frameworks
  • Contribute
  • Versions
  • Resources
  • Copyrights
  • About
  • 语言/Languages
ESP-IDF Programming Guide
  • »
  • API Reference »
  • Application Protocols »
  • ESP Serial Slave Link
  • Edit on GitHub

ESP Serial Slave Link¶

Overview¶

Espressif provides several chips that can work as slaves. These slave devices rely on some common buses, and have their own communication protocols over those buses. The esp_serial_slave_link component is designed for the master to communicate with ESP slave devices through those protocols over the bus drivers.

After an esp_serial_slave_link device is initialized properly, the application can use it to communicate with the ESP slave devices conveniently.

Espressif Device protocols¶

For more details about Espressif device protocols, see the following documents.

  • ESP SPI Slave HD (Half Duplex) Mode Protocol

Terminology¶

  • ESSL: Abbreviation for ESP Serial Slave Link, the component described by this document.

  • Master: The device running the esp_serial_slave_link component.

  • ESSL device: a virtual device on the master associated with an ESP slave device. The device context has the knowledge of the slave protocol above the bus, relying on some bus drivers to communicate with the slave.

  • ESSL device handle: a handle to ESSL device context containing the configuration, status and data required by the ESSL component. The context stores the driver configurations, communication state, data shared by master and slave, etc.

    The context should be initialized before it is used, and get deinitialized if not used any more. The master application operates on the ESSL device through this handle.

  • ESP slave: the slave device connected to the bus, which ESSL component is designed to communicate with.

  • Bus: The bus over which the master and the slave communicate with each other.

  • Slave protocol: The special communication protocol specified by Espressif HW/SW over the bus.

  • TX buffer num: a counter, which is on the slave and can be read by the master, indicates the accumulated buffer numbers that the slave has loaded to the hardware to receive data from the master.

  • RX data size: a counter, which is on the slave and can be read by the master, indicates the accumulated data size that the slave has loaded to the hardware to send to the master.

Services provided by ESP slave¶

There are some common services provided by the Espressif slaves:

  1. Tohost Interrupts: The slave can inform the master about certain events by the interrupt line. (optional)

  2. Frhost Interrupts: The master can inform the slave about certain events.

  3. Tx FIFO (master to slave): the slave can send data in stream to the master. The SDIO slave can also indicate it has new data to send to master by the interrupt line.

    The slave updates the TX buffer num to inform the master how much data it can receive, and the master then read the TX buffer num, and take off the used buffer number to know how many buffers are remaining.

  4. Rx FIFO (slave to master): the slave can receive data from the master in units of receiving buffers.

    The slave updates the RX data size to inform the master how much data it has prepared to send, and then the master read the data size, and take off the data length it has already received to know how many data is remaining.

  5. Shared registers: the master can read some part of the registers on the slave, and also write these registers to let the slave read.

The services provided by the slave depends on the slave’s model. See SPI Slave Capabilities of Espressif chips for more details.

Initialization of ESP Serial Slave Link¶

ESP SDIO Slave¶

The ESP SDIO slave link (ESSL SDIO) devices relies on the sdmmc component. It includes the usage of communicating with ESP SDIO Slave device via SDSPI feature. The ESSL device should be initialized as below:

  1. Initialize a sdmmc card (see :doc:` Document of SDMMC driver </api-reference/storage/sdmmc>`) structure.

  2. Call sdmmc_card_init() to initialize the card.

  3. Initialize the ESSL device with essl_sdio_config_t. The card member should be the sdmmc_card_t got in step 2, and the recv_buffer_size member should be filled correctly according to pre-negotiated value.

  4. Call essl_init() to do initialization of the SDIO part.

  5. Call essl_wait_for_ready() to wait for the slave to be ready.

ESP SPI Slave¶

Note

If you are communicating with the ESP SDIO Slave device through SPI interface, you should use the SDIO interface instead.

Hasn’t been supported yet.

APIs¶

After the initialization process above is performed, you can call the APIs below to make use of the services provided by the slave:

Tohost Interrupts (optional)¶

  1. Call essl_get_intr_ena() to know which events will trigger the interrupts to the master.

  2. Call essl_set_intr_ena() to set the events that will trigger interrupts to the master.

  3. Call essl_wait_int() to wait until interrupt from the slave, or timeout.

  4. When interrupt is triggered, call essl_get_intr() to know which events are active, and call essl_clear_intr() to clear them.

Frhost Interrupts¶

  1. Call essl_send_slave_intr() to trigger general purpose interrupt of the slave.

TX FIFO¶

  1. Call essl_get_tx_buffer_num() to know how many buffers the slave has prepared to receive data from the master. This is optional. The master will poll tx_buffer_num when it try to send packets to the slave, until the slave has enough buffer or timeout.

  2. Call essl_send_paket() to send data to the slave.

RX FIFO¶

  1. Call essl_get_rx_data_size() to know how many data the slave has prepared to send to the master. This is optional. When the master tries to receive data from the slave, it will update the rx_data_size for once, if the current rx_data_size is shorter than the buffer size the master prepared to receive. And it may poll the rx_data_size if the rx_dat_size keeps 0, until timeout.

  2. Call essl_get_packet() to receive data from the slave.

Reset counters (Optional)¶

Call essl_reset_cnt() to reset the internal counter if you find the slave has reset its counter.

Application Example¶

The example below shows how ESP32 SDIO host and slave communicate with each other. The host use the ESSL SDIO.

peripherals/sdio.

Please refer to the specific example README.md for details.

API Reference¶

Header File¶

  • esp_serial_slave_link/include/esp_serial_slave_link/essl.h

Functions¶

esp_err_t essl_init(essl_handle_t handle, uint32_t wait_ms)¶

Initialize the slave.

Return

ESP_OK if success, or other value returned from lower layer init.

Parameters
  • handle: Handle of a essl device.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_wait_for_ready(essl_handle_t handle, uint32_t wait_ms)¶

Wait for interrupt of a ESP32 slave device.

Return

  • ESP_OK if success

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_get_tx_buffer_num(essl_handle_t handle, uint32_t *out_tx_num, uint32_t wait_ms)¶

Get buffer num for the host to send data to the slave. The buffers are size of buffer_size.

Return

  • ESP_OK Success

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • out_tx_num: Output of buffer num that host can send data to ESP32 slave.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_get_rx_data_size(essl_handle_t handle, uint32_t *out_rx_size, uint32_t wait_ms)¶

Get amount of data the ESP32 slave preparing to send to host.

Return

  • ESP_OK Success

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • out_rx_size: Output of data size to read from slave.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_reset_cnt(essl_handle_t handle)¶

Reset the counters of this component. Usually you don’t need to do this unless you know the slave is reset.

Parameters
  • handle: Handle of a essl device.

esp_err_t essl_send_packet(essl_handle_t handle, const void *start, size_t length, uint32_t wait_ms)¶

Send a packet to the ESP32 slave. The slave receive the packet into buffers whose size is buffer_size (configured during initialization).

Return

  • ESP_OK Success

  • ESP_ERR_TIMEOUT No buffer to use, or error ftrom SDMMC host controller

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • start: Start address of the packet to send

  • length: Length of data to send, if the packet is over-size, the it will be divided into blocks and hold into different buffers automatically.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_get_packet(essl_handle_t handle, void *out_data, size_t size, size_t *out_length, uint32_t wait_ms)¶

Get a packet from ESP32 slave.

Return

  • ESP_OK Success, all the data are read from the slave.

  • ESP_ERR_NOT_FINISHED Read success, while there’re data remaining.

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • [out] out_data: Data output address

  • size: The size of the output buffer, if the buffer is smaller than the size of data to receive from slave, the driver returns ESP_ERR_NOT_FINISHED

  • [out] out_length: Output of length the data actually received from slave.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_write_reg(essl_handle_t handle, uint8_t addr, uint8_t value, uint8_t *value_o, uint32_t wait_ms)¶

Write general purpose R/W registers (8-bit) of ESP32 slave.

Note

sdio 28-31 are reserved, the lower API helps to skip.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Address not valid.

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • addr: Address of register to write. Valid address: 0-59.

  • value: Value to write to the register.

  • value_o: Output of the returned written value.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_read_reg(essl_handle_t handle, uint8_t add, uint8_t *value_o, uint32_t wait_ms)¶

Read general purpose R/W registers (8-bit) of ESP32 slave.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Address not valid.

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • add: Address of register to read. Valid address: 0-27, 32-63 (28-31 reserved, return interrupt bits on read).

  • value_o: Output value read from the register.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_wait_int(essl_handle_t handle, uint32_t wait_ms)¶

wait for an interrupt of the slave

Return

  • ESP_ERR_NOT_SUPPORTED Currently our driver doesnot support SDIO with SPI interface.

  • ESP_OK If interrupt triggered.

  • ESP_ERR_TIMEOUT No interrupts before timeout.

Parameters
  • handle: Handle of a essl device.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_clear_intr(essl_handle_t handle, uint32_t intr_mask, uint32_t wait_ms)¶

Clear interrupt bits of ESP32 slave. All the bits set in the mask will be cleared, while other bits will stay the same.

Return

  • ESP_OK Success

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • intr_mask: Mask of interrupt bits to clear.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_get_intr(essl_handle_t handle, uint32_t *intr_raw, uint32_t *intr_st, uint32_t wait_ms)¶

Get interrupt bits of ESP32 slave.

Return

  • ESP_OK Success

  • ESP_INVALID_ARG if both intr_raw and intr_st are NULL.

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • intr_raw: Output of the raw interrupt bits. Set to NULL if only masked bits are read.

  • intr_st: Output of the masked interrupt bits. set to NULL if only raw bits are read.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_set_intr_ena(essl_handle_t handle, uint32_t ena_mask, uint32_t wait_ms)¶

Set interrupt enable bits of ESP32 slave. The slave only sends interrupt on the line when there is a bit both the raw status and the enable are set.

Return

  • ESP_OK Success

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • ena_mask: Mask of the interrupt bits to enable.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_get_intr_ena(essl_handle_t handle, uint32_t *ena_mask_o, uint32_t wait_ms)¶

Get interrupt enable bits of ESP32 slave.

Return

  • ESP_OK Success

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • ena_mask_o: Output of interrupt bit enable mask.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

esp_err_t essl_send_slave_intr(essl_handle_t handle, uint32_t intr_mask, uint32_t wait_ms)¶

Send interrupts to slave. Each bit of the interrupt will be triggered.

Return

  • ESP_OK Success

  • One of the error codes from SDMMC host controller

Parameters
  • handle: Handle of a essl device.

  • intr_mask: Mask of interrupt bits to send to slave.

  • wait_ms: Millisecond to wait before timeout, will not wait at all if set to 0-9.

Macros¶

ESP_ERR_NOT_FINISHED¶

There is still remaining data.

Type Definitions¶

typedef struct essl_dev_t *essl_handle_t¶

Handle of an ESSL device.

Header File¶

  • esp_serial_slave_link/include/esp_serial_slave_link/essl_sdio.h

Functions¶

esp_err_t essl_sdio_init_dev(essl_handle_t *out_handle, const essl_sdio_config_t *config)¶

Initialize the ESSL SDIO device and get its handle.

Return

  • ESP_OK: on success

  • ESP_ERR_NO_MEM: memory exhausted.

Parameters
  • out_handle: Output of the handle.

  • config: Configuration for the ESSL SDIO device.

esp_err_t essl_sdio_deinit_dev(essl_handle_t handle)¶

Deinitialize and free the space used by the ESSL SDIO device.

Return

  • ESP_OK: on success

  • ESP_ERR_INVALID_ARG: wrong handle passed

Parameters
  • handle: Handle of the ESSL SDIO device to deinit.

Structures¶

struct essl_sdio_config_t¶

Configuration for the essl SDIO device.

Public Members

sdmmc_card_t *card¶

The initialized sdmmc card pointer of the slave.

int recv_buffer_size¶

The pre-negotiated recv buffer size used by both the host and the slave.

Header File¶

  • esp_serial_slave_link/include/esp_serial_slave_link/essl_spi.h

Functions¶

esp_err_t essl_spi_rdbuf(spi_device_handle_t spi, uint8_t *out_data, int addr, int len, uint32_t flags)¶

Read the shared buffer from the slave.

Note

out_data should be prepared in words and in the DRAM. The buffer may be written in words by the DMA. When a byte is written, the remaining bytes in the same word will also be overwritten, even the len is shorter than a word.

Return

  • ESP_OK: on success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • out_data: Buffer for read data, strongly suggested to be in the DRAM and align to 4

  • addr: Address of the slave shared buffer

  • len: Length to read

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

esp_err_t essl_spi_wrbuf(spi_device_handle_t spi, const uint8_t *data, int addr, int len, uint32_t flags)¶

Write the shared buffer of the slave.

Note

out_data should be prepared in words and in the DRAM. The buffer may be written in words by the DMA. When a byte is written, the remaining bytes in the same word will also be overwritten, even the len is shorter than a word.

Return

  • ESP_OK: success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • data: Buffer for data to send, strongly suggested to be in the DRAM and align to 4

  • addr: Address of the slave shared buffer,

  • len: Length to write

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

esp_err_t essl_spi_rddma(spi_device_handle_t spi, uint8_t *out_data, int len, int seg_len, uint32_t flags)¶

Receive long buffer in segments from the slave through its DMA.

Note

This function combines several :cpp:func:essl_spi_rddma_seg and one :cpp:func:essl_spi_rddma_done at the end. Used when the slave is working in segment mode.

Return

  • ESP_OK: success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • out_data: Buffer to hold the received data, strongly suggested to be in the DRAM and align to 4

  • len: Total length of data to receive.

  • seg_len: Length of each segment, which is not larger than the maximum transaction length allowed for the spi device. Suggested to be multiples of 4. When set < 0, means send all data in one segment (the rddma_done will still be sent.)

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

esp_err_t essl_spi_rddma_seg(spi_device_handle_t spi, uint8_t *out_data, int seg_len, uint32_t flags)¶

Read one data segment from the slave through its DMA.

Note

To read long buffer, call :cpp:func:essl_spi_rddma instead.

Return

  • ESP_OK: success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • out_data: Buffer to hold the received data, strongly suggested to be in the DRAM and align to 4

  • seg_len: Length of this segment

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

esp_err_t essl_spi_rddma_done(spi_device_handle_t spi, uint32_t flags)¶

Send the rddma_done command to the slave. Upon receiving this command, the slave will stop sending the current buffer even there are data unsent, and maybe prepare the next buffer to send.

Note

This is required only when the slave is working in segment mode.

Return

  • ESP_OK: success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

esp_err_t essl_spi_wrdma(spi_device_handle_t spi, const uint8_t *data, int len, int seg_len, uint32_t flags)¶

Send long buffer in segments to the slave through its DMA.

Note

This function combines several :cpp:func:essl_spi_wrdma_seg and one :cpp:func:essl_spi_wrdma_done at the end. Used when the slave is working in segment mode.

Return

  • ESP_OK: success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • data: Buffer for data to send, strongly suggested to be in the DRAM and align to 4

  • len: Total length of data to send.

  • seg_len: Length of each segment, which is not larger than the maximum transaction length allowed for the spi device. Suggested to be multiples of 4. When set < 0, means send all data in one segment (the wrdma_done will still be sent.)

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

esp_err_t essl_spi_wrdma_seg(spi_device_handle_t spi, const uint8_t *data, int seg_len, uint32_t flags)¶

Send one data segment to the slave through its DMA.

Note

To send long buffer, call :cpp:func:essl_spi_wrdma instead.

Return

  • ESP_OK: success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • data: Buffer for data to send, strongly suggested to be in the DRAM and align to 4

  • seg_len: Length of this segment

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

esp_err_t essl_spi_wrdma_done(spi_device_handle_t spi, uint32_t flags)¶

Send the wrdma_done command to the slave. Upon receiving this command, the slave will stop receiving, process the received data, and maybe prepare the next buffer to receive.

Note

This is required only when the slave is working in segment mode.

Return

  • ESP_OK: success

  • or other return value from :cpp:func:spi_device_transmit.

Parameters
  • spi: SPI device handle representing the slave

  • flags: SPI_TRANS_* flags to control the transaction mode of the transaction to send.

Provide feedback about this document

Next Previous

© Copyright 2016 - 2021, Espressif Systems (Shanghai) Co., Ltd

Built with Sphinx using a theme provided by Read the Docs.
Switch Version v: v4.3-beta1
Targets
ESP32
ESP32-S2
ESP32-C3
Languages
en
zh_CN
Versions
latest
stable
v5.4.1
v5.3.3
v5.2.5
v5.1.6
v5.0.8
Prereleases
release-v5.4
release-v5.3
release-v5.2
release-v5.1
release-v5.0
release-v4.4
release-v4.3
release-v4.2
release-v4.1
release-v4.0
release-v3.3
Old Versions
v5.4
v5.3.2
v5.3.1
v5.3
v5.2.4
v5.2.3
v5.2.2
v5.2.1
v5.2
v5.1.5
v5.1.4
v5.1.3
v5.1.2
v5.1.1
v5.1
v5.0.7
v5.0.6
v5.0.5
v5.0.4
v5.0.3
v5.0.2
v5.0.1
v5.0
v4.4.8
v4.4.7
v4.4.6
v4.4.5
v4.4.4
v4.4.3
v4.4.2
v4.4.1
v4.4
v4.3.7
v4.3.6
v4.3.5
v4.3.4
v4.3.3
v4.3.2
v4.3.1
v4.3
v4.2.5
v4.2.4
v4.2.3
v4.2.2
v4.2.1
v4.2
v4.1.4
v4.1.3
v4.1.2
v4.1.1
v4.1
v4.0.4
v4.0.3
v4.0.2
v4.0.1
v4.0
v3.3.6
v3.3.5
v3.3.4
v3.3.3
v3.3.2
v3.3.1
v3.3
v3.2.5
v3.1.7
Downloads
PDF
ESP-IDF Programming Guide
Project Home