This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

在RAM运行某个函数的实现的疑问

我在F28027的FOC例程lab_2b中看到这样的语句:

#ifdef FLASH
memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
#endif

这个应该就是把代码从ROM中复制到RAM中的语句,但是我奇怪的是,为什么在网上搜到的一些关于C20x系列芯片的资料里memCopy函数的后面还有一个InitFlash函数?

这里为什么不需要?

  • 这里也需要,可能封装在后面的函数里了。

    他的宏定义#ifdef FLASH,就是代表如果代码在FLASH里跑才执行这段代码。

  • 看了一下memCopy.c文件,未见封装到memCopy中。

    c文件的定义:

    // **************************************************************************
    // the functions

    void memCopy(uint16_t *srcStartAddr,uint16_t *srcEndAddr,uint16_t *dstAddr)
    {

    while(srcStartAddr <= srcEndAddr)
    {
    *dstAddr++ = *srcStartAddr++;
    }

    return;
    } // end of memCopy() function


    // end of file

    这就奇怪了。再往后面的程序的函数,不能把InitFlash函数封装进去呀~?

  • 少打一个字:

    再往后面的程序的函数,不可能把InitFlash函数封装进去呀~?

  • 在这个lab_02b工程中,相关的代码已经被封装到FLASH_setup了,但是没有被调用。

    查了一下资料,似乎InitFlash()函数并不是必须的,它只是优化Flash中程序的运行。

    我这个理解对么?

  • 你的理解不对,如果要在FLASH里跑程序就必须初始化FLAHS,

       EALLOW;
       //Enable Flash Pipeline mode to improve performance
       //of code executed from Flash.
       FlashRegs.FOPT.bit.ENPIPE = 1;

       //                CAUTION
       //Minimum waitstates required for the flash operating
       //at a given CPU rate must be characterized by TI.
       //Refer to the datasheet for the latest information.

       //Set the Paged Waitstate for the Flash
       FlashRegs.FBANKWAIT.bit.PAGEWAIT = 3;

       //Set the Random Waitstate for the Flash
       FlashRegs.FBANKWAIT.bit.RANDWAIT = 3;

       //Set the Waitstate for the OTP
       FlashRegs.FOTPWAIT.bit.OTPWAIT = 5;

       //                CAUTION
       //ONLY THE DEFAULT VALUE FOR THESE 2 REGISTERS SHOULD BE USED
       FlashRegs.FSTDBYWAIT.bit.STDBYWAIT = 0x01FF;
       FlashRegs.FACTIVEWAIT.bit.ACTIVEWAIT = 0x01FF;
       EDIS;

    这是InitFlash的代码,其中有优化也有配置,例如PAGEWAIT和RANDWAIT,就是必须配置的,否则程序都不能运行。因为他们决定了Flash告诉运行时的等待时间。

  • 但是我在instaSPIN-FOC的lab04中根本找不到InitFlash函数,倒是在flash.c中有如下的定义:

    #pragma CODE_SECTION(FLASH_setup, "ramfuncs");
    void FLASH_setup(FLASH_Handle flashHandle)
    {


    //Enable Flash Pipeline mode to improve performance
    //of code executed from Flash.
    FLASH_enablePipelineMode(flashHandle);
    // FlashRegs.FOPT.bit.ENPIPE = 1;

    // CAUTION
    //Minimum waitstates required for the flash operating
    //at a given CPU rate must be characterized by TI.
    //Refer to the datasheet for the latest information.

    #if (CPU_FRQ_50MHZ)
    //Set the Paged Waitstate for the Flash
    FLASH_setNumPagedReadWaitStates(flashHandle, FLASH_NumPagedWaitStates_2);
    // FlashRegs.FBANKWAIT.bit.PAGEWAIT = 2;

    //Set the Random Waitstate for the Flash
    FLASH_setNumRandomReadWaitStates(flashHandle, FLASH_NumRandomWaitStates_2);
    // FlashRegs.FBANKWAIT.bit.RANDWAIT = 2;

    //Set the Waitstate for the OTP
    FLASH_setOtpWaitStates(flashHandle, FLASH_NumOtpWaitStates_2);
    // FlashRegs.FOTPWAIT.bit.OTPWAIT = 2;

    #elif (CPU_FRQ_40MHZ)
    //Set the Paged Waitstate for the Flash
    FLASH_setNumPagedReadWaitStates(flashHandle, FLASH_NumPagedWaitStates_1);
    // FlashRegs.FBANKWAIT.bit.PAGEWAIT = 1;

    //Set the Random Waitstate for the Flash
    FLASH_setNumRandomReadWaitStates(flashHandle, FLASH_NumRandomWaitStates_1);
    // FlashRegs.FBANKWAIT.bit.RANDWAIT = 1;

    //Set the Waitstate for the OTP
    FLASH_setOtpWaitStates(flashHandle, FLASH_NumOtpWaitStates_1);
    // FlashRegs.FOTPWAIT.bit.OTPWAIT = 1;
    #endif
    // CAUTION
    //ONLY THE DEFAULT VALUE FOR THESE 2 REGISTERS SHOULD BE USED
    // FlashRegs.FSTDBYWAIT.bit.STDBYWAIT = 0x01FF;
    FLASH_setStandbyWaitCount(flashHandle, 0x01FF);
    // FlashRegs.FACTIVEWAIT.bit.ACTIVEWAIT = 0x01FF;
    FLASH_setActiveWaitCount(flashHandle, 0x01FF);


    //Force a pipeline flush to ensure that the write to
    //the last register configured occurs before returning.

    asm(" RPT #7 || NOP");

    }

    但是这个函数在这个工程中,根本没有被调用(即使把这个函数的定义删掉,也能正常生成out文件,并能运行)。

    这个讲不通呀!?

  • 我找到设置的地方了

    main()——>HAL_setParams——> HAL_setupFlash()里有:

    FLASH_enablePipelineMode(obj->flashHandle);

    FLASH_setNumPagedReadWaitStates(obj->flashHandle,FLASH_NumPagedWaitStates_2);

    FLASH_setNumRandomReadWaitStates(obj->flashHandle,FLASH_NumRandomWaitStates_2);

    FLASH_setOtpWaitStates(obj->flashHandle,FLASH_NumOtpWaitStates_3);

    FLASH_setStandbyWaitCount(obj->flashHandle,FLASH_STANDBY_WAIT_COUNT_DEFAULT);

    FLASH_setActiveWaitCount(obj->flashHandle,FLASH_ACTIVE_WAIT_COUNT_DEFAULT);

    return;

    但是,从程序上看,此时设置Flash,已经离开始进入main()函数有一段时间,这个过程中,已经执行了很多语句了。

    此时才设置,是不是可以认为:这些Flash优化的设置不是绝对必须的?

    要不之前执行了那么多代码,就不对了~

  • 如果不执行FLASH初始化,会在长时间运行后出现问题,比如等待时间的设置就会导致读出错误的数据,所以必须在初始化的代码中加入。

  • 追问一下,要是在Debug模式下,所有的指令和数据等都直接下载到RAM中运行的话,是不是就不需要InitFlash()了?

  • 另外,是不是只要在Flash中运行程序或使用了Flash中的数据,就要使用InitFlash()函数?

  • 是的,如果程序完全在RAM中运行就不需要了。

  • 只要使用了FLASH的数据就一定是在FLASH中运行程序。

  • 在问一下:把函数搬到RAM中运行,应该只是为了仿真调试吧?

    代码的在RAM中运行速度和在ROM中的运行速度有区别么?

  • 我知道了,是在RAM中的快,但是不知道是为什么?

  • 在MCU跑程序时,只是把实时中断放入RAM,因为发你FLASH中,如果主频超过40M,FLASH读写会有大于一个周期的等待时间。所以才会有RAM copy。

  • 谢谢!明白了~