non-uniform ticks

bertino wrote on Tuesday, March 18, 2014:

Hi,

I’m running FreeRTOS v7.6.0 on the cortex-M3 based STM32L151xB micro.
In my FreeRTOSConfig.h i have :


#define configTICK_RATE_HZ				( ( portTickType ) 1000 )

which makes me assume I have a tick triggering every millisecond.
Just to evaluate some application timing issues in relation to the tick I added GPIO pin toggling within the xPortSysTickHandler() to watch ticks on the oscilloscope, therefore in port.c I have :


void xPortSysTickHandler( void )
{
	/* The SysTick runs at the lowest interrupt priority, so when this interrupt
	executes all interrupts must be unmasked.  There is therefore no need to
	save and then restore the interrupt mask value as its value is already
	known. */
#ifdef ST_DEBUG
  GPIO_HIGH(GPIOC,GPIO_Pin_14);
#endif
	( void ) portSET_INTERRUPT_MASK_FROM_ISR();
	{
		/* Increment the RTOS tick. */
		if( xTaskIncrementTick() != pdFALSE )
		{
			/* A context switch is required.  Context switching is performed in
			the PendSV interrupt.  Pend the PendSV interrupt. */
			portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
		}
	}
	portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
#ifdef ST_DEBUG
  GPIO_LOW(GPIOC,GPIO_Pin_14);
#endif
}

my debug code added under ST_DEBUG defines.
Using an oscilloscope I can see the xPortSysTickHandler() pulses sent on GPIO C, pin 14 , they all have a duration between 3 and 5 microseconds. Here are the issues :

1)I was expecting ticks pulses to be uniformly distributed at regular 1ms intervals with at most some jitter here and there, but the situation I see is slightly different. I have groups of few ticks interleaved by 1ms as expected and then some gaps 5 or 6 ms long in between groups; I was initially thinking that those long gaps were due to higher priority interrupt service routines but things didn’t change after disabling them. I was just wondering if there is something else I’m missing, can be some macro/service that needs to be disabled within FreeRTOSConfig.h ? What are your thoughts on this ?

2)Minor point : returning to the code snippet I pasted above, if my understanding is right, the task context switch is done once within xPortSysTickHandler() we run the following statement :

portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;

which, as the comment suggests will cause the PendSV interrupt handler (as specified in portasm.s) to start running. So in general in this case the tick pulse duration measured as explained above might include the task switching time and pendSV interrupt time entry as well right ?

Thanks,
Adalberto

rtel wrote on Tuesday, March 18, 2014:

The systick and pendsv handlers run at the same priority, so one will run after the other - using the ARM Cortex-M tail chaining mechanism (so registers are not unstacked and then stacked again by the hardware in between the two interrupts).

Can you run the same experiment with no application tasks running at all - so only the idle and possible the timer (depending on the configUSE_TIMERS setting) will be running.

Please also post the GPIO_HIGH and GPIO_LOW code. Please surround any code you post in [pre] ... [/pre] tags (BUT USING ANGLE BRACKETS INSTEAD OF SQUARE BRACKETS) as this forum software extremely unhelpfully interprets C syntax as formatting tags. I have already edited your original post to do this.

Regards.

bertino wrote on Tuesday, March 18, 2014:

Ok,

so in order to follow the suggestions I removed createTask statements and main becomes :


int main(void)
{        
    Main_HW_Init();
    		
    /* Start the scheduler. */
    vTaskStartScheduler();

    /* we should never get here */
    for( ;; );
}

At this point Main_HW_Init() is reduced to very basic initializations :


void Main_HW_Init()
{    
    RCC_Configuration();
    
    /* Init I/O ports */
    Init_GPIOs ();

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
}

I still have the same tick gaps when I run after this.
Whether or not worth to mention I have some code in the vApplicationTickHook(…) that toggles a “lifeness” led every 1000 tick counts using the same GPIO_HIGH/LOW commands to drive a led as used on the debug pin.
Finally as you requested the GPIO high/low command definition:


#define GPIO_HIGH(a,b)          a->BSRRL = b
#define GPIO_LOW(a,b)           a->BSRRH = b

//plus defines below from stm32l1 libraries provided by default :

//from stm32l1xx.h :
#define GPIOC               ((GPIO_TypeDef *) GPIOC_BASE)

//from stm32l1xx_gpio.h :
#define GPIO_Pin_14                ((uint16_t)0x4000)  /*!< Pin 14 selected */

Please let me know if I’m still missing something.
Thanks,
Adalberto

rtel wrote on Tuesday, March 18, 2014:

As the IO functions are direct hardware access macros, and nothing else is running that could potentially be disabling interrupts, assuming the clock to the CPU is stable I cannot say why you get the results you do. I would expect to see very regular pulses.

Could it be the resolution of your scope? Try setting the pin high on one tick, then low on the next tick, instead of high then low within the same tick period.

Do you have configUSE_TICKLESS_IDLE defined? If so, is it defined to 0? (it will default to 0 if it is not defined).

Regards.

bertino wrote on Tuesday, March 18, 2014:

Hi,

due to other needs the measurement equipment on my desk was temporarily downgraded to an old scope on which I found out some issues with the record lenght. So yes, you were right, the issue was scope resolution (sorry).

Thanks a lot for keeping me going forward.
Regards,
Adalberto