Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ TARGET = badgemagic-ch582
######################################
# Uncomment below line to enable debugging
# DEBUG = 1
# Uncomment below to build for USB-C version
# USBC_VERSION = 1

# Hardware version selection:
# - Set USBC_VERSION = 1 for USB-C hardware (uses GPIO B6 for LED pin T)
# - Comment out or set to 0 for micro-USB hardware (uses GPIO B23 for LED pin T)
# Default: USB-C version
USBC_VERSION = 1

# optimization for size
OPT = -Os

Expand Down
23 changes: 23 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,32 @@ static void change_brightness()
static void mode_setup_download();
static void mode_setup_normal();

/*
* Mode state machine (KEY1 button press handler)
*
* Standard cycle: NORMAL(1) -> DOWNLOAD(2) -> POWER_OFF(3) -> NORMAL(1) -> ...
*
* Special case: BOOT(0)
* - BOOT is the initial mode on power-on, shows charging status during startup
* - Also entered automatically when USB charging detected (see DETECT_CHARGING event)
* - Not part of standard mode cycle (user cannot cycle INTO boot via button)
* - When user presses button in BOOT: transition directly to NORMAL
* - Rationale: Allows user to exit charging display and prevents accidental
* power-off during charging (since BOOT is outside the normal cycle)
*
* Implementation note: This special case should be preserved when adding new modes.
* BOOT should always transition directly to NORMAL on button press.
*/
__HIGH_CODE
static void change_mode()
{
// Special case: BOOT mode exits directly to NORMAL (not part of cycle)
if (mode == BOOT) {
mode = NORMAL;
return;
}

// Standard mode cycling
NEXT_STATE(mode, 0, MODES_COUNT);
Comment on lines +83 to 90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the AI here; special-casing BOOT like this is inelegant. Set NORMAL to the minimum state in the NEXT_STATE call instead.

Suggested change
// Special case: BOOT mode exits directly to NORMAL (not part of cycle)
if (mode == BOOT) {
mode = NORMAL;
return;
}
// Standard mode cycling
NEXT_STATE(mode, 0, MODES_COUNT);
NEXT_STATE(mode, NORMAL, MODES_COUNT);

const static void (*modes[])(void) = {
NULL,
Expand Down
9 changes: 8 additions & 1 deletion src/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ void poweroff()
GPIOA_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_Floating);
GPIOB_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_Floating);

// Configure wake-up
// Configure wake-up sources
// KEY1: Always enabled (button wake-up)
GPIOA_ModeCfg(KEY1_PIN, GPIO_ModeIN_PD);
GPIOA_ITModeCfg(KEY1_PIN, GPIO_ITMode_RiseEdge);

#ifndef USBC_VERSION
// CHARGE_STT: Enable for micro-USB version
// Note: Disabled on USB-C version because it interferes with KEY1 wake-up
GPIOA_ModeCfg(CHARGE_STT_PIN, GPIO_ModeIN_PU);
GPIOA_ITModeCfg(CHARGE_STT_PIN, GPIO_ITMode_FallEdge);
#endif

Comment on lines +17 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, since we don't know for sure there is a difference here between micro-USB and USB-C, let's not make this conditional.

Suggested change
#ifndef USBC_VERSION
// CHARGE_STT: Enable for micro-USB version
// Note: Disabled on USB-C version because it interferes with KEY1 wake-up
GPIOA_ModeCfg(CHARGE_STT_PIN, GPIO_ModeIN_PU);
GPIOA_ITModeCfg(CHARGE_STT_PIN, GPIO_ITMode_FallEdge);
#endif

PFIC_EnableIRQ(GPIO_A_IRQn);
PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);

Expand Down
Loading