FreeRTOS example for small memory footprint

which heap management are you using?

“which heap management are you using?”

heap_4 is used.

well then, it’s time to show us your linker command and map files…

MorningClock_M2.zip (22.3 KB)
Here are the MAP and Linker script.

That looks ok so far… what is the value in the first location of your IVT, ie where will the initial stack pointer at reset time point to?

At location 0x0800000 I see 0x20001000 and when the code stops at the entry to main the SP is 0x20000ff8.

Also looks valid.

Next thing I would do is inspect the ready task list at the time you call vTaskDelay(). From what you write it may be the case that either no idle task is properly configured, so the context switch after the wait() wouldn’t have anything to switch to, or the service interrupt is not invoked in a valid task context.

Need some help knowing what to examine please.

BTW I’d also #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
However, are you sure that the essential handlers like SVC_Handler etc. are correctly installed ? Can you step into and trough start of the scheduler resp. is your initial task started ? Can you set breakpoints into those handlers ?
Depending on your setup (using STM HAL ?) this post might be interesting, too.

Yes, the essential handlers are in place.

Stepping through starting the scheduler shows the Idle task being created without error.

I have also stepped through the vTaskDelay of my test task and the hard fault occurs at the end of the call to “portYIELD_WITHIN_API”.

are you positive that your service isr runs at the lowest possible pri?

I have not changed priorities from those generated by CubeMX.

It doesn’t look like you are using FreeRTOSConfig.h generated by CubeMX. If you use that version, do the symptoms change? The version you posted has errors relating to interrupt priorities (the STM32F038 has only 2 interrupt priority bits implemented) and has a suspicious note about vector installations.

I tried generating a project with CMSIS-RTOS V1 and it over-filled the RAM by about 2500+ bytes. So, this project has FreeRTOS added manually, as I do with my other projects.

Ah yes, I see. I don’t know why CubeMX started “forcing” CMSIS-RTOS. If I recall correctly, it used to let you select “none” for CMSIS-RTOS. But I suppose that’s off topic.

You’ll want to fix the errors in FreeRTOSConfig.h relating to interrupt priorities, but I don’t think they are causing your current problem. You should investigate the vectors, too. The PendSV handler in particular must be installed directly as the ISR, and not merely called by the ISR.

Your observation of apparent stack corruption might be your best clue here. Hopefully the debugger will help you figure out when and how the corruption is happening.

Would you share the contents of stm320xx_it.c?

Thanks.

that doesn’t answer the question…

@aggarg Sure …

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f0xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_spi1_tx;
extern SPI_HandleTypeDef hspi1;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M0 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVC_IRQn 0 */
	vPortSVCHandler() ;
  /* USER CODE END SVC_IRQn 0 */
  /* USER CODE BEGIN SVC_IRQn 1 */

  /* USER CODE END SVC_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */
	xPortPendSVHandler();
  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
  xPortSysTickHandler() ;
  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F0xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f0xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles DMA1 channel 2 and 3 interrupts.
  */
void DMA1_Channel2_3_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel2_3_IRQn 0 */

  /* USER CODE END DMA1_Channel2_3_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_spi1_tx);
  /* USER CODE BEGIN DMA1_Channel2_3_IRQn 1 */

  /* USER CODE END DMA1_Channel2_3_IRQn 1 */
}

/**
  * @brief This function handles SPI1 global interrupt.
  */
void SPI1_IRQHandler(void)
{
  /* USER CODE BEGIN SPI1_IRQn 0 */

  /* USER CODE END SPI1_IRQn 0 */
  HAL_SPI_IRQHandler(&hspi1);
  /* USER CODE BEGIN SPI1_IRQn 1 */

  /* USER CODE END SPI1_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

That may cause problems because compiler may generate epilogue and prologue while the definitions of xPortPendSVHandler and vPortSVCHandler are naked functions to be installed as ISRs. Try the following:

  1. Delete the definitions of SVC_Handler and PendSV_Handler from stm320xx_it.c.
  2. Enable the following in FreeRTOSConfig.h:
#define vPortSVCHandler								SVC_Handler
#define xPortPendSVHandler							PendSV_Handler

In addition, you should use some other timer as HAL Timebase (CubeMX should have given a warning about it).

Thanks.

@aggarg Many thanks that resolved the Hard Fault Issue and my test task now runs nicely.