diff --git a/cores/rp2040/flash_wrapper.cpp b/cores/rp2040/flash_wrapper.cpp new file mode 100644 index 000000000..f447c60b1 --- /dev/null +++ b/cores/rp2040/flash_wrapper.cpp @@ -0,0 +1,75 @@ +/* + Flash wrappers to protect PSRAM access on the RP2350 + + Copyright (c) 2024 Earle F. Philhower, III + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +#ifdef PICO_RP2350 +#include +#endif + +extern "C" { + extern void __real_flash_range_erase(uint32_t flash_offs, size_t count); + void __wrap_flash_range_erase(uint32_t flash_offs, size_t count) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_range_erase(flash_offs, count); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } + + extern void __real_flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count); + void __wrap_flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_range_program(flash_offs, data, count); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } + + extern void __real_flash_get_unique_id(uint8_t *id_out); + void __wrap_flash_get_unique_id(uint8_t *id_out) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_get_unique_id(id_out); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } + + extern void __real_flash_do_cmd(const uint8_t *txbuf, uint8_t *rxbuf, size_t count); + void __wrap_flash_do_cmd(const uint8_t *txbuf, uint8_t *rxbuf, size_t count) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_do_cmd(txbuf, rxbuf, count); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } +}; diff --git a/lib/core_wrap.txt b/lib/core_wrap.txt index 0dd4570d9..7532fa301 100644 --- a/lib/core_wrap.txt +++ b/lib/core_wrap.txt @@ -67,3 +67,8 @@ -Wl,--wrap=cyw43_tcpip_link_status -Wl,--wrap=cyw43_cb_tcpip_init -Wl,--wrap=cyw43_cb_tcpip_deinit + +-Wl,--wrap=flash_range_erase +-Wl,--wrap=flash_range_program +-Wl,--wrap=flash_get_unique_id +-Wl,--wrap=flash_do_cmd