S32K312 RTD 7.0.1 – Review of relocating C40 Flash erase/write execution stack to ITCM Hello NXP Team, I am working on the C40 Flash driver integration for S32K312 using RTD 7.0.1, S32 Design Studio 3.6.6 and GCC 10.2. I encountered a HardFault during Flash erase/write operations, particularly when changing compiler optimization levels. 1. Issue observed Initially, I relocated the C40-related functions to SRAM using a RAM code section. The implementation worked with -O0, but when I changed the compiler optimization to -O3 or -Os, some of the functions that were expected to execute from RAM were appearing in Flash again, according to the linker map. This resulted in HardFaults during Flash operations, presumably due to instruction fetches from the Flash block that was being operated on. To address this, I decided to move the complete C40 erase/write execution path to ITCM instead of SRAM. 2. Changes made in system.h I modified the Sys_GetCoreID() declaration in system.h as follows: #define PLATFORM_STOP_SEC_CODE #include "Platform_MemMap.h" /* Place Sys_GetCoreID in the ITCM code section. */ HAL_C40_RAMCODE_NO_INLINE uint8 Sys_GetCoreID(void); #define PLATFORM_START_SEC_CODE #include "Platform_MemMap.h" The intention is to ensure that Sys_GetCoreID() executes from ITCM when called from the C40 Flash access sequence. 3. Changes made in SchM_Mem_43_INFLS I defined the following macro: #define HAL_C40_RAMCODE_NO_INLINE \ __attribute__((section(".itcm_text"), noinline)) I then applied this attribute to the exclusive-area functions that are involved in the C40 erase/write execution path. For example, in the declarations within SchM_Mem_43_INFLS.c: #define RTE_STOP_SEC_CODE #include "Rte_MemMap.h" /* Place selected SchM functions in ITCM. */ extern void SchM_Enter_Mem_43_INFLS_MEM_EXCLUSIVE_AREA_10(void) HAL_C40_RAMCODE_NO_INLINE; extern void SchM_Exit_Mem_43_INFLS_MEM_EXCLUSIVE_AREA_10(void) HAL_C40_RAMCODE_NO_INLINE; extern void SchM_Enter_Mem_43_INFLS_MEM_EXCLUSIVE_AREA_12(void) HAL_C40_RAMCODE_NO_INLINE; extern void SchM_Exit_Mem_43_INFLS_MEM_EXCLUSIVE_AREA_12(void) HAL_C40_RAMCODE_NO_INLINE; #define RTE_START_SEC_CODE #include "Rte_MemMap.h" The other exclusive-area functions remain in the normal code section. I have applied the corresponding placement attribute to the actual function implementations as well. 4. C40 APIs relocated to ITCM I have also placed the following C40 IP functions in the .itcm_text section: Sys_GetCoreID() C40_Ip_Compare() C40_Ip_MainInterfaceHVJobStatus() C40_Ip_MainInterfaceSectorErase() C40_Ip_MainInterfaceSectorEraseStatus() C40_Ip_MainInterfaceWrite() C40_Ip_MainInterfaceWriteStatus() The relevant SchM exclusive-area functions used in the erase/write execution path. The objective is to ensure that the complete execution path required during Flash programming and erase does not depend on instruction fetches from the Flash block currently undergoing an operation. 5. Current observation With these changes, the implementation appears to work as expected. I have tested the Flash erase/write operations with different compiler optimization levels, including: -O0 -O3 -Os The previously observed optimization-dependent HardFault has not occurred in my testing so far. My main concern is ensuring that the implementation is reliable across optimization levels and that no hidden Flash-resident dependency can cause a read-while-write access or HardFault. I have tested the current implementation successfully, but I would like to confirm that the approach is technically correct and follows NXP's recommended practice. Re: S32K312 RTD 7.0.1 – Review of relocating C40 Flash erase/write execution stack to ITCM Hi@Yogs
I have reviewed your questions and would like to address them separately.
1. Why do some APIs originally placed in SRAM execute from Flash after changing the compiler optimization level?
This behavior may be related to compiler optimization, particularly function inlining.
At -O0, the compiler generally preserves a separate function call.
However, at higher optimization levels such as -O2 or -Os, the compiler may inline the function into its caller.
If the caller is located in Flash, the inlined code will also execute from Flash, even if the original function was assigned to the .ramcode section.
I noticed that your code already uses the following attribute:
__attribute__((section(".ramcode"), noinline))
The “noinline” attribute should normally prevent this behavior.
you are perfectly free to use the same modification method to confirm it.
2. Why does the issue not occur when the function is placed in ITCM?
In the modified code, the function placed in ITCM uses the “noinline” attribute.
This may prevent the compiler from inlining the function into a caller located in Flash, allowing the function to remain as an independent function in the intended ITCM section.
Therefore, the difference may not be caused by ITCM itself, but by the use and effective application of the “noinline” attribute in the modified implementation.
View full article