[SOLVED] Determine if function is called from Timer or interrupt

  • [SOLVED] Determine if function is called from Timer or interrupt

    Hello.

    I am using EmbOS CortexM GCC V5.02 on an ATSAM4E8C.

    I have a function which I would like to take different code paths if called from a thread or from a timer or interrupt handler.

    Basically my function is normally called from a task, but because it uses a MUTEX, this will fail with OS Error 161 if called from a timer handler or 160 if called from an ISR.

    Is it possible to determine if my function is called from a timer or interrupt handler, so I can take a different code path, avoiding trying to acquire the mutex?

    I would like to do something like the following:

    C Source Code

    1. if (OS_InTimerHandler() || OS_In_ISR()) {
    2. do_something_special();
    3. } else {
    4. if (0==OS_MUTEX_LockTimed(&my_mutex, 100)) {
    5. error_mutex_timeout();
    6. } else {
    7. do_something_that_requires_mutex();
    8. OS_MUTEX_Unlock(&my_mutex);
    9. }
    10. }
    Note: The function also performs a number of other things before and after the mutex, so replacing all calls from within timer and interrupt handlers with a special version is not desirable.

    Best regards
    Mikkel Holm Olsen
  • Hello Mikkel,

    with the most recent embOS version you can use OS_TIMER_GetCurrent() and OS_INT_InInterrupt().
    With older embOS versions the return value of OS_TIMER_GetCurrent() was valid during the execution of a timer callback function only.

    segger.com/doc/UM01001_embOS.html#OS_TIMER_GetCurrent
    segger.com/doc/UM01001_embOS.html#OS_INT_InInterrupt

    Would it be possible for you to update to the latest embOS Cortex-M GCC V5.18.0.1?
    That would be the easiest solution.

    Best regards,
    Til
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.