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();
This commit is contained in:
Gregg 2021-10-03 18:08:07 -05:00
parent dc5844b520
commit 7980273609
3 changed files with 56 additions and 78 deletions

View File

@ -1,7 +1,6 @@
#include <Arduino.h> #include <Arduino.h>
#include <soc/rmt_reg.h> #include <soc/rmt_reg.h>
#include <soc/dport_reg.h>
#include "RFControl.h" #include "RFControl.h"
@ -9,18 +8,40 @@
RFControl::RFControl(uint8_t pin){ RFControl::RFControl(uint8_t pin){
// config=new rmt_config_t; #ifdef CONFIG_IDF_TARGET_ESP32C3
if(nChannels==RMT_CHANNEL_MAX/2){
config.rmt_mode=RMT_MODE_TX; #else
config.tx_config.carrier_en=false; if(nChannels==RMT_CHANNEL_MAX){
config.channel=RMT_CHANNEL_0; #endif
config.clk_div = 1; Serial.printf("\n*** ERROR: Can't create RFControl(%d) - no open channels ***\n\n",pin);
config.mem_block_num=1; return;
config.gpio_num=(gpio_num_t)pin; }
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 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;i<nCycles;i++) // loop over nCycles for(int i=0;i<nCycles;i++) // loop over nCycles
rmt_write_items(RMT_CHANNEL_0, (rmt_item32_t *) data, nData, true); // start transmission and wait until completed before returning rmt_write_items(config->channel, (rmt_item32_t *) data, nData, true); // start transmission and wait until completed before returning
} }
/////////////////// ///////////////////

View File

@ -10,7 +10,7 @@ using std::vector;
class RFControl { class RFControl {
private: private:
rmt_config_t config; rmt_config_t *config=NULL;
vector<uint32_t> data; vector<uint32_t> data;
boolean lowWord=true; boolean lowWord=true;
static uint8_t nChannels; static uint8_t nChannels;

View File

@ -8,73 +8,27 @@ void setup() {
Serial.flush(); Serial.flush();
delay(1000); // wait for interface to 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); for(int i=0;i<8;i++)
// rf.phase(9000,LOW); new RFControl(18);
// rf.phase(1000,HIGH);
// rf.phase(9000,LOW); #define NPOINTS 3
// 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<NPOINTS;i++){
// if(i<NPOINTS-1)
// data[i]=RF_PULSE(1000,9000);
// else
// data[i]=RF_PULSE(1000,30000);
//
// Serial.println((uint32_t)data[i],HEX);
// }
//
// rf.start(data,NPOINTS,1,100);
//
// Serial.println("Done.");
// while(1);
uint32_t data[NPOINTS];
rf.clear(); // clear the pulse train memory buffer for(int i=0;i<NPOINTS;i++){
if(i<NPOINTS-1)
rf.add(5000,5000); // create a pulse train with three 5000-tick high/low pulses data[i]=RF_PULSE(1000,9000);
rf.add(5000,5000); else
rf.add(5000,10000); // double duration of final low period data[i]=RF_PULSE(1000,30000);
}
Serial.print("Starting 4 cycles of three 500 ms on pulses...");
rf.start(data,NPOINTS,2,100);
rf.start(4,100); // start transmission of 4 cycles of the pulse train with 1 tick=100 microseconds
Serial.println("End Example");
Serial.print("Done!\n");
delay(2000);
rf.clear();
for(int i=1000;i<10000;i+=1000)
rf.add(i,10000-i);
rf.add(10000,10000);
Serial.print("Starting 3 cycles of 100-1000 ms pulses...");
rf.start(3,100); // start transmission of 3 cycles of the pulse train with 1 tick=100 microseconds
Serial.print("Done!\n");
Serial.print("\nEnd Example");
} // end of setup() } // end of setup()