From 79802736094b5298f7850374964ef405bce7cc51 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 3 Oct 2021 18:08:07 -0500 Subject: [PATCH] Completed update of RFControl for ESP32-S2 and -C3 compatability Testing completed for RFControl and PWM on all three ESP32 chip types. To Do: update RFControl documentation to include total number of usable channels per chip, as well as the alternate version of start(); --- src/extras/RFControl.cpp | 52 +++++++++++++++++++------- src/extras/RFControl.h | 2 +- src/extras/extras.ino | 80 +++++++++------------------------------- 3 files changed, 56 insertions(+), 78 deletions(-) diff --git a/src/extras/RFControl.cpp b/src/extras/RFControl.cpp index 81d3c34..c71c4ce 100644 --- a/src/extras/RFControl.cpp +++ b/src/extras/RFControl.cpp @@ -1,7 +1,6 @@ #include #include -#include #include "RFControl.h" @@ -9,18 +8,40 @@ RFControl::RFControl(uint8_t pin){ -// config=new rmt_config_t; - - config.rmt_mode=RMT_MODE_TX; - config.tx_config.carrier_en=false; - config.channel=RMT_CHANNEL_0; - config.clk_div = 1; - config.mem_block_num=1; - config.gpio_num=(gpio_num_t)pin; +#ifdef CONFIG_IDF_TARGET_ESP32C3 + if(nChannels==RMT_CHANNEL_MAX/2){ +#else + if(nChannels==RMT_CHANNEL_MAX){ +#endif + Serial.printf("\n*** ERROR: Can't create RFControl(%d) - no open channels ***\n\n",pin); + return; + } + + config=new rmt_config_t; + + config->rmt_mode=RMT_MODE_TX; + config->tx_config.carrier_en=false; + config->channel=(rmt_channel_t)nChannels; + config->flags=0; + config->clk_div = 1; + config->mem_block_num=1; + config->gpio_num=(gpio_num_t)pin; + config->tx_config.idle_output_en=false; + config->tx_config.loop_en=false; + + rmt_config(config); + rmt_driver_install(config->channel,0,0); + + // Below we set the base clock to 1 MHz so tick-units are in microseconds (before CLK_DIV) + +#ifdef CONFIG_IDF_TARGET_ESP32C3 + REG_SET_FIELD(RMT_SYS_CONF_REG,RMT_SCLK_DIV_NUM,80); // ESP32-C3 does not have a 1 MHz REF Tick Clock, but allows the 80 MHz APB clock to be scaled by an additional RMT-specific divider +#else + rmt_set_source_clk(config->channel,RMT_BASECLK_REF); // use 1 MHz REF Tick Clock for ESP32 and ESP32-S2 +#endif + + nChannels++; - ESP_ERROR_CHECK(rmt_config(&config)); - ESP_ERROR_CHECK(rmt_driver_install(config.channel,0,0)); - rmt_set_source_clk(RMT_CHANNEL_0,RMT_BASECLK_REF); } /////////////////// @@ -33,10 +54,13 @@ void RFControl::start(uint8_t nCycles, uint8_t tickTime){ // starts transmis void RFControl::start(uint32_t *data, int nData, uint8_t nCycles, uint8_t tickTime){ // starts transmission of pulses from specified data pointer, repeated for nCycles, where each tick in pulse is tickTime microseconds long - rmt_set_clk_div(RMT_CHANNEL_0,tickTime); // set clock divider + if(!config || nData==0) + return; + + rmt_set_clk_div(config->channel,tickTime); // set clock divider for(int i=0;ichannel, (rmt_item32_t *) data, nData, true); // start transmission and wait until completed before returning } /////////////////// diff --git a/src/extras/RFControl.h b/src/extras/RFControl.h index 7bc2430..b840f37 100644 --- a/src/extras/RFControl.h +++ b/src/extras/RFControl.h @@ -10,7 +10,7 @@ using std::vector; class RFControl { private: - rmt_config_t config; + rmt_config_t *config=NULL; vector data; boolean lowWord=true; static uint8_t nChannels; diff --git a/src/extras/extras.ino b/src/extras/extras.ino index 6730da6..86e56d3 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -8,73 +8,27 @@ void setup() { Serial.flush(); delay(1000); // wait for interface to flush - Serial.print("\n\nHomeSpan RF Transmitter Example\n\n"); + Serial.println("\n\nHomeSpan RF Transmitter Example"); - RFControl rf(17); // create an instance of RFControl with signal output to pin 17 of the ESP32 + RFControl rf(19); // create an instance of RFControl with signal output to pin 6 -// rf.phase(1000,HIGH); -// rf.phase(9000,LOW); -// rf.phase(1000,HIGH); -// rf.phase(9000,LOW); -// rf.phase(1000,HIGH); -// rf.phase(30000,LOW); - -// rf.add(1000,9000); // create a pulse train with three 5000-tick high/low pulses -// rf.add(1000,9000); // create a pulse train with three 5000-tick high/low pulses -// rf.add(1000,9000); // create a pulse train with three 5000-tick high/low pulses -// rf.add(1000,30000); // create a pulse train with three 5000-tick high/low pulses -// -// rf.start(2,100); -// Serial.println("Done"); -// while(1); - -// -//#define NPOINTS 3 -// -// uint32_t data[NPOINTS]; -// -// for(int i=0;i