Table of Contents
- 1. Dual Bank Switching Mechanism
- 1.1 Basic Concepts
- 1.2 Implementation Plan
- 1.3 FreeRTOS Integration Points
- 2. Implementation of Security Verification Process
- 2.1 Verification Mechanism Design
- 2.2 Encryption Algorithm Selection
- 2.3 Tamper Protection
- 3. Practical Demonstration
- 3.1 Code Structure
- 3.2 Key Code Snippets
- Best Practice Recommendations

1. Dual Bank Switching Mechanism
1.1 Basic Concepts

1.2 Implementation Plan
| Step | Action | FreeRTOS Considerations |
|---|---|---|
| 1 | Divide Flash into Bank0/Bank1 | Consider memory alignment and partition size |
| 2 | Implement Bootloader Jump Logic | Disable all interrupts and tasks |
| 3 | Design State Flag Storage Area | Use NVS or independent Flash sector |
| 4 | Power Failure Recovery Handling | Add watchdog timeout protection |
1.3 FreeRTOS Integration Points
- Execute jump after
<span>vTaskEndScheduler()</span> - Pause all tasks during upgrade:
vTaskSuspendAll(); // Perform Flash operations xTaskResumeAll();
2. Implementation of Security Verification Process
2.1 Verification Mechanism Design
Triple Verification Mechanism:
- CRC32 Check (Fast Verification)
- SHA-256 Digest Check
- Digital Signature Verification (Optional)
2.2 Encryption Algorithm Selection
| Algorithm Type | Recommended Implementation | Resource Consumption |
|---|---|---|
| Hash Algorithm | mbedTLS-SHA256 | ~3KB ROM |
| Asymmetric Encryption | ECDSA | ~8KB ROM |
| Symmetric Encryption | AES-128 | ~2KB ROM |
2.3 Tamper Protection
- Implementation Plan:
if(verify_signature(firmware)!= PASS){ erase_bank(1);// Immediately erase abnormal firmware system_reset(); }
3. Practical Demonstration
3.1 Code Structure
ota/
├── bootloader/ # Bootloader with FreeRTOS
├── bank_swap/ # Dual Bank Switching Implementation
├── crypto/ # Security Verification Component
└── ota_task.c # OTA Background Task
3.2 Key Code Snippets
// OTA Task Example
void vOTATask(void* pvParameters){
while(1){
if(xOTAEventGroup & DOWNLOAD_COMPLETE_BIT){
vPortEnterCritical();
if(verify_firmware()){
set_active_bank(1);
vTaskDelay(pdMS_TO_TICKS(1000));// Wait for UART output to complete
NVIC_SystemReset();
}
vPortExitCritical();
}
vTaskDelay(pdMS_TO_TICKS(100));
}
Best Practice Recommendations
- Always keep a rollback version
- Use hardware security modules (e.g., STM32’s HASH peripheral)
- Implement progress reporting mechanism (via UART/network)
- Test various exceptional scenarios (power failure, verification failure, etc.)