Hi @曾经怀念
MPU is a module which affects the cache attributes. If you want to make some memory area cache inhibited or cacheable, this must be done by configuration of MPU.
If the execution time is changed in that way, it’s obviously caused by different cache attributes – default MPU attributes when MPU is disabled vs. attributes configured by your application.
So, you need to take this into consideration.
From my experience, it’s better to keep such hard loops in cacheable memory. Here’s the reason why (simplified):
If some loop is placed to non-cacheable memory, it depends on exact address. If the loop fits within one flash line, it will be read from physical flash (which is slow) only first time then then the code will be fetched from flash read buffer. So, the execution time will be fast. If you shift the code, so the loop will be split to two flash lines, the physical flash needs to be read many times due to buffer miss. This will make the execution significantly slower.
So, anytime you add some code to your project, everything may be shifted and it can affect the hard loops.
If you keep this code in cacheable memory, the instruction cache will eliminate most of such effects.
Another option is to keep such loop at defined address which is non-cacheable. The disadvantage is that you need to have such area defined in linker file.
Regards,
Lukas