From 3ea82dc5b4b6d0d32cee5a0dd5729b300d5353b0 Mon Sep 17 00:00:00 2001 From: Tao Jin Date: Thu, 21 Nov 2024 21:51:27 -0800 Subject: [PATCH] add M5StickC-Plus2 port pin toolchain to a known working ver add board definition based on v2.x esp-arduino use macro to define plus2 pinouts use macro to impl dummy axp for plus2 fix confliting ir/led pin --- arduino_workspace/AlphaFairy/AlphaFairy.ino | 15 +- arduino_workspace/AlphaFairy/TallyLite.ino | 16 +- arduino_workspace/AlphaFairy/alfy_conf.h | 6 +- .../libraries/M5StickC-Plus/src/AXP192.cpp | 243 ++++++++++++++++++ .../libraries/M5StickC-Plus/src/AXP192.h | 5 + .../M5StickC-Plus/src/utility/Config.h | 26 ++ .../M5StickC-Plus/src/utility/In_eSPI_Setup.h | 5 + .../SonyCameraInfraredRemote.cpp | 9 +- boards/m5stack-stickc-plus.json | 28 ++ boards/m5stack-stickc-plus2.json | 32 +++ platformio.ini | 23 +- 11 files changed, 388 insertions(+), 20 deletions(-) create mode 100644 boards/m5stack-stickc-plus.json create mode 100644 boards/m5stack-stickc-plus2.json diff --git a/arduino_workspace/AlphaFairy/AlphaFairy.ino b/arduino_workspace/AlphaFairy/AlphaFairy.ino index 43e599d..74b7f1a 100644 --- a/arduino_workspace/AlphaFairy/AlphaFairy.ino +++ b/arduino_workspace/AlphaFairy/AlphaFairy.ino @@ -42,11 +42,22 @@ FairyEncoder fencoder; FairySubmenu main_menu(NULL, 0); void setup() -{ +{ + Serial.begin(SERIAL_PORT_BAUDRATE); + + #ifdef ARDUINO_M5Stick_C_Plus2 + // enable power HOLD + pinMode(POWER_HOLD_PIN, OUTPUT); + digitalWrite(POWER_HOLD_PIN, HIGH); + // enable LCD BL + ledcSetup(LCD_PWM_CHAN, LCD_BL_HZ, LCD_BL_RESOLUTION_BITS); + ledcAttachPin(LCD_BL_PIN, LCD_PWM_CHAN); + ledcWrite(LCD_PWM_CHAN, 1 << LCD_BL_RESOLUTION_BITS - 1); + #endif dbg_ser.enabled = true; - Wire1.begin(21, 22); + Wire1.begin(GPIO_NUM_21, GPIO_NUM_22); Wire1.setClock(400000); cpufreq_init(); diff --git a/arduino_workspace/AlphaFairy/TallyLite.ino b/arduino_workspace/AlphaFairy/TallyLite.ino index c1ff1b1..9fb4dcb 100644 --- a/arduino_workspace/AlphaFairy/TallyLite.ino +++ b/arduino_workspace/AlphaFairy/TallyLite.ino @@ -1,5 +1,9 @@ #include "AlphaFairy.h" - +#ifndef ARDUINO_M5Stick_C_Plus2 +#define LED_PIN GPIO_NUM_10 +#else +#define LED_PIN GPIO_NUM_19 +#endif extern bool redraw_flag; bool tallylite_nopoll = false; // used to prevent recursion in app_poll() bool tallylite_enable = true; @@ -66,17 +70,17 @@ bool tallylite_ledIsOn = false; void tallylite_ledOn() { - pinMode(10, OUTPUT); - digitalWrite(10, LOW); - tallylite_ledIsOn = true; + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, LOW); + tallylite_ledIsOn = true; } void tallylite_ledOff() { if (tallylite_ledIsOn) { - pinMode(10, INPUT); - digitalWrite(10, HIGH); + pinMode(LED_PIN, INPUT); + digitalWrite(LED_PIN, HIGH); } tallylite_ledIsOn = false; } diff --git a/arduino_workspace/AlphaFairy/alfy_conf.h b/arduino_workspace/AlphaFairy/alfy_conf.h index 238e2c7..0b06b62 100644 --- a/arduino_workspace/AlphaFairy/alfy_conf.h +++ b/arduino_workspace/AlphaFairy/alfy_conf.h @@ -14,8 +14,8 @@ //#define DISABLE_STATUS_BAR //#define DISABLE_POWER_SAVE -#define DISABLE_CMD_LINE -#define DISABLE_ALL_MSG +// #define DISABLE_CMD_LINE +// #define DISABLE_ALL_MSG #define MUTE_NETMSG_ON_BOOT //#define HTTP_MOCKBTNS_ENABLE //#define HTTP_ON_BOOT @@ -42,7 +42,7 @@ #define BTN_DEBOUNCE 50 -#define SERIAL_PORT_BAUDRATE 500000 +#define SERIAL_PORT_BAUDRATE 115200 //#define ENABLE_BUILD_LEPTON //#define ENABLE_BUILD_LEPTON_TRIGGER_SIMPLE diff --git a/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.cpp b/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.cpp index e51acc9..8372d04 100644 --- a/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.cpp +++ b/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.cpp @@ -1,8 +1,10 @@ #include "AXP192.h" +#include "utility/Config.h" AXP192::AXP192() { } +#ifndef ARDUINO_M5Stick_C_Plus2 void AXP192::begin(void) { Wire1.begin(21, 22); Wire1.setClock(400000); @@ -426,3 +428,244 @@ void AXP192::SetLDO2(bool State) { void AXP192::PowerOff() { Write1Byte(0x32, Read8bit(0x32) | 0x80); // MSB for Power Off } +#else +void AXP192::begin(void) { BtnC.read(); } +void AXP192::Write1Byte(uint8_t Addr, uint8_t Data) {} + +uint8_t AXP192::Read8bit(uint8_t Addr) { + return 0; +} + +uint16_t AXP192::Read12Bit(uint8_t Addr) { + uint16_t Data = 0; + return Data; +} + +uint16_t AXP192::Read13Bit(uint8_t Addr) { + uint16_t Data = 0; + return Data; +} + +uint16_t AXP192::Read16bit(uint8_t Addr) { + uint16_t ReData = 0; + return ReData; +} + +uint32_t AXP192::Read24bit(uint8_t Addr) { + uint32_t ReData = 0; + return ReData; +} + +uint32_t AXP192::Read32bit(uint8_t Addr) { + uint32_t ReData = 0; + return ReData; +} + +void AXP192::ReadBuff(uint8_t Addr, uint8_t Size, uint8_t *Buff) {} + +void AXP192::ScreenBreath(uint8_t brightness) { + if (brightness > 12) { + brightness = 12; + } else if (brightness < 1) { + brightness = 1; + } + ledcWrite(LCD_PWM_CHAN, gamma_lut[brightness]); +} + +void AXP192::ScreenSwitch(bool state) { + uint8_t brightness; + if (state == false) { + brightness = 0; + } else if (state == true) { + brightness = LCD_BL_RESOLUTION_BITS; + } + ledcWrite(LCD_PWM_CHAN, gamma_lut[brightness]); +} + +bool AXP192::GetBatState() { + // TODO: + return true; +} +//---------coulombcounter_from_here--------- +// enable: void EnableCoulombcounter(void); +// disable: void DisableCOulombcounter(void); +// stop: void StopCoulombcounter(void); +// clear: void ClearCoulombcounter(void); +// get charge data: uint32_t GetCoulombchargeData(void); +// get discharge data: uint32_t GetCoulombdischargeData(void); +// get coulomb val affter calculation: float GetCoulombData(void); +//------------------------------------------ +void AXP192::EnableCoulombcounter(void) { Write1Byte(0xB8, 0x80); } + +void AXP192::DisableCoulombcounter(void) { Write1Byte(0xB8, 0x00); } + +void AXP192::StopCoulombcounter(void) { Write1Byte(0xB8, 0xC0); } + +void AXP192::ClearCoulombcounter(void) { Write1Byte(0xB8, 0xA0); } + +uint32_t AXP192::GetCoulombchargeData(void) { return 0; } + +uint32_t AXP192::GetCoulombdischargeData(void) { return 0; } + +float AXP192::GetCoulombData(void) { + uint32_t coin = 0; + uint32_t coout = 0; + +// coin = GetCoulombchargeData(); +// coout = GetCoulombdischargeData(); + + // c = 65536 * current_LSB * (coin - coout) / 3600 / ADC rate + // Adc rate can be read from 84H ,change this variable if you change the ADC + // reate + float ccc = 65536 * 0.5 * (int32_t)(coin - coout) / 3600.0 / 25.0; + + return ccc; +} +//----------coulomb_end_at_here---------- + +uint16_t AXP192::GetVbatData(void) { + uint16_t vbat = 4; + return vbat; +} + +uint16_t AXP192::GetVinData(void) { + uint16_t vin = 5; + return vin; +} + +uint16_t AXP192::GetIinData(void) { + return 0; +} + +uint16_t AXP192::GetVusbinData(void) { return 5; } + +uint16_t AXP192::GetIusbinData(void) { + return 0; +} + +uint16_t AXP192::GetIchargeData(void) { + return 0; +} + +uint16_t AXP192::GetIdischargeData(void) { + return 0; +} + +uint16_t AXP192::GetTempData(void) { + return 0; +} + +uint32_t AXP192::GetPowerbatData(void) { + return 0; +} + +uint16_t AXP192::GetVapsData(void) { + return 0; +} + +void AXP192::SetSleep(void) { + +} + +uint8_t AXP192::GetWarningLeve(void) { + // TODO:; + uint8_t buf = 1; + return (buf & 0x01); +} + +// -- sleep +void AXP192::DeepSleep(uint64_t time_in_us) { + SetSleep(); + + if (time_in_us > 0) { + esp_sleep_enable_timer_wakeup(time_in_us); + } else { + esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER); + } + (time_in_us == 0) ? esp_deep_sleep_start() : esp_deep_sleep(time_in_us); +} + +void AXP192::LightSleep(uint64_t time_in_us) { + SetSleep(); + + if (time_in_us > 0) { + esp_sleep_enable_timer_wakeup(time_in_us); + } else { + esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER); + } + esp_light_sleep_start(); +} + +// 0 not press, 0x01 long press, 0x02 press +uint8_t AXP192::GetBtnPress() { + BtnC.read(); + if (BtnC.wasPressed()) { + return 0x02; + } + return 0; +} + +uint8_t AXP192::GetWarningLevel(void) { return 0; } + +float AXP192::GetBatVoltage() { + return 4.2; +} + +float AXP192::GetBatCurrent() { + return 0.0; +} + +float AXP192::GetVinVoltage() { + return 5.0; +} + +float AXP192::GetVinCurrent() { + return 0.0; +} + +float AXP192::GetVBusVoltage() { + return 4.2; +} + +float AXP192::GetVBusCurrent() { + return 0.0; +} + +float AXP192::GetTempInAXP192() { + return 37; +} + +float AXP192::GetBatPower() { + float VoltageLSB = 1.1; + float CurrentLCS = 0.5; + uint32_t ReData = 0; + return VoltageLSB * CurrentLCS * ReData / 1000.0; +} + +float AXP192::GetBatChargeCurrent() { + float ADCLSB = 0.5; + uint16_t ReData = 0; + return ReData * ADCLSB; +} +float AXP192::GetAPSVoltage() { + float ADCLSB = 1.4 / 1000.0; + uint16_t ReData = 0; + return ReData * ADCLSB; +} + +float AXP192::GetBatCoulombInput() { + uint32_t ReData = 0; + return ReData * 65536 * 0.5 / 3600 / 25.0; +} + +float AXP192::GetBatCoulombOut() { + uint32_t ReData = 0; + return ReData * 65536 * 0.5 / 3600 / 25.0; +} + +void AXP192::SetCoulombClear() {} + +void AXP192::SetLDO2(bool State) {} + +void AXP192::PowerOff() { digitalWrite(POWER_HOLD_PIN, LOW); } +#endif \ No newline at end of file diff --git a/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.h b/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.h index 8d4448d..5474a4f 100644 --- a/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.h +++ b/arduino_workspace/libraries/M5StickC-Plus/src/AXP192.h @@ -3,6 +3,7 @@ #include #include +#include "utility/Button.h" #define SLEEP_MSEC(us) (((uint64_t)us) * 1000L) #define SLEEP_SEC(us) (((uint64_t)us) * 1000000L) @@ -37,6 +38,10 @@ class AXP192 { uint16_t GetIusbinData(void) __attribute__((deprecated)); uint16_t GetVapsData(void) __attribute__((deprecated)); uint8_t GetBtnPress(void); +#define DEBOUNCE_MS 10 +#ifdef ARDUINO_M5Stick_C_Plus2 + Button BtnC = Button(35, true, DEBOUNCE_MS); +#endif // -- sleep void SetSleep(void); diff --git a/arduino_workspace/libraries/M5StickC-Plus/src/utility/Config.h b/arduino_workspace/libraries/M5StickC-Plus/src/utility/Config.h index 4bd75e2..2032b63 100644 --- a/arduino_workspace/libraries/M5StickC-Plus/src/utility/Config.h +++ b/arduino_workspace/libraries/M5StickC-Plus/src/utility/Config.h @@ -1,8 +1,34 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ +#ifndef ARDUINO_M5Stick_C_Plus2 #define M5_IR 9 #define M5_LED 10 +#else +#define M5_IR 19 +#define M5_LED 19 +#define POWER_HOLD_PIN 4 +#define LCD_BL_PIN 27 +#define LCD_PWM_CHAN 3 +// NOTE: PWM HZ and BITS has to be carefully chosen https://lastminuteengineers.com/esp32-pwm-tutorial/ +#define LCD_BL_HZ 500 +#define LCD_BL_RESOLUTION_BITS 12 +const uint32_t gamma_lut[LCD_BL_RESOLUTION_BITS + 1] = {0,516,920,1289,1639,1974,2298,2613,2920,3222,3517,3808,4095}; +#endif +/* +import matplotlib.pyplot as plt +import numpy as np + +max_bits = int(12) +gamma = float(2.2) + +x = np.arange(0, max_bits + 1) +y = np.power(x, 1. / gamma) +y = y / np.max(y) * (2 ** max_bits - 1) +plt.plot(x, y) +# plt.show() +print(",".join(y.astype(int).astype(str))) +*/ #define M5_BUTTON_HOME 37 #define M5_BUTTON_RST 39 diff --git a/arduino_workspace/libraries/M5StickC-Plus/src/utility/In_eSPI_Setup.h b/arduino_workspace/libraries/M5StickC-Plus/src/utility/In_eSPI_Setup.h index 694d079..1b75a91 100644 --- a/arduino_workspace/libraries/M5StickC-Plus/src/utility/In_eSPI_Setup.h +++ b/arduino_workspace/libraries/M5StickC-Plus/src/utility/In_eSPI_Setup.h @@ -196,8 +196,13 @@ #define TFT_MOSI 15 #define TFT_SCLK 13 #define TFT_CS 5 // Chip select line for TFT display on Shield +#ifndef ARDUINO_M5Stick_C_Plus2 #define TFT_DC 23 // Data/command line for TFT on Shield #define TFT_RST 18 // Reset line for TFT is handled by seesaw! +#else +#define TFT_DC 14 // Data/command line for TFT on Shield +#define TFT_RST 12 // Reset line for TFT is handled by seesaw! +#endif // ###### EDIT THE PINs BELOW TO SUIT YOUR ESP32 PARALLEL TFT SETUP ###### diff --git a/arduino_workspace/libraries/SonyCameraInfraredRemote/SonyCameraInfraredRemote.cpp b/arduino_workspace/libraries/SonyCameraInfraredRemote/SonyCameraInfraredRemote.cpp index ed6ce51..a7a220f 100644 --- a/arduino_workspace/libraries/SonyCameraInfraredRemote/SonyCameraInfraredRemote.cpp +++ b/arduino_workspace/libraries/SonyCameraInfraredRemote/SonyCameraInfraredRemote.cpp @@ -13,16 +13,17 @@ #include #include -#define IR_TX_PIN 9 -//#define IR_TX_PIN 26 - #endif #ifdef USE_ESP32_RMT #include "driver/rmt.h" #include "soc/rmt_reg.h" +#ifndef ARDUINO_M5Stick_C_Plus2 #define IR_TX_PIN GPIO_NUM_9 +#else +#define IR_TX_PIN GPIO_NUM_19 +#endif //#define IR_TX_PIN GPIO_NUM_26 #define RMT_CHANNEL RMT_CHANNEL_3 @@ -33,7 +34,7 @@ void SonyCamIr_Init() { #ifdef USE_IRREMOTE - IrSender.begin(9); + IrSender.begin(IR_TX_PIN); #endif #ifdef USE_ESP32_RMT rmt_config_t rmt_tx; diff --git a/boards/m5stack-stickc-plus.json b/boards/m5stack-stickc-plus.json new file mode 100644 index 0000000..34aa1b2 --- /dev/null +++ b/boards/m5stack-stickc-plus.json @@ -0,0 +1,28 @@ +{ + "build": { + "arduino": { + "ldscript": "esp32_out.ld" + }, + "core": "esp32", + "extra_flags": "-DARDUINO_M5Stick_C_Plus", + "f_cpu": "240000000L", + "f_flash": "80000000L", + "flash_mode": "dio", + "mcu": "esp32", + "variant": "m5stick_c" + }, + "frameworks": [ + "arduino", + "espidf" + ], + "name": "M5StickC-Plus", + "upload": { + "flash_size": "4MB", + "maximum_ram_size": 327680, + "maximum_size": 4194304, + "require_upload_port": true, + "speed": 1500000 + }, + "url": "https://docs.m5stack.com/en/core/m5stickc_plus", + "vendor": "M5Stack" +} \ No newline at end of file diff --git a/boards/m5stack-stickc-plus2.json b/boards/m5stack-stickc-plus2.json new file mode 100644 index 0000000..41b5fd8 --- /dev/null +++ b/boards/m5stack-stickc-plus2.json @@ -0,0 +1,32 @@ +{ + "build": { + "arduino": { + "ldscript": "esp32_out.ld", + "partitions": "default_8MB.csv" + }, + "core": "esp32", + "extra_flags": [ + "-DARDUINO_M5Stick_C_Plus2", + "-DBOARD_HAS_PSRAM" + ], + "f_cpu": "240000000L", + "f_flash": "80000000L", + "flash_mode": "qio", + "mcu": "esp32", + "variant": "m5stick_c" + }, + "frameworks": [ + "arduino", + "espidf" + ], + "name": "M5StickC-Plus", + "upload": { + "flash_size": "8MB", + "maximum_ram_size": 327680, + "maximum_size": 8388608, + "require_upload_port": true, + "speed": 1500000 + }, + "url": "https://docs.m5stack.com/en/core/M5StickC%20PLUS2", + "vendor": "M5Stack" +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 60b7f54..aea4c53 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,9 +14,22 @@ lib_dir = arduino_workspace/libraries data_dir = arduino_workspace/AlphaFairy/data -[env:m5stick-c] -platform = espressif32 -board = m5stick-c +[esp-arduino] +; platform = https://github.com/pioarduino/platform-espressif32.git +platform = espressif32 @ 5.2.0 framework = arduino -# no jtag pin on m5stick-c for debugging -# debug_tool = esp-prog +monitor_speed = 115200 + +[env:m5stickc-plus] +extends = esp-arduino +board = m5stack-stickc-plus + +[env:m5stickc-plus2] +extends = esp-arduino +board = m5stack-stickc-plus2 + +[env:m5stickc-plus2-debug] +extends = env:m5stickc-plus2 +build_type = debug +build_flags = -DCORE_DEBUG_LEVEL=5 +monitor_filters = esp32_exception_decoder