AnsweredAssumed Answered

How to use FLEXSPI_SoftwareReset in own _ramfunc without hard fault?

Question asked by Stefan Mitterhauser on Apr 1, 2020
Latest reply on Apr 13, 2020 by Stefan Mitterhauser



I am using IAR with no optimization enabled on a RT1020 evaluation board. I want to call e.g. FLEXSPI_SoftwareReset from fsl_flexspi with XIP enabled in my own code and have trouble with it.


In the example project flexspi_nor_polling_transfer the complete fsl_flexspi.o and flexspi_nor_flash_ops objects  get copied to RAM by icf file settings.

initialize by copy {
  /* Place in RAM flash and performance dependent functions */
  object flexspi_nor_flash_ops.o,
  object fsl_flexspi.o,
  section .textrw

I do not want to place my complete module in RAM that way because it would waste too much RAM. Therefore I use the keyword _ramfunc to mark only functions I want to keep in RAM. If I now call FLEXSPI_SoftwareReset (which gets placed in RAM by "initialize by copy") in my own _ramfunc it generates a hard fault.


I think it has something to do with the definition of the FLEXSPI_SoftwareReset function

static inline void FLEXSPI_SoftwareReset(FLEXSPI_Type *base)
    while (base->MCR0 & FLEXSPI_MCR0_SWRESET_MASK)

With no enabled optimization in IAR the function does not get inlined and I think the linker links the function call to the function placed in flash because if I copy the code inside the function call to my _ramfunc everything works fine.


The part of the map file without optimization looks like this

    845                FLEXSPI_SoftwareReset(psInterfaceData->peripheralBase);
   \       0x70   0x6820             LDR      R0,[R4, #+0]
   \       0x72   0x....'....        BL       FLEXSPI_SoftwareReset

and will result in the HardFault


With optimization it looks like the following

    845                FLEXSPI_SoftwareReset(psInterfaceData->peripheralBase);
   \       0x3E   0x9800             LDR      R0,[SP, #+0]
   \       0x40   0x6801             LDR      R1,[R0, #+0]
   \       0x42   0xF041 0x0101      ORR      R1,R1,#0x1
   \       0x46   0x6001             STR      R1,[R0, #+0]
   \                     ??FlexSpiFlashInit_2: (+1)
   \       0x48   0x6801             LDR      R1,[R0, #+0]
   \       0x4A   0x07C9             LSLS     R1,R1,#+31
   \       0x4C   0xD4FC             BMI.N    ??FlexSpiFlashInit_2

which is the inlined FLEXSPI_SoftwareReset function and there will be no hard fault.


How does NXP suggest to use the function without getting a hardfault and without placing the complete module in RAM via linker file?


Kind regards,