vTaskDelay doesn't return and Timers not running

tobisf wrote on Wednesday, June 06, 2018:


I’m trying to set up my first project with FreeRTOS. I want to run it on a STM32L0, for the first tests a NUCLEO-L031K6 using Keil µVision 5.

I included the FreeRTOS by adding FreeRTOS/source, FreeRTOS/soruce/include and FreeRTOS/source/portable/RVDS/ARM_CM0 directorys to the include path and the files to the project. I took the FreeTROSConfig.h from an generated project.

To get into FreeRTOS, the first thing I wanted to implement is just one task what does some periodic UART printouts. For creating a delay between the printouts, first I used a simple Loop and it worked. Second I wanted to use vTaskDelay, but it seems that it never returns. After that I wanted to create a Timer to achive a delay between printouts, but the Timer Callback is never called.

Since I don’t took my task to sleep, I thought maybe it’s a priority problem. But the TimerTask is running at priority 6 and my task at priority 2.

Another strange thing was, when I switched from a STM32L011 to a STM32L031 (because of memory issues), I did not got the error that the SysTick_Handler is defined multiple times. So is this maybe a problem that some SysTick in FreeRTOS is not running correctly?

How can I check if everything is running correctly?

Here is my code:


[Auto Generated Config Code]
	/* Start the scheduler so the demo tasks start to execute. */

  /* USER CODE END 2 */

  /* Infinite loop */
  while (1)
[Empty Loop]


#include "UartTask.h"

// STM Includes
#include "stm32l0xx_hal.h"
#include "usart.h"

/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "timers.h"
#include "assert.h"

/* ---------- Timers ---------- */
static TimerHandle_t timerHandle = NULL;
static StaticTimer_t timer;

static volatile bool timerFlag = false;

/* ---------- Task Variables ---------- */

/* The priority at which the task that performs the tests is created. */
/* The size of the stack used by the task that runs the tests. */
#define UART_TASK_STACK_SIZE			(256)
/* StaticTask_t is a publicly accessible structure that has the same size and
alignment requirements as the real TCB structure.  It is provided as a mechanism
for applications to know the size of the TCB (which is dependent on the
architecture and configuration file settings) without breaking the strict data
hiding policy by exposing the real TCB.  This StaticTask_t variable is passed
into the xTaskCreateStatic() function that creates the
prvStaticallyAllocatedCreator() task, and will hold the TCB of the created
tasks. */
static StaticTask_t uartTaskTCBBuffer;
/* This is the stack that will be used by the prvStaticallyAllocatedCreator()
task, which is itself created using statically allocated buffers (so without any
dynamic memory allocation). */
static StackType_t uartTaskStackBuffer[UART_TASK_STACK_SIZE];

/* ---------- Private Function Declarations ----------- */

static void task_function(void *pvParameters);

/* ---------- Callback Declarations ---------- */

static void timerCallback(TimerHandle_t xTimer);

/* ---------- Public Function ---------- */

void UartTask_init(void)
	timerHandle = NULL;
	timerFlag = false;
	timerHandle = xTimerCreateStatic(
								/* Just a text name. Not used by the RTOS kernel. */
								/* The timer period, in ticks. Must be greater than 0. */
                /* The timers will auto-reload themselves when they expire. */
                /* The ID*/
                /* Each timer calls the same callback when it expires. */
                /* Pass in the address of a StaticTimer_t variable, which will hold the data associated with the timer being created. */
	if(timerHandle == NULL)
		_Error_Handler(__FILE__, __LINE__);
	/* Create a single task, which then repeatedly creates and deletes the other
		 RTOS objects using both statically and dynamically allocated RAM. */
	xTaskCreateStatic(task_function,								/* The function that implements the task being created. */
										"UartTask",										/* Text name for the task - not used by the RTOS, its just to assist debugging. */
										UART_TASK_STACK_SIZE,					/* Size of the buffer passed in as the stack - in words, not bytes! */
										NULL,													/* Parameter passed into the task - not used in this case. */
										UART_TASK_PRIORITY,						/* Priority of the task. */
										&(uartTaskStackBuffer[0]),		/* The buffer to use as the task's stack. */
										&uartTaskTCBBuffer );					/* The variable that will hold the task's TCB. */

/* ----------- Private Functions ---------- */

static void task_function(void *pvParameters)
	/* Avoid compiler warnings. */
	volatile int foo = 0;
	if (xTimerStart(timerHandle, 0) == pdFAIL)
		foo++; // Code does not reach here, so xTimerStart does NOT return pdFAIL
	while (true)
		/* ------ With Timer ------ */
		if (timerFlag)
			timerFlag = false;
			HAL_UART_Transmit(&huart2, (uint8_t*)"Timer\n", 6, 1000);
		/* ----- With Delay ------ */
//		HAL_UART_Transmit(&huart2, (uint8_t*)"Delay\n", 6, 1000);
//		vTaskDelay(pdMS_TO_TICKS(1000));
		/* ----- With DelayLoop ----- */
//		HAL_StatusTypeDef status = HAL_UART_Transmit(&huart2, (uint8_t*)"Loop\n", 5, 1000);
//		while (foo < 1000000)
//			foo++;
//		foo = 0;

/* ---------- Callbacks ---------- */

static void timerCallback(TimerHandle_t xTimer)
	timerFlag = true;


    FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
    All rights reserved


    This file is part of the FreeRTOS distribution.

    FreeRTOS is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License (version 2) as published by the
    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.

    >>!   NOTE: The modification to the GPL is included to allow you to     !<<
    >>!   distribute a combined work that includes FreeRTOS without being   !<<
    >>!   obliged to provide the source code for proprietary components     !<<
    >>!   outside of the FreeRTOS kernel.                                   !<<

    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  Full license text is available on the following
    link: http://www.freertos.org/a00114.html

     *                                                                       *
     *    FreeRTOS provides completely free yet professionally developed,    *
     *    robust, strictly quality controlled, supported, and cross          *
     *    platform software that is more than just the market leader, it     *
     *    is the industry's de facto standard.                               *
     *                                                                       *
     *    Help yourself get started quickly while simultaneously helping     *
     *    to support the FreeRTOS project by purchasing a FreeRTOS           *
     *    tutorial book, reference manual, or both:                          *
     *    http://www.FreeRTOS.org/Documentation                              *
     *                                                                       *

    http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
	the FAQ page "My application does not run, what could be wrong?".  Have you
	defined configASSERT()?

	http://www.FreeRTOS.org/support - In return for receiving this top quality
	embedded software for free we request you assist our global community by
	participating in the support forum.

	http://www.FreeRTOS.org/training - Investing in training allows your team to
	be as productive as possible as early as possible.  Now you can receive
	FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
	Ltd, and the world's leading authority on the world's leading RTOS.

    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
    including FreeRTOS+Trace - an indispensable productivity tool, a DOS
    compatible FAT file system, and our tiny thread aware UDP/IP stack.

    http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
    Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.

    http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
    Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
    licenses offer ticketed support, indemnification and commercial middleware.

    http://www.SafeRTOS.com - High Integrity Systems also provide a safety
    engineered and independently SIL3 certified version for use in safety and
    mission critical applications that require provable dependability.

    1 tab == 4 spaces!


 * Application specific definitions.
 * These definitions should be adjusted for your particular hardware and
 * application requirements.
 * See http://www.freertos.org/a00110.html.

/* USER CODE BEGIN Includes */   	      
/* Section where include file can be added */
/* USER CODE END Includes */ 

/* Ensure stdint is only used by the compiler, and not the assembler. */
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
    #include <stdint.h>
    #include "main.h" 
    extern uint32_t SystemCoreClock;

#define configUSE_PREEMPTION                     1
#define configSUPPORT_STATIC_ALLOCATION          1
#define configSUPPORT_DYNAMIC_ALLOCATION         0
#define configUSE_IDLE_HOOK                      0
#define configUSE_TICK_HOOK                      0
#define configCPU_CLOCK_HZ                       ( SystemCoreClock )
#define configTICK_RATE_HZ                       ((TickType_t)1000)
#define configMAX_PRIORITIES                     ( 7 )
#define configMINIMAL_STACK_SIZE                 ((uint16_t)64)
#define configMAX_TASK_NAME_LEN                  ( 16 )
#define configUSE_16_BIT_TICKS                   0
#define configUSE_MUTEXES                        1
#define configQUEUE_REGISTRY_SIZE                8

/* Co-routine definitions. */
#define configUSE_CO_ROUTINES                    0
#define configMAX_CO_ROUTINE_PRIORITIES          ( 2 )

/* Software timer definitions. */
#define configUSE_TIMERS                         1
//#define configTIMER_TASK_PRIORITY                ( 2 )
#define configTIMER_TASK_PRIORITY                ( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH                 16
#define configTIMER_TASK_STACK_DEPTH             128

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet            1
#define INCLUDE_uxTaskPriorityGet           1
#define INCLUDE_vTaskDelete                 1
#define INCLUDE_vTaskCleanUpResources       0
#define INCLUDE_vTaskSuspend                1
#define INCLUDE_vTaskDelayUntil             0
#define INCLUDE_vTaskDelay                  1
#define INCLUDE_xTaskGetSchedulerState      1

/* Cortex-M specific definitions. */
 /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
 #define configPRIO_BITS         __NVIC_PRIO_BITS
 #define configPRIO_BITS         2

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
PRIORITY THAN THIS! (higher priorities are lower numeric values. */

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */

/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
/* USER CODE BEGIN 1 */   
#define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); for( ;; );} 

/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler    SVC_Handler
#define xPortPendSVHandler PendSV_Handler

/* IMPORTANT: This define MUST be commented when used with STM32Cube firmware, 
              to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
/* #define xPortSysTickHandler SysTick_Handler */

/* USER CODE BEGIN Defines */   	      
/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
/* USER CODE END Defines */ 

#endif /* FREERTOS_CONFIG_H */

rtel wrote on Wednesday, June 06, 2018:

In the first case it sounds like the tick interrupt is not executing -
which probably just means its not installed. if the tick count is not
incrementing then as far as the kernel is concerned time is standing
still and no timeouts will even expire. See the ‘special note for ARM
Cortex-M users’ in the first Q on this page of the FAQ: