test_rmt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 "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 (4) // 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]")
  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_cfg = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 0);
  151. for (int i = 0; i < SOC_RMT_TX_CHANNELS_NUM; i++) {
  152. tx_cfg.channel = i;
  153. TEST_ESP_OK(rmt_config(&tx_cfg));
  154. TEST_ESP_OK(rmt_driver_install(tx_cfg.channel, 0, 0));
  155. }
  156. for (int i = 0; i < SOC_RMT_TX_CHANNELS_NUM; i++) {
  157. TEST_ESP_OK(rmt_driver_uninstall(i));
  158. }
  159. rmt_config_t rx_cfg = RMT_DEFAULT_CONFIG_RX(RMT_DATA_IO, RMT_RX_CHANNEL_ENCODING_START);
  160. for (int i = RMT_RX_CHANNEL_ENCODING_START; i < SOC_RMT_CHANNELS_NUM; i++) {
  161. rx_cfg.channel = i;
  162. TEST_ESP_OK(rmt_config(&rx_cfg));
  163. TEST_ESP_OK(rmt_driver_install(rx_cfg.channel, 0, 0));
  164. }
  165. for (int i = RMT_RX_CHANNEL_ENCODING_START; i < SOC_RMT_CHANNELS_NUM; i++) {
  166. TEST_ESP_OK(rmt_driver_uninstall(i));
  167. }
  168. }
  169. TEST_CASE("RMT install/uninstall test", "[rmt]")
  170. {
  171. rmt_config_t tx_cfg = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, RMT_TX_CHANNEL_ENCODING_END);
  172. TEST_ESP_OK(rmt_config(&tx_cfg));
  173. for (int i = 0; i < 100; i++) {
  174. TEST_ESP_OK(rmt_driver_install(tx_cfg.channel, 1000, 0));
  175. TEST_ESP_OK(rmt_driver_uninstall(tx_cfg.channel));
  176. }
  177. rmt_config_t rx_cfg = RMT_DEFAULT_CONFIG_RX(RMT_DATA_IO, RMT_RX_CHANNEL_ENCODING_START);
  178. TEST_ESP_OK(rmt_config(&rx_cfg));
  179. for (int i = 0; i < 100; i++) {
  180. TEST_ESP_OK(rmt_driver_install(rx_cfg.channel, 1000, 0));
  181. TEST_ESP_OK(rmt_driver_uninstall(rx_cfg.channel));
  182. }
  183. }
  184. static void test_rmt_translator(const void *src, rmt_item32_t *dest, size_t src_size,
  185. size_t wanted_num, size_t *translated_size, size_t *item_num)
  186. {
  187. const rmt_item32_t bit0 = {{{ 10, 1, 20, 0 }}}; //Logical 0
  188. const rmt_item32_t bit1 = {{{ 20, 1, 10, 0 }}}; //Logical 1
  189. size_t size = 0;
  190. size_t num = 0;
  191. uint8_t *psrc = (uint8_t *)src;
  192. rmt_item32_t *pdest = dest;
  193. while (size < src_size && num < wanted_num) {
  194. for (int i = 0; i < 8; i++) {
  195. // MSB first
  196. if (*psrc & (1 << (7 - i))) {
  197. pdest->val = bit1.val;
  198. } else {
  199. pdest->val = bit0.val;
  200. }
  201. num++;
  202. pdest++;
  203. }
  204. size++;
  205. psrc++;
  206. }
  207. *translated_size = size;
  208. *item_num = num;
  209. int *user_data = NULL;
  210. rmt_translator_get_context(item_num, (void **)&user_data);
  211. esp_rom_printf("user data=%d\r\n", *user_data);
  212. *user_data = 100;
  213. }
  214. TEST_CASE("RMT translator with user context", "[rmt]")
  215. {
  216. rmt_config_t tx_cfg = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 0);
  217. TEST_ESP_OK(rmt_config(&tx_cfg));
  218. TEST_ESP_OK(rmt_driver_install(tx_cfg.channel, 0, 0));
  219. rmt_translator_init(tx_cfg.channel, test_rmt_translator);
  220. int user_data = 999;
  221. rmt_translator_set_context(tx_cfg.channel, &user_data);
  222. uint8_t test_buf[] = {1, 2, 3, 4, 5, 6};
  223. rmt_write_sample(tx_cfg.channel, test_buf, sizeof(test_buf), true);
  224. vTaskDelay(pdMS_TO_TICKS(100));
  225. TEST_ASSERT_EQUAL(100, user_data);
  226. TEST_ESP_OK(rmt_driver_uninstall(tx_cfg.channel));
  227. }
  228. static void do_nec_tx_rx(uint32_t flags)
  229. {
  230. RingbufHandle_t rb = NULL;
  231. rmt_item32_t *items = NULL;
  232. size_t length = 0;
  233. uint32_t addr = 0x10;
  234. uint32_t cmd = 0x20;
  235. bool repeat = false;
  236. int tx_channel = 0;
  237. int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
  238. // test on different flags combinations
  239. rmt_setup_testbench(tx_channel, rx_channel, flags);
  240. // get ready to receive
  241. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  242. TEST_ASSERT_NOT_NULL(rb);
  243. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  244. vTaskDelay(pdMS_TO_TICKS(1000));
  245. // build NEC codes
  246. cmd = 0x20;
  247. while (cmd <= 0x30) {
  248. ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
  249. // Send new key code
  250. TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
  251. TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &items, &length));
  252. if (cmd & 0x01) {
  253. TEST_ESP_OK(rmt_write_items(tx_channel, items, length, false)); // no wait
  254. TEST_ESP_OK(rmt_wait_tx_done(tx_channel, portMAX_DELAY));
  255. } else {
  256. TEST_ESP_OK(rmt_write_items(tx_channel, items, length, true)); // wait until done
  257. }
  258. cmd++;
  259. }
  260. // parse NEC codes
  261. while (rb) {
  262. items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  263. if (items) {
  264. length /= 4; // one RMT = 4 Bytes
  265. if (s_ir_parser->input(s_ir_parser, items, length) == ESP_OK) {
  266. if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  267. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  268. }
  269. }
  270. vRingbufferReturnItem(rb, (void *) items);
  271. } else {
  272. ESP_LOGI(TAG, "done");
  273. break;
  274. }
  275. }
  276. TEST_ASSERT_EQUAL(0x30, cmd);
  277. rmt_clean_testbench(tx_channel, rx_channel);
  278. }
  279. // basic nec tx and rx test, using APB source clock, no modulation
  280. TEST_CASE("RMT NEC TX and RX (APB)", "[rmt]")
  281. {
  282. do_nec_tx_rx(0);
  283. }
  284. // test with RMT_TESTBENCH_FLAGS_ALWAYS_ON will take a long time (REF_TICK is much slower than APB CLOCK)
  285. TEST_CASE("RMT NEC TX and RX (always on)", "[rmt][timeout=240]")
  286. {
  287. do_nec_tx_rx(RMT_TESTBENCH_FLAGS_ALWAYS_ON);
  288. }
  289. #if SOC_RMT_SUPPORT_RX_DEMODULATION
  290. // basic nec tx and rx test, using APB source clock, with modulation and demodulation on
  291. TEST_CASE("RMT NEC TX and RX (Modulation/Demodulation)", "[rmt]")
  292. {
  293. do_nec_tx_rx(RMT_TESTBENCH_FLAGS_CARRIER_ON);
  294. }
  295. #endif
  296. TEST_CASE("RMT TX (SOC_RMT_CHANNEL_MEM_WORDS-1) symbols", "[rmt][boundary]")
  297. {
  298. int tx_channel = 0;
  299. rmt_setup_testbench(tx_channel, -1, 0);
  300. rmt_item32_t *items = malloc(sizeof(rmt_item32_t) * (SOC_RMT_CHANNEL_MEM_WORDS - 1));
  301. for (int i = 0; i < SOC_RMT_CHANNEL_MEM_WORDS - 1; i++) {
  302. items[i] = (rmt_item32_t) {
  303. {{
  304. 200, 1, 200, 0
  305. }
  306. }
  307. };
  308. }
  309. TEST_ESP_OK(rmt_write_items(tx_channel, items, SOC_RMT_CHANNEL_MEM_WORDS - 1, 1));
  310. free(items);
  311. rmt_clean_testbench(tx_channel, -1);
  312. }
  313. TEST_CASE("RMT TX stop", "[rmt]")
  314. {
  315. RingbufHandle_t rb = NULL;
  316. rmt_item32_t *frames = NULL;
  317. size_t length = 0;
  318. uint32_t count = 10;
  319. uint32_t addr = 0x10;
  320. uint32_t cmd = 0x20;
  321. bool repeat = false;
  322. int tx_channel = 0;
  323. int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
  324. rmt_setup_testbench(tx_channel, rx_channel, 0);
  325. // re-install ir_builder, to enlarge internal buffer size
  326. TEST_ESP_OK(s_ir_builder->del(s_ir_builder));
  327. ir_builder_config_t ir_builder_config = IR_BUILDER_DEFAULT_CONFIG((ir_dev_t)tx_channel);
  328. ir_builder_config.buffer_size *= count;
  329. ir_builder_config.flags = IR_TOOLS_FLAGS_PROTO_EXT;
  330. s_ir_builder = ir_builder_rmt_new_nec(&ir_builder_config);
  331. TEST_ASSERT_NOT_NULL(s_ir_builder);
  332. // get ready to receive
  333. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  334. TEST_ASSERT_NOT_NULL(rb);
  335. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  336. vTaskDelay(pdMS_TO_TICKS(1000));
  337. // build NEC codes
  338. ESP_LOGI(TAG, "Plan to send command 0x%x~0x%x to address 0x%x", cmd, cmd + count, addr);
  339. for (int i = 0; i <= count; i++) {
  340. TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
  341. cmd++;
  342. }
  343. TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &frames, &length));
  344. // send for 1 second and then stop
  345. TEST_ESP_OK(rmt_write_items(tx_channel, frames, length, true));
  346. vTaskDelay(pdMS_TO_TICKS(100));
  347. TEST_ESP_OK(rmt_tx_stop(tx_channel));
  348. // parse NEC codes
  349. uint32_t num = 0;
  350. while (rb) {
  351. frames = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  352. if (frames) {
  353. length /= 4; // one RMT = 4 Bytes
  354. if (s_ir_parser->input(s_ir_parser, frames, length) == ESP_OK) {
  355. if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  356. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  357. num++;
  358. }
  359. }
  360. vRingbufferReturnItem(rb, (void *) frames);
  361. } else {
  362. ESP_LOGI(TAG, "done");
  363. break;
  364. }
  365. }
  366. TEST_ASSERT(num < count);
  367. rmt_clean_testbench(tx_channel, rx_channel);
  368. }
  369. #if SOC_RMT_SUPPORT_RX_PINGPONG
  370. TEST_CASE("RMT Ping-Pong operation", "[rmt]")
  371. {
  372. int tx_channel = 0;
  373. int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
  374. rmt_item32_t frames[SOC_RMT_CHANNEL_MEM_WORDS * 2]; // send two block data using ping-pong
  375. RingbufHandle_t rb = NULL;
  376. uint32_t size = sizeof(frames) / sizeof(frames[0]);
  377. // The design of the following test frame should trigger three rx threshold interrupt and one rx end interrupt
  378. int i = 0;
  379. for (i = 0; i < size - 1; i++) {
  380. frames[i].level0 = 1;
  381. frames[i].duration0 = 100;
  382. frames[i].level1 = 0;
  383. frames[i].duration1 = 100;
  384. }
  385. frames[i].level0 = 1;
  386. frames[i].duration0 = 0;
  387. frames[i].level1 = 0;
  388. frames[i].duration1 = 0;
  389. rmt_setup_testbench(tx_channel, rx_channel, 0);
  390. // get ready to receive
  391. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  392. TEST_ASSERT_NOT_NULL(rb);
  393. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  394. vTaskDelay(pdMS_TO_TICKS(1000));
  395. for (uint32_t test_count = 0; test_count < 5; test_count++) {
  396. TEST_ESP_OK(rmt_write_items(tx_channel, frames, size, true));
  397. // parse received data
  398. size_t length = 0;
  399. rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  400. if (items) {
  401. vRingbufferReturnItem(rb, (void *) items);
  402. }
  403. TEST_ASSERT_EQUAL(4 * (size - 1), length);
  404. }
  405. rmt_clean_testbench(tx_channel, rx_channel);
  406. }
  407. #endif
  408. #if SOC_RMT_SUPPORT_TX_GROUP
  409. static uint32_t tx_end_time0, tx_end_time1;
  410. static void rmt_tx_end_cb(rmt_channel_t channel, void *arg)
  411. {
  412. if (channel == 0) {
  413. tx_end_time0 = cpu_hal_get_cycle_count();
  414. } else {
  415. tx_end_time1 = cpu_hal_get_cycle_count();
  416. }
  417. }
  418. TEST_CASE("RMT TX simultaneously", "[rmt]")
  419. {
  420. rmt_item32_t frames[SOC_RMT_CHANNEL_MEM_WORDS];
  421. uint32_t size = sizeof(frames) / sizeof(frames[0]);
  422. int channel0 = 0;
  423. int channel1 = 1;
  424. int i = 0;
  425. for (i = 0; i < size - 1; i++) {
  426. frames[i].level0 = 1;
  427. frames[i].duration0 = 1000;
  428. frames[i].level1 = 0;
  429. frames[i].duration1 = 1000;
  430. }
  431. frames[i].level0 = 0;
  432. frames[i].duration0 = 0;
  433. frames[i].level1 = 0;
  434. frames[i].duration1 = 0;
  435. rmt_config_t tx_config0 = RMT_DEFAULT_CONFIG_TX(4, channel0);
  436. rmt_config_t tx_config1 = RMT_DEFAULT_CONFIG_TX(5, channel1);
  437. TEST_ESP_OK(rmt_config(&tx_config0));
  438. TEST_ESP_OK(rmt_config(&tx_config1));
  439. TEST_ESP_OK(rmt_driver_install(channel0, 0, 0));
  440. TEST_ESP_OK(rmt_driver_install(channel1, 0, 0));
  441. rmt_register_tx_end_callback(rmt_tx_end_cb, NULL);
  442. TEST_ESP_OK(rmt_add_channel_to_group(channel0));
  443. TEST_ESP_OK(rmt_add_channel_to_group(channel1));
  444. TEST_ESP_OK(rmt_write_items(channel0, frames, size, false));
  445. vTaskDelay(pdMS_TO_TICKS(1000));
  446. TEST_ESP_OK(rmt_write_items(channel1, frames, size, false));
  447. TEST_ESP_OK(rmt_wait_tx_done(channel0, portMAX_DELAY));
  448. TEST_ESP_OK(rmt_wait_tx_done(channel1, portMAX_DELAY));
  449. ESP_LOGI(TAG, "tx_end_time0=%u, tx_end_time1=%u", tx_end_time0, tx_end_time1);
  450. TEST_ASSERT_LESS_OR_EQUAL_UINT32(2000, tx_end_time1 - tx_end_time0);
  451. TEST_ESP_OK(rmt_remove_channel_from_group(channel0));
  452. TEST_ESP_OK(rmt_remove_channel_from_group(channel1));
  453. TEST_ESP_OK(rmt_driver_uninstall(channel0));
  454. TEST_ESP_OK(rmt_driver_uninstall(channel1));
  455. }
  456. #endif
  457. #if SOC_RMT_SUPPORT_TX_LOOP_COUNT
  458. static void rmt_tx_loop_end(rmt_channel_t channel, void *arg)
  459. {
  460. rmt_tx_stop(channel);
  461. }
  462. TEST_CASE("RMT TX loop", "[rmt]")
  463. {
  464. RingbufHandle_t rb = NULL;
  465. rmt_item32_t *items = NULL;
  466. size_t length = 0;
  467. uint32_t addr = 0x10;
  468. uint32_t cmd = 0x20;
  469. bool repeat = false;
  470. int tx_channel = 0;
  471. int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
  472. uint32_t count = 0;
  473. rmt_setup_testbench(tx_channel, rx_channel, RMT_TESTBENCH_FLAGS_LOOP_ON);
  474. // get ready to receive
  475. TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
  476. TEST_ASSERT_NOT_NULL(rb);
  477. TEST_ESP_OK(rmt_rx_start(rx_channel, true));
  478. vTaskDelay(pdMS_TO_TICKS(1000));
  479. // register callback functions, invoked when tx loop count to ceiling
  480. rmt_register_tx_end_callback(rmt_tx_loop_end, NULL);
  481. // build NEC codes
  482. ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
  483. // Send new key code
  484. TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
  485. TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &items, &length));
  486. TEST_ESP_OK(rmt_write_items(tx_channel, items, length, true)); // wait until done
  487. // parse NEC codes
  488. while (rb) {
  489. items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
  490. if (items) {
  491. length /= 4; // one RMT = 4 Bytes
  492. if (s_ir_parser->input(s_ir_parser, items, length) == ESP_OK) {
  493. if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
  494. count++;
  495. ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
  496. }
  497. }
  498. vRingbufferReturnItem(rb, (void *) items);
  499. } else {
  500. ESP_LOGI(TAG, "done");
  501. break;
  502. }
  503. }
  504. TEST_ASSERT_EQUAL(10, count);
  505. rmt_clean_testbench(tx_channel, rx_channel);
  506. }
  507. #endif