From 435adaa22ad97c6d6c1b3b2a53263f176248d63c Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 19 Nov 2018 19:20:28 +0800 Subject: [PATCH] spi_master: add check for trans len The driver decide whether use MOSI/MISO phases according to the buffer address together with the SPI_TRANS_USE_*DATA. However someone may assue that these phases will be skipped when the ``length``/``rxlength`` is set to 0. In fact it is a feature that ``rxlength`` is allowed to be set to 0 when tx and rx have the same length, and the driver fill the ``rxlength`` to ``length``. An error check is added when the rxlength is 0 but there is data to be sent. --- components/driver/spi_master.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/components/driver/spi_master.c b/components/driver/spi_master.c index 2478278796..514a3be683 100644 --- a/components/driver/spi_master.c +++ b/components/driver/spi_master.c @@ -1103,6 +1103,14 @@ static SPI_MASTER_ISR_ATTR esp_err_t check_trans_valid(spi_device_handle_t handl SPI_CHECK(!((trans_desc->flags & (SPI_TRANS_MODE_DIO|SPI_TRANS_MODE_QIO)) && (!(handle->cfg.flags & SPI_DEVICE_HALFDUPLEX))), "incompatible iface params", ESP_ERR_INVALID_ARG); SPI_CHECK( !(handle->cfg.flags & SPI_DEVICE_HALFDUPLEX) || host->dma_chan == 0 || !(trans_desc->flags & SPI_TRANS_USE_RXDATA || trans_desc->rx_buffer != NULL) || !(trans_desc->flags & SPI_TRANS_USE_TXDATA || trans_desc->tx_buffer!=NULL), "SPI half duplex mode does not support using DMA with both MOSI and MISO phases.", ESP_ERR_INVALID_ARG ); + //MOSI phase is skipped only when both tx_buffer and SPI_TRANS_USE_TXDATA are not set. + SPI_CHECK(trans_desc->length != 0 || (trans_desc->tx_buffer == NULL && !(trans_desc->flags & SPI_TRANS_USE_TXDATA)), + "trans tx_buffer should be NULL and SPI_TRANS_USE_TXDATA should be cleared to skip MOSI phase.", ESP_ERR_INVALID_ARG); + //MISO phase is skipped only when both rx_buffer and SPI_TRANS_USE_RXDATA are not set. + //If set rxlength=0 in full_duplex mode, it will be automatically set to length + SPI_CHECK(!(handle->cfg.flags & SPI_DEVICE_HALFDUPLEX) || trans_desc->rxlength != 0 || + (trans_desc->rx_buffer == NULL && ((trans_desc->flags & SPI_TRANS_USE_RXDATA)==0)), + "trans rx_buffer should be NULL and SPI_TRANS_USE_RXDATA should be cleared to skip MISO phase.", ESP_ERR_INVALID_ARG); //In Full duplex mode, default rxlength to be the same as length, if not filled in. // set rxlength to length is ok, even when rx buffer=NULL if (trans_desc->rxlength==0 && !(handle->cfg.flags & SPI_DEVICE_HALFDUPLEX)) {