Skip to content

Add startup delay and BOOT0 recovery documentation to prevent debug lockout#589

Open
adamivie wants to merge 1 commit intoEFeru:mainfrom
adamivie:add-boot0-recovery-and-startup-delay
Open

Add startup delay and BOOT0 recovery documentation to prevent debug lockout#589
adamivie wants to merge 1 commit intoEFeru:mainfrom
adamivie:add-boot0-recovery-and-startup-delay

Conversation

@adamivie
Copy link
Copy Markdown

Problem

After flashing certain firmware variants (especially USART), users experience debug lockout where ST-Link cannot reconnect to the board. This prevents reflashing without using special recovery procedures.

Solution

1. Startup Delay (Prevention)

  • Added a 2-second delay in main.c after HAL_Init()
  • Gives debuggers time to connect before firmware fully initializes
  • Applies to all variants since it's in the main initialization code

2. BOOT0 Recovery Documentation

  • Created comprehensive docs/BOOT0_RECOVERY.md guide
  • Covers 3 different recovery methods
  • Includes pin location diagrams and step-by-step instructions
  • Prevents users from being permanently locked out of their boards

3. README Update

  • Added prominent link to BOOT0 recovery guide in Table of Contents
  • Makes recovery information easily discoverable

Testing

  • Successfully tested on STM32F103RC main board
  • Recovered from debug lockout using BOOT0 method
  • Reflashed successfully with startup delay enabled

Benefits

  • Users can recover from any flash-related lockout
  • Prevents future lockout issues with startup delay
  • Reduces support burden for cannot connect issues
  • Improves user experience for all firmware variants

- Added 2-second startup delay in main.c to prevent debug lockout
- Created comprehensive BOOT0 recovery guide for users who cannot flash
- Updated README with link to recovery documentation
- Prevents ST-Link connection issues after flashing firmware variants

This solves the issue where users cannot reflash after using USART or other
variants that make immediate use of GPIO pins, potentially blocking debugger
access. The startup delay gives debuggers a window to connect, and the BOOT0
guide provides a recovery method for existing installations.
Copilot AI review requested due to automatic review settings March 10, 2026 01:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses the problem of debug lockout after flashing certain firmware variants (especially USART) on hoverboard mainboards with STM32F103RC, where ST-Link cannot reconnect after a flash. It provides both a preventive code measure (startup delay) and a recovery documentation guide.

Changes:

  • Added a startup delay loop in Src/main.c immediately after HAL_Init() to give ST-Link time to connect during boot.
  • Added a new docs/BOOT0_RECOVERY.md guide explaining BOOT0-based recovery, ST-Link Utility methods, and prevention strategies.
  • Updated README.md Table of Contents with a prominent link to the new recovery guide.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
Src/main.c Adds a busy-wait loop after HAL_Init() to introduce a startup delay for debugger connection
docs/BOOT0_RECOVERY.md New comprehensive recovery guide covering BOOT0 hardware method, ST-Link software methods, prevention, and troubleshooting
README.md Adds a ToC entry linking to the new BOOT0 recovery guide

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +176 to +182

// Startup delay to allow debugger connection (prevents debug lockout)
// This gives ST-Link time to connect before firmware fully initializes
for(volatile uint32_t i = 0; i < 2000000; i++) {
__NOP(); // 2 second delay at 72MHz
}

Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The delay loop is placed before SystemClock_Config() (called at line 201), so the CPU is still running on the default HSI at 8 MHz — not at 72 MHz as the comment states. At 8 MHz, the loop body executes roughly 9x faster, meaning the actual delay is on the order of ~200 ms rather than ~2 seconds. The comment "2 second delay at 72MHz" is therefore incorrect, and the delay will fail to serve its intended purpose of giving the ST-Link debugger adequate time to connect.

Consider moving the delay after SystemClock_Config() and replacing it with HAL_Delay(2000), which is clock-agnostic, readable, and guaranteed to delay for exactly 2 seconds once the clocks are properly configured.

Suggested change
// Startup delay to allow debugger connection (prevents debug lockout)
// This gives ST-Link time to connect before firmware fully initializes
for(volatile uint32_t i = 0; i < 2000000; i++) {
__NOP(); // 2 second delay at 72MHz
}
// Startup delay to allow debugger connection (prevents debug lockout)
// Use HAL_Delay for a clock-agnostic 2 second delay
HAL_Delay(2000);

Copilot uses AI. Check for mistakes.
Comment on lines +50 to +53
Or use the flash script:
```bash
./flash_board.ps1
```
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The documentation references ./flash_board.ps1 as a flashing script, but no such file exists anywhere in the repository. This will confuse users who follow this guide. Either remove this reference or add the missing script to the repository.

Suggested change
Or use the flash script:
```bash
./flash_board.ps1
```

Copilot uses AI. Check for mistakes.
Comment on lines +88 to +99
```c
int main(void) {
HAL_Init();

// Startup delay to allow debugger connection
for(volatile uint32_t i = 0; i < 2000000; i++) {
__NOP(); // 2 second delay
}

// Rest of initialization...
}
```
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The inline code snippet in the "Prevention: Firmware Startup Delay" section has the same bug as the actual code: it presents the loop with the magic count 2000000 alongside the comment // 2 second delay, without accounting for the fact that this runs at 8 MHz HSI (before SystemClock_Config()). Documentation users copying this snippet will face the same timing issue. The example should be updated to use HAL_Delay(2000) placed after SystemClock_Config(), or at minimum note that the loop count needs to be calibrated against the actual clock speed when placed before clock configuration.

Copilot uses AI. Check for mistakes.

BOOT0 is a special pin on the STM32F103RC microcontroller that determines boot source:
- **BOOT0 = LOW (GND)**: Normal operation - runs firmware from flash memory
- **BOOT0 = HIGH (3.3V)**: Bootloader mode - STM32 enters DFU/programming mode
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The documentation states that when BOOT0 is HIGH, the STM32 enters "DFU/programming mode." However, DFU (Device Firmware Upgrade over USB) is not available on the STM32F103RC — that family only supports a UART/SWD bootloader via the system memory boot mode. Referring to this as "DFU mode" is inaccurate and may cause users to expect USB DFU flashing to work, which it will not. The label should be corrected to "System Memory Bootloader" or "UART bootloader" mode.

Suggested change
- **BOOT0 = HIGH (3.3V)**: Bootloader mode - STM32 enters DFU/programming mode
- **BOOT0 = HIGH (3.3V)**: System memory bootloader mode - STM32 enters the factory UART bootloader (not USB DFU)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants