From 8639e3511b84a3d393c817580a3027aa58bfe847 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Fri, 21 May 2021 11:15:55 -0300 Subject: [PATCH 1/6] spi_slave: Fix MOSI/MISO enable on transaction preparation MOSI and MISO enablement were conditioned to the existence of TX and RX buffers, respectively. This is valid for the SPI Master, but for the SPI Slave the opposite is expected. --- components/hal/spi_slave_hal_iram.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/hal/spi_slave_hal_iram.c b/components/hal/spi_slave_hal_iram.c index 5fb3f4dd1a..bf43561478 100644 --- a/components/hal/spi_slave_hal_iram.c +++ b/components/hal/spi_slave_hal_iram.c @@ -71,8 +71,8 @@ void spi_slave_hal_prepare_data(const spi_slave_hal_context_t *hal) spi_ll_slave_set_rx_bitlen(hal->hw, hal->bitlen); spi_ll_slave_set_tx_bitlen(hal->hw, hal->bitlen); - spi_ll_enable_mosi(hal->hw, (hal->tx_buffer == NULL) ? 0 : 1); - spi_ll_enable_miso(hal->hw, (hal->rx_buffer == NULL) ? 0 : 1); + spi_ll_enable_mosi(hal->hw, (hal->rx_buffer == NULL) ? 0 : 1); + spi_ll_enable_miso(hal->hw, (hal->tx_buffer == NULL) ? 0 : 1); } void spi_slave_hal_store_result(spi_slave_hal_context_t *hal) From 8f822db28cd97983e7c0214d7daf137c88560dec Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Mon, 24 May 2021 10:34:41 -0300 Subject: [PATCH 2/6] spi: Fix SPI Slave TX/RX bitlen configuring wrong registers The maximum input length for the SPI Slave should be applied to the read buffer configuration, not for the write buffer. Similarly, the output configuration should also target the write buffer. --- components/hal/esp32/include/hal/spi_ll.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/hal/esp32/include/hal/spi_ll.h b/components/hal/esp32/include/hal/spi_ll.h index d26dcab13b..efd5170309 100644 --- a/components/hal/esp32/include/hal/spi_ll.h +++ b/components/hal/esp32/include/hal/spi_ll.h @@ -744,7 +744,7 @@ static inline void spi_ll_set_mosi_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) { - hw->slv_wrbuf_dlen.bit_len = bitlen - 1; + hw->slv_rdbuf_dlen.bit_len = bitlen - 1; } /** @@ -755,7 +755,7 @@ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_tx_bitlen(spi_dev_t *hw, size_t bitlen) { - hw->slv_rdbuf_dlen.bit_len = bitlen - 1; + hw->slv_wrbuf_dlen.bit_len = bitlen - 1; } /** From fb8d9f76b354cac028733290b7f8e665842e53ad Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Mon, 31 May 2021 19:25:59 -0300 Subject: [PATCH 3/6] spi: Remove Slave TX/RX set bitlen not effective for ESP32-S2/C3/S3 Furthermore, RX_EOF_EN should only be set when SPI Slave is configured for segment transfer mode and the "ms_data_bitlen" field is configured to control the "IN_SUC_EOF" interrupt. Since "ms_data_bitlen" is not set anymore for S2, C3 and S3, "RX_EOF_EN" should be cleared. --- components/hal/esp32c3/include/hal/spi_ll.h | 5 ++--- components/hal/esp32s2/include/hal/spi_ll.h | 5 ++--- components/hal/esp32s3/include/hal/spi_ll.h | 5 ++--- components/hal/spi_slave_hal_iram.c | 3 +++ components/hal/spi_slave_hd_hal.c | 1 - 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/components/hal/esp32c3/include/hal/spi_ll.h b/components/hal/esp32c3/include/hal/spi_ll.h index dbaecbbea1..c3a66e23a5 100644 --- a/components/hal/esp32c3/include/hal/spi_ll.h +++ b/components/hal/esp32c3/include/hal/spi_ll.h @@ -584,7 +584,6 @@ static inline void spi_ll_master_set_io_mode(spi_dev_t *hw, spi_ll_io_mode_t io_ static inline void spi_ll_slave_set_seg_mode(spi_dev_t *hw, bool seg_trans) { hw->dma_conf.dma_seg_trans_en = seg_trans; - hw->dma_conf.rx_eof_en = seg_trans; } /** @@ -819,7 +818,7 @@ static inline void spi_ll_set_miso_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) { - spi_ll_set_mosi_bitlen(hw, bitlen); + //This is not used in esp32c3 } /** @@ -830,7 +829,7 @@ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_tx_bitlen(spi_dev_t *hw, size_t bitlen) { - spi_ll_set_mosi_bitlen(hw, bitlen); + //This is not used in esp32c3 } /** diff --git a/components/hal/esp32s2/include/hal/spi_ll.h b/components/hal/esp32s2/include/hal/spi_ll.h index 5993a5f34a..733022f8c0 100644 --- a/components/hal/esp32s2/include/hal/spi_ll.h +++ b/components/hal/esp32s2/include/hal/spi_ll.h @@ -545,7 +545,6 @@ static inline void spi_ll_master_set_io_mode(spi_dev_t *hw, spi_ll_io_mode_t io_ static inline void spi_ll_slave_set_seg_mode(spi_dev_t *hw, bool seg_trans) { hw->dma_conf.dma_seg_trans_en = seg_trans; - hw->dma_conf.rx_eof_en = seg_trans; } /** @@ -807,7 +806,7 @@ static inline void spi_ll_set_mosi_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) { - spi_ll_set_miso_bitlen(hw, bitlen); + //This is not used in esp32s2 } /** @@ -818,7 +817,7 @@ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_tx_bitlen(spi_dev_t *hw, size_t bitlen) { - spi_ll_set_miso_bitlen(hw, bitlen); + //This is not used in esp32s2 } /** diff --git a/components/hal/esp32s3/include/hal/spi_ll.h b/components/hal/esp32s3/include/hal/spi_ll.h index 9a7bf4f5c6..57099df19b 100644 --- a/components/hal/esp32s3/include/hal/spi_ll.h +++ b/components/hal/esp32s3/include/hal/spi_ll.h @@ -584,7 +584,6 @@ static inline void spi_ll_master_set_io_mode(spi_dev_t *hw, spi_ll_io_mode_t io_ static inline void spi_ll_slave_set_seg_mode(spi_dev_t *hw, bool seg_trans) { hw->dma_conf.dma_seg_trans_en = seg_trans; - hw->dma_conf.rx_eof_en = seg_trans; } /** @@ -827,7 +826,7 @@ static inline void spi_ll_set_miso_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) { - spi_ll_set_mosi_bitlen(hw, bitlen); + //This is not used in esp32s3 } /** @@ -838,7 +837,7 @@ static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen) */ static inline void spi_ll_slave_set_tx_bitlen(spi_dev_t *hw, size_t bitlen) { - spi_ll_set_mosi_bitlen(hw, bitlen); + //This is not used in esp32s3 } /** diff --git a/components/hal/spi_slave_hal_iram.c b/components/hal/spi_slave_hal_iram.c index bf43561478..d3a0e3924e 100644 --- a/components/hal/spi_slave_hal_iram.c +++ b/components/hal/spi_slave_hal_iram.c @@ -71,8 +71,11 @@ void spi_slave_hal_prepare_data(const spi_slave_hal_context_t *hal) spi_ll_slave_set_rx_bitlen(hal->hw, hal->bitlen); spi_ll_slave_set_tx_bitlen(hal->hw, hal->bitlen); +#ifdef CONFIG_IDF_TARGET_ESP32 + //SPI Slave mode on ESP32 requires MOSI/MISO enable spi_ll_enable_mosi(hal->hw, (hal->rx_buffer == NULL) ? 0 : 1); spi_ll_enable_miso(hal->hw, (hal->tx_buffer == NULL) ? 0 : 1); +#endif } void spi_slave_hal_store_result(spi_slave_hal_context_t *hal) diff --git a/components/hal/spi_slave_hd_hal.c b/components/hal/spi_slave_hd_hal.c index 181f915dbe..4662f676dd 100644 --- a/components/hal/spi_slave_hd_hal.c +++ b/components/hal/spi_slave_hd_hal.c @@ -150,7 +150,6 @@ void spi_slave_hd_hal_rxdma(spi_slave_hd_hal_context_t *hal, uint8_t *out_buf, s spi_ll_infifo_full_clr(hal->dev); spi_ll_clear_intr(hal->dev, SPI_LL_INTR_CMD7); - spi_ll_slave_set_rx_bitlen(hal->dev, len * 8); spi_ll_dma_rx_enable(hal->dev, 1); spi_dma_ll_rx_start(hal->dma_in, hal->rx_dma_chan, &hal->dmadesc_rx->desc); } From 4bc9e1812449297f91c1fa48dca6d4ca142ba058 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Tue, 1 Jun 2021 10:28:16 -0300 Subject: [PATCH 4/6] spi: Ensure DMA In-Link EOF is generated by trans_done on SPI Slave --- components/hal/esp32c3/include/hal/spi_ll.h | 2 ++ components/hal/esp32s2/include/hal/spi_ll.h | 3 +++ components/hal/esp32s3/include/hal/spi_ll.h | 2 ++ 3 files changed, 7 insertions(+) diff --git a/components/hal/esp32c3/include/hal/spi_ll.h b/components/hal/esp32c3/include/hal/spi_ll.h index c3a66e23a5..cf773da678 100644 --- a/components/hal/esp32c3/include/hal/spi_ll.h +++ b/components/hal/esp32c3/include/hal/spi_ll.h @@ -136,6 +136,8 @@ static inline void spi_ll_slave_init(spi_dev_t *hw) hw->user.usr_miso_highpart = 0; hw->user.usr_mosi_highpart = 0; + // Configure DMA In-Link to not be terminated when transaction bit counter exceeds + hw->dma_conf.rx_eof_en = 0; hw->dma_conf.dma_seg_trans_en = 0; //Disable unneeded ints diff --git a/components/hal/esp32s2/include/hal/spi_ll.h b/components/hal/esp32s2/include/hal/spi_ll.h index 733022f8c0..16cf0b99eb 100644 --- a/components/hal/esp32s2/include/hal/spi_ll.h +++ b/components/hal/esp32s2/include/hal/spi_ll.h @@ -137,6 +137,9 @@ static inline void spi_ll_slave_init(spi_dev_t *hw) hw->user.usr_miso_highpart = 0; hw->user.usr_mosi_highpart = 0; + // Configure DMA In-Link to not be terminated when transaction bit counter exceeds + hw->dma_conf.rx_eof_en = 0; + //Disable unneeded ints hw->slave.val &= ~SPI_LL_UNUSED_INT_MASK; } diff --git a/components/hal/esp32s3/include/hal/spi_ll.h b/components/hal/esp32s3/include/hal/spi_ll.h index 57099df19b..8c85823b4e 100644 --- a/components/hal/esp32s3/include/hal/spi_ll.h +++ b/components/hal/esp32s3/include/hal/spi_ll.h @@ -136,6 +136,8 @@ static inline void spi_ll_slave_init(spi_dev_t *hw) hw->user.usr_miso_highpart = 0; hw->user.usr_mosi_highpart = 0; + // Configure DMA In-Link to not be terminated when transaction bit counter exceeds + hw->dma_conf.rx_eof_en = 0; hw->dma_conf.dma_seg_trans_en = 0; //Disable unneeded ints From f5bfa0babf35c718ab1e3e0038117f59e0a890eb Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Mon, 31 May 2021 19:27:23 -0300 Subject: [PATCH 5/6] driver: Create TX/RX-only test cases for SPI Slave --- components/driver/test/test_spi_slave.c | 120 ++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/components/driver/test/test_spi_slave.c b/components/driver/test/test_spi_slave.c index d7988f63cc..2f156c1c46 100644 --- a/components/driver/test/test_spi_slave.c +++ b/components/driver/test/test_spi_slave.c @@ -83,6 +83,126 @@ static void slave_init(void) TEST_ESP_OK(spi_slave_initialize(TEST_SLAVE_HOST, &buscfg, &slvcfg, SPI_DMA_CH_AUTO)); } +TEST_CASE("test fullduplex slave with only RX direction","[spi]") +{ + WORD_ALIGNED_ATTR uint8_t master_txbuf[320]=MASTER_SEND; + WORD_ALIGNED_ATTR uint8_t slave_rxbuf[320]; + + spi_device_handle_t spi; + //initial master + master_init_nodma( &spi ); + //initial slave + slave_init(); + + //do internal connection + int_connect( PIN_NUM_MOSI, spi_periph_signal[TEST_SPI_HOST].spid_out, spi_periph_signal[TEST_SLAVE_HOST].spiq_in ); + int_connect( PIN_NUM_MISO, spi_periph_signal[TEST_SLAVE_HOST].spiq_out, spi_periph_signal[TEST_SPI_HOST].spid_in ); + int_connect( PIN_NUM_CS, spi_periph_signal[TEST_SPI_HOST].spics_out[0], spi_periph_signal[TEST_SLAVE_HOST].spics_in ); + int_connect( PIN_NUM_CLK, spi_periph_signal[TEST_SPI_HOST].spiclk_out, spi_periph_signal[TEST_SLAVE_HOST].spiclk_in ); + + for ( int i = 0; i < 4; i ++ ) { + //slave send + spi_slave_transaction_t slave_t; + spi_slave_transaction_t* out; + memset(&slave_t, 0, sizeof(spi_slave_transaction_t)); + slave_t.length=8*32; + slave_t.tx_buffer=NULL; + slave_t.rx_buffer=slave_rxbuf; + + TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY)); + + //send + spi_transaction_t t = {}; + t.length = 32*(i+1); + if ( t.length != 0 ) { + t.tx_buffer = master_txbuf; + t.rx_buffer = NULL; + } + spi_device_transmit( spi, (spi_transaction_t*)&t ); + + //wait for end + TEST_ESP_OK(spi_slave_get_trans_result(TEST_SLAVE_HOST, &out, portMAX_DELAY)); + + //show result + ESP_LOGI(SLAVE_TAG, "trans_len: %d", slave_t.trans_len); + ESP_LOG_BUFFER_HEX( "master tx", t.tx_buffer, t.length/8 ); + ESP_LOG_BUFFER_HEX( "slave rx", slave_t.rx_buffer, (slave_t.trans_len+7)/8); + + TEST_ASSERT_EQUAL_HEX8_ARRAY( t.tx_buffer, slave_t.rx_buffer, t.length/8 ); + TEST_ASSERT_EQUAL( t.length, slave_t.trans_len ); + + //clean + memset( slave_rxbuf, 0x66, sizeof(slave_rxbuf)); + } + + TEST_ASSERT(spi_slave_free(TEST_SLAVE_HOST) == ESP_OK); + + TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK); + TEST_ASSERT(spi_bus_free(TEST_SPI_HOST) == ESP_OK); + + ESP_LOGI(SLAVE_TAG, "test passed."); +} + +TEST_CASE("test fullduplex slave with only TX direction","[spi]") +{ + WORD_ALIGNED_ATTR uint8_t master_rxbuf[320]; + WORD_ALIGNED_ATTR uint8_t slave_txbuf[320]=SLAVE_SEND; + + spi_device_handle_t spi; + //initial master + master_init_nodma( &spi ); + //initial slave + slave_init(); + + //do internal connection + int_connect( PIN_NUM_MOSI, spi_periph_signal[TEST_SPI_HOST].spid_out, spi_periph_signal[TEST_SLAVE_HOST].spiq_in ); + int_connect( PIN_NUM_MISO, spi_periph_signal[TEST_SLAVE_HOST].spiq_out, spi_periph_signal[TEST_SPI_HOST].spid_in ); + int_connect( PIN_NUM_CS, spi_periph_signal[TEST_SPI_HOST].spics_out[0], spi_periph_signal[TEST_SLAVE_HOST].spics_in ); + int_connect( PIN_NUM_CLK, spi_periph_signal[TEST_SPI_HOST].spiclk_out, spi_periph_signal[TEST_SLAVE_HOST].spiclk_in ); + + for ( int i = 0; i < 4; i ++ ) { + //slave send + spi_slave_transaction_t slave_t; + spi_slave_transaction_t* out; + memset(&slave_t, 0, sizeof(spi_slave_transaction_t)); + slave_t.length=8*32; + slave_t.tx_buffer=slave_txbuf; + slave_t.rx_buffer=NULL; + + TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY)); + + //send + spi_transaction_t t = {}; + t.length = 32*(i+1); + if ( t.length != 0 ) { + t.tx_buffer = NULL; + t.rx_buffer = master_rxbuf; + } + spi_device_transmit( spi, (spi_transaction_t*)&t ); + + //wait for end + TEST_ESP_OK(spi_slave_get_trans_result(TEST_SLAVE_HOST, &out, portMAX_DELAY)); + + //show result + ESP_LOGI(SLAVE_TAG, "trans_len: %d", slave_t.trans_len); + ESP_LOG_BUFFER_HEX( "master rx", t.rx_buffer, t.length/8 ); + ESP_LOG_BUFFER_HEX( "slave tx", slave_t.tx_buffer, (slave_t.trans_len+7)/8); + + TEST_ASSERT_EQUAL_HEX8_ARRAY( slave_t.tx_buffer, t.rx_buffer, t.length/8 ); + TEST_ASSERT_EQUAL( t.length, slave_t.trans_len ); + + //clean + memset( master_rxbuf, 0x66, sizeof(master_rxbuf)); + } + + TEST_ASSERT(spi_slave_free(TEST_SLAVE_HOST) == ESP_OK); + + TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK); + TEST_ASSERT(spi_bus_free(TEST_SPI_HOST) == ESP_OK); + + ESP_LOGI(SLAVE_TAG, "test passed."); +} + TEST_CASE("test slave send unaligned","[spi]") { WORD_ALIGNED_ATTR uint8_t master_txbuf[320]=MASTER_SEND; From ef0041b545eacc61b70e65cf94673da0bc9c2fcd Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Wed, 2 Jun 2021 10:09:20 -0300 Subject: [PATCH 6/6] driver: Refactor and clean up SPI Slave test --- components/driver/test/test_spi_slave.c | 113 +++++++++++------------- 1 file changed, 50 insertions(+), 63 deletions(-) diff --git a/components/driver/test/test_spi_slave.c b/components/driver/test/test_spi_slave.c index 2f156c1c46..240f2e7df0 100644 --- a/components/driver/test/test_spi_slave.c +++ b/components/driver/test/test_spi_slave.c @@ -23,9 +23,14 @@ #ifndef CONFIG_SPIRAM //This test should be removed once the timing test is merged. -#define MASTER_SEND {0x93, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xaa, 0xcc, 0xff, 0xee, 0x55, 0x77, 0x88, 0x43} -#define SLAVE_SEND { 0xaa, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x13, 0x57, 0x9b, 0xdf, 0x24, 0x68, 0xac, 0xe0 } +static spi_device_handle_t spi; +static WORD_ALIGNED_ATTR uint8_t master_txbuf[320]; +static WORD_ALIGNED_ATTR uint8_t master_rxbuf[320]; +static WORD_ALIGNED_ATTR uint8_t slave_txbuf[320]; +static WORD_ALIGNED_ATTR uint8_t slave_rxbuf[320]; +static const uint8_t master_send[] = { 0x93, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xaa, 0xcc, 0xff, 0xee, 0x55, 0x77, 0x88, 0x43 }; +static const uint8_t slave_send[] = { 0xaa, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x13, 0x57, 0x9b, 0xdf, 0x24, 0x68, 0xac, 0xe0 }; static inline void int_connect( uint32_t gpio, uint32_t sigo, uint32_t sigi ) { @@ -33,7 +38,7 @@ static inline void int_connect( uint32_t gpio, uint32_t sigo, uint32_t sigi ) esp_rom_gpio_connect_in_signal( gpio, sigi, false ); } -static void master_init_nodma( spi_device_handle_t* spi) +static void master_init( spi_device_handle_t* spi) { esp_err_t ret; spi_bus_config_t buscfg={ @@ -83,22 +88,36 @@ static void slave_init(void) TEST_ESP_OK(spi_slave_initialize(TEST_SLAVE_HOST, &buscfg, &slvcfg, SPI_DMA_CH_AUTO)); } -TEST_CASE("test fullduplex slave with only RX direction","[spi]") -{ - WORD_ALIGNED_ATTR uint8_t master_txbuf[320]=MASTER_SEND; - WORD_ALIGNED_ATTR uint8_t slave_rxbuf[320]; +static void custom_setup(void) { + //Initialize buffers + memset(master_txbuf, 0, sizeof(master_txbuf)); + memset(master_rxbuf, 0, sizeof(master_rxbuf)); + memset(slave_txbuf, 0, sizeof(slave_txbuf)); + memset(slave_rxbuf, 0, sizeof(slave_rxbuf)); - spi_device_handle_t spi; - //initial master - master_init_nodma( &spi ); - //initial slave + //Initialize SPI Master + master_init( &spi ); + //Initialize SPI Slave slave_init(); - //do internal connection + //Do internal connections int_connect( PIN_NUM_MOSI, spi_periph_signal[TEST_SPI_HOST].spid_out, spi_periph_signal[TEST_SLAVE_HOST].spiq_in ); int_connect( PIN_NUM_MISO, spi_periph_signal[TEST_SLAVE_HOST].spiq_out, spi_periph_signal[TEST_SPI_HOST].spid_in ); int_connect( PIN_NUM_CS, spi_periph_signal[TEST_SPI_HOST].spics_out[0], spi_periph_signal[TEST_SLAVE_HOST].spics_in ); int_connect( PIN_NUM_CLK, spi_periph_signal[TEST_SPI_HOST].spiclk_out, spi_periph_signal[TEST_SLAVE_HOST].spiclk_in ); +} + +static void custom_teardown(void) { + TEST_ASSERT(spi_slave_free(TEST_SLAVE_HOST) == ESP_OK); + TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK); + TEST_ASSERT(spi_bus_free(TEST_SPI_HOST) == ESP_OK); +} + +TEST_CASE("test fullduplex slave with only RX direction","[spi]") +{ + custom_setup(); + + memcpy(master_txbuf, master_send, sizeof(master_send)); for ( int i = 0; i < 4; i ++ ) { //slave send @@ -109,6 +128,9 @@ TEST_CASE("test fullduplex slave with only RX direction","[spi]") slave_t.tx_buffer=NULL; slave_t.rx_buffer=slave_rxbuf; + // Colorize RX buffer with known pattern + memset( slave_rxbuf, 0x66, sizeof(slave_rxbuf)); + TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY)); //send @@ -130,35 +152,18 @@ TEST_CASE("test fullduplex slave with only RX direction","[spi]") TEST_ASSERT_EQUAL_HEX8_ARRAY( t.tx_buffer, slave_t.rx_buffer, t.length/8 ); TEST_ASSERT_EQUAL( t.length, slave_t.trans_len ); - - //clean - memset( slave_rxbuf, 0x66, sizeof(slave_rxbuf)); } - TEST_ASSERT(spi_slave_free(TEST_SLAVE_HOST) == ESP_OK); - - TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK); - TEST_ASSERT(spi_bus_free(TEST_SPI_HOST) == ESP_OK); + custom_teardown(); ESP_LOGI(SLAVE_TAG, "test passed."); } TEST_CASE("test fullduplex slave with only TX direction","[spi]") { - WORD_ALIGNED_ATTR uint8_t master_rxbuf[320]; - WORD_ALIGNED_ATTR uint8_t slave_txbuf[320]=SLAVE_SEND; + custom_setup(); - spi_device_handle_t spi; - //initial master - master_init_nodma( &spi ); - //initial slave - slave_init(); - - //do internal connection - int_connect( PIN_NUM_MOSI, spi_periph_signal[TEST_SPI_HOST].spid_out, spi_periph_signal[TEST_SLAVE_HOST].spiq_in ); - int_connect( PIN_NUM_MISO, spi_periph_signal[TEST_SLAVE_HOST].spiq_out, spi_periph_signal[TEST_SPI_HOST].spid_in ); - int_connect( PIN_NUM_CS, spi_periph_signal[TEST_SPI_HOST].spics_out[0], spi_periph_signal[TEST_SLAVE_HOST].spics_in ); - int_connect( PIN_NUM_CLK, spi_periph_signal[TEST_SPI_HOST].spiclk_out, spi_periph_signal[TEST_SLAVE_HOST].spiclk_in ); + memcpy(slave_txbuf, slave_send, sizeof(slave_send)); for ( int i = 0; i < 4; i ++ ) { //slave send @@ -169,6 +174,9 @@ TEST_CASE("test fullduplex slave with only TX direction","[spi]") slave_t.tx_buffer=slave_txbuf; slave_t.rx_buffer=NULL; + // Colorize RX buffer with known pattern + memset( master_rxbuf, 0x66, sizeof(master_rxbuf)); + TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY)); //send @@ -190,37 +198,19 @@ TEST_CASE("test fullduplex slave with only TX direction","[spi]") TEST_ASSERT_EQUAL_HEX8_ARRAY( slave_t.tx_buffer, t.rx_buffer, t.length/8 ); TEST_ASSERT_EQUAL( t.length, slave_t.trans_len ); - - //clean - memset( master_rxbuf, 0x66, sizeof(master_rxbuf)); } - TEST_ASSERT(spi_slave_free(TEST_SLAVE_HOST) == ESP_OK); - - TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK); - TEST_ASSERT(spi_bus_free(TEST_SPI_HOST) == ESP_OK); + custom_teardown(); ESP_LOGI(SLAVE_TAG, "test passed."); } TEST_CASE("test slave send unaligned","[spi]") { - WORD_ALIGNED_ATTR uint8_t master_txbuf[320]=MASTER_SEND; - WORD_ALIGNED_ATTR uint8_t master_rxbuf[320]; - WORD_ALIGNED_ATTR uint8_t slave_txbuf[320]=SLAVE_SEND; - WORD_ALIGNED_ATTR uint8_t slave_rxbuf[320]; + custom_setup(); - spi_device_handle_t spi; - //initial master - master_init_nodma( &spi ); - //initial slave - slave_init(); - - //do internal connection - int_connect( PIN_NUM_MOSI, spi_periph_signal[TEST_SPI_HOST].spid_out, spi_periph_signal[TEST_SLAVE_HOST].spiq_in ); - int_connect( PIN_NUM_MISO, spi_periph_signal[TEST_SLAVE_HOST].spiq_out, spi_periph_signal[TEST_SPI_HOST].spid_in ); - int_connect( PIN_NUM_CS, spi_periph_signal[TEST_SPI_HOST].spics_out[0], spi_periph_signal[TEST_SLAVE_HOST].spics_in ); - int_connect( PIN_NUM_CLK, spi_periph_signal[TEST_SPI_HOST].spiclk_out, spi_periph_signal[TEST_SLAVE_HOST].spiclk_in ); + memcpy(master_txbuf, master_send, sizeof(master_send)); + memcpy(slave_txbuf, slave_send, sizeof(slave_send)); for ( int i = 0; i < 4; i ++ ) { //slave send @@ -231,6 +221,10 @@ TEST_CASE("test slave send unaligned","[spi]") slave_t.tx_buffer=slave_txbuf+i; slave_t.rx_buffer=slave_rxbuf; + // Colorize RX buffers with known pattern + memset( master_rxbuf, 0x66, sizeof(master_rxbuf)); + memset( slave_rxbuf, 0x66, sizeof(slave_rxbuf)); + TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY)); //send @@ -255,18 +249,11 @@ TEST_CASE("test slave send unaligned","[spi]") TEST_ASSERT_EQUAL_HEX8_ARRAY( t.tx_buffer, slave_t.rx_buffer, t.length/8 ); TEST_ASSERT_EQUAL_HEX8_ARRAY( slave_t.tx_buffer, t.rx_buffer, t.length/8 ); TEST_ASSERT_EQUAL( t.length, slave_t.trans_len ); - - //clean - memset( master_rxbuf, 0x66, sizeof(master_rxbuf)); - memset( slave_rxbuf, 0x66, sizeof(slave_rxbuf)); } - TEST_ASSERT(spi_slave_free(TEST_SLAVE_HOST) == ESP_OK); + custom_teardown(); - TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK); - TEST_ASSERT(spi_bus_free(TEST_SPI_HOST) == ESP_OK); - - ESP_LOGI(MASTER_TAG, "test passed."); + ESP_LOGI(SLAVE_TAG, "test passed."); } #endif // !CONFIG_SPIRAM