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

RTX stack overflow check problem

I've now spent a lot of time debugging a problem/issue with the built-in stack overflow check in the RTX OS.

In my project I have a task that receives input from a UART (LPC 2368). The chacacters are read vith getchar(), which in turn calls fgetc, which is a retargetted function under my control. fgetc reads the characters from a SWI-function that I've written, and fgetc loops until something is received. I'm not sure why, but for some reason this bypasses the task switching for this given task, and thus any stack overflow is not detected.

The situation can be remedied by inserting a os_dly_wait-call in the task after getchar has returned a character, since this will allow the task switch mechanism to do it's task checking as usual.

I haven't analysed this fully, but I'd imagine that it may not be what Keil intended. Having a task sitting in a loop calling a (user) SWI-function may not be usual, but it isn't unheard of either.

Regards
-Øyvind

  • When the cpu is in supervisor mode (SWI), tick timer IRQs are blocked.

  • I know, but it is not sitting inside the SWI-function in a loop. If that was the case, nothing would work, and I do have a few other tasks also. Everything runs normally, but stack overflows in this one task is not detected.

    Here's some pseudo-code to show the setup:

    void __task task1(void)
    {
       int c;
    
       for(;;)
       {
          c = getchar();
          /* do_stuff with char */
       }
    }
    
    int getchar(void)
    {
       int ch;
    
       do
       {
          ch = com_get();
       }
       while(ch == EOF);
    
       return ch;
    }
    
    int __SWI_8 com_get(void)
    {
       if(char_available)
          return char;
       else
          return EOF;
    }
    

    As you can see, the task calls getchar, which in turn repeatedly calls the SWI-function com_get until something is received.

    If task1's stack overflows, this is not detected by the mechanisms in RTX. However, as I said, if a call to os_dly_wait (or os_tsk_pass) is inserted in the loop in the task, like this:

    void __task task1(void)
    {
       int c;
    
       for(;;)
       {
          c = getchar();
          os_tsk_pass();
          /* do_stuff with char */
       }
    }
    

    ...then the stack check is performed.

    Regards
    -Oyvind

  • I recommend that you consider the use of a mailbox or event for received data, so your task don't have to busy-wait.