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.

How can I know the running context? (Am I in a swi or task or idle?)

Hi, I am writing something to record error message into flash.  I will send simple error message to a task which will send message to flash through spi. Other engineers should be able to use my function to commit error message to record. But I got a problem with the interface. If it is commit from swis, it should not wait, but copy msg into my buff, and just return, so it won't block the flash task. If it is commit from other tasks, I allow them to use big message so just pass pointer of msg to me and block until it is done. If it is commit from idle, then it should not block at all (I heard that pend or sleep in idle will crash the program). So when my interface func is called, is there anyway I can tell this is called form swi, task or idle? I ask other engineers to give a parameter whether it is from swi, task or idle and got challenged by other engineers saying they don't know who will call their functions either. Any help?

  • BIOS_getThreadType() gets you most of the way there. It returns one of the following:

        enum ThreadType {
            ThreadType_Hwi,         /*! Current thread is a Hwi */
            ThreadType_Swi,         /*! Current thread is a Swi */
            ThreadType_Task,        /*! Current thread is a Task */
            ThreadType_Main         /*! Current thread is Boot/Main */
        };

     Idle is a little trickier. Once you know it is a task, you can use the following to determine if it is Idle

        if (Task_getPri(Task_self) == 0) { // Idle has priority zero
            //  it's Idle....
        }

    Note: I think your code should do this determination and not force the calling code to do it.

    Todd