From cf34c1dfee4038dd9d761cbea270ac74597437b8 Mon Sep 17 00:00:00 2001 From: ole00 Date: Sun, 21 Apr 2024 00:12:10 +0100 Subject: [PATCH] Sketch: support Arduino based on ESP32-S2 MCU Because of the internal reference of 2.5V on Analogue pins of ESP32-S2, the R6 must be modified from 20k Ohm to 16.67k Ohm. This can be achieved by placing a new 100k Ohm resistor in parallel with R6 resistor. Use this mod only when Arduino is ESP32-S2 based. --- aftb_vpp.h | 51 +++++++++++++++++++++++++++++++++++++++++++------ afterburner.ino | 18 +++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/aftb_vpp.h b/aftb_vpp.h index 1f7681d..7625c61 100644 --- a/aftb_vpp.h +++ b/aftb_vpp.h @@ -14,6 +14,20 @@ #define POT_DAT A5 #define VPP A0 +#if CONFIG_IDF_TARGET_ESP32S2 == 1 +// ESP32-S2 +#include "driver/adc.h" +#define ADC_PIN ADC2_CHANNEL_3 +#define EEPROM_BEGIN() EEPROM.begin(128) +#define EEPROM_UPDATE(A,V) if ((V) != EEPROM.read((A))) EEPROM.write((A),(V)) +#define EEPROM_END() EEPROM.end() +#else +// AVR +#define EEPROM_BEGIN() +#define EEPROM_UPDATE(A,V) EEPROM.update((A),(V)) +#define EEPROM_END() +#endif + #include "aftb_mcp4131.h" #ifndef FAIL #define FAIL 0 @@ -73,22 +87,25 @@ int8_t calOffset = 0; // VPP calibration offset: value 10 is 0.1V, value -10 is static void varVppReadCalib(void) { uint8_t i; + EEPROM_BEGIN(); //calibration not found if (EEPROM.read(0) != 0xAF || EEPROM.read(1) != 0xCA) { vppWiper[0] = 0; Serial.println(F("No calibration data in EEPROM")); + EEPROM_END(); return; } calOffset = (int8_t) EEPROM.read(2); for (i = 0; i < MAX_WIPER; i++) { vppWiper[i] = EEPROM.read(i + 3); -#if 0 +#if 0 Serial.print(F("Calib ")); Serial.print(i); Serial.print(F(":")); Serial.println(vppWiper[i]); #endif } + EEPROM_END(); } // internal use only - set the wiper value on the digital pot @@ -154,6 +171,13 @@ static void varVppSet(uint8_t value) { // SAMPLE_SHIFT moves the ADC gain error up/down #define SAMPLE_SHIFT -45; +// ESP32-S2 (VREF 2.5V) +#elif CONFIG_IDF_TARGET_ESP32S2 == 1 +#define SAMPLE_CNT 18 +#define SAMPLE_DIVIDER 10 +#define SAMPLE_MULTIPLIER 1 +#define SAMPLE_OFFSET 5 + //AVR based Arduinos (no ADC gain errors measured) #else #define SAMPLE_CNT 14 @@ -296,23 +320,38 @@ ret: } + static void varVppStoreWiperCalib() { uint8_t i = 0; //sanity check if (vppWiper[0] == 0) { +#ifdef VPP_VERBOSE + Serial.println(F("VPP wiper is 0")); +#endif return; } - +#ifdef VPP_VERBOSE + Serial.println(F("VPP storing calibration")); +#endif + EEPROM_BEGIN(); //write Afterburner calibration header - EEPROM.update(0, 0xAF); - EEPROM.update(1, 0xCA); - EEPROM.update(2, (uint8_t) calOffset); + EEPROM_UPDATE(0, 0xAF); + EEPROM_UPDATE(1, 0xCA); + EEPROM_UPDATE(2, (uint8_t) calOffset); while (i < MAX_WIPER) { - EEPROM.update(3 + i, vppWiper[i]); + EEPROM_UPDATE(3 + i, vppWiper[i]); i++; } + EEPROM_END(); } +#if CONFIG_IDF_TARGET_ESP32S2 == 1 +static void analogReference(uint8_t ref) { + analogReadResolution(10); + adc2_config_channel_atten(ADC_PIN, ADC_ATTEN_DB_11); // AREF 2.5V +} +#endif + //return 1 on success (variable VPP functionality present), 0 on failure (VPP not detected on board) static int8_t varVppInit(void) { diff --git a/afterburner.ino b/afterburner.ino index 5f682ab..682c91a 100644 --- a/afterburner.ino +++ b/afterburner.ino @@ -81,6 +81,19 @@ #define PIN_ZIF23 3 #define PIN_ZIF_GND_CTRL 13 +#if CONFIG_IDF_TARGET_ESP32S2 == 1 +//A0: VPP sense +//A3: DIGI_POT CS +#define A0 14 +#define A1 15 +#define A2 16 +#define A3 17 +//clk and dat is shared SPI bus +#define A4 18 +#define A5 21 +#endif + +// AVR, or UNO R4 //A0: VPP sense //A3: DIGI_POT CS #define PIN_SHR_EN A1 @@ -208,6 +221,11 @@ typedef enum { #define RAM_BIG #endif +//ESP32-S2 +#if CONFIG_IDF_TARGET_ESP32S2 == 1 +#define RAM_BIG +#endif + // common CFG fuse address map for cfg16V8 and cfg20V8 // the only difference is the starting address: 2048 for cfg16V8 and 2560 for cfg20V8