Skip to content

Commit 3466a4c

Browse files
Workaround PSRAM cache invalid'm by reading flash
Fixes #2537 While waiting for a working direct cache flush routine, try and force the cache to evict all PSRAM values by reading a bunch of flash addresses (which share the XIP cache). This hurts performance when PSRAM is not used, but is required for correctness until we have a working XIP flush.
1 parent 4aee8be commit 3466a4c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cores/rp2040/flash_wrapper.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,37 @@
1717
License along with this library; if not, write to the Free Software
1818
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1919
*/
20-
20+
#include <Arduino.h>
2121
#include <hardware/flash.h>
2222

2323
#ifdef PICO_RP2350
2424
#include <hardware/structs/qmi.h>
2525
#endif
2626

27+
#if defined(RP2350_PSRAM_CS)
28+
static volatile uint32_t __wastedsum = 0;
29+
static void __no_inline_not_in_flash_func(flushcache)() {
30+
//for (volatile uint8_t* cache = (volatile uint8_t*)0x18000001; cache < (volatile uint8_t*)(0x18000001 + 2048 * 8); cache += 8) {
31+
// *cache = 0;
32+
//}
33+
uint32_t sum = 0; // Ignored, just to ensure not optimized out
34+
for (volatile uint32_t *flash = (volatile uint32_t *)0x11000000; flash < (volatile uint32_t *)(0x11000000 + 48*1024*4); flash++) {
35+
sum += *flash;
36+
}
37+
__wastedsum += sum;
38+
}
39+
#else
40+
static uint32_t flushcache() {
41+
return 0;
42+
}
43+
#endif
44+
2745
extern "C" {
2846
extern void __real_flash_range_erase(uint32_t flash_offs, size_t count);
2947
void __wrap_flash_range_erase(uint32_t flash_offs, size_t count) {
3048
#ifdef PICO_RP2350
3149
auto s = qmi_hw->m[1];
50+
flushcache();
3251
#endif
3352
__real_flash_range_erase(flash_offs, count);
3453
#ifdef PICO_RP2350
@@ -41,6 +60,7 @@ extern "C" {
4160
void __wrap_flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count) {
4261
#ifdef PICO_RP2350
4362
auto s = qmi_hw->m[1];
63+
flushcache();
4464
#endif
4565
__real_flash_range_program(flash_offs, data, count);
4666
#ifdef PICO_RP2350
@@ -53,6 +73,7 @@ extern "C" {
5373
void __wrap_flash_get_unique_id(uint8_t *id_out) {
5474
#ifdef PICO_RP2350
5575
auto s = qmi_hw->m[1];
76+
flushcache();
5677
#endif
5778
__real_flash_get_unique_id(id_out);
5879
#ifdef PICO_RP2350
@@ -65,6 +86,7 @@ extern "C" {
6586
void __wrap_flash_do_cmd(const uint8_t *txbuf, uint8_t *rxbuf, size_t count) {
6687
#ifdef PICO_RP2350
6788
auto s = qmi_hw->m[1];
89+
flushcache();
6890
#endif
6991
__real_flash_do_cmd(txbuf, rxbuf, count);
7092
#ifdef PICO_RP2350

0 commit comments

Comments
 (0)