# AVR32: uxTaskGetStackHighWaterMark

**URL:** <https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253>\
**Category:** Kernel\
**Created:** [April 8, 2010, 10:36am UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253 "2010-04-08T10:36:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 8, 2010, 10:36am UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/1 "2010-04-08T10:36:34Z")

</div>

**[karsten\_klein](https://sourceforge.net/u/karsten_klein/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#abec):

Hi,  
with the function uxTaskGetStackHighWaterMark(NULL), I wanted to check the stack of each task running in the system.  
Therefore I called this function an the end of each task (there are 3 tasks running, with different priorities).  
Unfortunately it doesnt work. The system crashes immediatedly with an exception (\_handle\_Bus\_Error\_Instruction\_Fetch).  
In the function uxTaskGetStackHighWaterMark() and usTaskCheckFreeStackSpace() I cant see why this should not work.

Any suggestions why this fails ?

Best regards  
Karsten

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 8, 2010, 11:30am UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/2 "2010-04-08T11:30:28Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#c550):

Can you step into the function to see where the error occurs?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 8, 2010, 11:45am UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/3 "2010-04-08T11:45:10Z")

</div>

**[karsten\_klein](https://sourceforge.net/u/karsten_klein/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#86ca):

No, not really. I tried already, but by stepping through the function, it seams to work.  
I think it has something to with the taskwitch.  
If a task is just inside this function, and a task&nbsp; with higher priority suspends the currently running task, than the exception could happen (but I am not sure).  
Regards  
Karsten

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 8, 2010, 3:56pm UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/4 "2010-04-08T15:56:19Z")

</div>

**[destremps](https://sourceforge.net/u/destremps/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#5313):

As an experiment surround your stack checking work with vTaskSuspendAll / xTaskResumeAll.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 8, 2010, 6:20pm UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/5 "2010-04-08T18:20:27Z")

</div>

**[karsten\_klein](https://sourceforge.net/u/karsten_klein/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#b2e7):

Yes I think it would work (I will try it tomorrow morning) because the function vTaskList() works as well and&nbsp; does it in the same way.  
But the intention was NOT to suspend all the tasks. I wanted to do the stack checking at the end of each currently running task, in order not to disturb (suspend, block) the other tasks.  
But I cant see any reason in the source-code of the function uxTaskGetStackHighWaterMark(NULL) which could be responsable for the exception, even if a task with higher priority suspends the active task which is just inside the uxTaskGetStackHighWaterMark(NULL).

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 8, 2010, 6:26pm UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/6 "2010-04-08T18:26:46Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#7598):

I don’t understand where you are attempting to call uxTaskGetStackHighWaterMark().&nbsp; What do you mean ‘at the end of each currently running task’.&nbsp; Do you mean using something like the traceTASK\_SWITCHED\_OUT() macro?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 8, 2010, 6:45pm UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/7 "2010-04-08T18:45:24Z")

</div>

**[karsten\_klein](https://sourceforge.net/u/karsten_klein/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#5154):

Hi Richard,  
thanks for the reply.

Just an example:

void vTaskCode1( void \* pvParameters )  
{  
&nbsp;&nbsp;&nbsp;&nbsp; for( ;; )  
&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vTaskDelay( sometime );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Task code  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ……  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ……  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack\_task1 = uxTaskGetStackHighWaterMark(NULL);  
&nbsp;&nbsp;&nbsp;&nbsp; }  
}  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

void vTaskCode2( void \* pvParameters )  
{  
&nbsp;&nbsp;&nbsp;&nbsp; for( ;; )  
&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vTaskDelay( sometime );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Task code  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ……  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ……  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack\_task2 = uxTaskGetStackHighWaterMark(NULL);  
&nbsp;&nbsp;&nbsp;&nbsp; }  
}

One task has a higher priority than the other.

Best regards  
Karsten

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 8, 2010, 7:50pm UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/8 "2010-04-08T19:50:28Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, April 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#0e45):

Hmm, I cannot see anything wrong there.&nbsp; Did you try the suggestion of placing the call inside a critical section?&nbsp; I don’t think it should need it, but it would be interesting if it cured the problem.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 9, 2010, 8:42am UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/9 "2010-04-09T08:42:19Z")

</div>

**[karsten\_klein](https://sourceforge.net/u/karsten_klein/)** wrote on [Friday, April 09, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#7e6c):

Hi Richard,  
I tried the following for each task.  
portENTER\_CRITICAL();  
&nbsp;&nbsp;&nbsp; stack\_task\_1= uxTaskGetStackHighWaterMark(xHandleApplicationTask);  
portEXIT\_CRITICAL();

Afterwards the exception error DID NOT occur, but the program runs not&nbsp; very well, respectively it runs very slow.  
-Task 1 normally works every 10ms | usStackDepth = 4000 (16kB) | lower priority  
-Task 2 normally works every 1ms | usStackDepth = 256 (1kB) | higher priority

I measured the time for&nbsp; the uxTaskGetStackHighWaterMark(NULL) call:  
Inside Task1: about 10ms !!  
Inside Task2: about 0,6ms

- During the runtime of uxTaskGetStackHighWaterMark(NULL) in Task1,&nbsp; Task2 is blocked.  
- The active-time of Task1 is as double as normal.  
==\> This result in the loss of speed, which I can observe.

So, without portENTER\_CRITICAL()/EXIT the Task1 will be suspended by Task2, definitely during the execution of uxTaskGetStackHighWaterMark(NULL). Task1 then starts uxTaskGetStackHighWaterMark(NULL) again.

But why is this a problem which results in an exception?  
Each call of uxTaskGetStackHighWaterMark(NULL) should do its job by itself, without affecting the other running uxTaskGetStackHighWaterMark(NULL) , isnt it ?  
It is something like reentrancy ?

I know now that I cant solve the stackchecking in this way, but I want to understand the reason for the exception.

Best regard  
Karsten

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 9, 2010, 8:52am UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/10 "2010-04-09T08:52:05Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, April 09, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#3f5a):

I would have to run the code to understand what is causing the problem.&nbsp; Just by looking at the code I would say there should not be a reentrancy problem, but there is obviously something more subtle going on.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 9, 2010, 9:06am UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/11 "2010-04-09T09:06:33Z")

</div>

**[karsten\_klein](https://sourceforge.net/u/karsten_klein/)** wrote on [Friday, April 09, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#667d):

Do you need more information from my side to investigate the problem ?  
To send you the whole project does not make sense because you would need the hardware as well to get something to run.  
Regards  
Karsten

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 15, 2010, 10:11pm UTC](https://forums.freertos.org/t/avr32-uxtaskgetstackhighwatermark/3253/12 "2010-04-15T22:11:08Z")

</div>

**[chrismy](https://sourceforge.net/u/chrismy/)** wrote on [Thursday, April 15, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/94bff8b0/#3be8):

Hi Karsten,

Did you resolve your problem?&nbsp; I only ask as I have had a bus\_error\_data\_fetch exception problem with the AVR32’s.&nbsp; I would be very interested to know how you fixed your exception problem.&nbsp; In my case it seemed to occur around the time when a context was being switched, but was intermittent and sometimes very hard to make it fail.&nbsp; In all cases the exception (although a data fetch, not instruction fetch) would occur somewhere within one of the “C” libraries.&nbsp; I believe my problem (although I’m not yet fully convinced) was to do with the “C” libraries not being re-entrant.&nbsp; I’ve created a struct \_reent for each task and I point the “C” library’s \_impure\_ptr at the appropriate one each time a context switch is done.&nbsp; I have not seen an the exception since I’d made these changes.&nbsp; I can send you more details if that helps.

Cheers,  
Chris.
