FreeRTOS CORTEX_STM32F103_IAR demo

fosfolipid wrote on Thursday, July 25, 2013:

hi,
this is default implementation for tickless idle mode in demo:

/* Enter a critical section but don't use the taskENTER_CRITICAL()
		method as that will mask interrupts that should exit sleep mode. */
		__disable_interrupt();
		/* If a context switch is pending or a task is waiting for the scheduler
		to be unsuspended then abandon the low power entry. */
		if( eTaskConfirmSleepModeStatus() == eAbortSleep )
		{
                        /* Restart SysTick. */
			portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
			/* Re-enable interrupts - see comments above __disable_interrupt()
			call above. */
			__enable_interrupt();
		}
		else
		{
			/* Set the new reload value. */
			portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
			/* Clear the SysTick count flag and set the count value back to
			zero. */
			portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
			/* Restart SysTick. */
			portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
			/* Sleep until something happens.  configPRE_SLEEP_PROCESSING() can
			set its parameter to 0 to indicate that its implementation contains
			its own wait for interrupt or wait for event instruction, and so wfi
			should not be executed again.  However, the original expected idle
			time variable must remain unmodified, so a copy is taken. */
			xModifiableIdleTime = xExpectedIdleTime;
			configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
			if( xModifiableIdleTime > 0 )
			{
				__WFI();
				__DSB();
				__ISB();
			}
			configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
			/* Stop SysTick.  Again, the time the SysTick is stopped for is
			accounted for as best it can be, but using the tickless mode will
			inevitably result in some tiny drift of the time maintained by the
			kernel with respect to calendar time. */
			portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT;
			/* Re-enable interrupts - see comments above __disable_interrupt()
			call above. */
			__enable_interrupt();

I’m a little bit confused when I try to understand it. Before the first ‘if’ statement there is ‘__disable_interrupt()’. During I was debugging program, I noticed that this function set PRIMASK to 1, so it prevents the activation of all exceptions with configurable priority. Let’s assume that condition eTaskConfirmSleepModeStatus() == eAbortSleep isn’t met - so MCU is forced to sleep because of __WFI(). And my question is - why does MCU wake up? SysTick interrupt has configurable priority so I think it should by masked by setting PRIMASK to 1, am I wrong? I’m reading documentation from ST, from ARM and from FreeRTOS but I can’t come to terms

rtel wrote on Thursday, July 25, 2013:

First - the implementation you have posted has changed a little to remove the requirement to reset the reload value in the tick handler.  Please take the implementation out of FreeRTOS V7.5.2.

To answer your question:  If interrupts are globally disable by __diable_interrupt() when WFI is called then an interrupt will still bring the MCU out of sleep mode, but the interrupt will not actually be handled until interrupts are globally enabled again.

If on the other hand interrupts are masked in BASEPRI (as they would be if taskENTER_CRITICAL() or taskDISABLE_INTERRUPTS() was called) then only interrupts that have a priority above that set in BASEPRI are able to bring the MCU out of sleep mode.

Regards.

fosfolipid wrote on Thursday, July 25, 2013:

Many thanks! It explained me a lot