PIC32Starter kit_IdleHook_v5.1.0_Newbie

pistole89 wrote on Monday, March 28, 2011:

Hallo Everyone,

I am new in FreeRTOS (version 5.1.0). I used PIC32 starter kit  (PIC32MX360F512L)
C32 Compiler v 1.05. I compile it directly using JTAG in PIC32 Starter kit.

On my first, I made 2 simple tasks to turn on 2 LEDs with different vTaskDelay. and it works.
then I try to utilize vApplicationIdleHook… but until now my program never go to this function.

I have made
#define configUSE_IDLE_HOOK 1

I also have declared this in my main function
void vApplicationTickHook( void );

my function is just simple which only turn on the third LED when it is in idle
void vApplicationTickHook( void )
{
    for( ;; )
{
mPORTDSetBits(BIT_0);    
}
}

I also have looked on RTOS viewer in MPLAB IDE v8.60, that Idle Task is running
However,  The program never go to vApplicationIdleHook function ( I used breakdown point there)
I am really confused.

Any help figuring this out will be appreciated.
Thanks in advance guys :slight_smile:

woops_ wrote on Monday, March 28, 2011:

Do you want an idle hook or a tick hook? You set configUSE_IDLE_HOOK 1, but define a tick hook function. Idle hook functions are called vApplicationIdleHook()

pistole89 wrote on Monday, March 28, 2011:

Hallo woops_

Thanks for your reply.
Sorry I make mistake during write in this forum.
Actually, on my program I use idle hook.
void vApplicationIdleHook( void );

void vApplicationIdleHook( void )
{
      for( ;; )
     {
        mPORTDSetBits(BIT_0);
      }
}

Yes I have set configUSE_IDLE_HOOK 1
and it never goes to the void vApplicationIdleHook( void ) function

Do you have any idea?

thanks woops_

Best regards

rtel wrote on Monday, March 28, 2011:

Can you show the structure of your LED tasks please?

Also, how do you know it is not entering the idle hook function?  I presume because you have put a break point in the function?

Try removing the for( ;; ) loop in the idle hook function so the hook function is continuously entered and exited.  That would make it easier to see if the break point is hit, especially if optimisation is on.

Regards.

pistole89 wrote on Monday, March 28, 2011:

Thanks for your reply.

Here is the program to set up hardware

static void prvSetupHardware( void );

static void prvSetupHardware( void )
{
       //Used for Initialization of IO pins
mPORTDClearBits(BIT_0);  
  mPORTDSetPinsDigitalOut(BIT_0); 
  mPORTDClearBits(BIT_1);    
   mPORTDSetPinsDigitalOut(BIT_1);  
mPORTDClearBits(BIT_2);    
   mPORTDSetPinsDigitalOut(BIT_2);  
/* Set the system and peripheral bus speeds and enable the program cache*/
    SYSTEMConfigPerformance( configCPU_CLOCK_HZ - 1 );
mOSCSetPBDIV( OSC_PB_DIV_2 );

/* Setup to use the external interrupt controller. */
    INTEnableSystemMultiVectoredInt();

portDISABLE_INTERRUPTS();
}

Here are the Tasks

xTaskCreate( prvTestTask1, “Blink RD0”, configMINIMAL_STACK_SIZE, NULL, 2, NULL );
xTaskCreate( prvTestTask2, “Blink RD1”, configMINIMAL_STACK_SIZE, NULL, 2, NULL );

static void prvTestTask1( void *pvParameters )
{
for( ;; )
{
  mPORTDToggleBits(BIT_2);
  vTaskDelay( smallDelay );
  }
}

static void prvTestTask2( void *pvParameters )
{
for( ;; )
{
  mPORTDToggleBits(BIT_1);
  vTaskDelay( mediumDelay );

  }
}

void vApplicationIdleHook( void )
{
mPORTDSetBits(BIT_0);    
}

The connection pin to LED is simple. just pin into anode of LED and resistor 330 Ohm then ground.

Yes, I used Breakpoint to know whether it is executed vApplicationIdleHook.

I also have tried to remove for loop in vApplicationIdleHook, but it is still not working.
It never goes to the fuction.

Thank you very much for your kindness richardbarry. :slight_smile:

rtel wrote on Monday, March 28, 2011:

Well it looks ok, so its a matter of ascertaining why its not working.

Is it possible that smallDelay and/or mediumDelay could be zero?  If so then the idle task would not run.  Are the LEDs visibly toggling at the rate you expect?

The other thing to do would be to place a break point in the idle task directly.  To do that, open up FreeRTOS/source/tasks.c, and search for “static portTASK_FUNCTION( prvIdleTask, pvParameters )” to find the idle task definition.  Put a break point on the call to prvCheckTasksWaitingTermination(); at the top of the idle talk loop.  When you run the code, is it ever hit?  If so then the idle task is getting some CPU time and you can step through it in the debugger to see why the idle hook is not called.  In fact, in the same function, at the bottom of the idle task loop, you can see the call to vApplicationIdleHook() - if you cannot put a break point on that line (with zero optimisation) then it is not getting built in.

Regards.

pistole89 wrote on Monday, March 28, 2011:

Thanks Mr. Richardbarry.

No the delay is not zero, and the LEDs toggling rate is ok.
smallDelay = 2000 ms
mediumDelay = 12000ms.

I have tried it.
it can hit to prvCheckTasksWaitingTermination();

and then I run the debug using Step into, but it never go to vApplicationIdleHook()
It only go to if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists ) ) > ( unsigned portBASE_TYPE ) 1 )
then go back again to prvCheckTasksWaitingTermination();

I also cannot put breakpoint to  vApplicationIdleHook()

Do you know why it is happened ?
Why is it not getting built in ?

Thank you very much for your kindness. :slight_smile:

rtel wrote on Monday, March 28, 2011:

It sounds like it is not seeing the definition of configUSE_IDLE_HOOK.

Where have you defined configUSE_IDLE_HOOK?  It must be in FreeRTOSConfig.h.

Can you check the spelling of configUSE_IDLE_HOOK to make sure it is correct?

Regards.

pistole89 wrote on Monday, March 28, 2011:

#define configUSE_IDLE_HOOK 1
It is in FreeRTOSConfig.h

the spelling is also correct.

static portTASK_FUNCTION( prvIdleTask, pvParameters )
{
/* Stop warnings. */
( void ) pvParameters;

for( ;; )
{
/* See if any tasks have been deleted. */
prvCheckTasksWaitingTermination();

#if ( configUSE_PREEMPTION == 0 )
{
/* If we are not using preemption we keep forcing a task switch to
see if any other task has become available.  If we are using
preemption we don’t need to do this as any task becoming available
will automatically get the processor anyway. */
taskYIELD();
}
#endif

#if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
{
/* When using preemption tasks of equal priority will be
timesliced.  If a task that is sharing the idle priority is ready
to run then the idle task should yield before the end of the
timeslice.

A critical region is not required here as we are just reading from
the list, and an occasional incorrect value will not matter.  If
the ready list at the idle priority contains more than one task
then a task other than the idle task is ready to execute.
*/
if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists ) ) > ( unsigned portBASE_TYPE ) 1 )
{
taskYIELD();
}
}
#endif

#if ( configUSE_IDLE_HOOK == 1 )
{
extern void vApplicationIdleHook( void );

/* Call the user defined function from within the idle task.  This
allows the application designer to add background functionality
without the overhead of a separate task.
NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
CALL A FUNCTION THAT MIGHT BLOCK. */
vApplicationIdleHook();
}
#endif
}
} /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */

That is the function in tasks.c.
In tasks.c , there is include FreeRTOS.h. and inside FreeRTOS.h there is include FreeRTOSConfig.h

Thank you very much for your kindness. :slight_smile:

pistole89 wrote on Tuesday, March 29, 2011:

Hallo Richardbarry

Now I can use vApplicationIdleHook. but it looks very strange.
my previous program is just the same. I only copy the whole file to a new folder.
then change the name of .mcp
on the first, there is warning like below :
WARNING: Boot Flash Write Protect and/or Primary Oscillator Configuration set to safe defaults.

I do not know what happened,
then I closed it and reopened again.
debugger PIC32Starter kit, buid all, and there is no such warning again.
hmm… strange…
and it can go to vApplicationIdleHook.

then I try my previous program again.
There is also same Warning,
I closed, reopened, do the same like before.
There is no such warning anymore.
but It cannot go to vApplicationIdleHook. like before.

I do not get any reasons, still confused. However my new folder with same code works.

Thanks for your help richardbarry :slight_smile: