Hi
My previous question: Use FlexSPI while decrypted with OTFAD
Sorry for posting a new thread for my problem, I am doing this as I found there are actually multiple problems stack together with OTFAD.
Right now I suspect that the way I change FlexRam is conflicting with OTFAD.
Here is some observation of my problem
| Result | Modify FlexRam | Calling ITCM Ram Func | Using OTFAD |
| Not Running | Yes | Yes | Yes |
| Running | No | Yes | Yes |
| Running | Yes | No | Yes |
| Running | Yes/No | Yes/No | No |
So I believe FlexRam is the first problem avoiding the RT1011 running encrypted image.
I am using MCUXpresso IDE for development and I use the following codes in ResetISR function to change FlexRam Setting.
__attribute__ ((section(".after_vectors.reset")))
void ResetISR(void) {
// Disable interrupts
__asm volatile ("cpsid i");
/* SP reset */
__asm volatile ("MSR msp, %0" : : "r" (&_vStackTop) : );
__asm volatile ("MSR psp, %0" : : "r" (&_vStackTop) : );
/* config FlexRam */
FLEXRAM->TCM_CTRL = 4;
IOMUXC_GPR->GPR17 = 0xAB;//0xAA;//0x6B;
IOMUXC_GPR->GPR16 |= 0x7;The whole startup.c file is attached.
I use MCUBootUtility to build and flash the OTFAD encrypted image and reset the SP register suggested here.
May I know what is the proper way to change FlexRam when using OTFAD encryption at the same time?
Thanks
P.S.
Update 1:
The ram function can be executed if I add an SDK_DelayAtLeastUs before it.
But OTFAD will still have a problem when the frequency adjustment code is placed in SRAM_ITC instead of non-encrypted flash region.
Update 2:
I create a new project in case there is other stuff in my original project causing the problem.
The new project shows that when the FlexRam is modified and only two peripheral(adc, adc_etc) is inited using MCUXpresso GUI, the program will work with no problem. However, when I added one more peripheral(lpi2c or lpspi), the program will crash.
Update 3:
When the encryption range is 0x60001000-0x60004000, the program based on the new project will work, but if the encryption range is 0x60001000-0x60005000, the program won't work.
Update 4:
By toggling the LED around the code and placing the trap code, I found that for the not working program, they crash at this function in clock_config.c
CLOCK_SetMux(kCLOCK_PeriphMux, 0);
And I found the reply about XIP through OTFAD problems on RT1011, this reply said that it is not working when they place the clock adjustment code in ram? So is placing clock adjustment code in not encrypted flash is the only way to keep OTFAD working? This could be a security problem as hackers may modify that not encrypted region and execute their own code to read the decrypted code. (consider no HAB enabled).
Update 5:
It turns out changing the FlexRam layout is not the actual cause of this problem, when I replace the three lines of code for changing FlexRam with this
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");The program will still crash.
Update 6:
I think one of the functions will have a problem when it is placing at one specific address. When I added three nop assembly in the main function, the chip won't work, but if I remove it, it works again. I attached the working project and not the working project. Please help me to find what causes this problem.
Update 7:
I had tested placing nop assembly according to the image table, and I observe this

Thanks