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.

Problem with DELAYUS();

Hello, I'm having problem with DELAYUS(); function...

When i put DELAYUS(200); it works fine, but when I have DELAYUS(delay); and delay is global variabe defined as uint16_t delay=200; i dont get the same delay time

I have read on forum that DELAYUS() works fine from RAM, and my code is in FLASH...

Now i'd like to have my code in FLASH, but I'd like to copy that critical part in RAM... How can I do that???

Do i have just copy my variable "delay" to RAM or i have to move something else???

Can you give me some examples???

Thanks, Regards...

Ivan

  • Hello!

    You can refer to 8787.Copying Compiler Sections From Flash to RAM.pdf (especially p.3.1).

    Regards,

    Igor

  • Hi Ivan,

    That's true, you'll get some lag when running Delayus(); via Flash. Simple way is to increase the value and check for the value that gives you desired delay in flash itself else as Igor has attached the doc; go through it. You can implement your delay routine in RAM and rest in flash.

    Regards,

    Gautam

  • Ivan,

    Ivan Debelec said:

    When i put DELAYUS(200); it works fine, but when I have DELAYUS(delay); and delay is global variabe defined as uint16_t delay=200; i dont get the same delay time

    I think this is because DELAYUS() is a macro, not a function.  It looks like this:

    #define DELAY_US(A)  DSP28x_usDelay(((((long double) A * 1000.0L) / (long double)CPU_RATE) - 9.0L) / 5.0L)

    So, if you code DELAY_US (constant), the compiler evaluates the argument calculations at compile time.  But if you code DELAY_US(variable), the argument calculations must be performed by the C28x at runtime.  You'll notice these are all floating point calculations, including a division.  They will take a long time to execute especially if you are using a fixed-pt. C2000 device (you don't say what device you are using).

    So far as flash vs. RAM execution goes, the delay function in the header files is highly susceptible to the effects of the flash wait-states because it is using a conditional branch construct:

    _DSP28x_usDelay:
         SUB    ACC,#1
         BF     _DSP28x_usDelay,GEQ    ;; Loop if ACC >= 0
         LRETR

    Personally, I prefer to use a RPT (repeat single) construct since then the inner loop of the function is invariant to the flash wait-states (once the function is fetched from flash, it is repeated without incurring wait-state penalties).  If you download appnote SPRA958 there are DelayUs.asm source files for each device that implement this type of delay function.  The functions are tuned for the MHz the device is running at since they are hardcoded with the correct RPT count to perform a 1 uS delay.  Therefore, they will run the same whether you pass a constant or a variable as the delay parameter (the downside of this is that the functions need to be hard-coded for a particular MHz, unlike the macro approach that the header files use).  Note that the functions are NOT exact, but close.  There is call/return overhead and a little bit of one-time incurred overhead in the functions that are not accounted for.  Also, while the inner 1uS loop is essentially invariant to flash vs. RAM, the outer BF loop is not invariant so there will be some slight difference between RAM and flash execution.  One last thing is that the RPT is not interruptible.  That means you could have up to a 1uS latency before the RPT loop finishes and the interrupt can be taken.  The macro approach that the header file delay function uses does not have this issue.

    http://www.ti.com/mcu/docs/litabsmultiplefilelist.tsp?sectionId=96&tabId=1502&literatureNumber=spra958l&docCategoryId=1&familyId=1414

    Regards,

    David 

  • Hello all, thanks for replays...

    David thank you for your explanation... I have found DelayUS function, it is hardcoded for 60MHz that i use... I'm trying to use this function but i have some problems when i compile it...

    I have added 

    extern void DelayUs(Uint16);

    before main and i have copied this file in my project and now i'm getting some strange errors on picture...

    Can you help me with including this function to my project???

    Thanks...

  • Ivan,

    The function name is DelayUs().  The error message is indicating DelayUS() is unresolved.  It looks like you have a captialization error in your code someplace where you called the function.

    Regards,

    David

  • David, 

    thank you so much, i have one function call al DelayUS instead DelayUs...  :)

    Tomorow i will try to see if it will be fine for my application, and how much will FLASH have influence on that delay...

    Regards..