Skip to content

Commit 2647cbb

Browse files
refactor(rmt): refactored RMT loopback example (espressif#11221)
* feat(rmt): refactored RMT loopback example * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 8e8b1cb commit 2647cbb

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

Diff for: libraries/ESP32/examples/RMT/RMTLoopback/RMTLoopback.ino

+31-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
1+
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -35,40 +35,53 @@
3535
rmt_data_t my_data[256];
3636
rmt_data_t data[256];
3737

38-
static EventGroupHandle_t events;
39-
4038
#define RMT_FREQ 10000000 // tick time is 100ns
41-
#define RMT_NUM_EXCHANGED_DATA 30
39+
#define RMT_NUM_EXCHANGED_DATA 32
4240

4341
void setup() {
4442
Serial.begin(115200);
45-
events = xEventGroupCreate();
4643

4744
if (!rmtInit(RMT_TX_PIN, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, RMT_FREQ)) {
4845
Serial.println("init sender failed\n");
4946
}
5047
if (!rmtInit(RMT_RX_PIN, RMT_RX_MODE, RMT_MEM_RX, RMT_FREQ)) {
5148
Serial.println("init receiver failed\n");
5249
}
50+
Serial.println();
51+
Serial.println("RMT tick set to: 100ns");
5352

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

59-
Serial.println("real tick set to: 100ns");
60-
Serial.printf("\nPlease connect GPIO %d to GPIO %d, now.\n", RMT_TX_PIN, RMT_RX_PIN);
61-
}
62-
63-
void loop() {
64-
// Init data
65-
int i;
66-
for (i = 0; i < 255; i++) {
67-
data[i].val = 0x80010001 + ((i % 13) << 16) + 13 - (i % 13);
58+
// create multiple pulses with different width to be sent
59+
for (int i = 0; i < 255; i++) {
60+
data[i].level0 = 1; // HIGH
61+
data[i].duration0 = 1 + 13 - (i % 13); // number of Tick on High
62+
data[i].level1 = 0; // LOW
63+
data[i].duration1 = 1 + (i % 13); // number of Ticks on Low
6864
my_data[i].val = 0;
6965
}
7066
data[255].val = 0;
67+
Serial.println();
68+
Serial.println("====================================================================================================");
69+
Serial.println("Preloaded Data that will sent (time in 0.1us):");
70+
// Printout the received data plus the original values
71+
for (int i = 0; i < RMT_NUM_EXCHANGED_DATA; i++) {
72+
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);
7173

74+
if (!((i + 1) % 4)) {
75+
Serial.println();
76+
}
77+
}
78+
Serial.println("====================================================================================================");
79+
Serial.printf("Please connect GPIO %d to GPIO %d, now.", RMT_TX_PIN, RMT_RX_PIN);
80+
Serial.println();
81+
Serial.println();
82+
}
83+
84+
void loop() {
7285
// Start an async data read
7386
size_t rx_num_symbols = RMT_NUM_EXCHANGED_DATA;
7487
rmtReadAsync(RMT_RX_PIN, my_data, &rx_num_symbols);
@@ -84,13 +97,13 @@ void loop() {
8497
Serial.printf("Got %d RMT symbols\n", rx_num_symbols);
8598

8699
// Printout the received data plus the original values
87-
for (i = 0; i < 60; i++) {
100+
for (int i = 0; i < RMT_NUM_EXCHANGED_DATA; i++) {
88101
Serial.printf("%08lx=%08lx ", my_data[i].val, data[i].val);
89102
if (!((i + 1) % 4)) {
90-
Serial.println("");
103+
Serial.println();
91104
}
92105
}
93-
Serial.println("\n");
106+
Serial.println();
94107

95-
delay(500);
108+
delay(2000);
96109
}

0 commit comments

Comments
 (0)