Skip to content

Commit

Permalink
Support dynamic location for flash bank offset
Browse files Browse the repository at this point in the history
Allow the pico_flash_bank_get_offset function to be changed by
defining pico_flash_bank_get_offset_func

Fixes #1278
  • Loading branch information
peterharperuk committed Mar 3, 2023
1 parent 18479e0 commit 70ea92f
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/rp2_common/pico_btstack/btstack_flash_bank.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// Check sizes
static_assert(PICO_FLASH_BANK_TOTAL_SIZE % (FLASH_SECTOR_SIZE * 2) == 0, "PICO_FLASH_BANK_TOTAL_SIZE invalid");
static_assert(PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big");
static_assert(PICO_FLASH_BANK_STORAGE_OFFSET + PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big");

// Size of one bank
#define PICO_FLASH_BANK_SIZE (PICO_FLASH_BANK_TOTAL_SIZE / 2)
Expand All @@ -33,11 +32,26 @@ static uint32_t pico_flash_bank_get_alignment(void * context) {
return 1;
}

#ifndef pico_flash_bank_get_offset_func
static inline uint32_t pico_flash_bank_get_fixed_offset(void) {
static_assert(PICO_FLASH_BANK_STORAGE_OFFSET + PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big");
#ifndef NDEBUG
// Check we're not overlapping the binary in flash
extern char __flash_binary_end;
assert(((uintptr_t)&__flash_binary_end - XIP_BASE <= PICO_FLASH_BANK_STORAGE_OFFSET));
#endif
return PICO_FLASH_BANK_STORAGE_OFFSET;
}
#define pico_flash_bank_get_offset_func pico_flash_bank_get_fixed_offset
#else
extern uint32_t pico_flash_bank_get_offset_func(void);
#endif

static void pico_flash_bank_erase(void * context, int bank) {
(void)(context);
DEBUG_PRINT("erase: bank %d\n", bank);
uint32_t status = save_and_disable_interrupts();
flash_range_erase(PICO_FLASH_BANK_STORAGE_OFFSET + (PICO_FLASH_BANK_SIZE * bank), PICO_FLASH_BANK_SIZE);
flash_range_erase(pico_flash_bank_get_offset_func() + (PICO_FLASH_BANK_SIZE * bank), PICO_FLASH_BANK_SIZE);
restore_interrupts(status);
}

Expand All @@ -55,7 +69,7 @@ static void pico_flash_bank_read(void *context, int bank, uint32_t offset, uint8
if ((offset + size) > PICO_FLASH_BANK_SIZE) return;

// Flash is xip
memcpy(buffer, (void *)(XIP_BASE + PICO_FLASH_BANK_STORAGE_OFFSET + (PICO_FLASH_BANK_SIZE * bank) + offset), size);
memcpy(buffer, (void *)(XIP_BASE + pico_flash_bank_get_offset_func() + (PICO_FLASH_BANK_SIZE * bank) + offset), size);
}

static void pico_flash_bank_write(void * context, int bank, uint32_t offset, const uint8_t *data, uint32_t size) {
Expand All @@ -74,7 +88,7 @@ static void pico_flash_bank_write(void * context, int bank, uint32_t offset, con
if (size == 0) return;

// calc bank start position
const uint32_t bank_start_pos = PICO_FLASH_BANK_STORAGE_OFFSET + (PICO_FLASH_BANK_SIZE * bank);
const uint32_t bank_start_pos = pico_flash_bank_get_offset_func() + (PICO_FLASH_BANK_SIZE * bank);

// Calculate first and last page in the bank
const uint32_t first_page = offset / FLASH_PAGE_SIZE;
Expand Down Expand Up @@ -133,12 +147,5 @@ static const hal_flash_bank_t pico_flash_bank_instance_obj = {
};

const hal_flash_bank_t *pico_flash_bank_instance(void) {

#ifndef NDEBUG
// Check we're not overlapping the binary in flash
extern char __flash_binary_end;
assert((uintptr_t)&__flash_binary_end - XIP_BASE <= PICO_FLASH_BANK_STORAGE_OFFSET);
#endif

return &pico_flash_bank_instance_obj;
}

0 comments on commit 70ea92f

Please sign in to comment.