test_rmt.c 20 KB

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