Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RP2040: Bootloader fixes #165

Merged
merged 2 commits into from
Mar 30, 2023
Merged
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
12 changes: 12 additions & 0 deletions lib/ZuluSCSI_platform_BS2/ZuluSCSI_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -94,7 +95,9 @@ void platform_init()
// Get flash chip size
uint8_t cmd_read_jedec_id[4] = {0x9f, 0, 0, 0};
uint8_t response_jedec[4] = {0};
__disable_irq();
flash_do_cmd(cmd_read_jedec_id, response_jedec, 4);
__enable_irq();
g_flash_chip_size = (1 << response_jedec[3]);
logmsg("Flash chip size: ", (int)(g_flash_chip_size / 1024), " kB");

Expand Down Expand Up @@ -383,6 +386,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)
Expand All @@ -394,6 +398,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);
Expand Down Expand Up @@ -422,6 +433,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;
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/ZuluSCSI_platform_RP2040/ZuluSCSI_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -110,7 +111,9 @@ void platform_init()
// Get flash chip size
uint8_t cmd_read_jedec_id[4] = {0x9f, 0, 0, 0};
uint8_t response_jedec[4] = {0};
__disable_irq();
flash_do_cmd(cmd_read_jedec_id, response_jedec, 4);
__enable_irq();
g_flash_chip_size = (1 << response_jedec[3]);
logmsg("Flash chip size: ", (int)(g_flash_chip_size / 1024), " kB");

Expand Down Expand Up @@ -464,6 +467,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)
Expand All @@ -475,6 +479,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);
Expand Down Expand Up @@ -503,6 +514,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;
}
}
Expand Down