This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Interrupt Service Routine (ISR) and os_mut_wait()

When ISR calls os_mut_wait() the processor generates an exception.

Is os_mut_wait() prohibited from being called from an ISR?

Parents
  • Then, what is a good way to share memory between the ISR and and lets say a get funtion?

    There are two approaches that would work great. The first is a mutex mechanism. The second is turning off the interrupt source. In this case disabling the interrupt source would be the best because I don't know how os_mut_wait() is written. Is it written to disable interrupts?

    The first example:

    ///< Global Counter
    I32U Counter = 0;
    ///< Mutex used between ISR and Get function
    OS_MUT mutex;
    
    
    /**************************
    * Initializes Values
    **************************/
    void Init(void)
    {
            os_mut_init(mutex);
    }
    
    /**************************
    * Returns Counter Value
    **************************/
    I32U GetCounter(void)
    {
            I32U value;
    
            // Lock the mutex
            os_mut_wait(mutex);
            // Now, Get the value
            value = Counter;
            // Release the mutex
            os_mut_release();
    
            return value;
    }
    
    /**************************
    * Returns Counter Value
    **************************/
    void TimerISR(void)
    {
            // Lock the mutex
            os_mut_wait(mutex);
    
            // Now, change the value
            ++Counter;
    
            // Release the mutex
            os_mut_release();
    }
    

    The second example

    ///< Global Counter
    I32U Counter = 0;
    
    /**************************
    * Returns Counter Value
    **************************/
    I32U GetCounter(void)
    {
            I32U value;
    
            // Disable the timer
             TIMER = DISABLE;
    
            // Now, Get the value
            value = Counter;
    
            // Enable the timer again
            TIMER = ENABLE;
    
            return value;
    }
    
    /**************************
    * Returns Counter Value
    **************************/
    void TimerISR(void)
    {
            // Now, change the value
            ++Counter;
    }
    

Reply
  • Then, what is a good way to share memory between the ISR and and lets say a get funtion?

    There are two approaches that would work great. The first is a mutex mechanism. The second is turning off the interrupt source. In this case disabling the interrupt source would be the best because I don't know how os_mut_wait() is written. Is it written to disable interrupts?

    The first example:

    ///< Global Counter
    I32U Counter = 0;
    ///< Mutex used between ISR and Get function
    OS_MUT mutex;
    
    
    /**************************
    * Initializes Values
    **************************/
    void Init(void)
    {
            os_mut_init(mutex);
    }
    
    /**************************
    * Returns Counter Value
    **************************/
    I32U GetCounter(void)
    {
            I32U value;
    
            // Lock the mutex
            os_mut_wait(mutex);
            // Now, Get the value
            value = Counter;
            // Release the mutex
            os_mut_release();
    
            return value;
    }
    
    /**************************
    * Returns Counter Value
    **************************/
    void TimerISR(void)
    {
            // Lock the mutex
            os_mut_wait(mutex);
    
            // Now, change the value
            ++Counter;
    
            // Release the mutex
            os_mut_release();
    }
    

    The second example

    ///< Global Counter
    I32U Counter = 0;
    
    /**************************
    * Returns Counter Value
    **************************/
    I32U GetCounter(void)
    {
            I32U value;
    
            // Disable the timer
             TIMER = DISABLE;
    
            // Now, Get the value
            value = Counter;
    
            // Enable the timer again
            TIMER = ENABLE;
    
            return value;
    }
    
    /**************************
    * Returns Counter Value
    **************************/
    void TimerISR(void)
    {
            // Now, change the value
            ++Counter;
    }
    

Children