I wanted a program that demonstrates cache influence. To demonstrate behaviour without caching I put the program into FLASHMEM and disable the instruction cache with a routine I found in CMSIS:core_cm7.h.
The behaviour of the program depends very sensitively on the circumstances. If one line is enabled the loop cycle time is 6.6ns. That can only be explained by caching. In this case the instruction cache seems not to be disabled. If the line is disabled the loop is much slower. That probably is the slow access to FLASHMEM.
What is my fault such that the instruction cache is not disabled und all circumstances?
Code:
#define led 13
void SCB_DisableICache (void){
asm("dsb");
asm("isb");
SCB_CCR &= ~(uint32_t)SCB_CCR_IC ; /* disable I-Cache */
SCB_CACHE_ICIALLU = 0UL; /* invalidate I-Cache */
asm("dsb");
asm("isb");
}
double xx ;
FLASHMEM void setup() {
pinMode(led, OUTPUT);
Serial.begin(115200);
while(!Serial){} ;
Serial.print("Hello ARTICLEteensy40disableIcacheDemo1forumPost...\n");
Serial.printf("setup=%08XH\n",setup) ;
SCB_DisableICache();
// if the following line is deleted a loop cycle needs 130ns, if this line is enabled a loop needs 6.6ns
Serial.printf("SCB_CCR & SCB_CCR_IC)=%08XH\n", SCB_CCR & SCB_CCR_IC ) ;
Serial.printf("wait 5 secs...") ; delay(5000) ;
while(1){
xx=0.0 ;
for(int k=0 ; k<20 ; k++){
CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; // here-1
xx=xx+ 1.0 ;
CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; // here-2
}
Serial.printf("x=%15.10f\n",xx) ;
delay(50) ;
}
}
void loop(){
}
What is my fault such that the instruction cache is not disabled und all circumstances?