From c0ff98614a09cef01a03badafb0269488f46fc32 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Thu, 18 Oct 2018 16:18:32 -0500 Subject: [PATCH] Disable the MPU when flashing When programming flash using the FlashIAP API allow execution from ram. Many devices require flashing to be done from RAM. Also allow execution from ram when running the low level flash tests. --- .../mbed_hal/flash/functional_tests/main.cpp | 12 ++++++- drivers/FlashIAP.cpp | 34 ++++++++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/TESTS/mbed_hal/flash/functional_tests/main.cpp b/TESTS/mbed_hal/flash/functional_tests/main.cpp index aec1ffea81..46d99065b0 100644 --- a/TESTS/mbed_hal/flash/functional_tests/main.cpp +++ b/TESTS/mbed_hal/flash/functional_tests/main.cpp @@ -21,6 +21,7 @@ #include "utest/utest.h" #include "unity/unity.h" #include "greentea-client/test_env.h" +#include "platform/mbed_mpu_mgmt.h" #include "mbed.h" #include "flash_api.h" @@ -251,11 +252,20 @@ Case cases[] = { utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + mbed_mpu_manager_lock_mem_xn(); + GREENTEA_SETUP(20, "default_auto"); return greentea_test_setup_handler(number_of_cases); } -Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); +void greentea_test_teardown(const size_t passed, const size_t failed, const failure_t failure) +{ + mbed_mpu_manager_unlock_mem_xn(); + + greentea_test_teardown_handler(passed, failed, failure); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown); int main() { diff --git a/drivers/FlashIAP.cpp b/drivers/FlashIAP.cpp index b6b1c2377d..a265066ea4 100644 --- a/drivers/FlashIAP.cpp +++ b/drivers/FlashIAP.cpp @@ -25,6 +25,7 @@ #include #include "FlashIAP.h" #include "platform/mbed_assert.h" +#include "platform/MpuXnLock.h" #ifdef DEVICE_FLASH @@ -56,8 +57,11 @@ int FlashIAP::init() { int ret = 0; _mutex->lock(); - if (flash_init(&_flash)) { - ret = -1; + { + MpuXnLock xn; + if (flash_init(&_flash)) { + ret = -1; + } } uint32_t page_size = get_page_size(); _page_buf = new uint8_t[page_size]; @@ -70,8 +74,11 @@ int FlashIAP::deinit() { int ret = 0; _mutex->lock(); - if (flash_free(&_flash)) { - ret = -1; + { + MpuXnLock xn; + if (flash_free(&_flash)) { + ret = -1; + } } delete[] _page_buf; _mutex->unlock(); @@ -83,7 +90,10 @@ int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size) { int32_t ret = -1; _mutex->lock(); - ret = flash_read(&_flash, addr, (uint8_t *) buffer, size); + { + MpuXnLock xn; + ret = flash_read(&_flash, addr, (uint8_t *) buffer, size); + } _mutex->unlock(); return ret; } @@ -126,9 +136,12 @@ int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size) prog_buf = buf; prog_size = chunk; } - if (flash_program_page(&_flash, addr, prog_buf, prog_size)) { - ret = -1; - break; + { + MpuXnLock xn; + if (flash_program_page(&_flash, addr, prog_buf, prog_size)) { + ret = -1; + break; + } } size -= chunk; addr += chunk; @@ -170,7 +183,10 @@ int FlashIAP::erase(uint32_t addr, uint32_t size) int32_t ret = 0; _mutex->lock(); while (size) { - ret = flash_erase_sector(&_flash, addr); + { + MpuXnLock xn; + ret = flash_erase_sector(&_flash, addr); + } if (ret != 0) { ret = -1; break;