• AVR Freaks

AnsweredHot!Harmony 3 System Timer and Interrupts

Author
jdeguire
Super Member
  • Total Posts : 592
  • Reward points : 0
  • Joined: 2012/01/13 07:48:44
  • Location: United States
  • Status: offline
2019/06/19 13:36:44 (permalink)
0

Harmony 3 System Timer and Interrupts

In the process of evaluating Harmony 3, I have converted our free-running timer code that previously accessed the MIPS Core Timer directly over to using the Harmony 3 Timer System Service instead (in Harmony 3 at core/system/time/...).  I replaced our Core Timer interrupt, which accessed the CP0 Count register, with a callback that is given to SYS_TIME_CallbackRegisterMS().  My callback function gets the software counter using SYS_TIME_CounterGet() (it does other things with the raw counter value), which worked okay until I also started using other code that called that function from a non-interrupt context.  What would I would see is the counter value jump by extremely large amounts.
 
(The eventual plan is to ditch our free-running timer and use the H3 Timer service directly--or probably through a thin C++ wrapper--but I just wanted to get something up and running for now.  This let me find issues like this one!)
 
To cut a long story short, what was happening is I was essentially getting an underflow condition that was causing the counter to increment by (1<<32 - some_small_number).  To make the story even shorter, I believe I have fixed the issue by modifying the SYS_TIME_PLIBCallback() function to update the 64-bit counter value before calling our callback, like so:
 
static void SYS_TIME_PLIBCallback(uint32_t status, uintptr_t context)
{
    SYS_TIME_COUNTER_OBJ* counterObj = (SYS_TIME_COUNTER_OBJ *)&gSystemCounterObj;
    SYS_TIME_TIMER_OBJ* tmrActive = counterObj->tmrActive;
    uint32_t elapsedCount = 0;
    bool interruptState;

    counterObj->hwTimerCurrentValue = counterObj->timePlib->timerCounterGet();

    elapsedCount = SYS_TIME_GetElapsedCount(counterObj->hwTimerCurrentValue);

    SYS_TIME_Counter64Update(elapsedCount); // MOVED HERE...

    if (tmrActive != NULL)
    {
        counterObj->interruptNestingCount++;

        SYS_TIME_UpdateTime(elapsedCount);

        counterObj->interruptNestingCount--;
    }
// SYS_TIME_Counter64Update(elapsedCount); // ...FROM HERE

    interruptState = SYS_INT_Disable();
    SYS_TIME_HwTimerCompareUpdate();
    SYS_INT_Restore(interruptState);
}

 
Looking through the other functions called here, I don't believe they are adversely affected by this modification.
 
On another note, am I correct in thinking that these functions should NOT be called from interrupts other than our timer callbacks we register?  I ask because it looks like doing so can cause your interrupt to deadlock inside the SYS_TIME_ResourceLock() function or return a useless value if it doesn't (this depends on what OSAL layer you're using).  We probably could wrap all of our SYS_TIME calls in interrupt disables if we really needed to, but I think it'll be easier to just avoid using these in interrupts if we really shouldn't.
 
#1
jdeguire
Super Member
  • Total Posts : 592
  • Reward points : 0
  • Joined: 2012/01/13 07:48:44
  • Location: United States
  • Status: offline
Re: Harmony 3 System Timer and Interrupts 2019/12/13 08:23:15 (permalink)
0
I'm bumping this old thread just to point out that this is still a problem in Harmony 3 as of the time of this writing.
#2
vgandhi
Senior Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2016/10/24 21:28:35
  • Location: 0
  • Status: offline
Re: Harmony 3 System Timer and Interrupts 2019/12/18 01:42:10 (permalink) ☼ Best Answerby jdeguire 2019/12/18 11:08:28
5 (1)
Thank you for reporting the issue. A ticket has been created and the issue will be fixed in the next release. 
#3
jdeguire
Super Member
  • Total Posts : 592
  • Reward points : 0
  • Joined: 2012/01/13 07:48:44
  • Location: United States
  • Status: offline
Re: Harmony 3 System Timer and Interrupts 2019/12/18 11:09:46 (permalink)
0
Thanks for the response!  I'm glad this will be getting addressed.
#4
Jump to:
© 2020 APG vNext Commercial Version 4.5