test_rmt.c 18 KB

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