test_spi_master.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. Tests for the spi_master device driver
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9. #include "rom/ets_sys.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/semphr.h"
  13. #include "freertos/queue.h"
  14. #include "freertos/xtensa_api.h"
  15. #include "unity.h"
  16. #include "driver/spi_master.h"
  17. #include "driver/spi_slave.h"
  18. #include "soc/dport_reg.h"
  19. #include "soc/spi_reg.h"
  20. #include "soc/spi_struct.h"
  21. #include "esp_heap_caps.h"
  22. #include "esp_log.h"
  23. #include "freertos/ringbuf.h"
  24. const static char TAG[] = "test_spi";
  25. static void check_spi_pre_n_for(int clk, int pre, int n)
  26. {
  27. esp_err_t ret;
  28. spi_device_handle_t handle;
  29. spi_device_interface_config_t devcfg={
  30. .command_bits=0,
  31. .address_bits=0,
  32. .dummy_bits=0,
  33. .clock_speed_hz=clk,
  34. .duty_cycle_pos=128,
  35. .mode=0,
  36. .spics_io_num=21,
  37. .queue_size=3
  38. };
  39. char sendbuf[16]="";
  40. spi_transaction_t t;
  41. memset(&t, 0, sizeof(t));
  42. ret=spi_bus_add_device(HSPI_HOST, &devcfg, &handle);
  43. TEST_ASSERT(ret==ESP_OK);
  44. t.length=16*8;
  45. t.tx_buffer=sendbuf;
  46. ret=spi_device_transmit(handle, &t);
  47. printf("Checking clk rate %dHz. expect pre %d n %d, got pre %d n %d\n", clk, pre, n, SPI2.clock.clkdiv_pre+1, SPI2.clock.clkcnt_n+1);
  48. TEST_ASSERT(SPI2.clock.clkcnt_n+1==n);
  49. TEST_ASSERT(SPI2.clock.clkdiv_pre+1==pre);
  50. ret=spi_bus_remove_device(handle);
  51. TEST_ASSERT(ret==ESP_OK);
  52. }
  53. TEST_CASE("SPI Master clockdiv calculation routines", "[spi]")
  54. {
  55. spi_bus_config_t buscfg={
  56. .mosi_io_num=4,
  57. .miso_io_num=26,
  58. .sclk_io_num=25,
  59. .quadwp_io_num=-1,
  60. .quadhd_io_num=-1
  61. };
  62. esp_err_t ret;
  63. ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
  64. TEST_ASSERT(ret==ESP_OK);
  65. check_spi_pre_n_for(26000000, 1, 3);
  66. check_spi_pre_n_for(20000000, 1, 4);
  67. check_spi_pre_n_for(8000000, 1, 10);
  68. check_spi_pre_n_for(800000, 2, 50);
  69. check_spi_pre_n_for(100000, 16, 50);
  70. check_spi_pre_n_for(333333, 4, 60);
  71. check_spi_pre_n_for(900000, 2, 44);
  72. check_spi_pre_n_for(1, 8192, 64); //Actually should generate the minimum clock speed, 152Hz
  73. check_spi_pre_n_for(26000000, 1, 3);
  74. ret=spi_bus_free(HSPI_HOST);
  75. TEST_ASSERT(ret==ESP_OK);
  76. }
  77. static spi_device_handle_t setup_spi_bus(int clkspeed, bool dma) {
  78. spi_bus_config_t buscfg={
  79. .mosi_io_num=4,
  80. .miso_io_num=26,
  81. .sclk_io_num=25,
  82. .quadwp_io_num=-1,
  83. .quadhd_io_num=-1,
  84. .max_transfer_sz=4096*3
  85. };
  86. spi_device_interface_config_t devcfg={
  87. .command_bits=0,
  88. .address_bits=0,
  89. .dummy_bits=0,
  90. .clock_speed_hz=clkspeed,
  91. .duty_cycle_pos=128,
  92. .mode=0,
  93. .spics_io_num=21,
  94. .queue_size=3,
  95. };
  96. esp_err_t ret;
  97. spi_device_handle_t handle;
  98. printf("THIS TEST NEEDS A JUMPER BETWEEN IO4 AND IO26\n");
  99. ret=spi_bus_initialize(HSPI_HOST, &buscfg, dma?1:0);
  100. TEST_ASSERT(ret==ESP_OK);
  101. ret=spi_bus_add_device(HSPI_HOST, &devcfg, &handle);
  102. TEST_ASSERT(ret==ESP_OK);
  103. printf("Bus/dev inited.\n");
  104. return handle;
  105. }
  106. static void spi_test(spi_device_handle_t handle, int num_bytes) {
  107. esp_err_t ret;
  108. int x;
  109. srand(num_bytes);
  110. char *sendbuf=heap_caps_malloc(num_bytes, MALLOC_CAP_DMA);
  111. char *recvbuf=heap_caps_malloc(num_bytes, MALLOC_CAP_DMA);
  112. for (x=0; x<num_bytes; x++) {
  113. sendbuf[x]=rand()&0xff;
  114. recvbuf[x]=0x55;
  115. }
  116. spi_transaction_t t;
  117. memset(&t, 0, sizeof(t));
  118. t.length=num_bytes*8;
  119. t.tx_buffer=sendbuf;
  120. t.rx_buffer=recvbuf;
  121. t.addr=0xA00000000000000FL;
  122. t.cmd=0x55;
  123. printf("Transmitting %d bytes...\n", num_bytes);
  124. ret=spi_device_transmit(handle, &t);
  125. TEST_ASSERT(ret==ESP_OK);
  126. srand(num_bytes);
  127. for (x=0; x<num_bytes; x++) {
  128. if (sendbuf[x]!=(rand()&0xff)) {
  129. printf("Huh? Sendbuf corrupted at byte %d\n", x);
  130. TEST_ASSERT(0);
  131. }
  132. if (sendbuf[x]!=recvbuf[x]) break;
  133. }
  134. if (x!=num_bytes) {
  135. int from=x-16;
  136. if (from<0) from=0;
  137. printf("Error at %d! Sent vs recved: (starting from %d)\n" , x, from);
  138. for (int i=0; i<32; i++) {
  139. if (i+from<num_bytes) printf("%02X ", sendbuf[from+i]);
  140. }
  141. printf("\n");
  142. for (int i=0; i<32; i++) {
  143. if (i+from<num_bytes) printf("%02X ", recvbuf[from+i]);
  144. }
  145. printf("\n");
  146. // TEST_ASSERT(0);
  147. }
  148. printf("Success!\n");
  149. free(sendbuf);
  150. free(recvbuf);
  151. }
  152. static void destroy_spi_bus(spi_device_handle_t handle) {
  153. esp_err_t ret;
  154. ret=spi_bus_remove_device(handle);
  155. TEST_ASSERT(ret==ESP_OK);
  156. ret=spi_bus_free(HSPI_HOST);
  157. TEST_ASSERT(ret==ESP_OK);
  158. }
  159. #define TEST_LEN 111
  160. TEST_CASE("SPI Master test", "[spi][ignore]")
  161. {
  162. printf("Testing bus at 80KHz\n");
  163. spi_device_handle_t handle=setup_spi_bus(80000, true);
  164. spi_test(handle, 16); //small
  165. spi_test(handle, 21); //small, unaligned
  166. spi_test(handle, 36); //aligned
  167. spi_test(handle, 128); //aligned
  168. spi_test(handle, 129); //unaligned
  169. spi_test(handle, 4096-2); //multiple descs, edge case 1
  170. spi_test(handle, 4096-1); //multiple descs, edge case 2
  171. spi_test(handle, 4096*3); //multiple descs
  172. destroy_spi_bus(handle);
  173. printf("Testing bus at 80KHz, non-DMA\n");
  174. handle=setup_spi_bus(80000, false);
  175. spi_test(handle, 4); //aligned
  176. spi_test(handle, 16); //small
  177. spi_test(handle, 21); //small, unaligned
  178. destroy_spi_bus(handle);
  179. printf("Testing bus at 26MHz\n");
  180. handle=setup_spi_bus(20000000, true);
  181. spi_test(handle, 128); //DMA, aligned
  182. spi_test(handle, 4096*3); //DMA, multiple descs
  183. destroy_spi_bus(handle);
  184. printf("Testing bus at 900KHz\n");
  185. handle=setup_spi_bus(9000000, true);
  186. spi_test(handle, 128); //DMA, aligned
  187. spi_test(handle, 4096*3); //DMA, multiple descs
  188. destroy_spi_bus(handle);
  189. }
  190. TEST_CASE("SPI Master test, interaction of multiple devs", "[spi][ignore]") {
  191. esp_err_t ret;
  192. spi_device_interface_config_t devcfg={
  193. .command_bits=0,
  194. .address_bits=0,
  195. .dummy_bits=0,
  196. .clock_speed_hz=1000000,
  197. .duty_cycle_pos=128,
  198. .mode=0,
  199. .spics_io_num=23,
  200. .queue_size=3,
  201. };
  202. spi_device_handle_t handle1=setup_spi_bus(80000, true);
  203. spi_device_handle_t handle2;
  204. spi_bus_add_device(HSPI_HOST, &devcfg, &handle2);
  205. printf("Sending to dev 1\n");
  206. spi_test(handle1, 7);
  207. printf("Sending to dev 1\n");
  208. spi_test(handle1, 15);
  209. printf("Sending to dev 2\n");
  210. spi_test(handle2, 15);
  211. printf("Sending to dev 1\n");
  212. spi_test(handle1, 32);
  213. printf("Sending to dev 2\n");
  214. spi_test(handle2, 32);
  215. printf("Sending to dev 1\n");
  216. spi_test(handle1, 63);
  217. printf("Sending to dev 2\n");
  218. spi_test(handle2, 63);
  219. printf("Sending to dev 1\n");
  220. spi_test(handle1, 5000);
  221. printf("Sending to dev 2\n");
  222. spi_test(handle2, 5000);
  223. ret=spi_bus_remove_device(handle2);
  224. TEST_ASSERT(ret==ESP_OK);
  225. destroy_spi_bus(handle1);
  226. }
  227. TEST_CASE("SPI Master no response when switch from host1 (HSPI) to host2 (VSPI)", "[spi]")
  228. {
  229. //spi config
  230. spi_bus_config_t bus_config;
  231. spi_device_interface_config_t device_config;
  232. spi_device_handle_t spi;
  233. spi_host_device_t host;
  234. int dma = 1;
  235. memset(&bus_config, 0, sizeof(spi_bus_config_t));
  236. memset(&device_config, 0, sizeof(spi_device_interface_config_t));
  237. bus_config.miso_io_num = -1;
  238. bus_config.mosi_io_num = 26;
  239. bus_config.sclk_io_num = 25;
  240. bus_config.quadwp_io_num = -1;
  241. bus_config.quadhd_io_num = -1;
  242. device_config.clock_speed_hz = 50000;
  243. device_config.mode = 0;
  244. device_config.spics_io_num = -1;
  245. device_config.queue_size = 1;
  246. device_config.flags = SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST;
  247. struct spi_transaction_t transaction = {
  248. .flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA,
  249. .length = 16,
  250. .rx_buffer = NULL,
  251. .tx_data = {0x04, 0x00}
  252. };
  253. //initialize for first host
  254. host = 1;
  255. TEST_ASSERT(spi_bus_initialize(host, &bus_config, dma) == ESP_OK);
  256. TEST_ASSERT(spi_bus_add_device(host, &device_config, &spi) == ESP_OK);
  257. printf("before first xmit\n");
  258. TEST_ASSERT(spi_device_transmit(spi, &transaction) == ESP_OK);
  259. printf("after first xmit\n");
  260. TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK);
  261. TEST_ASSERT(spi_bus_free(host) == ESP_OK);
  262. //for second host and failed before
  263. host = 2;
  264. TEST_ASSERT(spi_bus_initialize(host, &bus_config, dma) == ESP_OK);
  265. TEST_ASSERT(spi_bus_add_device(host, &device_config, &spi) == ESP_OK);
  266. printf("before second xmit\n");
  267. // the original version (bit mis-written) stucks here.
  268. TEST_ASSERT(spi_device_transmit(spi, &transaction) == ESP_OK);
  269. // test case success when see this.
  270. printf("after second xmit\n");
  271. TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK);
  272. TEST_ASSERT(spi_bus_free(host) == ESP_OK);
  273. }
  274. IRAM_ATTR static uint32_t data_iram[320];
  275. DRAM_ATTR static uint32_t data_dram[320];
  276. //force to place in code area.
  277. static const uint32_t data_drom[320] = {0};
  278. #define PIN_NUM_MISO 25
  279. #define PIN_NUM_MOSI 23
  280. #define PIN_NUM_CLK 19
  281. #define PIN_NUM_CS 22
  282. #define PIN_NUM_DC 21
  283. #define PIN_NUM_RST 18
  284. #define PIN_NUM_BCKL 5
  285. TEST_CASE("SPI Master DMA test, TX and RX in different regions", "[spi]")
  286. {
  287. uint32_t data_rxdram[320];
  288. esp_err_t ret;
  289. spi_device_handle_t spi;
  290. spi_bus_config_t buscfg={
  291. .miso_io_num=PIN_NUM_MISO,
  292. .mosi_io_num=PIN_NUM_MOSI,
  293. .sclk_io_num=PIN_NUM_CLK,
  294. .quadwp_io_num=-1,
  295. .quadhd_io_num=-1
  296. };
  297. spi_device_interface_config_t devcfg={
  298. .clock_speed_hz=10000000, //Clock out at 10 MHz
  299. .mode=0, //SPI mode 0
  300. .spics_io_num=PIN_NUM_CS, //CS pin
  301. .queue_size=7, //We want to be able to queue 7 transactions at a time
  302. .pre_cb=NULL, //Specify pre-transfer callback to handle D/C line
  303. };
  304. //Initialize the SPI bus
  305. ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
  306. TEST_ASSERT(ret==ESP_OK);
  307. //Attach the LCD to the SPI bus
  308. ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
  309. TEST_ASSERT(ret==ESP_OK);
  310. static spi_transaction_t trans[6];
  311. int x;
  312. printf("iram: %p, dram: %p, drom: %p\n", data_iram, data_dram, data_drom);
  313. memset(trans, 0, 6*sizeof(spi_transaction_t));
  314. trans[0].length = 320*8,
  315. trans[0].tx_buffer = data_iram;
  316. trans[0].rx_buffer = data_rxdram;
  317. trans[1].length = 320*8,
  318. trans[1].tx_buffer = data_dram;
  319. trans[1].rx_buffer = data_rxdram;
  320. trans[2].length = 320*8,
  321. trans[2].tx_buffer = data_drom;
  322. trans[2].rx_buffer = data_rxdram;
  323. trans[3].length = 320*8,
  324. trans[3].tx_buffer = data_drom;
  325. trans[3].rx_buffer = data_iram;
  326. trans[4].length = 320*8,
  327. trans[4].rxlength = 8*4;
  328. trans[4].tx_buffer = data_drom;
  329. trans[4].flags = SPI_TRANS_USE_RXDATA;
  330. trans[5].length = 8*4;
  331. trans[5].flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA;
  332. //Queue all transactions.
  333. for (x=0; x<6; x++) {
  334. ret=spi_device_queue_trans(spi,&trans[x], portMAX_DELAY);
  335. TEST_ASSERT(ret==ESP_OK);
  336. }
  337. for (x=0; x<6; x++) {
  338. spi_transaction_t* ptr;
  339. ret=spi_device_get_trans_result(spi,&ptr, portMAX_DELAY);
  340. TEST_ASSERT(ret==ESP_OK);
  341. TEST_ASSERT(ptr = trans+x);
  342. }
  343. TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK);
  344. TEST_ASSERT(spi_bus_free(HSPI_HOST) == ESP_OK);
  345. }
  346. static inline void int_connect( uint32_t gpio, uint32_t sigo, uint32_t sigi )
  347. {
  348. gpio_matrix_out( gpio, sigo, false, false );
  349. gpio_matrix_in( gpio, sigi, false );
  350. }
  351. //this part tests 3 DMA issues in master mode, full-duplex in IDF2.1
  352. // 1. RX buffer not aligned (start and end)
  353. // 2. not setting rx_buffer
  354. // 3. setting rx_length != length
  355. TEST_CASE("SPI Master DMA test: length, start, not aligned", "[spi]")
  356. {
  357. uint8_t tx_buf[320]={0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xaa, 0xcc, 0xff, 0xee, 0x55, 0x77, 0x88, 0x43};
  358. uint8_t rx_buf[320];
  359. esp_err_t ret;
  360. spi_device_handle_t spi;
  361. spi_bus_config_t buscfg={
  362. .miso_io_num=PIN_NUM_MISO,
  363. .mosi_io_num=PIN_NUM_MOSI,
  364. .sclk_io_num=PIN_NUM_CLK,
  365. .quadwp_io_num=-1,
  366. .quadhd_io_num=-1
  367. };
  368. spi_device_interface_config_t devcfg={
  369. .clock_speed_hz=10*1000*1000, //Clock out at 10 MHz
  370. .mode=0, //SPI mode 0
  371. .spics_io_num=PIN_NUM_CS, //CS pin
  372. .queue_size=7, //We want to be able to queue 7 transactions at a time
  373. .pre_cb=NULL,
  374. };
  375. //Initialize the SPI bus
  376. ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
  377. TEST_ASSERT(ret==ESP_OK);
  378. //Attach the LCD to the SPI bus
  379. ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
  380. TEST_ASSERT(ret==ESP_OK);
  381. //do internal connection
  382. int_connect( PIN_NUM_MOSI, HSPID_OUT_IDX, HSPIQ_IN_IDX );
  383. memset(rx_buf, 0x66, 320);
  384. for ( int i = 0; i < 8; i ++ ) {
  385. memset( rx_buf, 0x66, sizeof(rx_buf));
  386. spi_transaction_t t = {};
  387. t.length = 8*(i+1);
  388. t.rxlength = 0;
  389. t.tx_buffer = tx_buf+2*i;
  390. t.rx_buffer = rx_buf + i;
  391. if ( i == 1 ) {
  392. //test set no start
  393. t.rx_buffer = NULL;
  394. } else if ( i == 2 ) {
  395. //test rx length != tx_length
  396. t.rxlength = t.length - 8;
  397. }
  398. spi_device_transmit( spi, &t );
  399. for( int i = 0; i < 16; i ++ ) {
  400. printf("%02X ", rx_buf[i]);
  401. }
  402. printf("\n");
  403. if ( i == 1 ) {
  404. // no rx, skip check
  405. } else if ( i == 2 ) {
  406. //test rx length = tx length-1
  407. TEST_ASSERT( memcmp(t.tx_buffer, t.rx_buffer, t.length/8-1)==0 );
  408. } else {
  409. //normal check
  410. TEST_ASSERT( memcmp(t.tx_buffer, t.rx_buffer, t.length/8)==0 );
  411. }
  412. }
  413. TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK);
  414. TEST_ASSERT(spi_bus_free(HSPI_HOST) == ESP_OK);
  415. }
  416. static const char MASTER_TAG[] = "test_master";
  417. static const char SLAVE_TAG[] = "test_slave";
  418. DRAM_ATTR static uint8_t master_send[] = {0x93, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xaa, 0xcc, 0xff, 0xee, 0x55, 0x77, 0x88, 0x43};
  419. DRAM_ATTR static uint8_t slave_send[] = { 0xaa, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x13, 0x57, 0x9b, 0xdf, 0x24, 0x68, 0xac, 0xe0 };
  420. static void master_init( spi_device_handle_t* spi, int mode, uint32_t speed)
  421. {
  422. esp_err_t ret;
  423. spi_bus_config_t buscfg={
  424. .miso_io_num=PIN_NUM_MISO,
  425. .mosi_io_num=PIN_NUM_MOSI,
  426. .sclk_io_num=PIN_NUM_CLK,
  427. .quadwp_io_num=-1,
  428. .quadhd_io_num=-1
  429. };
  430. spi_device_interface_config_t devcfg={
  431. .clock_speed_hz=speed, //currently only up to 4MHz for internel connect
  432. .mode=mode, //SPI mode 0
  433. .spics_io_num=PIN_NUM_CS, //CS pin
  434. .queue_size=16, //We want to be able to queue 7 transactions at a time
  435. .pre_cb=NULL,
  436. .cs_ena_pretrans = 0,
  437. };
  438. //Initialize the SPI bus
  439. ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
  440. TEST_ASSERT(ret==ESP_OK);
  441. //Attach the LCD to the SPI bus
  442. ret=spi_bus_add_device(HSPI_HOST, &devcfg, spi);
  443. TEST_ASSERT(ret==ESP_OK);
  444. }
  445. static void slave_init(int mode, int dma_chan)
  446. {
  447. //Configuration for the SPI bus
  448. spi_bus_config_t buscfg={
  449. .mosi_io_num=PIN_NUM_MOSI,
  450. .miso_io_num=PIN_NUM_MISO,
  451. .sclk_io_num=PIN_NUM_CLK
  452. };
  453. //Configuration for the SPI slave interface
  454. spi_slave_interface_config_t slvcfg={
  455. .mode=mode,
  456. .spics_io_num=PIN_NUM_CS,
  457. .queue_size=3,
  458. .flags=0,
  459. };
  460. //Enable pull-ups on SPI lines so we don't detect rogue pulses when no master is connected.
  461. gpio_set_pull_mode(PIN_NUM_MOSI, GPIO_PULLUP_ONLY);
  462. gpio_set_pull_mode(PIN_NUM_CLK, GPIO_PULLUP_ONLY);
  463. gpio_set_pull_mode(PIN_NUM_CS, GPIO_PULLUP_ONLY);
  464. //Initialize SPI slave interface
  465. TEST_ESP_OK( spi_slave_initialize(VSPI_HOST, &buscfg, &slvcfg, dma_chan) );
  466. }
  467. typedef struct {
  468. uint32_t len;
  469. uint8_t *start;
  470. } slave_txdata_t;
  471. typedef struct {
  472. uint32_t len;
  473. uint8_t data[1];
  474. } slave_rxdata_t;
  475. typedef struct {
  476. RingbufHandle_t data_received;
  477. QueueHandle_t data_to_send;
  478. } spi_slave_task_context_t;
  479. esp_err_t init_slave_context(spi_slave_task_context_t *context)
  480. {
  481. context->data_to_send = xQueueCreate( 16, sizeof( slave_txdata_t ));
  482. if ( context->data_to_send == NULL ) {
  483. return ESP_ERR_NO_MEM;
  484. }
  485. context->data_received = xRingbufferCreate( 1024, RINGBUF_TYPE_NOSPLIT );
  486. if ( context->data_received == NULL ) {
  487. return ESP_ERR_NO_MEM;
  488. }
  489. return ESP_OK;
  490. }
  491. void deinit_slave_context(spi_slave_task_context_t *context)
  492. {
  493. TEST_ASSERT( context->data_to_send != NULL );
  494. vQueueDelete( context->data_to_send );
  495. context->data_to_send = NULL;
  496. TEST_ASSERT( context->data_received != NULL );
  497. vRingbufferDelete( context->data_received );
  498. context->data_received = NULL;
  499. }
  500. static void task_slave(void* arg)
  501. {
  502. spi_slave_task_context_t* context = (spi_slave_task_context_t*) arg;
  503. QueueHandle_t queue = context->data_to_send;
  504. RingbufHandle_t ringbuf = context->data_received;
  505. uint8_t recvbuf[320+4];
  506. slave_txdata_t txdata;
  507. ESP_LOGI( SLAVE_TAG, "slave up" );
  508. //never quit, but blocked by the queue, waiting to be killed, when no more send from main task.
  509. while( 1 ) {
  510. xQueueReceive( queue, &txdata, portMAX_DELAY );
  511. ESP_LOGI( "test", "received: %p", txdata.start );
  512. spi_slave_transaction_t t = {};
  513. t.length = txdata.len;
  514. t.tx_buffer = txdata.start;
  515. t.rx_buffer = recvbuf+4;
  516. //loop until trans_len != 0 to skip glitches
  517. do {
  518. TEST_ESP_OK( spi_slave_transmit( VSPI_HOST, &t, portMAX_DELAY ) );
  519. } while ( t.trans_len == 0 );
  520. *(uint32_t*)recvbuf = t.trans_len;
  521. ESP_LOGI( SLAVE_TAG, "received: %d", t.trans_len );
  522. xRingbufferSend( ringbuf, recvbuf, 4+(t.trans_len+7)/8, portMAX_DELAY );
  523. }
  524. }
  525. TEST_CASE("SPI master variable cmd & addr test","[spi]")
  526. {
  527. uint8_t *tx_buf=master_send;
  528. uint8_t rx_buf[320];
  529. uint8_t *rx_buf_ptr = rx_buf;
  530. spi_slave_task_context_t slave_context = {};
  531. esp_err_t err = init_slave_context( &slave_context );
  532. TEST_ASSERT( err == ESP_OK );
  533. spi_device_handle_t spi;
  534. //initial master, mode 0, 1MHz
  535. master_init( &spi, 0, 1*1000*1000 );
  536. //initial slave, mode 0, no dma
  537. slave_init(0, 0);
  538. //do internal connection
  539. int_connect( PIN_NUM_MOSI, HSPID_OUT_IDX, VSPIQ_IN_IDX );
  540. int_connect( PIN_NUM_MISO, VSPIQ_OUT_IDX, HSPID_IN_IDX );
  541. int_connect( PIN_NUM_CS, HSPICS0_OUT_IDX, VSPICS0_IN_IDX );
  542. int_connect( PIN_NUM_CLK, HSPICLK_OUT_IDX, VSPICLK_IN_IDX );
  543. TaskHandle_t handle_slave;
  544. xTaskCreate( task_slave, "spi_slave", 4096, &slave_context, 0, &handle_slave);
  545. slave_txdata_t slave_txdata[16];
  546. spi_transaction_ext_t trans[16];
  547. for( int i= 0; i < 16; i ++ ) {
  548. //prepare slave tx data
  549. slave_txdata[i] = (slave_txdata_t) {
  550. .start = slave_send + 4*(i%3),
  551. .len = 256,
  552. };
  553. xQueueSend( slave_context.data_to_send, &slave_txdata[i], portMAX_DELAY );
  554. //prepare master tx data
  555. trans[i] = (spi_transaction_ext_t) {
  556. .base = {
  557. .flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
  558. .addr = 0x456789ab,
  559. .cmd = 0xcdef,
  560. .length = 8*i,
  561. .tx_buffer = tx_buf+i,
  562. .rx_buffer = rx_buf_ptr,
  563. },
  564. .command_bits = ((i+1)%3) * 8,
  565. .address_bits = ((i/3)%5) * 8,
  566. };
  567. if ( trans[i].base.length == 0 ) {
  568. trans[i].base.tx_buffer = NULL;
  569. trans[i].base.rx_buffer = NULL;
  570. } else {
  571. rx_buf_ptr += (trans[i].base.length + 31)/32*4;
  572. }
  573. }
  574. vTaskDelay(10);
  575. for ( int i = 0; i < 16; i ++ ) {
  576. TEST_ESP_OK (spi_device_queue_trans( spi, (spi_transaction_t*)&trans[i], portMAX_DELAY ) );
  577. vTaskDelay(10);
  578. }
  579. for( int i= 0; i < 16; i ++ ) {
  580. //wait for both master and slave end
  581. ESP_LOGI( MASTER_TAG, "===== test%d =====", i );
  582. spi_transaction_ext_t *t;
  583. size_t rcv_len;
  584. spi_device_get_trans_result( spi, (spi_transaction_t**)&t, portMAX_DELAY );
  585. TEST_ASSERT( t == &trans[i] );
  586. if ( trans[i].base.length != 0 ) {
  587. ESP_LOG_BUFFER_HEX( "master tx", trans[i].base.tx_buffer, trans[i].base.length/8 );
  588. ESP_LOG_BUFFER_HEX( "master rx", trans[i].base.rx_buffer, trans[i].base.length/8 );
  589. } else {
  590. ESP_LOGI( "master tx", "no data" );
  591. ESP_LOGI( "master rx", "no data" );
  592. }
  593. slave_rxdata_t *rcv_data = xRingbufferReceive( slave_context.data_received, &rcv_len, portMAX_DELAY );
  594. uint8_t *buffer = rcv_data->data;
  595. rcv_len = rcv_data->len;
  596. ESP_LOGI(SLAVE_TAG, "trans_len: %d", rcv_len);
  597. ESP_LOG_BUFFER_HEX( "slave tx", slave_txdata[i].start, (rcv_len+7)/8);
  598. ESP_LOG_BUFFER_HEX( "slave rx", buffer, (rcv_len+7)/8);
  599. //check result
  600. uint8_t *ptr_addr = (uint8_t*)&t->base.addr;
  601. uint8_t *ptr_cmd = (uint8_t*)&t->base.cmd;
  602. for ( int j = 0; j < t->command_bits/8; j ++ ) {
  603. TEST_ASSERT_EQUAL( buffer[j], ptr_cmd[t->command_bits/8-j-1] );
  604. }
  605. for ( int j = 0; j < t->address_bits/8; j ++ ) {
  606. TEST_ASSERT_EQUAL( buffer[t->command_bits/8+j], ptr_addr[t->address_bits/8-j-1] );
  607. }
  608. if ( t->base.length != 0) {
  609. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->base.tx_buffer, buffer + (t->command_bits + t->address_bits)/8, t->base.length/8);
  610. TEST_ASSERT_EQUAL_HEX8_ARRAY(slave_txdata[i].start + (t->command_bits + t->address_bits)/8, t->base.rx_buffer, t->base.length/8);
  611. }
  612. TEST_ASSERT_EQUAL( t->base.length + t->command_bits + t->address_bits, rcv_len );
  613. //clean
  614. vRingbufferReturnItem( slave_context.data_received, buffer );
  615. }
  616. vTaskDelete( handle_slave );
  617. handle_slave = 0;
  618. deinit_slave_context(&slave_context);
  619. TEST_ASSERT(spi_slave_free(VSPI_HOST) == ESP_OK);
  620. TEST_ASSERT(spi_bus_remove_device(spi) == ESP_OK);
  621. TEST_ASSERT(spi_bus_free(HSPI_HOST) == ESP_OK);
  622. ESP_LOGI(MASTER_TAG, "test passed.");
  623. }
  624. #define RECORD_TIME_PREPARE() uint32_t __t1, __t2
  625. #define RECORD_TIME_START() do {__t1 = xthal_get_ccount();}while(0)
  626. #define RECORD_TIME_END(p_time) do{__t2 = xthal_get_ccount(); *p_time = (__t2-__t1)/240;}while(0)
  627. static void speed_setup(spi_device_handle_t* spi, bool use_dma)
  628. {
  629. esp_err_t ret;
  630. spi_bus_config_t buscfg={
  631. .miso_io_num=PIN_NUM_MISO,
  632. .mosi_io_num=PIN_NUM_MOSI,
  633. .sclk_io_num=PIN_NUM_CLK,
  634. .quadwp_io_num=-1,
  635. .quadhd_io_num=-1
  636. };
  637. spi_device_interface_config_t devcfg={
  638. .clock_speed_hz=10*1000*1000, //currently only up to 4MHz for internel connect
  639. .mode=0, //SPI mode 0
  640. .spics_io_num=PIN_NUM_CS, //CS pin
  641. .queue_size=8, //We want to be able to queue 7 transactions at a time
  642. .pre_cb=NULL,
  643. .cs_ena_pretrans = 0,
  644. };
  645. //Initialize the SPI bus and the device to test
  646. ret=spi_bus_initialize(HSPI_HOST, &buscfg, (use_dma?1:0));
  647. TEST_ASSERT(ret==ESP_OK);
  648. ret=spi_bus_add_device(HSPI_HOST, &devcfg, spi);
  649. TEST_ASSERT(ret==ESP_OK);
  650. }
  651. static void speed_deinit(spi_device_handle_t spi)
  652. {
  653. TEST_ESP_OK( spi_bus_remove_device(spi) );
  654. TEST_ESP_OK( spi_bus_free(HSPI_HOST) );
  655. }
  656. static void sorted_array_insert(uint32_t* array, int* size, uint32_t item)
  657. {
  658. int pos;
  659. for (pos = *size; pos>0; pos--) {
  660. if (array[pos-1] < item) break;
  661. array[pos] = array[pos-1];
  662. }
  663. array[pos]=item;
  664. (*size)++;
  665. }
  666. #define TEST_TIMES 11
  667. TEST_CASE("spi_speed","[spi]")
  668. {
  669. RECORD_TIME_PREPARE();
  670. uint32_t t_flight;
  671. //to get rid of the influence of randomly interrupts, we measured the performance by median value
  672. uint32_t t_flight_sorted[TEST_TIMES];
  673. int t_flight_num = 0;
  674. spi_device_handle_t spi;
  675. const bool use_dma = true;
  676. WORD_ALIGNED_ATTR spi_transaction_t trans = {
  677. .length = 1*8,
  678. .flags = SPI_TRANS_USE_TXDATA,
  679. };
  680. //first work with DMA
  681. speed_setup(&spi, use_dma);
  682. //first time introduces a device switch, which costs more time. we skip this
  683. spi_device_transmit(spi, &trans);
  684. //record flight time by isr, with DMA
  685. t_flight_num = 0;
  686. for (int i = 0; i < TEST_TIMES; i++) {
  687. RECORD_TIME_START();
  688. spi_device_transmit(spi, &trans);
  689. RECORD_TIME_END(&t_flight);
  690. sorted_array_insert(t_flight_sorted, &t_flight_num, t_flight);
  691. }
  692. TEST_PERFORMANCE_LESS_THAN(SPI_PER_TRANS_NO_POLLING, "%d us", t_flight_sorted[(TEST_TIMES+1)/2]);
  693. for (int i = 0; i < TEST_TIMES; i++) {
  694. ESP_LOGI(TAG, "%d", t_flight_sorted[i]);
  695. }
  696. speed_deinit(spi);
  697. speed_setup(&spi, !use_dma);
  698. //first time introduces a device switch, which costs more time. we skip this
  699. spi_device_transmit(spi, &trans);
  700. //record flight time by isr, without DMA
  701. t_flight_num = 0;
  702. for (int i = 0; i < TEST_TIMES; i++) {
  703. RECORD_TIME_START();
  704. spi_device_transmit(spi, &trans);
  705. RECORD_TIME_END(&t_flight);
  706. sorted_array_insert(t_flight_sorted, &t_flight_num, t_flight);
  707. }
  708. TEST_PERFORMANCE_LESS_THAN( SPI_PER_TRANS_NO_POLLING_NO_DMA, "%d us", t_flight_sorted[(TEST_TIMES+1)/2]);
  709. for (int i = 0; i < TEST_TIMES; i++) {
  710. ESP_LOGI(TAG, "%d", t_flight_sorted[i]);
  711. }
  712. speed_deinit(spi);
  713. }