test_rmt.c 20 KB

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