mirror of
https://github.com/espressif/esp-idf
synced 2025-03-10 01:29:21 -04:00
driver: Refactor and clean up SPI Slave test
This commit is contained in:
parent
f5bfa0babf
commit
ef0041b545
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user