From 971c37575116302ac93ca155bd2917a7795c8548 Mon Sep 17 00:00:00 2001 From: Renze Nicolai Date: Tue, 5 Jul 2022 20:07:38 +0200 Subject: [PATCH] Add check for flashing via esptool.py --- i2c_peripheral.c | 22 +++++++++++++++------- i2c_peripheral.h | 7 +++++-- uart_task.c | 7 ++++++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/i2c_peripheral.c b/i2c_peripheral.c index e139b18..f273c80 100644 --- a/i2c_peripheral.c +++ b/i2c_peripheral.c @@ -40,24 +40,24 @@ static bool webusb_interrupt = false; static int ir_statemachine = -1; -struct { +static struct { uint8_t registers[256]; bool modified[256]; uint8_t address; bool write_in_progress; } i2c_registers; -uint8_t i2c_controlled_gpios[] = {SAO_IO0_PIN, SAO_IO1_PIN, PROTO_0_PIN, PROTO_1_PIN}; -uint8_t input1_gpios[] = {BUTTON_HOME, BUTTON_MENU, BUTTON_START, BUTTON_ACCEPT, BUTTON_BACK, FPGA_CDONE}; -uint8_t input2_gpios[] = {BUTTON_JOY_A, BUTTON_JOY_B, BUTTON_JOY_C, BUTTON_JOY_D, BUTTON_JOY_E}; +static const uint8_t i2c_controlled_gpios[] = {SAO_IO0_PIN, SAO_IO1_PIN, PROTO_0_PIN, PROTO_1_PIN}; +static const uint8_t input1_gpios[] = {BUTTON_HOME, BUTTON_MENU, BUTTON_START, BUTTON_ACCEPT, BUTTON_BACK, FPGA_CDONE}; +static const uint8_t input2_gpios[] = {BUTTON_JOY_A, BUTTON_JOY_B, BUTTON_JOY_C, BUTTON_JOY_D, BUTTON_JOY_E}; static absolute_time_t next_adc_read; -uint8_t next_adc_channel; +static uint8_t next_adc_channel; -const bool i2c_registers_read_only[256] = { +static const bool i2c_registers_read_only[256] = { true, false, true, false, false, false, true, true, // 0-7 true, true, false, true, true, true, true, true, // 8-15 - false, false, false, false, false, false, false, false, // 16-23 + false, false, true, false, false, true, true, true, // 16-23 true, true, true, true, true, true, true, true, // 24-31 false, false, false, false, false, false, false, false, // 32-39 false, false, false, false, false, false, false, false, // 40-47 @@ -342,3 +342,11 @@ void i2c_set_webusb_mode(uint8_t mode) { } void i2c_set_crash_debug_state(bool crashed, bool debug) { i2c_registers.registers[I2C_REGISTER_CRASH_DEBUG] = (crashed & 1) | ((debug << 1) & 2); } + +void i2c_set_reset_attempted(bool attempted) { + i2c_registers.registers[I2C_REGISTER_RESET_ATTEMPTED] = attempted; +} + +bool i2c_get_reset_allowed() { + return !i2c_registers.registers[I2C_REGISTER_RESET_LOCK]; +} diff --git a/i2c_peripheral.h b/i2c_peripheral.h index 0998e5b..4060d14 100644 --- a/i2c_peripheral.h +++ b/i2c_peripheral.h @@ -21,6 +21,9 @@ void i2c_set_webusb_mode(uint8_t mode); void i2c_set_crash_debug_state(bool crashed, bool debug); +void i2c_set_reset_attempted(bool attempted); +bool i2c_get_reset_allowed(); + enum { // 0-7 I2C_REGISTER_FW_VER, @@ -46,8 +49,8 @@ enum { I2C_REGISTER_BL_TRIGGER, I2C_REGISTER_WEBUSB_MODE, I2C_REGISTER_CRASH_DEBUG, - I2C_REGISTER_RESERVED2, - I2C_REGISTER_RESERVED3, + I2C_REGISTER_RESET_LOCK, + I2C_REGISTER_RESET_ATTEMPTED, I2C_REGISTER_CHARGING_STATE, I2C_REGISTER_ADC_VALUE_TEMP_LO, I2C_REGISTER_ADC_VALUE_TEMP_HI, diff --git a/uart_task.c b/uart_task.c index 11cc486..6c1b31f 100644 --- a/uart_task.c +++ b/uart_task.c @@ -304,7 +304,12 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) { if ((esp32_reset_state == 1) && (dtr2) && (!rts2)) esp32_reset_state = 2; if ((esp32_reset_state == 2) && (!dtr2) && (rts2)) esp32_reset_state = 3; if ((esp32_reset_state == 3) && (dtr2) && (rts2)) { - esp32_reset(true); + if (i2c_get_reset_allowed()) { + esp32_reset(true); + } else { + i2c_set_reset_attempted(true); + esp32_reset(false); + } } if ((esp32_reset_app_state == 0) && ((!dtr2) && rts2)) esp32_reset_app_state = 1;