test_common_spi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "test/test_common_spi.h"
  2. #include "driver/spi_slave.h"
  3. #include "esp_log.h"
  4. #include "driver/gpio.h"
  5. #include "hal/gpio_hal.h"
  6. int test_freq_default[]=TEST_FREQ_DEFAULT();
  7. const char MASTER_TAG[] = "test_master";
  8. const char SLAVE_TAG[] = "test_slave";
  9. DRAM_ATTR uint8_t spitest_master_send[] = {
  10. 0x93, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xaa, 0xcc, 0xff, 0xee, 0x55, 0x77, 0x88, 0x43,
  11. 0x74,
  12. 0x93, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xaa, 0xcc, 0xff, 0xee, 0x55, 0x77, 0x88, 0x43,
  13. 0x74,
  14. 0x93, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xaa, 0xcc, 0xff, 0xee, 0x55, 0x77, 0x88, 0x43,
  15. 0x74,
  16. };
  17. DRAM_ATTR uint8_t spitest_slave_send[] = {
  18. 0xaa, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x13, 0x57, 0x9b, 0xdf, 0x24, 0x68, 0xac, 0xe0,
  19. 0xda,
  20. 0xaa, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x13, 0x57, 0x9b, 0xdf, 0x24, 0x68, 0xac, 0xe0,
  21. 0xda,
  22. 0xaa, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x13, 0x57, 0x9b, 0xdf, 0x24, 0x68, 0xac, 0xe0,
  23. 0xda,
  24. };
  25. void spitest_def_param(void* arg)
  26. {
  27. spitest_param_set_t *param_set=(spitest_param_set_t*)arg;
  28. param_set->test_size = 8;
  29. if (param_set->freq_list==NULL) param_set->freq_list = test_freq_default;
  30. }
  31. /**********************************************************************************
  32. * functions for slave task
  33. *********************************************************************************/
  34. esp_err_t init_slave_context(spi_slave_task_context_t *context)
  35. {
  36. context->data_to_send = xQueueCreate( 16, sizeof( slave_txdata_t ));
  37. if ( context->data_to_send == NULL ) {
  38. return ESP_ERR_NO_MEM;
  39. }
  40. context->data_received = xRingbufferCreate( 1024, RINGBUF_TYPE_NOSPLIT );
  41. if ( context->data_received == NULL ) {
  42. return ESP_ERR_NO_MEM;
  43. }
  44. context->spi=TEST_SLAVE_HOST;
  45. return ESP_OK;
  46. }
  47. void deinit_slave_context(spi_slave_task_context_t *context)
  48. {
  49. TEST_ASSERT( context->data_to_send != NULL );
  50. vQueueDelete( context->data_to_send );
  51. context->data_to_send = NULL;
  52. TEST_ASSERT( context->data_received != NULL );
  53. vRingbufferDelete( context->data_received );
  54. context->data_received = NULL;
  55. }
  56. /* The task requires a queue and a ringbuf, which should be initialized before task starts.
  57. Send ``slave_txdata_t`` to the queue to make the task send data;
  58. the task returns data got to the ringbuf, which should have sufficient size.
  59. */
  60. void spitest_slave_task(void* arg)
  61. {
  62. spi_slave_task_context_t* context = (spi_slave_task_context_t*) arg;
  63. QueueHandle_t queue = context->data_to_send;
  64. RingbufHandle_t ringbuf = context->data_received;
  65. uint8_t recvbuf[320+8];
  66. slave_txdata_t txdata;
  67. ESP_LOGI( SLAVE_TAG, "slave up" );
  68. //never quit, but blocked by the queue, waiting to be killed, when no more send from main task.
  69. while( 1 ) {
  70. BaseType_t ret = xQueueReceive( queue, &txdata, portMAX_DELAY );
  71. assert(ret);
  72. spi_slave_transaction_t t = {};
  73. t.length = txdata.len;
  74. t.tx_buffer = txdata.start;
  75. t.rx_buffer = recvbuf+8;
  76. //loop until trans_len != 0 to skip glitches
  77. do {
  78. TEST_ESP_OK( spi_slave_transmit( context->spi, &t, portMAX_DELAY ) );
  79. } while ( t.trans_len <= 2 );
  80. memcpy(recvbuf, &t.trans_len, sizeof(uint32_t));
  81. *(uint8_t**)(recvbuf+4) = (uint8_t*)txdata.start;
  82. ESP_LOGD( SLAVE_TAG, "received: %d", t.trans_len );
  83. xRingbufferSend( ringbuf, recvbuf, 8+(t.trans_len+7)/8, portMAX_DELAY );
  84. }
  85. }
  86. void slave_pull_up(const spi_bus_config_t* cfg, int spics_io_num)
  87. {
  88. gpio_set_pull_mode(cfg->mosi_io_num, GPIO_PULLUP_ONLY);
  89. gpio_set_pull_mode(cfg->sclk_io_num, GPIO_PULLUP_ONLY);
  90. gpio_set_pull_mode(spics_io_num, GPIO_PULLUP_ONLY);
  91. }
  92. /**********************************************************************************
  93. * functions for slave task
  94. *********************************************************************************/
  95. static int test_len[] = {1, 3, 5, 7, 9, 11, 33, 64};
  96. void spitest_init_transactions(const spitest_param_set_t *cfg, spitest_context_t* context)
  97. {
  98. spi_transaction_t* trans = context->master_trans;
  99. uint8_t *rx_buf_ptr = context->master_rxbuf;
  100. const spi_dup_t dup = cfg->dup;
  101. for (int i = 0; i < cfg->test_size; i++) {
  102. const void* tx_buffer = spitest_master_send + i%8;
  103. int length = 8*test_len[i];
  104. if (cfg->length_aligned) length = (length+31)&(~31);
  105. if (dup == HALF_DUPLEX_MISO) {
  106. trans[i] = (spi_transaction_t) {
  107. .rx_buffer = rx_buf_ptr,
  108. .rxlength = length,
  109. };
  110. } else if (dup == HALF_DUPLEX_MOSI) {
  111. trans[i] = (spi_transaction_t) {
  112. .tx_buffer = tx_buffer,
  113. .length = length,
  114. };
  115. } else {
  116. trans[i] = (spi_transaction_t) {
  117. .tx_buffer = tx_buffer,
  118. .length = length,
  119. .rx_buffer = rx_buf_ptr,
  120. };
  121. }
  122. rx_buf_ptr = (uint8_t*)( (uint32_t)(rx_buf_ptr + (length+7)/8 + 3) & (~3));
  123. const void* slave_tx = spitest_slave_send + (cfg->slave_unaligned_addr? i%3: (i%3)*4);
  124. //prepare slave tx data
  125. context->slave_trans[i] = (slave_txdata_t) {
  126. .start = slave_tx,
  127. .len = 512,
  128. };
  129. if (cfg->slave_dma_chan != 0) context->slave_trans[i].len = 1024;
  130. }
  131. }
  132. void spitest_master_print_data(spi_transaction_t *t, int rxlength)
  133. {
  134. if (t->tx_buffer) ESP_LOG_BUFFER_HEX( "master tx", t->tx_buffer, t->length/8 );
  135. if (t->rx_buffer) ESP_LOG_BUFFER_HEX( "master rx", t->rx_buffer, rxlength/8 );
  136. }
  137. void spitest_slave_print_data(slave_rxdata_t *t, bool print_rxdata)
  138. {
  139. int rcv_len = (t->len+7)/8;
  140. ESP_LOGI(SLAVE_TAG, "trans_len: %d", t->len);
  141. ESP_LOG_BUFFER_HEX("slave tx", t->tx_start, rcv_len);
  142. if (print_rxdata) ESP_LOG_BUFFER_HEX("slave rx", t->data, rcv_len);
  143. }
  144. esp_err_t spitest_check_data(int len, spi_transaction_t *master_t, slave_rxdata_t *slave_t, bool check_master_data, bool check_slave_len, bool check_slave_data)
  145. {
  146. esp_err_t ret = ESP_OK;
  147. uint32_t rcv_len = slave_t->len;
  148. //currently the rcv_len can be in range of [t->length-1, t->length+3]
  149. if (check_slave_len &&
  150. (rcv_len < len - 1 || rcv_len > len + 4)) {
  151. ret = ESP_FAIL;
  152. }
  153. if (check_master_data &&
  154. memcmp(slave_t->tx_start, master_t->rx_buffer, (len + 7) / 8) != 0 ) {
  155. ret = ESP_FAIL;
  156. }
  157. if (check_slave_data &&
  158. memcmp(master_t->tx_buffer, slave_t->data, (len + 7) / 8) != 0 ) {
  159. ret = ESP_FAIL;
  160. }
  161. if (ret != ESP_OK) {
  162. ESP_LOGI(SLAVE_TAG, "slave_recv_len: %d", rcv_len);
  163. spitest_master_print_data(master_t, len);
  164. spitest_slave_print_data(slave_t, true);
  165. //already failed, try to use the TEST_ASSERT to output the reason...
  166. if (check_slave_len) {
  167. TEST_ASSERT(rcv_len >= len - 1 && rcv_len <= len + 4);
  168. }
  169. TEST_ASSERT_EQUAL_HEX8_ARRAY(slave_t->tx_start, master_t->rx_buffer, (len + 7) / 8);
  170. TEST_ASSERT_EQUAL_HEX8_ARRAY(master_t->tx_buffer, slave_t->data, (len + 7) / 8);
  171. }
  172. return ESP_OK;
  173. }
  174. void master_free_device_bus(spi_device_handle_t spi)
  175. {
  176. TEST_ESP_OK( spi_bus_remove_device(spi) );
  177. TEST_ESP_OK( spi_bus_free(TEST_SPI_HOST) );
  178. }
  179. void spitest_gpio_output_sel(uint32_t gpio_num, int func, uint32_t signal_idx)
  180. {
  181. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio_num], func);
  182. GPIO.func_out_sel_cfg[gpio_num].func_sel = signal_idx;
  183. }
  184. void spitest_gpio_input_sel(uint32_t gpio_num, int func, uint32_t signal_idx)
  185. {
  186. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio_num], func);
  187. GPIO.func_in_sel_cfg[signal_idx].func_sel = gpio_num;
  188. }
  189. //Note this cs_num is the ID of the connected devices' ID, e.g. if 2 devices are connected to the bus,
  190. //then the cs_num of the 1st and 2nd devices are 0 and 1 respectively.
  191. void same_pin_func_sel(spi_bus_config_t bus, spi_device_interface_config_t dev, uint8_t cs_num)
  192. {
  193. spitest_gpio_output_sel(bus.mosi_io_num, FUNC_GPIO, spi_periph_signal[TEST_SPI_HOST].spid_out);
  194. spitest_gpio_input_sel(bus.mosi_io_num, FUNC_GPIO, spi_periph_signal[TEST_SLAVE_HOST].spid_in);
  195. spitest_gpio_output_sel(bus.miso_io_num, FUNC_GPIO, spi_periph_signal[TEST_SLAVE_HOST].spiq_out);
  196. spitest_gpio_input_sel(bus.miso_io_num, FUNC_GPIO, spi_periph_signal[TEST_SPI_HOST].spiq_in);
  197. spitest_gpio_output_sel(dev.spics_io_num, FUNC_GPIO, spi_periph_signal[TEST_SPI_HOST].spics_out[cs_num]);
  198. spitest_gpio_input_sel(dev.spics_io_num, FUNC_GPIO, spi_periph_signal[TEST_SLAVE_HOST].spics_in);
  199. spitest_gpio_output_sel(bus.sclk_io_num, FUNC_GPIO, spi_periph_signal[TEST_SPI_HOST].spiclk_out);
  200. spitest_gpio_input_sel(bus.sclk_io_num, FUNC_GPIO, spi_periph_signal[TEST_SLAVE_HOST].spiclk_in);
  201. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  202. GPIO.func_in_sel_cfg[FSPIQ_IN_IDX].sig_in_sel = 1;
  203. #endif
  204. }