Add clock selection to RFControl (80 MHz or 1 MHz)

Added second argument to RFControl(uint8_t pin, boolean refClock=true) to allow choice of Ref Tick (1 MHz) clock or APB (80 MHz) clock.  Default is to use 1 MHz Ref Tick.

Also fixed bug in logic that divides clock for ESP32-C3.  Factor should be 79, not 80, since divider is apparently configured to divide by factor+1.
This commit is contained in:
Gregg 2021-10-15 18:16:18 -05:00
parent 5bced71345
commit 4ad607951f
3 changed files with 8 additions and 19 deletions

View File

@ -6,7 +6,7 @@
///////////////////
RFControl::RFControl(uint8_t pin){
RFControl::RFControl(uint8_t pin, boolean refClock){
#ifdef CONFIG_IDF_TARGET_ESP32C3
if(nChannels==RMT_CHANNEL_MAX/2){
@ -34,10 +34,11 @@ RFControl::RFControl(uint8_t pin){
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)
// If specified, set the base clock to 1 MHz so tick-units are in microseconds (before any CLK_DIV is applied), otherwise default will be 80 MHz APB clock
if(refClock)
#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
REG_SET_FIELD(RMT_SYS_CONF_REG,RMT_SCLK_DIV_NUM,79); // 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

View File

@ -16,7 +16,7 @@ class RFControl {
static uint8_t nChannels;
public:
RFControl(uint8_t pin); // creates transmitter on pin
RFControl(uint8_t pin, boolean refClock=true); // creates transmitter on pin, using 1-MHz Ref Tick clock
void start(uint32_t *data, int nData, uint8_t nCycles=1, uint8_t tickTime=1); // starts transmission of pulses from specified data pointer, repeated for numCycles, where each tick in pulse is tickTime microseconds long
void start(uint8_t nCycles=1, uint8_t tickTime=1); // starts transmission of pulses from internal data structure, repeated for numCycles, where each tick in pulse is tickTime microseconds long

View File

@ -10,33 +10,21 @@ void setup() {
Serial.println("\n\nHomeSpan RF Transmitter Example");
RFControl rf(17); // create an instance of RFControl with signal output to pin 17
RFControl rf(10); // create an instance of RFControl with signal output to pin 17
rf.clear();
rf.add(10000,10000);
rf.add(10000,10000);
rf.add(10000,40000);
rf.add(10000,30000);
uint32_t t0=micros();
rf.start(4,100);
rf.start(4,1);
uint32_t t1=micros();
Serial.println("End Example");
Serial.println((t1-t0)/1000);
rf.clear();
rf.add(10000,10000);
rf.add(10000,10000);
t0=micros();
rf.start(4,100);
t1=micros();
Serial.println("End Example");
Serial.println((t1-t0)/1000);
} // end of setup()
void loop(){