From 692e542d587145062ac8d90aae91cabb1b649f2e Mon Sep 17 00:00:00 2001 From: Mike Nix Date: Sun, 3 Nov 2019 16:23:36 +0800 Subject: [PATCH] precache() - preload code into the flash cache. (#6628) * precache() - preload code into the flash cache. By preloading code into the flash cache we can take control over when SPI Flash reads will occur when code is executing. This can be useful where the timing of a section of code is extremely critical and we don't want random pauses to pull code in from the SPI flash chip. It can also be useful for code that accesses/uses SPI0 which is connected to the flash chip. Non interrupt handler code that is infrequently called but might otherwise require being in valuable IRAM - such as bit-banging I/O code or some code run at bootup can avoid being permanently in IRAM. Macros are provided to make precaching one or more blocks of code in any function easy. * Fix missing include * Make precache extern "C" * Attempt 2 at making precache extern "C" * Fix calculation of number of cache lines to preload With certain alignments/lengths of code it was possible to not read enough into the flash cache. This commit makes the length calculation clearer and adds an extra cache line to ensure we precache enough code. * Add noinline to PRECACHE_ATTR macro Precached code needs to be noinline to ensure the no-reorder-blocks is applied. --- cores/esp8266/core_esp8266_features.cpp | 50 +++++++++++++++++++++++++ cores/esp8266/core_esp8266_features.h | 22 +++++++++++ 2 files changed, 72 insertions(+) create mode 100644 cores/esp8266/core_esp8266_features.cpp diff --git a/cores/esp8266/core_esp8266_features.cpp b/cores/esp8266/core_esp8266_features.cpp new file mode 100644 index 000000000..03396e500 --- /dev/null +++ b/cores/esp8266/core_esp8266_features.cpp @@ -0,0 +1,50 @@ + +/* + core_esp8266_features.cpp + + Copyright (c) 2019 Mike Nix. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + 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 + +/* precache() + * pre-loads flash data into the flash cache + * if f==0, preloads instructions starting at the address we were called from. + * otherwise preloads flash at the given address. + * All preloads are word aligned. + */ +#ifdef __cplusplus +extern "C" { +#endif + +void precache(void *f, uint32_t bytes) { + // Size of a cache page in bytes. We only need to read one word per + // page (ie 1 word in 8) for this to work. + #define CACHE_PAGE_SIZE 32 + + register uint32_t a0 asm("a0"); + register uint32_t lines = (bytes/CACHE_PAGE_SIZE)+2; + volatile uint32_t *p = (uint32_t*)((f ? (uint32_t)f : a0) & ~0x03); + uint32_t x; + for (uint32_t i=0; i