2021-05-23 19:06:17 -04:00
|
|
|
/*
|
2022-02-08 00:57:01 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-05-23 19:06:17 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-02-24 08:27:00 -05:00
|
|
|
// RMT driver unit test is based on extended NEC protocol
|
2019-11-19 03:10:02 -05:00
|
|
|
#include <stdio.h>
|
2018-01-02 07:17:49 -05:00
|
|
|
#include <string.h>
|
2019-11-19 03:10:02 -05:00
|
|
|
#include "sdkconfig.h"
|
2021-01-11 08:03:45 -05:00
|
|
|
#include "hal/cpu_hal.h"
|
2021-03-15 22:55:05 -04:00
|
|
|
#include "hal/gpio_hal.h"
|
2018-01-02 07:17:49 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_log.h"
|
2020-02-24 08:27:00 -05:00
|
|
|
#include "driver/rmt.h"
|
|
|
|
#include "ir_tools.h"
|
|
|
|
#include "unity.h"
|
|
|
|
#include "test_utils.h"
|
2020-06-19 00:00:58 -04:00
|
|
|
#include "esp_rom_gpio.h"
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2021-02-07 04:18:39 -05:00
|
|
|
#define RMT_RX_CHANNEL_ENCODING_START (SOC_RMT_CHANNELS_PER_GROUP-SOC_RMT_TX_CANDIDATES_PER_GROUP)
|
|
|
|
#define RMT_TX_CHANNEL_ENCODING_END (SOC_RMT_TX_CANDIDATES_PER_GROUP-1)
|
2020-08-20 00:22:36 -04:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
// CI ONLY: Don't connect any other signals to this GPIO
|
2022-02-08 00:57:01 -05:00
|
|
|
#define RMT_DATA_IO (0) // bind signal RMT_SIG_OUT0_IDX and RMT_SIG_IN0_IDX on the same GPIO
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-03-17 07:47:38 -04:00
|
|
|
#define RMT_TESTBENCH_FLAGS_ALWAYS_ON (1<<0)
|
|
|
|
#define RMT_TESTBENCH_FLAGS_CARRIER_ON (1<<1)
|
2020-03-18 06:13:27 -04:00
|
|
|
#define RMT_TESTBENCH_FLAGS_LOOP_ON (1<<2)
|
2020-03-17 07:47:38 -04:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
static const char *TAG = "RMT.test";
|
|
|
|
static ir_builder_t *s_ir_builder = NULL;
|
|
|
|
static ir_parser_t *s_ir_parser = NULL;
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
static void rmt_setup_testbench(int tx_channel, int rx_channel, uint32_t flags)
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
2020-02-24 08:27:00 -05:00
|
|
|
// RMT channel configuration
|
|
|
|
if (tx_channel >= 0) {
|
|
|
|
rmt_config_t tx_config = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, tx_channel);
|
2020-03-17 07:47:38 -04:00
|
|
|
if (flags & RMT_TESTBENCH_FLAGS_ALWAYS_ON) {
|
2020-11-02 05:03:33 -05:00
|
|
|
tx_config.flags |= RMT_CHANNEL_FLAGS_AWARE_DFS;
|
2020-03-17 07:47:38 -04:00
|
|
|
}
|
|
|
|
if (flags & RMT_TESTBENCH_FLAGS_CARRIER_ON) {
|
|
|
|
tx_config.tx_config.carrier_en = true;
|
|
|
|
}
|
2020-03-25 05:13:10 -04:00
|
|
|
#if SOC_RMT_SUPPORT_TX_LOOP_COUNT
|
2020-03-18 06:13:27 -04:00
|
|
|
if (flags & RMT_TESTBENCH_FLAGS_LOOP_ON) {
|
|
|
|
tx_config.tx_config.loop_en = true;
|
|
|
|
tx_config.tx_config.loop_count = 10;
|
|
|
|
}
|
|
|
|
#endif
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(rmt_config(&tx_config));
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
if (rx_channel >= 0) {
|
|
|
|
rmt_config_t rx_config = RMT_DEFAULT_CONFIG_RX(RMT_DATA_IO, rx_channel);
|
2020-03-17 07:47:38 -04:00
|
|
|
if (flags & RMT_TESTBENCH_FLAGS_ALWAYS_ON) {
|
2020-11-02 05:03:33 -05:00
|
|
|
rx_config.flags |= RMT_CHANNEL_FLAGS_AWARE_DFS;
|
2020-03-17 07:47:38 -04:00
|
|
|
}
|
2020-03-25 05:13:10 -04:00
|
|
|
#if SOC_RMT_SUPPORT_RX_DEMODULATION
|
2020-03-17 07:47:38 -04:00
|
|
|
if (flags & RMT_TESTBENCH_FLAGS_CARRIER_ON) {
|
|
|
|
rx_config.rx_config.rm_carrier = true;
|
|
|
|
rx_config.rx_config.carrier_freq_hz = 38000;
|
|
|
|
rx_config.rx_config.carrier_duty_percent = 33;
|
|
|
|
rx_config.rx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
|
|
|
|
}
|
|
|
|
#endif
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(rmt_config(&rx_config));
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
// Routing internal signals by IO Matrix (bind rmt tx and rx signal on the same GPIO)
|
2021-03-15 22:55:05 -04:00
|
|
|
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[RMT_DATA_IO], PIN_FUNC_GPIO);
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(gpio_set_direction(RMT_DATA_IO, GPIO_MODE_INPUT_OUTPUT));
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_connect_out_signal(RMT_DATA_IO, RMT_SIG_OUT0_IDX + tx_channel, 0, 0);
|
|
|
|
esp_rom_gpio_connect_in_signal(RMT_DATA_IO, RMT_SIG_IN0_IDX + rx_channel, 0);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
// install driver
|
|
|
|
if (tx_channel >= 0) {
|
|
|
|
TEST_ESP_OK(rmt_driver_install(tx_channel, 0, 0));
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
ir_builder_config_t ir_builder_config = IR_BUILDER_DEFAULT_CONFIG((ir_dev_t)tx_channel);
|
|
|
|
ir_builder_config.flags = IR_TOOLS_FLAGS_PROTO_EXT;
|
|
|
|
s_ir_builder = ir_builder_rmt_new_nec(&ir_builder_config);
|
|
|
|
TEST_ASSERT_NOT_NULL(s_ir_builder);
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
if (rx_channel >= 0) {
|
|
|
|
TEST_ESP_OK(rmt_driver_install(rx_channel, 3000, 0));
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
ir_parser_config_t ir_parser_config = IR_PARSER_DEFAULT_CONFIG((ir_dev_t)rx_channel);
|
|
|
|
ir_parser_config.flags = IR_TOOLS_FLAGS_PROTO_EXT | IR_TOOLS_FLAGS_INVERSE;
|
|
|
|
s_ir_parser = ir_parser_rmt_new_nec(&ir_parser_config);
|
|
|
|
TEST_ASSERT_NOT_NULL(s_ir_parser);
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
static void rmt_clean_testbench(int tx_channel, int rx_channel)
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
2020-02-24 08:27:00 -05:00
|
|
|
if (tx_channel >= 0) {
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(tx_channel));
|
|
|
|
TEST_ESP_OK(s_ir_builder->del(s_ir_builder));
|
|
|
|
s_ir_builder = NULL;
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
if (rx_channel >= 0) {
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(rx_channel));
|
|
|
|
TEST_ESP_OK(s_ir_parser->del(s_ir_parser));
|
|
|
|
s_ir_parser = NULL;
|
2020-02-14 01:33:07 -05:00
|
|
|
}
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:03:48 -05:00
|
|
|
TEST_CASE("RMT wrong configuration", "[rmt]")
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
2020-02-24 08:27:00 -05:00
|
|
|
rmt_config_t correct_config = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 0);
|
|
|
|
rmt_config_t wrong_config = correct_config;
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
wrong_config.clk_div = 0;
|
|
|
|
TEST_ASSERT(rmt_config(&wrong_config) == ESP_ERR_INVALID_ARG);
|
2020-01-10 02:08:40 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
wrong_config = correct_config;
|
2021-02-07 04:18:39 -05:00
|
|
|
wrong_config.channel = SOC_RMT_CHANNELS_PER_GROUP;
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ASSERT(rmt_config(&wrong_config) == ESP_ERR_INVALID_ARG);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
wrong_config = correct_config;
|
|
|
|
wrong_config.channel = 2;
|
|
|
|
wrong_config.mem_block_num = 8;
|
|
|
|
TEST_ASSERT(rmt_config(&wrong_config) == ESP_ERR_INVALID_ARG);
|
|
|
|
TEST_ASSERT(rmt_set_mem_block_num(wrong_config.channel, -1) == ESP_ERR_INVALID_ARG);
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_CASE("RMT miscellaneous functions", "[rmt]")
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
2020-10-09 04:41:41 -04:00
|
|
|
rmt_channel_t channel = 0;
|
2018-01-02 07:17:49 -05:00
|
|
|
uint8_t div_cnt;
|
|
|
|
rmt_source_clk_t src_clk;
|
|
|
|
uint8_t memNum;
|
2020-02-24 08:27:00 -05:00
|
|
|
uint16_t idle_thres;
|
2018-01-02 07:17:49 -05:00
|
|
|
rmt_mem_owner_t owner;
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
// TX related functions
|
|
|
|
rmt_setup_testbench(channel, -1, 0);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_set_mem_block_num(channel, 2));
|
|
|
|
TEST_ESP_OK(rmt_get_mem_block_num(channel, &memNum));
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ASSERT_EQUAL_UINT8(2, memNum);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(rmt_set_clk_div(channel, 160));
|
2018-01-02 07:17:49 -05:00
|
|
|
TEST_ESP_OK(rmt_get_clk_div(channel, &div_cnt));
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ASSERT_EQUAL_UINT8(160, div_cnt);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-08-13 04:55:56 -04:00
|
|
|
#if SOC_RMT_SUPPORT_REF_TICK
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(rmt_set_source_clk(channel, RMT_BASECLK_REF));
|
|
|
|
TEST_ESP_OK(rmt_get_source_clk(channel, &src_clk));
|
|
|
|
TEST_ASSERT_EQUAL_INT(RMT_BASECLK_REF, src_clk);
|
2020-08-13 04:55:56 -04:00
|
|
|
#endif
|
|
|
|
|
2020-10-09 04:41:41 -04:00
|
|
|
#if SOC_RMT_SUPPORT_XTAL
|
2020-08-13 04:55:56 -04:00
|
|
|
TEST_ESP_OK(rmt_set_source_clk(channel, RMT_BASECLK_XTAL));
|
|
|
|
TEST_ESP_OK(rmt_get_source_clk(channel, &src_clk));
|
|
|
|
TEST_ASSERT_EQUAL_INT(RMT_BASECLK_XTAL, src_clk);
|
|
|
|
#endif
|
2020-02-24 08:27:00 -05:00
|
|
|
|
2018-01-02 07:17:49 -05:00
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_set_tx_carrier(channel, 0, 1, 0, 1));
|
|
|
|
TEST_ESP_OK(rmt_set_idle_level(channel, 1, 0));
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
rmt_clean_testbench(channel, -1);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
// RX related functions
|
2020-10-09 04:41:41 -04:00
|
|
|
channel = RMT_RX_CHANNEL_ENCODING_START;
|
2020-02-24 08:27:00 -05:00
|
|
|
rmt_setup_testbench(-1, channel, 0);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(rmt_set_rx_idle_thresh(channel, 200));
|
|
|
|
TEST_ESP_OK(rmt_get_rx_idle_thresh(channel, &idle_thres));
|
|
|
|
TEST_ASSERT_EQUAL_UINT16(200, idle_thres);
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(rmt_set_rx_filter(channel, 1, 100));
|
|
|
|
|
2020-10-09 04:41:41 -04:00
|
|
|
TEST_ESP_OK(rmt_set_memory_owner(channel, RMT_MEM_OWNER_RX));
|
|
|
|
TEST_ESP_OK(rmt_get_memory_owner(channel, &owner));
|
|
|
|
TEST_ASSERT_EQUAL_INT(RMT_MEM_OWNER_RX, owner);
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
rmt_clean_testbench(-1, channel);
|
2019-11-19 03:10:02 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_CASE("RMT multiple channels", "[rmt]")
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
2020-12-16 04:03:48 -05:00
|
|
|
rmt_config_t tx_cfg = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 0);
|
2021-02-07 04:18:39 -05:00
|
|
|
for (int i = 0; i < SOC_RMT_TX_CANDIDATES_PER_GROUP; i++) {
|
2020-12-16 04:03:48 -05:00
|
|
|
tx_cfg.channel = i;
|
|
|
|
TEST_ESP_OK(rmt_config(&tx_cfg));
|
|
|
|
TEST_ESP_OK(rmt_driver_install(tx_cfg.channel, 0, 0));
|
|
|
|
}
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2021-02-07 04:18:39 -05:00
|
|
|
for (int i = 0; i < SOC_RMT_TX_CANDIDATES_PER_GROUP; i++) {
|
2020-12-16 04:03:48 -05:00
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(i));
|
|
|
|
}
|
2020-02-24 08:27:00 -05:00
|
|
|
|
2020-12-16 04:03:48 -05:00
|
|
|
rmt_config_t rx_cfg = RMT_DEFAULT_CONFIG_RX(RMT_DATA_IO, RMT_RX_CHANNEL_ENCODING_START);
|
2021-02-07 04:18:39 -05:00
|
|
|
for (int i = RMT_RX_CHANNEL_ENCODING_START; i < SOC_RMT_CHANNELS_PER_GROUP; i++) {
|
2020-12-16 04:03:48 -05:00
|
|
|
rx_cfg.channel = i;
|
|
|
|
TEST_ESP_OK(rmt_config(&rx_cfg));
|
|
|
|
TEST_ESP_OK(rmt_driver_install(rx_cfg.channel, 0, 0));
|
|
|
|
}
|
2020-02-24 08:27:00 -05:00
|
|
|
|
2021-02-07 04:18:39 -05:00
|
|
|
for (int i = RMT_RX_CHANNEL_ENCODING_START; i < SOC_RMT_CHANNELS_PER_GROUP; i++) {
|
2020-12-16 04:03:48 -05:00
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(i));
|
|
|
|
}
|
2018-09-05 23:59:06 -04:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:03:48 -05:00
|
|
|
TEST_CASE("RMT install/uninstall test", "[rmt]")
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
2020-10-09 04:41:41 -04:00
|
|
|
rmt_config_t tx_cfg = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, RMT_TX_CHANNEL_ENCODING_END);
|
2022-11-14 05:46:58 -05:00
|
|
|
// uninstall function is allowed to be called at any time
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(tx_cfg.channel));
|
2020-10-09 04:41:41 -04:00
|
|
|
TEST_ESP_OK(rmt_config(&tx_cfg));
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
TEST_ESP_OK(rmt_driver_install(tx_cfg.channel, 1000, 0));
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(tx_cfg.channel));
|
|
|
|
}
|
2022-11-14 05:46:58 -05:00
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(tx_cfg.channel));
|
2020-10-09 04:41:41 -04:00
|
|
|
rmt_config_t rx_cfg = RMT_DEFAULT_CONFIG_RX(RMT_DATA_IO, RMT_RX_CHANNEL_ENCODING_START);
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ESP_OK(rmt_config(&rx_cfg));
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
TEST_ESP_OK(rmt_driver_install(rx_cfg.channel, 1000, 0));
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(rx_cfg.channel));
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 03:58:28 -05:00
|
|
|
static void test_rmt_translator(const void *src, rmt_item32_t *dest, size_t src_size,
|
|
|
|
size_t wanted_num, size_t *translated_size, size_t *item_num)
|
|
|
|
{
|
|
|
|
const rmt_item32_t bit0 = {{{ 10, 1, 20, 0 }}}; //Logical 0
|
|
|
|
const rmt_item32_t bit1 = {{{ 20, 1, 10, 0 }}}; //Logical 1
|
|
|
|
size_t size = 0;
|
|
|
|
size_t num = 0;
|
|
|
|
uint8_t *psrc = (uint8_t *)src;
|
|
|
|
rmt_item32_t *pdest = dest;
|
|
|
|
while (size < src_size && num < wanted_num) {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
// MSB first
|
|
|
|
if (*psrc & (1 << (7 - i))) {
|
|
|
|
pdest->val = bit1.val;
|
|
|
|
} else {
|
|
|
|
pdest->val = bit0.val;
|
|
|
|
}
|
|
|
|
num++;
|
|
|
|
pdest++;
|
|
|
|
}
|
|
|
|
size++;
|
|
|
|
psrc++;
|
|
|
|
}
|
|
|
|
*translated_size = size;
|
|
|
|
*item_num = num;
|
|
|
|
int *user_data = NULL;
|
|
|
|
rmt_translator_get_context(item_num, (void **)&user_data);
|
|
|
|
esp_rom_printf("user data=%d\r\n", *user_data);
|
|
|
|
*user_data = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("RMT translator with user context", "[rmt]")
|
|
|
|
{
|
|
|
|
rmt_config_t tx_cfg = RMT_DEFAULT_CONFIG_TX(RMT_DATA_IO, 0);
|
|
|
|
TEST_ESP_OK(rmt_config(&tx_cfg));
|
|
|
|
TEST_ESP_OK(rmt_driver_install(tx_cfg.channel, 0, 0));
|
|
|
|
rmt_translator_init(tx_cfg.channel, test_rmt_translator);
|
|
|
|
int user_data = 999;
|
|
|
|
rmt_translator_set_context(tx_cfg.channel, &user_data);
|
|
|
|
uint8_t test_buf[] = {1, 2, 3, 4, 5, 6};
|
|
|
|
rmt_write_sample(tx_cfg.channel, test_buf, sizeof(test_buf), true);
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
|
|
TEST_ASSERT_EQUAL(100, user_data);
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(tx_cfg.channel));
|
|
|
|
}
|
|
|
|
|
2020-03-17 07:47:38 -04:00
|
|
|
static void do_nec_tx_rx(uint32_t flags)
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
|
|
|
RingbufHandle_t rb = NULL;
|
2020-02-24 08:27:00 -05:00
|
|
|
rmt_item32_t *items = NULL;
|
2020-11-26 03:56:13 -05:00
|
|
|
size_t length = 0;
|
2020-02-24 08:27:00 -05:00
|
|
|
uint32_t addr = 0x10;
|
|
|
|
uint32_t cmd = 0x20;
|
|
|
|
bool repeat = false;
|
|
|
|
int tx_channel = 0;
|
2020-10-09 04:41:41 -04:00
|
|
|
int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
|
2020-02-24 08:27:00 -05:00
|
|
|
|
|
|
|
// test on different flags combinations
|
2020-03-17 07:47:38 -04:00
|
|
|
rmt_setup_testbench(tx_channel, rx_channel, flags);
|
|
|
|
|
|
|
|
// get ready to receive
|
|
|
|
TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
|
|
|
|
TEST_ASSERT_NOT_NULL(rb);
|
|
|
|
TEST_ESP_OK(rmt_rx_start(rx_channel, true));
|
|
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
|
|
|
|
// build NEC codes
|
|
|
|
cmd = 0x20;
|
|
|
|
while (cmd <= 0x30) {
|
|
|
|
ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
|
|
|
|
// Send new key code
|
|
|
|
TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
|
|
|
|
TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &items, &length));
|
|
|
|
if (cmd & 0x01) {
|
|
|
|
TEST_ESP_OK(rmt_write_items(tx_channel, items, length, false)); // no wait
|
|
|
|
TEST_ESP_OK(rmt_wait_tx_done(tx_channel, portMAX_DELAY));
|
|
|
|
} else {
|
|
|
|
TEST_ESP_OK(rmt_write_items(tx_channel, items, length, true)); // wait until done
|
2020-02-24 08:27:00 -05:00
|
|
|
}
|
2020-03-17 07:47:38 -04:00
|
|
|
cmd++;
|
|
|
|
}
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-03-17 07:47:38 -04:00
|
|
|
// parse NEC codes
|
|
|
|
while (rb) {
|
|
|
|
items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
|
|
|
|
if (items) {
|
|
|
|
length /= 4; // one RMT = 4 Bytes
|
|
|
|
if (s_ir_parser->input(s_ir_parser, items, length) == ESP_OK) {
|
|
|
|
if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
|
2020-02-24 08:27:00 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-17 07:47:38 -04:00
|
|
|
vRingbufferReturnItem(rb, (void *) items);
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "done");
|
|
|
|
break;
|
2020-02-24 08:27:00 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-17 07:47:38 -04:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL(0x30, cmd);
|
|
|
|
rmt_clean_testbench(tx_channel, rx_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// basic nec tx and rx test, using APB source clock, no modulation
|
|
|
|
TEST_CASE("RMT NEC TX and RX (APB)", "[rmt]")
|
|
|
|
{
|
|
|
|
do_nec_tx_rx(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with RMT_TESTBENCH_FLAGS_ALWAYS_ON will take a long time (REF_TICK is much slower than APB CLOCK)
|
2020-10-09 04:41:41 -04:00
|
|
|
TEST_CASE("RMT NEC TX and RX (always on)", "[rmt][timeout=240]")
|
2020-03-17 07:47:38 -04:00
|
|
|
{
|
|
|
|
do_nec_tx_rx(RMT_TESTBENCH_FLAGS_ALWAYS_ON);
|
|
|
|
}
|
|
|
|
|
2020-03-25 05:13:10 -04:00
|
|
|
#if SOC_RMT_SUPPORT_RX_DEMODULATION
|
2020-03-17 07:47:38 -04:00
|
|
|
// basic nec tx and rx test, using APB source clock, with modulation and demodulation on
|
|
|
|
TEST_CASE("RMT NEC TX and RX (Modulation/Demodulation)", "[rmt]")
|
|
|
|
{
|
|
|
|
do_nec_tx_rx(RMT_TESTBENCH_FLAGS_CARRIER_ON);
|
2020-02-14 01:33:07 -05:00
|
|
|
}
|
2020-03-17 07:47:38 -04:00
|
|
|
#endif
|
2020-02-14 01:33:07 -05:00
|
|
|
|
2021-02-07 04:18:39 -05:00
|
|
|
TEST_CASE("RMT TX (SOC_RMT_MEM_WORDS_PER_CHANNEL-1) symbols", "[rmt][boundary]")
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
2020-02-24 08:27:00 -05:00
|
|
|
int tx_channel = 0;
|
|
|
|
rmt_setup_testbench(tx_channel, -1, 0);
|
2021-02-07 04:18:39 -05:00
|
|
|
rmt_item32_t *items = malloc(sizeof(rmt_item32_t) * (SOC_RMT_MEM_WORDS_PER_CHANNEL - 1));
|
|
|
|
for (int i = 0; i < SOC_RMT_MEM_WORDS_PER_CHANNEL - 1; i++) {
|
2020-02-24 08:27:00 -05:00
|
|
|
items[i] = (rmt_item32_t) {
|
|
|
|
{{
|
|
|
|
200, 1, 200, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-02-07 04:18:39 -05:00
|
|
|
TEST_ESP_OK(rmt_write_items(tx_channel, items, SOC_RMT_MEM_WORDS_PER_CHANNEL - 1, 1));
|
2019-11-19 03:10:02 -05:00
|
|
|
free(items);
|
2020-02-24 08:27:00 -05:00
|
|
|
rmt_clean_testbench(tx_channel, -1);
|
2018-01-02 07:17:49 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_CASE("RMT TX stop", "[rmt]")
|
2018-01-02 07:17:49 -05:00
|
|
|
{
|
|
|
|
RingbufHandle_t rb = NULL;
|
2020-02-24 08:27:00 -05:00
|
|
|
rmt_item32_t *frames = NULL;
|
2020-11-26 03:56:13 -05:00
|
|
|
size_t length = 0;
|
2020-02-24 08:27:00 -05:00
|
|
|
uint32_t count = 10;
|
|
|
|
uint32_t addr = 0x10;
|
|
|
|
uint32_t cmd = 0x20;
|
|
|
|
bool repeat = false;
|
|
|
|
int tx_channel = 0;
|
2020-10-09 04:41:41 -04:00
|
|
|
int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
|
2020-02-24 08:27:00 -05:00
|
|
|
|
|
|
|
rmt_setup_testbench(tx_channel, rx_channel, 0);
|
|
|
|
|
|
|
|
// re-install ir_builder, to enlarge internal buffer size
|
|
|
|
TEST_ESP_OK(s_ir_builder->del(s_ir_builder));
|
|
|
|
ir_builder_config_t ir_builder_config = IR_BUILDER_DEFAULT_CONFIG((ir_dev_t)tx_channel);
|
|
|
|
ir_builder_config.buffer_size *= count;
|
|
|
|
ir_builder_config.flags = IR_TOOLS_FLAGS_PROTO_EXT;
|
|
|
|
s_ir_builder = ir_builder_rmt_new_nec(&ir_builder_config);
|
|
|
|
TEST_ASSERT_NOT_NULL(s_ir_builder);
|
|
|
|
|
|
|
|
// get ready to receive
|
|
|
|
TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
|
|
|
|
TEST_ASSERT_NOT_NULL(rb);
|
|
|
|
TEST_ESP_OK(rmt_rx_start(rx_channel, true));
|
|
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
|
|
|
|
// build NEC codes
|
|
|
|
ESP_LOGI(TAG, "Plan to send command 0x%x~0x%x to address 0x%x", cmd, cmd + count, addr);
|
|
|
|
for (int i = 0; i <= count; i++) {
|
|
|
|
TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
|
|
|
|
cmd++;
|
|
|
|
}
|
|
|
|
TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &frames, &length));
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
// send for 1 second and then stop
|
|
|
|
TEST_ESP_OK(rmt_write_items(tx_channel, frames, length, true));
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
|
|
TEST_ESP_OK(rmt_tx_stop(tx_channel));
|
2018-01-02 07:17:49 -05:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
// parse NEC codes
|
|
|
|
uint32_t num = 0;
|
|
|
|
while (rb) {
|
|
|
|
frames = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
|
|
|
|
if (frames) {
|
|
|
|
length /= 4; // one RMT = 4 Bytes
|
|
|
|
if (s_ir_parser->input(s_ir_parser, frames, length) == ESP_OK) {
|
|
|
|
if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vRingbufferReturnItem(rb, (void *) frames);
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "done");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 23:59:06 -04:00
|
|
|
|
2020-02-24 08:27:00 -05:00
|
|
|
TEST_ASSERT(num < count);
|
|
|
|
rmt_clean_testbench(tx_channel, rx_channel);
|
2018-09-06 00:00:36 -04:00
|
|
|
}
|
2020-03-17 07:58:05 -04:00
|
|
|
|
2020-03-25 05:13:10 -04:00
|
|
|
#if SOC_RMT_SUPPORT_RX_PINGPONG
|
2020-03-17 07:58:05 -04:00
|
|
|
TEST_CASE("RMT Ping-Pong operation", "[rmt]")
|
|
|
|
{
|
|
|
|
int tx_channel = 0;
|
2020-10-09 04:41:41 -04:00
|
|
|
int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
|
2021-02-07 04:18:39 -05:00
|
|
|
rmt_item32_t frames[SOC_RMT_MEM_WORDS_PER_CHANNEL * 2]; // send two block data using ping-pong
|
2020-03-17 07:58:05 -04:00
|
|
|
RingbufHandle_t rb = NULL;
|
|
|
|
uint32_t size = sizeof(frames) / sizeof(frames[0]);
|
|
|
|
|
|
|
|
// The design of the following test frame should trigger three rx threshold interrupt and one rx end interrupt
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < size - 1; i++) {
|
|
|
|
frames[i].level0 = 1;
|
|
|
|
frames[i].duration0 = 100;
|
|
|
|
frames[i].level1 = 0;
|
|
|
|
frames[i].duration1 = 100;
|
|
|
|
}
|
|
|
|
frames[i].level0 = 1;
|
|
|
|
frames[i].duration0 = 0;
|
|
|
|
frames[i].level1 = 0;
|
|
|
|
frames[i].duration1 = 0;
|
|
|
|
|
|
|
|
rmt_setup_testbench(tx_channel, rx_channel, 0);
|
|
|
|
|
|
|
|
// get ready to receive
|
|
|
|
TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
|
|
|
|
TEST_ASSERT_NOT_NULL(rb);
|
|
|
|
TEST_ESP_OK(rmt_rx_start(rx_channel, true));
|
|
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
|
|
|
|
for (uint32_t test_count = 0; test_count < 5; test_count++) {
|
|
|
|
TEST_ESP_OK(rmt_write_items(tx_channel, frames, size, true));
|
|
|
|
|
|
|
|
// parse received data
|
2020-11-26 03:56:13 -05:00
|
|
|
size_t length = 0;
|
2020-03-17 07:58:05 -04:00
|
|
|
rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
|
|
|
|
if (items) {
|
|
|
|
vRingbufferReturnItem(rb, (void *) items);
|
|
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL(4 * (size - 1), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
rmt_clean_testbench(tx_channel, rx_channel);
|
|
|
|
}
|
|
|
|
#endif
|
2021-02-07 04:18:39 -05:00
|
|
|
#if SOC_RMT_SUPPORT_TX_SYNCHRO
|
2020-03-19 03:26:49 -04:00
|
|
|
static uint32_t tx_end_time0, tx_end_time1;
|
|
|
|
static void rmt_tx_end_cb(rmt_channel_t channel, void *arg)
|
|
|
|
{
|
|
|
|
if (channel == 0) {
|
2021-01-11 08:03:45 -05:00
|
|
|
tx_end_time0 = cpu_hal_get_cycle_count();
|
2020-03-19 03:26:49 -04:00
|
|
|
} else {
|
2021-01-11 08:03:45 -05:00
|
|
|
tx_end_time1 = cpu_hal_get_cycle_count();
|
2020-03-19 03:26:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_CASE("RMT TX simultaneously", "[rmt]")
|
|
|
|
{
|
2021-02-07 04:18:39 -05:00
|
|
|
rmt_item32_t frames[SOC_RMT_MEM_WORDS_PER_CHANNEL];
|
2020-03-19 03:26:49 -04:00
|
|
|
uint32_t size = sizeof(frames) / sizeof(frames[0]);
|
|
|
|
int channel0 = 0;
|
|
|
|
int channel1 = 1;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < size - 1; i++) {
|
|
|
|
frames[i].level0 = 1;
|
|
|
|
frames[i].duration0 = 1000;
|
|
|
|
frames[i].level1 = 0;
|
|
|
|
frames[i].duration1 = 1000;
|
|
|
|
}
|
|
|
|
frames[i].level0 = 0;
|
|
|
|
frames[i].duration0 = 0;
|
|
|
|
frames[i].level1 = 0;
|
|
|
|
frames[i].duration1 = 0;
|
|
|
|
|
2020-12-16 04:03:48 -05:00
|
|
|
rmt_config_t tx_config0 = RMT_DEFAULT_CONFIG_TX(4, channel0);
|
|
|
|
rmt_config_t tx_config1 = RMT_DEFAULT_CONFIG_TX(5, channel1);
|
2020-03-19 03:26:49 -04:00
|
|
|
TEST_ESP_OK(rmt_config(&tx_config0));
|
|
|
|
TEST_ESP_OK(rmt_config(&tx_config1));
|
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_driver_install(channel0, 0, 0));
|
|
|
|
TEST_ESP_OK(rmt_driver_install(channel1, 0, 0));
|
|
|
|
|
|
|
|
rmt_register_tx_end_callback(rmt_tx_end_cb, NULL);
|
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_add_channel_to_group(channel0));
|
|
|
|
TEST_ESP_OK(rmt_add_channel_to_group(channel1));
|
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_write_items(channel0, frames, size, false));
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
TEST_ESP_OK(rmt_write_items(channel1, frames, size, false));
|
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_wait_tx_done(channel0, portMAX_DELAY));
|
|
|
|
TEST_ESP_OK(rmt_wait_tx_done(channel1, portMAX_DELAY));
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "tx_end_time0=%u, tx_end_time1=%u", tx_end_time0, tx_end_time1);
|
|
|
|
TEST_ASSERT_LESS_OR_EQUAL_UINT32(2000, tx_end_time1 - tx_end_time0);
|
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_remove_channel_from_group(channel0));
|
|
|
|
TEST_ESP_OK(rmt_remove_channel_from_group(channel1));
|
|
|
|
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(channel0));
|
|
|
|
TEST_ESP_OK(rmt_driver_uninstall(channel1));
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-25 05:13:10 -04:00
|
|
|
#if SOC_RMT_SUPPORT_TX_LOOP_COUNT
|
2020-03-18 06:13:27 -04:00
|
|
|
static void rmt_tx_loop_end(rmt_channel_t channel, void *arg)
|
|
|
|
{
|
|
|
|
rmt_tx_stop(channel);
|
|
|
|
}
|
2020-03-19 03:26:49 -04:00
|
|
|
TEST_CASE("RMT TX loop", "[rmt]")
|
|
|
|
{
|
|
|
|
RingbufHandle_t rb = NULL;
|
|
|
|
rmt_item32_t *items = NULL;
|
2020-11-26 03:56:13 -05:00
|
|
|
size_t length = 0;
|
2020-03-19 03:26:49 -04:00
|
|
|
uint32_t addr = 0x10;
|
|
|
|
uint32_t cmd = 0x20;
|
|
|
|
bool repeat = false;
|
|
|
|
int tx_channel = 0;
|
2020-10-09 04:41:41 -04:00
|
|
|
int rx_channel = RMT_RX_CHANNEL_ENCODING_START + 1;
|
2020-03-19 03:26:49 -04:00
|
|
|
uint32_t count = 0;
|
|
|
|
|
|
|
|
rmt_setup_testbench(tx_channel, rx_channel, RMT_TESTBENCH_FLAGS_LOOP_ON);
|
|
|
|
|
|
|
|
// get ready to receive
|
|
|
|
TEST_ESP_OK(rmt_get_ringbuf_handle(rx_channel, &rb));
|
|
|
|
TEST_ASSERT_NOT_NULL(rb);
|
|
|
|
TEST_ESP_OK(rmt_rx_start(rx_channel, true));
|
|
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
|
2020-03-18 06:13:27 -04:00
|
|
|
// register callback functions, invoked when tx loop count to ceiling
|
|
|
|
rmt_register_tx_end_callback(rmt_tx_loop_end, NULL);
|
2020-03-19 03:26:49 -04:00
|
|
|
// build NEC codes
|
|
|
|
ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
|
|
|
|
// Send new key code
|
|
|
|
TEST_ESP_OK(s_ir_builder->build_frame(s_ir_builder, addr, cmd));
|
|
|
|
TEST_ESP_OK(s_ir_builder->get_result(s_ir_builder, &items, &length));
|
|
|
|
TEST_ESP_OK(rmt_write_items(tx_channel, items, length, true)); // wait until done
|
|
|
|
|
|
|
|
// parse NEC codes
|
|
|
|
while (rb) {
|
|
|
|
items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
|
|
|
|
if (items) {
|
|
|
|
length /= 4; // one RMT = 4 Bytes
|
|
|
|
if (s_ir_parser->input(s_ir_parser, items, length) == ESP_OK) {
|
|
|
|
if (s_ir_parser->get_scan_code(s_ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
|
|
|
|
count++;
|
|
|
|
ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vRingbufferReturnItem(rb, (void *) items);
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "done");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL(10, count);
|
|
|
|
rmt_clean_testbench(tx_channel, rx_channel);
|
|
|
|
}
|
|
|
|
#endif
|