test_rmt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // RMT driver unit test is based on extended NEC protocol
  2. // Please don't use channel number: SOC_RMT_CHANNELS_NUM - 1
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "sdkconfig.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_log.h"
  9. #include "driver/rmt.h"
  10. #include "ir_tools.h"
  11. #include "unity.h"
  12. #include "test_utils.h"
  13. // CI ONLY: Don't connect any other signals to this GPIO
  14. #define RMT_DATA_IO (12) // bind signal RMT_SIG_OUT0_IDX and RMT_SIG_IN0_IDX on the same GPIO
  15. #define RMT_TESTBENCH_FLAGS_ALWAYS_ON (1<<0)
  16. #define RMT_TESTBENCH_FLAGS_CARRIER_ON (1<<1)
  17. #define RMT_TESTBENCH_FLAGS_LOOP_ON (1<<2)
  18. static const char *TAG = "RMT.test";
  19. static ir_builder_t *s_ir_builder = NULL;
  20. static ir_parser_t *s_ir_parser = NULL;
  21. static void rmt_setup_testbench(int tx_channel, int rx_channel, uint32_t flags)
  22. {
  23. // RMT channel configuration
  24. if (tx_channel >= 0) {
  25. rmt_config_t tx_config = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, tx_channel);
  26. if (flags & RMT_TESTBENCH_FLAGS_ALWAYS_ON) {
  27. tx_config.flags |= RMT_CHANNEL_FLAGS_ALWAYS_ON;
  28. }
  29. if (flags & RMT_TESTBENCH_FLAGS_CARRIER_ON) {
  30. tx_config.tx_config.carrier_en = true;
  31. }
  32. #if SOC_RMT_SUPPORT_TX_LOOP_COUNT
  33. if (flags & RMT_TESTBENCH_FLAGS_LOOP_ON) {
  34. tx_config.tx_config.loop_en = true;
  35. tx_config.tx_config.loop_count = 10;
  36. }
  37. #endif
  38. TEST_ESP_OK(rmt_config(&tx_config));
  39. }
  40. if (rx_channel >= 0) {
  41. rmt_config_t rx_config = RMT_DEFAULT_CONFIG_RX(RMT_DATA_IO, rx_channel);
  42. if (flags & RMT_TESTBENCH_FLAGS_ALWAYS_ON) {
  43. rx_config.flags |= RMT_CHANNEL_FLAGS_ALWAYS_ON;
  44. }
  45. #if SOC_RMT_SUPPORT_RX_DEMODULATION
  46. if (flags & RMT_TESTBENCH_FLAGS_CARRIER_ON) {
  47. rx_config.rx_config.rm_carrier = true;
  48. rx_config.rx_config.carrier_freq_hz = 38000;
  49. rx_config.rx_config.carrier_duty_percent = 33;
  50. rx_config.rx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
  51. }
  52. #endif
  53. TEST_ESP_OK(rmt_config(&rx_config));
  54. }
  55. // Routing internal signals by IO Matrix (bind rmt tx and rx signal on the same GPIO)
  56. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[RMT_DATA_IO], PIN_FUNC_GPIO);
  57. TEST_ESP_OK(gpio_set_direction(RMT_DATA_IO, GPIO_MODE_INPUT_OUTPUT));
  58. gpio_matrix_out(RMT_DATA_IO, RMT_SIG_OUT0_IDX + tx_channel, 0, 0);
  59. gpio_matrix_in(RMT_DATA_IO, RMT_SIG_IN0_IDX + rx_channel, 0);
  60. // install driver
  61. if (tx_channel >= 0) {
  62. TEST_ESP_OK(rmt_driver_install(tx_channel, 0, 0));
  63. ir_builder_config_t ir_builder_config = IR_BUILDER_DEFAULT_CONFIG((ir_dev_t)tx_channel);
  64. ir_builder_config.flags = IR_TOOLS_FLAGS_PROTO_EXT;
  65. s_ir_builder = ir_builder_rmt_new_nec(&ir_builder_config);
  66. TEST_ASSERT_NOT_NULL(s_ir_builder);
  67. }
  68. if (rx_channel >= 0) {
  69. TEST_ESP_OK(rmt_driver_install(rx_channel, 3000, 0));
  70. ir_parser_config_t ir_parser_config = IR_PARSER_DEFAULT_CONFIG((ir_dev_t)rx_channel);
  71. ir_parser_config.flags = IR_TOOLS_FLAGS_PROTO_EXT | IR_TOOLS_FLAGS_INVERSE;
  72. s_ir_parser = ir_parser_rmt_new_nec(&ir_parser_config);
  73. TEST_ASSERT_NOT_NULL(s_ir_parser);
  74. }
  75. }
  76. static void rmt_clean_testbench(int tx_channel, int rx_channel)
  77. {
  78. if (tx_channel >= 0) {
  79. TEST_ESP_OK(rmt_driver_uninstall(tx_channel));
  80. TEST_ESP_OK(s_ir_builder->del(s_ir_builder));
  81. s_ir_builder = NULL;
  82. }
  83. if (rx_channel >= 0) {
  84. TEST_ESP_OK(rmt_driver_uninstall(rx_channel));
  85. TEST_ESP_OK(s_ir_parser->del(s_ir_parser));
  86. s_ir_parser = NULL;
  87. }
  88. }
  89. TEST_CASE("RMT wrong configuration", "[rmt][error]")
  90. {
  91. rmt_config_t correct_config = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 0);
  92. rmt_config_t wrong_config = correct_config;
  93. wrong_config.clk_div = 0;
  94. TEST_ASSERT(rmt_config(&wrong_config) == ESP_ERR_INVALID_ARG);
  95. wrong_config = correct_config;
  96. wrong_config.channel = SOC_RMT_CHANNELS_NUM;
  97. TEST_ASSERT(rmt_config(&wrong_config) == ESP_ERR_INVALID_ARG);
  98. wrong_config = correct_config;
  99. wrong_config.channel = 2;
  100. wrong_config.mem_block_num = 8;
  101. TEST_ASSERT(rmt_config(&wrong_config) == ESP_ERR_INVALID_ARG);
  102. TEST_ASSERT(rmt_set_mem_block_num(wrong_config.channel, -1) == ESP_ERR_INVALID_ARG);
  103. }
  104. TEST_CASE("RMT miscellaneous functions", "[rmt]")
  105. {
  106. rmt_channel_t channel = SOC_RMT_CHANNELS_NUM - 2;
  107. uint8_t div_cnt;
  108. rmt_source_clk_t src_clk;
  109. uint8_t memNum;
  110. uint16_t idle_thres;
  111. rmt_mem_owner_t owner;
  112. // TX related functions
  113. rmt_setup_testbench(channel, -1, 0);
  114. TEST_ESP_OK(rmt_set_mem_block_num(channel, 2));
  115. TEST_ESP_OK(rmt_get_mem_block_num(channel, &memNum));
  116. TEST_ASSERT_EQUAL_UINT8(2, memNum);
  117. TEST_ESP_OK(rmt_set_clk_div(channel, 160));
  118. TEST_ESP_OK(rmt_get_clk_div(channel, &div_cnt));
  119. TEST_ASSERT_EQUAL_UINT8(160, div_cnt);
  120. TEST_ESP_OK(rmt_set_source_clk(channel, RMT_BASECLK_REF));
  121. TEST_ESP_OK(rmt_get_source_clk(channel, &src_clk));
  122. TEST_ASSERT_EQUAL_INT(RMT_BASECLK_REF, src_clk);
  123. TEST_ESP_OK(rmt_set_memory_owner(channel, RMT_MEM_OWNER_RX));
  124. TEST_ESP_OK(rmt_get_memory_owner(channel, &owner));
  125. TEST_ASSERT_EQUAL_INT(RMT_MEM_OWNER_RX, owner);
  126. TEST_ESP_OK(rmt_set_tx_carrier(channel, 0, 1, 0, 1));
  127. TEST_ESP_OK(rmt_set_idle_level(channel, 1, 0));
  128. rmt_clean_testbench(channel, -1);
  129. // RX related functions
  130. rmt_setup_testbench(-1, channel, 0);
  131. TEST_ESP_OK(rmt_set_rx_idle_thresh(channel, 200));
  132. TEST_ESP_OK(rmt_get_rx_idle_thresh(channel, &idle_thres));
  133. TEST_ASSERT_EQUAL_UINT16(200, idle_thres);
  134. TEST_ESP_OK(rmt_set_rx_filter(channel, 1, 100));
  135. rmt_clean_testbench(-1, channel);
  136. }
  137. TEST_CASE("RMT multiple channels", "[rmt]")
  138. {
  139. rmt_config_t tx_cfg1 = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 0);
  140. TEST_ESP_OK(rmt_config(&tx_cfg1));
  141. TEST_ESP_OK(rmt_driver_install(tx_cfg1.channel, 0, 0));
  142. rmt_config_t tx_cfg2 = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 1);
  143. TEST_ESP_OK(rmt_config(&tx_cfg2));
  144. TEST_ESP_OK(rmt_driver_install(tx_cfg2.channel, 0, 0));
  145. rmt_config_t tx_cfg3 = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 2);
  146. TEST_ESP_OK(rmt_config(&tx_cfg3));
  147. TEST_ESP_OK(rmt_driver_install(tx_cfg3.channel, 0, 0));
  148. TEST_ESP_OK(rmt_driver_uninstall(2));
  149. TEST_ESP_OK(rmt_driver_uninstall(1));
  150. TEST_ESP_OK(rmt_driver_uninstall(0));
  151. }
  152. TEST_CASE("RMT install/uninstall test", "[rmt][pressure]")
  153. {
  154. rmt_config_t rx_cfg = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, SOC_RMT_CHANNELS_NUM - 2);
  155. TEST_ESP_OK(rmt_config(&rx_cfg));
  156. for (int i = 0; i < 100; i++) {
  157. TEST_ESP_OK(rmt_driver_install(rx_cfg.channel, 1000, 0));
  158. TEST_ESP_OK(rmt_driver_uninstall(rx_cfg.channel));
  159. }
  160. }
  161. static void do_nec_tx_rx(uint32_t flags)
  162. {
  163. RingbufHandle_t rb = NULL;
  164. rmt_item32_t *items = NULL;
  165. uint32_t length = 0;
  166. uint32_t addr = 0x10;
  167. uint32_t cmd = 0x20;
  168. bool repeat = false;
  169. int tx_channel = 0;
  170. int rx_channel = 1;
  171. // test on different flags combinations
  172. rmt_setup_testbench(tx_channel, rx_channel, flags);
  173. // get ready to receive
  174. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  175. TEST_ASSERT_NOT_NULL(rb);
  176. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  177. vTaskDelay(pdMS_TO_TICKS(1000));
  178. // build NEC codes
  179. cmd = 0x20;
  180. while (cmd <= 0x30) {
  181. ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
  182. // Send new key code
  183. TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
  184. TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &items, &length));
  185. if (cmd & 0x01) {
  186. TEST_ESP_OK(rmt_write_items(tx_channel, items, length, false)); // no wait
  187. TEST_ESP_OK(rmt_wait_tx_done(tx_channel, portMAX_DELAY));
  188. } else {
  189. TEST_ESP_OK(rmt_write_items(tx_channel, items, length, true)); // wait until done
  190. }
  191. cmd++;
  192. }
  193. // parse NEC codes
  194. while (rb) {
  195. items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  196. if (items) {
  197. length /= 4; // one RMT = 4 Bytes
  198. if (s_ir_parser->input(s_ir_parser, items, length) == ESP_OK) {
  199. if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  200. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  201. }
  202. }
  203. vRingbufferReturnItem(rb, (void *) items);
  204. } else {
  205. ESP_LOGI(TAG, "done");
  206. break;
  207. }
  208. }
  209. TEST_ASSERT_EQUAL(0x30, cmd);
  210. rmt_clean_testbench(tx_channel, rx_channel);
  211. }
  212. // basic nec tx and rx test, using APB source clock, no modulation
  213. TEST_CASE("RMT NEC TX and RX (APB)", "[rmt]")
  214. {
  215. do_nec_tx_rx(0);
  216. }
  217. // test with RMT_TESTBENCH_FLAGS_ALWAYS_ON will take a long time (REF_TICK is much slower than APB CLOCK)
  218. TEST_CASE("RMT NEC TX and RX (REF_TICK)", "[rmt][timeout=240]")
  219. {
  220. do_nec_tx_rx(RMT_TESTBENCH_FLAGS_ALWAYS_ON);
  221. }
  222. #if SOC_RMT_SUPPORT_RX_DEMODULATION
  223. // basic nec tx and rx test, using APB source clock, with modulation and demodulation on
  224. TEST_CASE("RMT NEC TX and RX (Modulation/Demodulation)", "[rmt]")
  225. {
  226. do_nec_tx_rx(RMT_TESTBENCH_FLAGS_CARRIER_ON);
  227. }
  228. #endif
  229. TEST_CASE("RMT TX (SOC_RMT_CHANNEL_MEM_WORDS-1) symbols", "[rmt][boundary]")
  230. {
  231. int tx_channel = 0;
  232. rmt_setup_testbench(tx_channel, -1, 0);
  233. rmt_item32_t *items = malloc(sizeof(rmt_item32_t) * (SOC_RMT_CHANNEL_MEM_WORDS - 1));
  234. for (int i = 0; i < SOC_RMT_CHANNEL_MEM_WORDS - 1; i++) {
  235. items[i] = (rmt_item32_t) {
  236. {{
  237. 200, 1, 200, 0
  238. }
  239. }
  240. };
  241. }
  242. TEST_ESP_OK(rmt_write_items(tx_channel, items, SOC_RMT_CHANNEL_MEM_WORDS - 1, 1));
  243. free(items);
  244. rmt_clean_testbench(tx_channel, -1);
  245. }
  246. TEST_CASE("RMT TX stop", "[rmt]")
  247. {
  248. RingbufHandle_t rb = NULL;
  249. rmt_item32_t *frames = NULL;
  250. uint32_t length = 0;
  251. uint32_t count = 10;
  252. uint32_t addr = 0x10;
  253. uint32_t cmd = 0x20;
  254. bool repeat = false;
  255. int tx_channel = 0;
  256. int rx_channel = 1;
  257. rmt_setup_testbench(tx_channel, rx_channel, 0);
  258. // re-install ir_builder, to enlarge internal buffer size
  259. TEST_ESP_OK(s_ir_builder->del(s_ir_builder));
  260. ir_builder_config_t ir_builder_config = IR_BUILDER_DEFAULT_CONFIG((ir_dev_t)tx_channel);
  261. ir_builder_config.buffer_size *= count;
  262. ir_builder_config.flags = IR_TOOLS_FLAGS_PROTO_EXT;
  263. s_ir_builder = ir_builder_rmt_new_nec(&ir_builder_config);
  264. TEST_ASSERT_NOT_NULL(s_ir_builder);
  265. // get ready to receive
  266. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  267. TEST_ASSERT_NOT_NULL(rb);
  268. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  269. vTaskDelay(pdMS_TO_TICKS(1000));
  270. // build NEC codes
  271. ESP_LOGI(TAG, "Plan to send command 0x%x~0x%x to address 0x%x", cmd, cmd + count, addr);
  272. for (int i = 0; i <= count; i++) {
  273. TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
  274. cmd++;
  275. }
  276. TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &frames, &length));
  277. // send for 1 second and then stop
  278. TEST_ESP_OK(rmt_write_items(tx_channel, frames, length, true));
  279. vTaskDelay(pdMS_TO_TICKS(100));
  280. TEST_ESP_OK(rmt_tx_stop(tx_channel));
  281. // parse NEC codes
  282. uint32_t num = 0;
  283. while (rb) {
  284. frames = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  285. if (frames) {
  286. length /= 4; // one RMT = 4 Bytes
  287. if (s_ir_parser->input(s_ir_parser, frames, length) == ESP_OK) {
  288. if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  289. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  290. num++;
  291. }
  292. }
  293. vRingbufferReturnItem(rb, (void *) frames);
  294. } else {
  295. ESP_LOGI(TAG, "done");
  296. break;
  297. }
  298. }
  299. TEST_ASSERT(num < count);
  300. rmt_clean_testbench(tx_channel, rx_channel);
  301. }
  302. #if SOC_RMT_SUPPORT_RX_PINGPONG
  303. TEST_CASE("RMT Ping-Pong operation", "[rmt]")
  304. {
  305. int tx_channel = 0;
  306. int rx_channel = 1;
  307. rmt_item32_t frames[SOC_RMT_CHANNEL_MEM_WORDS * 2]; // send two block data using ping-pong
  308. RingbufHandle_t rb = NULL;
  309. uint32_t size = sizeof(frames) / sizeof(frames[0]);
  310. // The design of the following test frame should trigger three rx threshold interrupt and one rx end interrupt
  311. int i = 0;
  312. for (i = 0; i < size - 1; i++) {
  313. frames[i].level0 = 1;
  314. frames[i].duration0 = 100;
  315. frames[i].level1 = 0;
  316. frames[i].duration1 = 100;
  317. }
  318. frames[i].level0 = 1;
  319. frames[i].duration0 = 0;
  320. frames[i].level1 = 0;
  321. frames[i].duration1 = 0;
  322. rmt_setup_testbench(tx_channel, rx_channel, 0);
  323. // get ready to receive
  324. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  325. TEST_ASSERT_NOT_NULL(rb);
  326. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  327. vTaskDelay(pdMS_TO_TICKS(1000));
  328. for (uint32_t test_count = 0; test_count < 5; test_count++) {
  329. TEST_ESP_OK(rmt_write_items(tx_channel, frames, size, true));
  330. // parse received data
  331. uint32_t length = 0;
  332. rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  333. if (items) {
  334. vRingbufferReturnItem(rb, (void *) items);
  335. }
  336. TEST_ASSERT_EQUAL(4 * (size - 1), length);
  337. }
  338. rmt_clean_testbench(tx_channel, rx_channel);
  339. }
  340. #endif
  341. #if SOC_RMT_SUPPORT_TX_GROUP
  342. static uint32_t tx_end_time0, tx_end_time1;
  343. static void rmt_tx_end_cb(rmt_channel_t channel, void *arg)
  344. {
  345. if (channel == 0) {
  346. tx_end_time0 = esp_cpu_get_ccount();
  347. } else {
  348. tx_end_time1 = esp_cpu_get_ccount();
  349. }
  350. }
  351. TEST_CASE("RMT TX simultaneously", "[rmt]")
  352. {
  353. rmt_item32_t frames[SOC_RMT_CHANNEL_MEM_WORDS];
  354. uint32_t size = sizeof(frames) / sizeof(frames[0]);
  355. int channel0 = 0;
  356. int channel1 = 1;
  357. int i = 0;
  358. for (i = 0; i < size - 1; i++) {
  359. frames[i].level0 = 1;
  360. frames[i].duration0 = 1000;
  361. frames[i].level1 = 0;
  362. frames[i].duration1 = 1000;
  363. }
  364. frames[i].level0 = 0;
  365. frames[i].duration0 = 0;
  366. frames[i].level1 = 0;
  367. frames[i].duration1 = 0;
  368. rmt_config_t tx_config0 = RMT_DEFAULT_CONFIG_TX(12, channel0);
  369. rmt_config_t tx_config1 = RMT_DEFAULT_CONFIG_TX(13, channel1);
  370. TEST_ESP_OK(rmt_config(&tx_config0));
  371. TEST_ESP_OK(rmt_config(&tx_config1));
  372. TEST_ESP_OK(rmt_driver_install(channel0, 0, 0));
  373. TEST_ESP_OK(rmt_driver_install(channel1, 0, 0));
  374. rmt_register_tx_end_callback(rmt_tx_end_cb, NULL);
  375. TEST_ESP_OK(rmt_add_channel_to_group(channel0));
  376. TEST_ESP_OK(rmt_add_channel_to_group(channel1));
  377. TEST_ESP_OK(rmt_write_items(channel0, frames, size, false));
  378. vTaskDelay(pdMS_TO_TICKS(1000));
  379. TEST_ESP_OK(rmt_write_items(channel1, frames, size, false));
  380. TEST_ESP_OK(rmt_wait_tx_done(channel0, portMAX_DELAY));
  381. TEST_ESP_OK(rmt_wait_tx_done(channel1, portMAX_DELAY));
  382. ESP_LOGI(TAG, "tx_end_time0=%u, tx_end_time1=%u", tx_end_time0, tx_end_time1);
  383. TEST_ASSERT_LESS_OR_EQUAL_UINT32(2000, tx_end_time1 - tx_end_time0);
  384. TEST_ESP_OK(rmt_remove_channel_from_group(channel0));
  385. TEST_ESP_OK(rmt_remove_channel_from_group(channel1));
  386. TEST_ESP_OK(rmt_driver_uninstall(channel0));
  387. TEST_ESP_OK(rmt_driver_uninstall(channel1));
  388. }
  389. #endif
  390. #if SOC_RMT_SUPPORT_TX_LOOP_COUNT
  391. static void rmt_tx_loop_end(rmt_channel_t channel, void *arg)
  392. {
  393. rmt_tx_stop(channel);
  394. }
  395. TEST_CASE("RMT TX loop", "[rmt]")
  396. {
  397. RingbufHandle_t rb = NULL;
  398. rmt_item32_t *items = NULL;
  399. uint32_t length = 0;
  400. uint32_t addr = 0x10;
  401. uint32_t cmd = 0x20;
  402. bool repeat = false;
  403. int tx_channel = 0;
  404. int rx_channel = 1;
  405. uint32_t count = 0;
  406. rmt_setup_testbench(tx_channel, rx_channel, RMT_TESTBENCH_FLAGS_LOOP_ON);
  407. // get ready to receive
  408. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  409. TEST_ASSERT_NOT_NULL(rb);
  410. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  411. vTaskDelay(pdMS_TO_TICKS(1000));
  412. // register callback functions, invoked when tx loop count to ceiling
  413. rmt_register_tx_end_callback(rmt_tx_loop_end, NULL);
  414. // build NEC codes
  415. ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
  416. // Send new key code
  417. TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
  418. TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &items, &length));
  419. TEST_ESP_OK(rmt_write_items(tx_channel, items, length, true)); // wait until done
  420. // parse NEC codes
  421. while (rb) {
  422. items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  423. if (items) {
  424. length /= 4; // one RMT = 4 Bytes
  425. if (s_ir_parser->input(s_ir_parser, items, length) == ESP_OK) {
  426. if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  427. count++;
  428. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  429. }
  430. }
  431. vRingbufferReturnItem(rb, (void *) items);
  432. } else {
  433. ESP_LOGI(TAG, "done");
  434. break;
  435. }
  436. }
  437. TEST_ASSERT_EQUAL(10, count);
  438. rmt_clean_testbench(tx_channel, rx_channel);
  439. }
  440. #endif