1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

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.
This commit is contained in:
Mike Nix 2019-11-03 16:23:36 +08:00 committed by Develo
parent bf4a4351b6
commit 692e542d58
2 changed files with 72 additions and 0 deletions

View File

@ -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 <stdint.h>
/* 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<lines; i++, p+=CACHE_PAGE_SIZE/sizeof(uint32_t)) x=*p;
(void)x;
}
#ifdef __cplusplus
}
#endif

View File

@ -93,4 +93,26 @@ inline uint32_t esp_get_cycle_count() {
} }
#endif // not CORE_MOCK #endif // not CORE_MOCK
// Tools for preloading code into the flash cache
#define PRECACHE_ATTR __attribute__((optimize("no-reorder-blocks"))) \
__attribute__((noinline))
#define PRECACHE_START(tag) \
precache(NULL,(uint8_t *)&&_precache_end_##tag - (uint8_t*)&&_precache_start_##tag); \
_precache_start_##tag:
#define PRECACHE_END(tag) \
_precache_end_##tag:
#ifdef __cplusplus
extern "C" {
#endif
void precache(void *f, uint32_t bytes);
#ifdef __cplusplus
}
#endif
#endif // CORE_ESP8266_FEATURES_H #endif // CORE_ESP8266_FEATURES_H