#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
// ESP32 RMT 1KHz three phase square wave sync test
// on ESP32 the ESP32S3 code gives message
// ESP32S3 RMT !KHz three phase sync test
// setting up channel 0
// E (324) rmt: rmt_new_tx_channel(247): mem_block_symbols must be even and at least 64
// ESP_ERROR_CHECK failed: esp_err_t 0x102 (ESP_ERR_INVALID_ARG) at 0x400d631e
#include "driver/rmt_tx.h"
void app_main(void)
{
// while (true) {
// printf("Hello from app_main!\n");
// sleep(1);
// }
printf("ESP32 RMT !KHz three phase sync test\n");
//delay(2000);
// setup Tx channels
rmt_channel_handle_t tx_channels[3] = { NULL };
gpio_num_t tx_gpio_number[3] = { GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_18 }; // pin numbers
for (int i = 0; i < 3; i++) {
printf("setting up channel %d\n", i);
rmt_tx_channel_config_t tx_chan_config = {
.gpio_num = tx_gpio_number[i],
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = 1 * 1000 * 1000, // 1MHz clock
//#if 0
.mem_block_symbols = 64, // for ESP32
//#endif
// .mem_block_symbols = 48,
.trans_queue_depth = 1,
};
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &tx_channels[i]));
}
rmt_transmit_config_t transmitConfig = {
.loop_count = -1
};
rmt_encoder_handle_t copyEncoder =NULL;
rmt_copy_encoder_config_t copyEncoderConfig = {};
assert(rmt_new_copy_encoder(©EncoderConfig, ©Encoder) == ESP_OK && "Failed to Create Copy Encoder");
ESP_ERROR_CHECK(rmt_enable(tx_channels[2]));
ESP_ERROR_CHECK(rmt_enable(tx_channels[1]));
ESP_ERROR_CHECK(rmt_enable(tx_channels[0]));
// setup sync manage for the three pulses
rmt_sync_manager_handle_t synchro = NULL;
rmt_sync_manager_config_t synchro_config = {
.tx_channel_array = tx_channels,
.array_size = sizeof(tx_channels) / sizeof(tx_channels[0]),
};
ESP_ERROR_CHECK(rmt_new_sync_manager(&synchro_config, &synchro));
printf("Starting Transmitters\n");
// setup pulse patterns - clock is 1MHz
// two 200KHz pulses 40% duty cycle
const rmt_symbol_word_t pulsePattern1[] = {
{ .duration0 = 500, .level0 = 1, .duration1 = 500, .level1 = 0 },
};
const rmt_symbol_word_t pulsePattern2[] = {
{ .duration0 = 333, .level0 = 0, .duration1 = 500, .level1 = 1 },
{ .duration0 = 100, .level0 = 0, .duration1 = 67, .level1 = 0 },
};
const rmt_symbol_word_t pulsePattern3[] = {
{ .duration0 = 133, .level0 = 1, .duration1 = 500, .level1 = 0 },
{ .duration0 = 100, .level0 = 1, .duration1 = 267, .level1 = 1 },
};
assert(rmt_transmit(tx_channels[0], copyEncoder, &pulsePattern1, sizeof(pulsePattern1), &transmitConfig) == ESP_OK && "Failed to begin transmitting");
assert(rmt_transmit(tx_channels[1], copyEncoder, &pulsePattern2, sizeof(pulsePattern2), &transmitConfig) == ESP_OK && "Failed to begin transmitting");
assert(rmt_transmit(tx_channels[2], copyEncoder, &pulsePattern3, sizeof(pulsePattern3), &transmitConfig) == ESP_OK && "Failed to begin transmitting");
printf("Transmitters Running\n");
}