From 7e58a74f5a76128169e020893f604b6d764ff53e Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Tue, 6 May 2025 21:33:25 +0200 Subject: [PATCH] Added reset and shutdown registers --- Core/Inc/regs.h | 42 ++++++++++++++++++++++-------------------- Core/Src/main.c | 40 +++++++++++++++++++++++++++++++++++++--- Makefile | 2 +- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/Core/Inc/regs.h b/Core/Inc/regs.h index f6d1520..7fb0df2 100644 --- a/Core/Inc/regs.h +++ b/Core/Inc/regs.h @@ -6,44 +6,46 @@ enum reg_id { - REG_ID_VER = 0x01, // fw version + REG_ID_VER = 0x01, // fw version (7:4=Major, 3:0=Minor) REG_ID_CFG = 0x02, // config REG_ID_INT = 0x03, // interrupt status REG_ID_KEY = 0x04, // key status REG_ID_BKL = 0x05, // backlight steps (0-9) REG_ID_DEB = 0x06, // debounce cfg REG_ID_FRQ = 0x07, // poll freq cfg - REG_ID_RST = 0x08, // reset + REG_ID_RST = 0x08, // STM32 full reset REG_ID_FIF = 0x09, // fifo REG_ID_BK2 = 0x0A, // keyboard backlight (0-9) REG_ID_BAT = 0x0B, // battery REG_ID_C64_MTX = 0x0C,// read c64 matrix REG_ID_C64_JS = 0x0D, // joystick io bits + REG_ID_RST_PICO = 0x0E, // Pico reset + REG_ID_SHTDW = 0x0F, // self-shutdown + REG_ID_TYP = 0xFF, // firmware type (0=official, others=custom) + REG_ID_LAST, }; -#define CFG_OVERFLOW_ON (1 << 0) //When a FIFO overflow happens, should the new entry still be pushed, overwriting the oldest one. If 0 then new entry is lost. -#define CFG_OVERFLOW_INT (1 << 1) //Should an interrupt be generated when a FIFO overflow happens -#define CFG_CAPSLOCK_INT (1 << 2) //Should an interrupt be generated when Caps Lock is toggled. -#define CFG_NUMLOCK_INT (1 << 3) //Should an interrupt be generated when Num Lock is toggled. -#define CFG_KEY_INT (1 << 4) -#define CFG_PANIC_INT (1 << 5) -#define CFG_REPORT_MODS (1 << 6) // Should Alt, Sym and Shifts be reported as well -#define CFG_USE_MODS (1 << 7) // Should Alt, Sym and Shifts modify the keys reported +#define CFG_OVERFLOW_ON (1 << 0) //When a FIFO overflow happens, should the new entry still be pushed, overwriting the oldest one. If 0 then new entry is lost. +#define CFG_OVERFLOW_INT (1 << 1) //Should an interrupt be generated when a FIFO overflow happens +#define CFG_CAPSLOCK_INT (1 << 2) //Should an interrupt be generated when Caps Lock is toggled. +#define CFG_NUMLOCK_INT (1 << 3) //Should an interrupt be generated when Num Lock is toggled. +#define CFG_KEY_INT (1 << 4) +#define CFG_PANIC_INT (1 << 5) +#define CFG_REPORT_MODS (1 << 6) // Should Alt, Sym and Shifts be reported as well +#define CFG_USE_MODS (1 << 7) // Should Alt, Sym and Shifts modify the keys reported // CFG_STICKY_MODS // Pressing and releasing a mod affects next key pressed -#define INT_OVERFLOW (1 << 0) -#define INT_CAPSLOCK (1 << 1) -#define INT_NUMLOCK (1 << 2) -#define INT_KEY (1 << 3) -#define INT_PANIC (1 << 4) +#define INT_OVERFLOW (1 << 0) +#define INT_CAPSLOCK (1 << 1) +#define INT_NUMLOCK (1 << 2) +#define INT_KEY (1 << 3) +#define INT_PANIC (1 << 4) -#define KEY_CAPSLOCK (1 << 5) -#define KEY_NUMLOCK (1 << 6) -#define KEY_COUNT_MASK 0x1F //0x1F == 31 - -//#define VER_VAL ((VERSION_MAJOR << 4) | (VERSION_MINOR << 0)) +#define KEY_CAPSLOCK (1 << 5) +#define KEY_NUMLOCK (1 << 6) +#define KEY_COUNT_MASK 0x1F //0x1F == 31 uint8_t reg_get_value(enum reg_id reg); diff --git a/Core/Src/main.c b/Core/Src/main.c index 9edaf14..d34c794 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -76,7 +76,8 @@ static volatile uint8_t i2cs_w_len = 0; static enum i2cs_state i2cs_state = I2CS_STATE_IDLE; static uint8_t keycb_start = 0; -static uint32_t head_phone_status = 0; +static uint32_t head_phone_status = 0; // TODO: Combine status registers + volatile uint8_t pmu_irq = 0; static uint32_t pmu_online = 0; @@ -135,10 +136,10 @@ extern void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirect i2cs_w_buff[1] = item.key; } else if (reg == REG_ID_VER) { i2cs_w_buff[1] = reg_get_value(REG_ID_VER); - } else if (reg == REG_ID_BAT) { - i2cs_w_buff[1] = reg_get_value(REG_ID_BAT); } else if (reg == REG_ID_TYP) { i2cs_w_buff[1] = reg_get_value(REG_ID_TYP); + } else if (reg == REG_ID_BAT) { + i2cs_w_buff[1] = reg_get_value(REG_ID_BAT); } else if (reg == REG_ID_KEY) { i2cs_w_buff[0] = fifo_count(); i2cs_w_buff[0] |= keyboard_get_numlock() ? KEY_NUMLOCK : 0x00; @@ -151,6 +152,16 @@ extern void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirect i2cs_w_len = 10; } else if (reg == REG_ID_C64_JS) { i2cs_w_buff[1] = js_bits; + } else if (reg == REG_ID_RST_PICO) { + if (is_write) + reg_set_value(REG_ID_RST_PICO, 1); + i2cs_w_buff[1] = reg_get_value(REG_ID_RST_PICO); + } else if (reg == REG_ID_SHTDW) { + if (is_write) { + reg_set_value(REG_ID_SHTDW, 1); + return; // Ignore answer, everything will be shutdown + } + i2cs_w_buff[1] = 0; } else { i2cs_w_buff[0] = 0; i2cs_w_buff[1] = 0; @@ -364,6 +375,29 @@ int main(void) { check_pmu_int(); keyboard_process(); hw_check_HP_presence(); + + // Check internal status + if (reg_get_value(REG_ID_SHTDW) == 1) { + reg_set_value(REG_ID_SHTDW, 0); + LL_GPIO_ResetOutputPin(SP_AMP_EN_GPIO_Port, SP_AMP_EN_Pin); + LL_GPIO_ResetOutputPin(PICO_EN_GPIO_Port, PICO_EN_Pin); + AXP2101_setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG); + + AXP2101_shutdown(); + } else if (reg_get_value(REG_ID_RST_PICO) == 1) { + reg_set_value(REG_ID_RST_PICO, 0); + HAL_Delay(200); // Wait for final I2C answer + if (HAL_I2C_DisableListen_IT(&hi2c1) != HAL_OK) + Error_Handler(); + LL_GPIO_ResetOutputPin(SP_AMP_EN_GPIO_Port, SP_AMP_EN_Pin); + LL_GPIO_ResetOutputPin(PICO_EN_GPIO_Port, PICO_EN_Pin); + + HAL_Delay(200); // No need to use keyboard, so a simple delay should suffice + LL_GPIO_SetOutputPin(PICO_EN_GPIO_Port, PICO_EN_Pin); + LL_GPIO_SetOutputPin(SP_AMP_EN_GPIO_Port, SP_AMP_EN_Pin); + if (HAL_I2C_EnableListen_IT(&hi2c1) != HAL_OK) + Error_Handler(); + } } } diff --git a/Makefile b/Makefile index caf7e51..3fd66a9 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ BUILD_DIR = build C_SOURCES = \ Core/Src/main.c \ Core/Src/stm32f1xx_it.c \ -Core/Src/stm32f1xx_hal_msp.c \ +Core/Src/hal_interface.c \ Core/Src/axp2101.c \ Core/Src/backlight.c \ Core/Src/batt.c \