cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRTOS STM32F4 Question / Problem

Hz Lin
Associate II
Posted on December 23, 2017 at 21:40

I am a beginner in both STM32 and FreeRTOS. I tried writing a FreeRTOS program logging ADC reading through serial port. I have a DMA set-up to save ADC readings in an circular buffer array, and I also have a DMA set up for serial port. Meanwhile, I have another thread running to blink a LED through PWM using timer 12, and another defaultTask doing nothing. The board is STM32F446RE nucleo, and the initial code was generated using STM32Cube.

The part of code with modification is shown below:

/* StartDefaultTask function */

void StartDefaultTask(void const * argument)

{

/* USER CODE BEGIN StartDefaultTask */

/* Infinite loop */

osDelay(5);

for(;;)

{

osDelay(100);

}

/* USER CODE END StartDefaultTask */

}

/* StartTECContrlTask function */

void StartTECContrlTask(void const * argument)

{

/* USER CODE BEGIN StartTECContrlTask */

/* Infinite loop */

static float ratio_tmp=0;

for(;;)

{

ratio_tmp=(1.0+sinf(0.02*xTaskGetTickCount()))/2.0;

TEC_set_valuef(ratio_tmp,ratio_tmp);

osDelay(5);

}

/* USER CODE END StartTECContrlTask */

}

/* StartDataLogTask function */

void StartDataLogTask(void const * argument)

{

/* USER CODE BEGIN StartDataLogTask */

if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&uhADC_results, ADC_DATA_N) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/* Infinite loop */

for(;;)

{

uint16_t str_size=sprintf(print_buff, ''t %5d : %d %d \r\n'',xTaskGetTickCount(),uhADC_results[0],uhADC_results[1]);

//uint16_t str_size=strlen(print_buff);

//HAL_UART_Transmit(&huart2,print_buff,strlen(print_buff),1000);

while(HAL_UART_Transmit_DMA(&huart2,print_buff, str_size)!= HAL_OK){

osDelay(2);

}

osDelay(20);

//vTaskList(print_buff);

//HAL_UART_Transmit_DMA(&huart2,print_buff, 200);

//osDelay(30);

}

/* USER CODE END StartDataLogTask */

}

For some reason, the code stops around 10 seconds. I have tried several things:

  1. Increasing the delay amount seems to make it goes away.
  2. Whether the memory is statically or dynamically allocated does not seem to change it.
  3. I have logged the TaskList. Right  before it crashes, the stack watermark will suddenly go very low, and task name got corrupted somehow. (The code I post above have those parts commented because I am currently trying static memory, which does not support vTaskList)
  4. when it crashes, it goes here:
0690X00000609HjQAI.png

I don't know what is wrong with this arguably simple code. But I am a student really new in this, and would really appreciate any help.

Thanks!

#freertos+hal #freertos #stm32cube_fw_f4
4 REPLIES 4
S.Ma
Principal
Posted on December 24, 2017 at 06:15

How long does it take to get an adc value? [T1]

How long does it take to send the same data by uart? [T2]

Posted on December 24, 2017 at 15:36

It takes ~1.2ms to send the 30byte data through uart (I configured baud rate of 256000)

And I have my ADC running in scanning mode. It takes ~0.05ms to finish one round of conversion. I leave it to be always running.

Hz Lin
Associate II
Posted on December 30, 2017 at 23:54

I found this post which seems to be related to my issue. 

https://www.freertos.org/FreeRTOS_Support_Forum_Archive/July_2013/freertos_vTaskDelay_250_causes_a_Hard_Fault_..._8516990.html

I do observe corruption of the default task when I debug this application, which seems to agree with what the post mentions.

So I increase stack size of all the tasks to 1024 words and now it seems to works fine.

But still I am not understanding this issue. Could someone please explain what is going on? Why is the stack of the default task that becomes relevant? And is this the right way to solve this issue?

Thanks.

Posted on December 31, 2017 at 08:18

Not being verse in RTOS, it probably means your task buffers or related local variables or pointets need space in there. The initial question was to make sure the buffer acting as fifo was large enough for the feeding rate from adc and eating rate of serial interface.