mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 10:47:19 +02:00
driver: Refactor and clean up SPI Slave test
This commit is contained in:
committed by
bot
parent
7b587bf1d7
commit
de7d86b8a1
@ -18,9 +18,14 @@
|
|||||||
#ifndef CONFIG_SPIRAM
|
#ifndef CONFIG_SPIRAM
|
||||||
//This test should be removed once the timing test is merged.
|
//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}
|
static spi_device_handle_t spi;
|
||||||
#define SLAVE_SEND { 0xaa, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x13, 0x57, 0x9b, 0xdf, 0x24, 0x68, 0xac, 0xe0 }
|
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 )
|
static inline void int_connect( uint32_t gpio, uint32_t sigo, uint32_t sigi )
|
||||||
{
|
{
|
||||||
@ -28,7 +33,7 @@ static inline void int_connect( uint32_t gpio, uint32_t sigo, uint32_t sigi )
|
|||||||
esp_rom_gpio_connect_in_signal( gpio, sigi, false );
|
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;
|
esp_err_t ret;
|
||||||
spi_bus_config_t buscfg={
|
spi_bus_config_t buscfg={
|
||||||
@ -78,22 +83,36 @@ static void slave_init(void)
|
|||||||
TEST_ESP_OK(spi_slave_initialize(TEST_SLAVE_HOST, &buscfg, &slvcfg, SPI_DMA_CH_AUTO));
|
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]")
|
static void custom_setup(void) {
|
||||||
{
|
//Initialize buffers
|
||||||
WORD_ALIGNED_ATTR uint8_t master_txbuf[320]=MASTER_SEND;
|
memset(master_txbuf, 0, sizeof(master_txbuf));
|
||||||
WORD_ALIGNED_ATTR uint8_t slave_rxbuf[320];
|
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;
|
//Initialize SPI Master
|
||||||
//initial master
|
master_init( &spi );
|
||||||
master_init_nodma( &spi );
|
//Initialize SPI Slave
|
||||||
//initial slave
|
|
||||||
slave_init();
|
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_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_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_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 );
|
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 ++ ) {
|
for ( int i = 0; i < 4; i ++ ) {
|
||||||
//slave send
|
//slave send
|
||||||
@ -104,6 +123,9 @@ TEST_CASE("test fullduplex slave with only RX direction","[spi]")
|
|||||||
slave_t.tx_buffer=NULL;
|
slave_t.tx_buffer=NULL;
|
||||||
slave_t.rx_buffer=slave_rxbuf;
|
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));
|
TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY));
|
||||||
|
|
||||||
//send
|
//send
|
||||||
@ -125,35 +147,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_HEX8_ARRAY( t.tx_buffer, slave_t.rx_buffer, t.length/8 );
|
||||||
TEST_ASSERT_EQUAL( t.length, slave_t.trans_len );
|
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);
|
custom_teardown();
|
||||||
|
|
||||||
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.");
|
ESP_LOGI(SLAVE_TAG, "test passed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("test fullduplex slave with only TX direction","[spi]")
|
TEST_CASE("test fullduplex slave with only TX direction","[spi]")
|
||||||
{
|
{
|
||||||
WORD_ALIGNED_ATTR uint8_t master_rxbuf[320];
|
custom_setup();
|
||||||
WORD_ALIGNED_ATTR uint8_t slave_txbuf[320]=SLAVE_SEND;
|
|
||||||
|
|
||||||
spi_device_handle_t spi;
|
memcpy(slave_txbuf, slave_send, sizeof(slave_send));
|
||||||
//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 ++ ) {
|
for ( int i = 0; i < 4; i ++ ) {
|
||||||
//slave send
|
//slave send
|
||||||
@ -164,6 +169,9 @@ TEST_CASE("test fullduplex slave with only TX direction","[spi]")
|
|||||||
slave_t.tx_buffer=slave_txbuf;
|
slave_t.tx_buffer=slave_txbuf;
|
||||||
slave_t.rx_buffer=NULL;
|
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));
|
TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY));
|
||||||
|
|
||||||
//send
|
//send
|
||||||
@ -185,37 +193,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_HEX8_ARRAY( slave_t.tx_buffer, t.rx_buffer, t.length/8 );
|
||||||
TEST_ASSERT_EQUAL( t.length, slave_t.trans_len );
|
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);
|
custom_teardown();
|
||||||
|
|
||||||
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.");
|
ESP_LOGI(SLAVE_TAG, "test passed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("test slave send unaligned","[spi]")
|
TEST_CASE("test slave send unaligned","[spi]")
|
||||||
{
|
{
|
||||||
WORD_ALIGNED_ATTR uint8_t master_txbuf[320]=MASTER_SEND;
|
custom_setup();
|
||||||
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];
|
|
||||||
|
|
||||||
spi_device_handle_t spi;
|
memcpy(master_txbuf, master_send, sizeof(master_send));
|
||||||
//initial master
|
memcpy(slave_txbuf, slave_send, sizeof(slave_send));
|
||||||
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 ++ ) {
|
for ( int i = 0; i < 4; i ++ ) {
|
||||||
//slave send
|
//slave send
|
||||||
@ -226,6 +216,10 @@ TEST_CASE("test slave send unaligned","[spi]")
|
|||||||
slave_t.tx_buffer=slave_txbuf+i;
|
slave_t.tx_buffer=slave_txbuf+i;
|
||||||
slave_t.rx_buffer=slave_rxbuf;
|
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));
|
TEST_ESP_OK(spi_slave_queue_trans(TEST_SLAVE_HOST, &slave_t, portMAX_DELAY));
|
||||||
|
|
||||||
//send
|
//send
|
||||||
@ -250,18 +244,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( 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_HEX8_ARRAY( slave_t.tx_buffer, t.rx_buffer, t.length/8 );
|
||||||
TEST_ASSERT_EQUAL( t.length, slave_t.trans_len );
|
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);
|
ESP_LOGI(SLAVE_TAG, "test passed.");
|
||||||
TEST_ASSERT(spi_bus_free(TEST_SPI_HOST) == ESP_OK);
|
|
||||||
|
|
||||||
ESP_LOGI(MASTER_TAG, "test passed.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !CONFIG_SPIRAM
|
#endif // !CONFIG_SPIRAM
|
||||||
|
Reference in New Issue
Block a user