From bebae2a367eb27305b747e11cc4612adade071db Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Sun, 2 Mar 2025 23:33:37 +0100 Subject: [PATCH] shutdown button --- Firmware/main/CMakeLists.txt | 1 + Firmware/main/include/config.hpp | 9 +++++--- Firmware/main/include/shutdowner.hpp | 18 +++++++++++++++ Firmware/main/src/display.cpp | 13 ----------- Firmware/main/src/hello_world_main.cpp | 2 ++ Firmware/main/src/shutdowner.cpp | 32 ++++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 Firmware/main/include/shutdowner.hpp create mode 100644 Firmware/main/src/shutdowner.cpp diff --git a/Firmware/main/CMakeLists.txt b/Firmware/main/CMakeLists.txt index fa612de..74179e8 100644 --- a/Firmware/main/CMakeLists.txt +++ b/Firmware/main/CMakeLists.txt @@ -6,5 +6,6 @@ idf_component_register(SRCS src/i2c_global.cpp src/disp_tools.cpp src/disp_tty.cpp + src/shutdowner.cpp PRIV_REQUIRES spi_flash esp_driver_i2c driver INCLUDE_DIRS "include") diff --git a/Firmware/main/include/config.hpp b/Firmware/main/include/config.hpp index 42776f5..3adf9d1 100644 --- a/Firmware/main/include/config.hpp +++ b/Firmware/main/include/config.hpp @@ -1,8 +1,8 @@ #ifndef CB_CONFIG_HPP #define CB_CONFIG_HPP -#include "soc/gpio_num.h" #include "hal/spi_types.h" +#include "soc/gpio_num.h" #define I2C_SCL GPIO_NUM_8 #define I2C_SDA GPIO_NUM_9 @@ -14,7 +14,10 @@ #define SPI_BUS SPI2_HOST -#define DISP_WIDTH 400 +#define DISP_WIDTH 400 #define DISP_HEIGHT 240 -#endif \ No newline at end of file +#define PWR_INT GPIO_NUM_10 +#define PWR_KILL GPIO_NUM_12 + +#endif diff --git a/Firmware/main/include/shutdowner.hpp b/Firmware/main/include/shutdowner.hpp new file mode 100644 index 0000000..c65ecf0 --- /dev/null +++ b/Firmware/main/include/shutdowner.hpp @@ -0,0 +1,18 @@ +// +// Created by Stepan Usatiuk on 02.03.2025. +// + +#ifndef CB_SHUTDOWNER_HPP +#define CB_SHUTDOWNER_HPP + + +class Shutdowner { +public: + static Shutdowner& get(); +private: + Shutdowner(); + +}; + + +#endif // CB_SHUTDOWNER_HPP diff --git a/Firmware/main/src/display.cpp b/Firmware/main/src/display.cpp index 50a3808..92ebeaf 100644 --- a/Firmware/main/src/display.cpp +++ b/Firmware/main/src/display.cpp @@ -8,18 +8,6 @@ #include "driver/spi_master.h" -spi_device_interface_config_t devcfg = { - .command_bits = 0, - .address_bits = 0, - .mode = 0, // SPI mode 0 - .clock_speed_hz = 4 * 1000 * 1000, // Clock out at 10 MHz - .spics_io_num = SPI_DISP_CS, // CS pin - .flags = SPI_DEVICE_POSITIVE_CS, - .queue_size = 1, // We want to be able to queue 7 transactions at a time - // .pre_cb = lcd_spi_pre_transfer_callback, //Specify pre-transfer callback to handle D/C line -}; - - // This solution is attributed to Rich Schroeppel in the Programming Hacks section // TODO: Why does the device flag not work? unsigned char reverse_bits3(unsigned char b) { return (b * 0x0202020202ULL & 0x010884422010ULL) % 0x3ff; } @@ -35,7 +23,6 @@ std::array SMD::prep_line(const SMD::disp_line_t& line return data; } - SMD& SMD::get() { static SMD smd; return smd; diff --git a/Firmware/main/src/hello_world_main.cpp b/Firmware/main/src/hello_world_main.cpp index 633e929..20ec42f 100644 --- a/Firmware/main/src/hello_world_main.cpp +++ b/Firmware/main/src/hello_world_main.cpp @@ -25,6 +25,7 @@ #include "i2c_global.hpp" #include +#include #include #include @@ -35,6 +36,7 @@ extern "C" void app_main() { // .max_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, .min_freq_mhz = 16, .light_sleep_enable = true}; // ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); printf("Hello world!\n"); + Shutdowner::get(); I2cGlobal::get(); BatMon::get(); SpiGlobal::get(); diff --git a/Firmware/main/src/shutdowner.cpp b/Firmware/main/src/shutdowner.cpp new file mode 100644 index 0000000..42c3ee2 --- /dev/null +++ b/Firmware/main/src/shutdowner.cpp @@ -0,0 +1,32 @@ +// +// Created by Stepan Usatiuk on 02.03.2025. +// + +#include "shutdowner.hpp" + +#include + +#include "config.hpp" + +Shutdowner& Shutdowner::get() { + static Shutdowner instance; + return instance; +} + +static void shutdown(void* arg) { + // printf("Shutting down...\n"); + ESP_ERROR_CHECK(gpio_set_level(PWR_KILL, 0)); +} + +Shutdowner::Shutdowner() { + ESP_ERROR_CHECK(gpio_reset_pin(PWR_INT)); + ESP_ERROR_CHECK(gpio_reset_pin(PWR_KILL)); + + ESP_ERROR_CHECK(gpio_set_direction(PWR_INT, GPIO_MODE_INPUT)); + ESP_ERROR_CHECK(gpio_set_direction(PWR_KILL, GPIO_MODE_OUTPUT)); + ESP_ERROR_CHECK(gpio_set_level(PWR_KILL, 1)); + ESP_ERROR_CHECK(gpio_set_pull_mode(PWR_INT, GPIO_FLOATING)); + ESP_ERROR_CHECK(gpio_set_intr_type(PWR_INT, GPIO_INTR_NEGEDGE)); + ESP_ERROR_CHECK(gpio_install_isr_service(0)); + gpio_isr_handler_add(PWR_INT, shutdown, nullptr); +}