فهرست منبع

RP2040: Improve bootloader robustness

The SD-card bootloader occassionally crashed due to either:
- USB packets during flash writing (arduino/ArduinoCore-mbed#409)
- Some flash XIP issues when function is not in RAM.
Petteri Aimonen 2 سال پیش
والد
کامیت
720edaf891
2فایلهای تغییر یافته به همراه20 افزوده شده و 0 حذف شده
  1. 10 0
      lib/ZuluSCSI_platform_BS2/ZuluSCSI_platform.cpp
  2. 10 0
      lib/ZuluSCSI_platform_RP2040/ZuluSCSI_platform.cpp

+ 10 - 0
lib/ZuluSCSI_platform_BS2/ZuluSCSI_platform.cpp

@@ -30,6 +30,7 @@
 #include <hardware/spi.h>
 #include <hardware/flash.h>
 #include <hardware/structs/xip_ctrl.h>
+#include <hardware/structs/usb.h>
 #include <platform/mbed_error.h>
 #include <multicore.h>
 #include <USB/PluggableUSBSerial.h>
@@ -383,6 +384,7 @@ extern uint32_t __real_vectors_start;
 extern uint32_t __StackTop;
 static volatile void *g_bootloader_exit_req;
 
+__attribute__((section(".time_critical.platform_rewrite_flash_page")))
 bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE])
 {
     if (offset == PLATFORM_BOOTLOADER_SIZE)
@@ -394,6 +396,13 @@ bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_
         }
     }
 
+    if (NVIC_GetEnableIRQ(USBCTRL_IRQn))
+    {
+        logmsg("Disabling USB during firmware flashing");
+        NVIC_DisableIRQ(USBCTRL_IRQn);
+        usb_hw->main_ctrl = 0;
+    }
+
     dbgmsg("Writing flash at offset ", offset, " data ", bytearray(buffer, 4));
     assert(offset % PLATFORM_FLASH_PAGE_SIZE == 0);
     assert(offset >= PLATFORM_BOOTLOADER_SIZE);
@@ -422,6 +431,7 @@ bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_
         if (actual != expected)
         {
             logmsg("Flash verify failed at offset ", offset + i * 4, " got ", actual, " expected ", expected);
+            __enable_irq();
             return false;
         }
     }

+ 10 - 0
lib/ZuluSCSI_platform_RP2040/ZuluSCSI_platform.cpp

@@ -30,6 +30,7 @@
 #include <hardware/spi.h>
 #include <hardware/flash.h>
 #include <hardware/structs/xip_ctrl.h>
+#include <hardware/structs/usb.h>
 #include <platform/mbed_error.h>
 #include <multicore.h>
 #include <USB/PluggableUSBSerial.h>
@@ -464,6 +465,7 @@ extern uint32_t __real_vectors_start;
 extern uint32_t __StackTop;
 static volatile void *g_bootloader_exit_req;
 
+__attribute__((section(".time_critical.platform_rewrite_flash_page")))
 bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE])
 {
     if (offset == PLATFORM_BOOTLOADER_SIZE)
@@ -475,6 +477,13 @@ bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_
         }
     }
 
+    if (NVIC_GetEnableIRQ(USBCTRL_IRQn))
+    {
+        logmsg("Disabling USB during firmware flashing");
+        NVIC_DisableIRQ(USBCTRL_IRQn);
+        usb_hw->main_ctrl = 0;
+    }
+
     dbgmsg("Writing flash at offset ", offset, " data ", bytearray(buffer, 4));
     assert(offset % PLATFORM_FLASH_PAGE_SIZE == 0);
     assert(offset >= PLATFORM_BOOTLOADER_SIZE);
@@ -503,6 +512,7 @@ bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_
         if (actual != expected)
         {
             logmsg("Flash verify failed at offset ", offset + i * 4, " got ", actual, " expected ", expected);
+            __enable_irq();
             return false;
         }
     }