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 raspberrypi#1278
  • Loading branch information
peterharperuk committed Jun 6, 2023
1 parent 3525a96 commit 23f307a
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 Down Expand Up @@ -48,12 +47,27 @@ static void pico_flash_bank_perform_flash_mutation_operation(void *param) {
}
}

#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);
mutation_operation_t mop = {
.op_is_erase = true,
.p0 = PICO_FLASH_BANK_STORAGE_OFFSET + (PICO_FLASH_BANK_SIZE * bank),
.p0 = pico_flash_bank_get_offset_func() + (PICO_FLASH_BANK_SIZE * bank),
};
// todo better timeout
flash_safe_execute(pico_flash_bank_perform_flash_mutation_operation, &mop, UINT32_MAX);
Expand All @@ -73,7 +87,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 @@ -92,7 +106,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 @@ -155,12 +169,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 23f307a

Please sign in to comment.