Skip to content

refactor(rmt): refactored RMT loopback example #11221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions libraries/ESP32/examples/RMT/RMTLoopback/RMTLoopback.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,40 +35,55 @@
rmt_data_t my_data[256];
rmt_data_t data[256];

static EventGroupHandle_t events;

#define RMT_FREQ 10000000 // tick time is 100ns
#define RMT_NUM_EXCHANGED_DATA 30
#define RMT_NUM_EXCHANGED_DATA 32

void setup() {
Serial.begin(115200);
events = xEventGroupCreate();

if (!rmtInit(RMT_TX_PIN, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, RMT_FREQ)) {
Serial.println("init sender failed\n");
}
if (!rmtInit(RMT_RX_PIN, RMT_RX_MODE, RMT_MEM_RX, RMT_FREQ)) {
Serial.println("init receiver failed\n");
}
Serial.println();
Serial.println("RMT tick set to: 100ns");

// End of transmission shall be detected when line is idle for 2us = 20*100ns
rmtSetRxMaxThreshold(RMT_RX_PIN, 20);
// Disable Glitch filter
rmtSetRxMinThreshold(RMT_RX_PIN, 0);

Serial.println("real tick set to: 100ns");
Serial.printf("\nPlease connect GPIO %d to GPIO %d, now.\n", RMT_TX_PIN, RMT_RX_PIN);
}

void loop() {
// Init data
int i;
for (i = 0; i < 255; i++) {
data[i].val = 0x80010001 + ((i % 13) << 16) + 13 - (i % 13);
// create multiple pulses with different width to be sent
for (int i = 0; i < 255; i++) {
data[i].level0 = 1; // HIGH
data[i].duration0 = 1 + 13 - (i % 13); // number of Tick on High
data[i].level1 = 0; // LOW
data[i].duration1 = 1 + (i % 13); // number of Ticks on Low
my_data[i].val = 0;
}
data[255].val = 0;
Serial.println();
Serial.println("====================================================================================================");
Serial.println("Preloaded Data that will sent (time in 0.1us):");
// Printout the received data plus the original values
for (int i = 0; i < RMT_NUM_EXCHANGED_DATA; i++) {
Serial.printf("%08lx=[%c 0x%02x|%c 0x%02x] ", data[i].val,
data[i].level0 ? 'H' : 'L', data[i].duration0,
data[i].level1 ? 'H' : 'L', data[i].duration1);

if (!((i + 1) % 4)) {
Serial.println();
}
}
Serial.println("====================================================================================================");
Serial.printf("Please connect GPIO %d to GPIO %d, now.", RMT_TX_PIN, RMT_RX_PIN);
Serial.println();
Serial.println();
}

void loop() {
// Start an async data read
size_t rx_num_symbols = RMT_NUM_EXCHANGED_DATA;
rmtReadAsync(RMT_RX_PIN, my_data, &rx_num_symbols);
Expand All @@ -84,13 +99,13 @@ void loop() {
Serial.printf("Got %d RMT symbols\n", rx_num_symbols);

// Printout the received data plus the original values
for (i = 0; i < 60; i++) {
for (int i = 0; i < RMT_NUM_EXCHANGED_DATA; i++) {
Serial.printf("%08lx=%08lx ", my_data[i].val, data[i].val);
if (!((i + 1) % 4)) {
Serial.println("");
Serial.println();
}
}
Serial.println("\n");
Serial.println();

delay(500);
delay(2000);
}
Loading