Added reset and shutdown registers

This commit is contained in:
JackCarterSmith 2025-05-06 21:33:25 +02:00
parent f6d7a83f1b
commit 7e58a74f5a
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
3 changed files with 60 additions and 24 deletions

View File

@ -6,20 +6,24 @@
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,
};
@ -43,8 +47,6 @@ enum reg_id
#define KEY_NUMLOCK (1 << 6)
#define KEY_COUNT_MASK 0x1F //0x1F == 31
//#define VER_VAL ((VERSION_MAJOR << 4) | (VERSION_MINOR << 0))
uint8_t reg_get_value(enum reg_id reg);
uint8_t* reg_raw_access(void);

View File

@ -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();
}
}
}

View File

@ -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 \