# Full-duplex SPI transfer mechanism in TrustZone

Source: [https://docs.qualcomm.com/doc/80-88500-1/topic/39_Full_duplex_SPI_transfer_mechanism_in_TrustZone.html](https://docs.qualcomm.com/doc/80-88500-1/topic/39_Full_duplex_SPI_transfer_mechanism_in_TrustZone.html)

Due to the symmetric transfer mechanism, the `TZ_SPI` driver has specific
      requirements for using the TZ SPI API for full-duplex transfers. If these requirements are not
      met, the transfer is aborted abnormally. To use the `qsee_spi_full_duplex` API
      for full-duplex transfer, ensure the following:

- Only one buffer is used for both the `p_write_info` and
          `p_read_info` structures. The buffer should be large enough to contain the
        data to write (out) and the data to read (in).
- The same buffer length is used, `buf_len` (sum of the read and write length), for
        the `write_info` and `read_info` structures.

Following example illustrates the changes required to the `write_info` and
        `read_info` structures, before encapsulating these variables into the
        `qsee_spi_full_duplex` API:

    + const size_t total_transfer_size = tx_bytes + rx_bytes;
     qsee_spi_transaction_info_t write_info = 
    {
    - .buf_addr = tx, - .buf_len = tx_bytes, + .buf_addr = rx, + .buf_len = total_transfer_size,
    .total_bytes = 0, 
    }; 
    qsee_spi_transaction_info_t read_info = 
    {
    .buf_addr = rx, 
    - .buf_len = rx_bytes, 
    + .buf_len = total_transfer_size, 
    .total_bytes = 0, 
    }; 
    + if (tx != rx) { + memmove(rx, tx, tx_bytes); 
    + } 
    + 
    + memset(rx + tx_bytes, 0, rx_bytes); 
    +
    Copy to clipboard

**Parent Topic:** [SPI in QUP](https://docs.qualcomm.com/doc/80-88500-1/topic/30_SPI_in_QUP_v3.html)

Last Published: Aug 18, 2023

[Previous Topic
Enable SPI bus in TrustZone](https://docs.qualcomm.com/bundle/publicresource/80-88500-1/topics/38_Enable_SPI_bus_in_TrustZone.md) [Next Topic
Full-cycle timing mechanism of SPI bus](https://docs.qualcomm.com/bundle/publicresource/80-88500-1/topics/40_Full_cycle_timing_mechanism_of_SPI_bus.md)