Skip to content

Commit

Permalink
Use multicore lockout if required.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterharperuk committed Mar 28, 2023
1 parent 70ea92f commit 01b2a48
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/rp2_common/pico_btstack/btstack_flash_bank.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "pico/btstack_flash_bank.h"
#include "pico/multicore.h"
#include "hardware/flash.h"
#include "hardware/sync.h"
#include <string.h>
Expand All @@ -22,6 +23,31 @@ static_assert(PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_B
#define DEBUG_PRINT(...)
#endif

#ifndef pico_flash_access_guard
#if PICO_FLASH_BANK_USE_MULTICORE_LOCKOUT
static void pico_flash_access_guard(bool start) {
if (start) {
DEBUG_PRINT("flash bank access start\n");
multicore_lockout_start_blocking();
} else {
multicore_lockout_end_blocking();
DEBUG_PRINT("flash bank access end\n");
}
}
#else
static void pico_flash_access_guard(bool start) {
static uint32_t status;
if (start) {
DEBUG_PRINT("flash bank access start\n");
status = save_and_disable_interrupts();
} else {
restore_interrupts(status);
DEBUG_PRINT("flash bank access end\n");
}
}
#endif // PICO_FLASH_BANK_USE_MULTICORE_LOCKOUT
#endif // pico_flash_access_guard

static uint32_t pico_flash_bank_get_size(void * context) {
(void)(context);
return PICO_FLASH_BANK_SIZE;
Expand Down Expand Up @@ -50,9 +76,9 @@ extern uint32_t pico_flash_bank_get_offset_func(void);
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();
pico_flash_access_guard(true);
flash_range_erase(pico_flash_bank_get_offset_func() + (PICO_FLASH_BANK_SIZE * bank), PICO_FLASH_BANK_SIZE);
restore_interrupts(status);
pico_flash_access_guard(false);
}

static void pico_flash_bank_read(void *context, int bank, uint32_t offset, uint8_t *buffer, uint32_t size) {
Expand Down Expand Up @@ -132,9 +158,9 @@ static void pico_flash_bank_write(void * context, int bank, uint32_t offset, con
offset = 0;

// Now program the entire page
uint32_t status = save_and_disable_interrupts();
pico_flash_access_guard(true);
flash_range_program(bank_start_pos + (page * FLASH_PAGE_SIZE), page_data, FLASH_PAGE_SIZE);
restore_interrupts(status);
pico_flash_access_guard(false);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/rp2_common/pico_btstack/include/pico/btstack_flash_bank.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ extern "C" {
#define PICO_FLASH_BANK_STORAGE_OFFSET (PICO_FLASH_SIZE_BYTES - PICO_FLASH_BANK_TOTAL_SIZE)
#endif

// PICO_CONFIG: PICO_FLASH_BANK_USE_MULTICORE_LOCKOUT, True if flash access should be guarded by the multicore lockout or false to just disable interrupts, default=false, group=pico_btstack
#ifndef PICO_FLASH_BANK_USE_MULTICORE_LOCKOUT
#define PICO_FLASH_BANK_USE_MULTICORE_LOCKOUT 1
#endif

/**
* \brief Return the singleton BTstack HAL flash instance, used for non-volatile storage
* \ingroup pico_btstack
Expand Down

0 comments on commit 01b2a48

Please sign in to comment.