Skip to content

Commit f3ebd62

Browse files
Flash bank customisation (#1293)
* Support dynamic location for flash bank offset Allow the pico_flash_bank_get_offset function to be changed by defining pico_flash_bank_get_storage_offset_func
1 parent d315a04 commit f3ebd62

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/rp2_common/pico_btstack/btstack_flash_bank.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// Check sizes
1313
static_assert(PICO_FLASH_BANK_TOTAL_SIZE % (FLASH_SECTOR_SIZE * 2) == 0, "PICO_FLASH_BANK_TOTAL_SIZE invalid");
1414
static_assert(PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big");
15-
static_assert(PICO_FLASH_BANK_STORAGE_OFFSET + PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big");
1615

1716
// Size of one bank
1817
#define PICO_FLASH_BANK_SIZE (PICO_FLASH_BANK_TOTAL_SIZE / 2)
@@ -48,12 +47,27 @@ static void pico_flash_bank_perform_flash_mutation_operation(void *param) {
4847
}
4948
}
5049

50+
#ifndef pico_flash_bank_get_storage_offset_func
51+
static inline uint32_t pico_flash_bank_get_fixed_storage_offset(void) {
52+
static_assert(PICO_FLASH_BANK_STORAGE_OFFSET + PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big");
53+
#ifndef NDEBUG
54+
// Check we're not overlapping the binary in flash
55+
extern char __flash_binary_end;
56+
assert(((uintptr_t)&__flash_binary_end - XIP_BASE <= PICO_FLASH_BANK_STORAGE_OFFSET));
57+
#endif
58+
return PICO_FLASH_BANK_STORAGE_OFFSET;
59+
}
60+
#define pico_flash_bank_get_storage_offset_func pico_flash_bank_get_fixed_storage_offset
61+
#else
62+
extern uint32_t pico_flash_bank_get_storage_offset_func(void);
63+
#endif
64+
5165
static void pico_flash_bank_erase(void * context, int bank) {
5266
(void)(context);
5367
DEBUG_PRINT("erase: bank %d\n", bank);
5468
mutation_operation_t mop = {
5569
.op_is_erase = true,
56-
.p0 = PICO_FLASH_BANK_STORAGE_OFFSET + (PICO_FLASH_BANK_SIZE * bank),
70+
.p0 = pico_flash_bank_get_storage_offset_func() + (PICO_FLASH_BANK_SIZE * bank),
5771
};
5872
// todo choice of timeout and check return code... currently we have no way to return an error
5973
// to the caller anyway. flash_safe_execute asserts by default on problem other than timeout,
@@ -75,7 +89,7 @@ static void pico_flash_bank_read(void *context, int bank, uint32_t offset, uint8
7589
if ((offset + size) > PICO_FLASH_BANK_SIZE) return;
7690

7791
// Flash is xip
78-
memcpy(buffer, (void *)(XIP_BASE + PICO_FLASH_BANK_STORAGE_OFFSET + (PICO_FLASH_BANK_SIZE * bank) + offset), size);
92+
memcpy(buffer, (void *)(XIP_BASE + pico_flash_bank_get_storage_offset_func() + (PICO_FLASH_BANK_SIZE * bank) + offset), size);
7993
}
8094

8195
static void pico_flash_bank_write(void * context, int bank, uint32_t offset, const uint8_t *data, uint32_t size) {
@@ -94,7 +108,7 @@ static void pico_flash_bank_write(void * context, int bank, uint32_t offset, con
94108
if (size == 0) return;
95109

96110
// calc bank start position
97-
const uint32_t bank_start_pos = PICO_FLASH_BANK_STORAGE_OFFSET + (PICO_FLASH_BANK_SIZE * bank);
111+
const uint32_t bank_start_pos = pico_flash_bank_get_storage_offset_func() + (PICO_FLASH_BANK_SIZE * bank);
98112

99113
// Calculate first and last page in the bank
100114
const uint32_t first_page = offset / FLASH_PAGE_SIZE;
@@ -159,12 +173,5 @@ static const hal_flash_bank_t pico_flash_bank_instance_obj = {
159173
};
160174

161175
const hal_flash_bank_t *pico_flash_bank_instance(void) {
162-
163-
#ifndef NDEBUG
164-
// Check we're not overlapping the binary in flash
165-
extern char __flash_binary_end;
166-
assert((uintptr_t)&__flash_binary_end - XIP_BASE <= PICO_FLASH_BANK_STORAGE_OFFSET);
167-
#endif
168-
169176
return &pico_flash_bank_instance_obj;
170177
}

0 commit comments

Comments
 (0)