From 4ad607951fe61266a19718c06cddc449ce52ded9 Mon Sep 17 00:00:00 2001 From: Gregg Date: Fri, 15 Oct 2021 18:16:18 -0500 Subject: [PATCH] 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. --- src/extras/RFControl.cpp | 7 ++++--- src/extras/RFControl.h | 2 +- src/extras/extras.ino | 18 +++--------------- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/extras/RFControl.cpp b/src/extras/RFControl.cpp index 0f4d467..7f62c81 100644 --- a/src/extras/RFControl.cpp +++ b/src/extras/RFControl.cpp @@ -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 diff --git a/src/extras/RFControl.h b/src/extras/RFControl.h index 95c23be..5ca787f 100644 --- a/src/extras/RFControl.h +++ b/src/extras/RFControl.h @@ -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 diff --git a/src/extras/extras.ino b/src/extras/extras.ino index 795c6ad..f104b67 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -10,32 +10,20 @@ 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()