cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRtos not all tasks are running

PPopo.1
Senior

Hello,

 

I have a problem with threads. All of them are of same priority, with osDelay(300), except the uartThread which has osDelay(1). Everything else is the same for all threads. I have allocated 20k of HEAP and in ioc file it says im using only 3.5k so that should be fine i guess. However, only one of the threads is running (I2C_temp works fine besides the uartDATA), others never even enter the for(;;) loop. Any ideas?

So this is the freertos.c file where I create the tasks 

 

 

 

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
#include "i2c.h"
#include "spi.h"
#include "string.h"
#include "stepper.h"
#include <stdlib.h>
#include <stdio.h>
#include "stm32l4xx_it.h"

/* USER CODE END Includes */

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

/* USER CODE END PTD */

/* 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 Variables */
volatile char received_data;
volatile uint8_t commands;
e_UART_Status uartStatus = undefined;
/* USER CODE END Variables */
/* Definitions for stepper */
osThreadId_t stepperHandle;
const osThreadAttr_t stepper_attributes = {
    .name = "stepper",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};
/* Definitions for I2C_TempSens */
osThreadId_t I2C_TempSensHandle;
const osThreadAttr_t I2C_TempSens_attributes = {
    .name = "I2C_TempSens",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};
/* Definitions for SPI_TempSens */
osThreadId_t SPI_TempSensHandle;
const osThreadAttr_t SPI_TempSens_attributes = {
    .name = "SPI_TempSens",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};
/* Definitions for uart */
osThreadId_t uartHandle;
const osThreadAttr_t uart_attributes = {
    .name = "uart",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};
/* Definitions for uartQueue */
osMessageQueueId_t uartQueueHandle;
const osMessageQueueAttr_t uartQueue_attributes = {
    .name = "uartQueue"};
/* Definitions for Mutex1 */
osMutexId_t Mutex1Handle;
const osMutexAttr_t Mutex1_attributes = {
    .name = "Mutex1"};
/* Definitions for mutexStepper */
osMutexId_t mutexStepperHandle;
const osMutexAttr_t mutexStepper_attributes = {
    .name = "mutexStepper"};
/* Definitions for mutexUart */
osMutexId_t mutexUartHandle;
const osMutexAttr_t mutexUart_attributes = {
    .name = "mutexUart"};

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

/* USER CODE END FunctionPrototypes */

void StepperMotor(void *argument);
void I2C_temp(void *argument);
void SPI_temp(void *argument);
void uartDATA(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
 * @brief  FreeRTOS initialization
 *   None
 * @retval None
 */
void MX_FREERTOS_Init(void)
{
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */
  /* Create the mutex(es) */
  /* creation of Mutex1 */
  Mutex1Handle = osMutexNew(&Mutex1_attributes);

  /* creation of mutexStepper */
  mutexStepperHandle = osMutexNew(&mutexStepper_attributes);

  /* creation of mutexUart */
  mutexUartHandle = osMutexNew(&mutexUart_attributes);

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of uartQueue */
  uartQueueHandle = osMessageQueueNew(16, sizeof(uint16_t), &uartQueue_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of stepper */
  stepperHandle = osThreadNew(StepperMotor, NULL, &stepper_attributes);

  /* creation of I2C_TempSens */
  I2C_TempSensHandle = osThreadNew(I2C_temp, NULL, &I2C_TempSens_attributes);

  /* creation of SPI_TempSens */
  SPI_TempSensHandle = osThreadNew(SPI_temp, NULL, &SPI_TempSens_attributes);

  /* creation of uart */
  uartHandle = osThreadNew(uartDATA, NULL, &uart_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */
}

 

 

 

 

And this are the tasks. 

 

 

 

/* USER CODE END Header_StepperMotor */
void StepperMotor(void *argument)
{
  /* USER CODE BEGIN StepperMotor */
  char buffer[50];
  /* Infinite loop */
  for (;;)
  {
    if (commandCheck() == set_stepper_OK)
    {

      if (osMutexAcquire(mutexStepperHandle, portMAX_DELAY) == osOK)
      {
        HAL_UART_Transmit(&huart1, "BUSY SET_STEPPER\n", 18, HAL_MAX_DELAY);
        motorRotation(motorInfo.motorNum, motorInfo.numOfSteps);
        HAL_UART_Transmit(&huart1, "OK SET_STEPPER\n", 16, HAL_MAX_DELAY);
        osMutexRelease(mutexStepperHandle);
      }
      else
      {
        HAL_UART_Transmit(&huart1, "ERR\n", 5, HAL_MAX_DELAY);
      }
    }

    osDelay(300);
  }

  /* USER CODE END StepperMotor */
}

/* USER CODE BEGIN Header_I2C_temp */
/**
 * @brief Function implementing the mojTask thread.
 *  argument: Not used
 * @retval None
 */
/* USER CODE END Header_I2C_temp */
void I2C_temp(void *argument)
{
  /* USER CODE BEGIN I2C_temp */
  float temp1;
  char buffer[12] = {0};
  char bufferTemp[10] = {0};
      UBaseType_t testtest;
    testtest = uxTaskGetNumberOfTasks();
  for (;;)
  {


    if (uartStatus == get_temp_1_OK)
    {
      temp1 = read_I2C();
      floatToString(temp1, buffer);
      HAL_UART_Transmit(&huart1, buffer, sizeof(buffer), HAL_MAX_DELAY);
      uartStatus = undefined;
    }

    osDelay(300);
  }

  /* USER CODE END I2C_temp */
}

/* USER CODE BEGIN Header_SPI_temp */
/**
 * @brief Function implementing the SPI_TempSens thread.
 *  argument: Not used
 * @retval None
 */
/* USER CODE END Header_SPI_temp */
void SPI_temp(void *argument)
{
  /* USER CODE BEGIN SPI_temp */
  float temp1, temp2;
  char buffer[12] = {0};
  /* Infinite loop */
  for (;;)
  {
    if (uartStatus == get_temp_2_OK)
    {
      temp1 = read_SPI1();
      floatToString(temp1, buffer);
      HAL_UART_Transmit(&huart1, buffer, sizeof(buffer), HAL_MAX_DELAY);
      uartStatus = undefined;
    }
    else if (uartStatus == get_temp_3_OK)
    {
      temp2 = read_SPI2();
      floatToString(temp2, buffer);
      HAL_UART_Transmit(&huart1, buffer, sizeof(buffer), HAL_MAX_DELAY);
      uartStatus = undefined;
    }

    osDelay(300);
  }
  /* USER CODE END SPI_temp */
}

/* USER CODE BEGIN Header_uartDATA */
/**
 * @brief Function implementing the uart thread.
 *  argument: Not used
 * @retval None
 */
/* USER CODE END Header_uartDATA */
void uartDATA(void *argument)
{
  /* USER CODE BEGIN uartDATA */

  uint8_t cnt = 0;
  uint8_t temp;
  char buffer[50] = {'\0'};
  char msg = {0};
  char firstByte;
  __HAL_UART_CLEAR_FLAG(&huart1, UART_CLEAR_OREF);
  HAL_UART_Receive_IT(&huart1, (char *)&received_data, sizeof(received_data));
  /* Infinite loop */
  for (;;)
  {

    if (osMessageQueueGet(uartQueueHandle, &msg, NULL, osWaitForever) == osOK)
    {

      if (msg == '\r')
      {

        buffer[0] = firstByte;
        parse_UART(buffer);
        memset(buffer, 0, sizeof(buffer));
        cnt = 0;
        uartStatus = commandCheck();
        osMessageQueueReset(uartQueueHandle);
        // commands = commandCheck();
      }
      else if (msg == '\n')
      {
      }
      else if (cnt < (sizeof(buffer) - 1))
      {
        buffer[cnt] = msg;
        cnt++;
      }
      if (cnt == 1)
      {
        firstByte = buffer[0];
      }
    }

    osDelay(1);
  }

  /* USER CODE END uartDATA */
}

 

 

Threads and addresses:

PPopo1_0-1712832411372.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PPopo.1
Senior

So i found the solution. Even tho I used .ioc file to change the overall HEAP size, i did not allocate more heap to the threads. When I was using sprintf() the stack overflow happened. I manually increased stack size of the threads: 

osThreadId_t stepperHandle;
const osThreadAttr_t stepper_attributes = {
    .name = "stepper",
    .stack_size = 256 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};

and now it works.

View solution in original post

1 REPLY 1
PPopo.1
Senior

So i found the solution. Even tho I used .ioc file to change the overall HEAP size, i did not allocate more heap to the threads. When I was using sprintf() the stack overflow happened. I manually increased stack size of the threads: 

osThreadId_t stepperHandle;
const osThreadAttr_t stepper_attributes = {
    .name = "stepper",
    .stack_size = 256 * 4,
    .priority = (osPriority_t)osPriorityNormal,
};

and now it works.