88#include " logging.h"
99#include < SPI.h>
1010#if defined(PLATFORM_ESP32)
11- #include < analogWrite .h>
11+ #include < pwmWrite .h>
1212#endif
1313
1414#define SYNTHESIZER_REGISTER_A 0x00
2525
2626#define POWER_AMP_ON 0b00000100111110111111
2727#define POWER_AMP_OFF 0x00
28- #if defined(PLATFORM_ESP32)
29- // ESP32 DAC pins are 0-4095
30- #define MIN_PWM 1 // Testing required.
31- #define MAX_PWM 250 // Absolute max is 4095. But above 250 does nothing.
32- #else
33- // ESP8285 PWM is 0-4095
28+ // ESP32 DAC pins are 0-255
29+ #define MIN_DAC 1 // Testing required.
30+ #define MAX_DAC 250 // Absolute max is 255. But above 250 does nothing.
31+ // PWM is 0-4095
3432#define MIN_PWM 1000 // Testing required.
3533#define MAX_PWM 3600 // Absolute max is 4095. But above 3500 does nothing.
36- # endif
34+
3735#define VPD_BUFFER 5
3836
3937#define READ_BIT 0x00
@@ -52,6 +50,8 @@ static uint8_t vtxSPIPowerIdxCurrent = 0;
5250uint8_t vtxSPIPitmode = 1 ;
5351static uint8_t RfAmpVrefState = 0 ;
5452static uint16_t vtxSPIPWM = MAX_PWM;
53+ static uint16_t vtxMinPWM = MAX_PWM;
54+ static uint16_t vtxMaxPWM = MIN_PWM;
5555static uint16_t VpdSetPoint = 0 ;
5656static uint16_t Vpd = 0 ;
5757
@@ -77,6 +77,9 @@ static const uint16_t freqTable[48] = {
7777 5333 , 5373 , 5413 , 5453 , 5493 , 5533 , 5573 , 5613 // L
7878};
7979
80+ #if defined(PLATFORM_ESP32)
81+ static Pwm pwm;
82+ #endif
8083static SPIClass *vtxSPI;
8184static void rtc6705WriteRegister (uint32_t regData)
8285{
@@ -147,24 +150,40 @@ static void RfAmpVrefOff()
147150 RfAmpVrefState = 0 ;
148151}
149152
153+ static void setPWM ()
154+ {
155+ #if defined(PLATFORM_ESP32)
156+ if (GPIO_PIN_RF_AMP_PWM == 25 || GPIO_PIN_RF_AMP_PWM == 26 )
157+ {
158+ dacWrite (GPIO_PIN_RF_AMP_PWM, vtxSPIPWM >> 4 );
159+ }
160+ else
161+ {
162+ pwm.write (GPIO_PIN_RF_AMP_PWM, vtxSPIPWM);
163+ }
164+ #else
165+ analogWrite (GPIO_PIN_RF_AMP_PWM, vtxSPIPWM);
166+ #endif
167+ }
168+
150169void VTxOutputMinimum ()
151170{
152171 RfAmpVrefOff ();
153172
154- vtxSPIPWM = MAX_PWM ;
155- analogWrite (GPIO_PIN_RF_AMP_PWM, vtxSPIPWM );
173+ vtxSPIPWM = vtxMaxPWM ;
174+ setPWM ( );
156175}
157176
158177static void VTxOutputIncrease ()
159178{
160- if (vtxSPIPWM > MIN_PWM ) vtxSPIPWM -= 1 ;
161- analogWrite (GPIO_PIN_RF_AMP_PWM, vtxSPIPWM );
179+ if (vtxSPIPWM > vtxMinPWM ) vtxSPIPWM -= 1 ;
180+ setPWM ( );
162181}
163182
164183static void VTxOutputDecrease ()
165184{
166- if (vtxSPIPWM < MAX_PWM ) vtxSPIPWM += 1 ;
167- analogWrite (GPIO_PIN_RF_AMP_PWM, vtxSPIPWM );
185+ if (vtxSPIPWM < vtxMaxPWM ) vtxSPIPWM += 1 ;
186+ setPWM ( );
168187}
169188
170189static uint16_t LinearInterpVpdSetPointArray (const uint16_t VpdSetPointArray[])
@@ -275,11 +294,22 @@ static void initialize()
275294 #if defined(PLATFORM_ESP8266)
276295 pinMode (GPIO_PIN_RF_AMP_PWM, OUTPUT);
277296 analogWriteFreq (10000 ); // 10kHz
297+ analogWriteResolution (12 ); // 0 - 4095
278298 #else
279- analogWriteFrequency (GPIO_PIN_RF_AMP_PWM, 10000 ); // 10kHz
299+ // If using a DAC pin then adjust min/max and initial value
300+ if (GPIO_PIN_RF_AMP_PWM == 25 || GPIO_PIN_RF_AMP_PWM == 26 )
301+ {
302+ vtxMinPWM = MIN_DAC;
303+ vtxMaxPWM = MAX_DAC;
304+ vtxSPIPWM = vtxMaxPWM;
305+ }
306+ else
307+ {
308+ pwm.writeFrequency (GPIO_PIN_RF_AMP_PWM, 10000 ); // 10kHz
309+ pwm.writeResolution (12 ); // 0 - 4095
310+ }
280311 #endif
281- analogWriteResolution (12 ); // 0 - 4095
282- analogWrite (GPIO_PIN_RF_AMP_PWM, vtxSPIPWM);
312+ setPWM ();
283313
284314 delay (RTC6705_BOOT_DELAY);
285315 }
0 commit comments