cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with sensor and freertos

Dcapu.2440
Associate II

Hi i have a problem. When i use a sensor with freertos and i put in the same task the function to read the value from the sensor and the osdelay() my task do one misuration and after do nothing. I print the value with the HAL_UART_Transmit.

11 REPLIES 11
chaaalyy
Senior II

I think, we need more information about this...

How and where do you try to transmit the data ? If in another task, it cannot access the data of the sensor task. In this case, you have to pass over the data via a message queue....

How and where is the according variable declared? If inside the function, where you read the value, it´s destroyed after leaving the function. Is it declared "static" in source file (.c) or somewhere (maybe even without initialisation...) in the header file (.h) ? ...

Questions after questions .... :beaming_face_with_smiling_eyes:

/Charly

Dcapu.2440
Associate II

I declare the variable in this part of code

/* USER CODE BEGIN PV */

char str_tmp[100];

uint8_t temp_value=0;

/* USER CODE END PV */

and use this in one task that do simply the measure of the temperature.

void StartDefaultTask(void *argument)

{

 /* USER CODE BEGIN 5 */

 /* Infinite loop */

 for(;;)

 {

    temp_value=BSP_TSENSOR_ReadTemp();

    int tmp=temp_value;

    snprintf(str_tmp,sizeof(str_tmp),"temp\": %d,\n\r", tmp);

    HAL_UART_Transmit(&huart1, (uint8_t *) str_tmp,sizeof(str_tmp), 500);

    osDelay(700);

 }

 /* USER CODE END 5 */

}

is this the problem? do i have to create a queue? Thank you so much.

chaaalyy
Senior II

Hmmmm... I´m also just learning this c-stuff ;) Mostly i use examples from elsewhere (Youtube, provided ST examples, communities...) and adapt them to my needs =) Did i mention ? I really HATE arrays :beaming_face_with_smiling_eyes:

But anyway: I just checked one of my working examples, where i read some ADC values, triggered by interrupt into an array. This array is declared in the according source file as " uint16_t ADC_Converted_Values[10] = {0}; ". I´m a little bit surprised, but i even didn´t take care, if i can use it in another task (via extern uint16_t xxxxxx) and it seems to work without queue (as i said: I´m also learning that stuff, especially with freeRTOS...)...

Ok, my ADC puts the values (10) into that array... That´s, what your snprintf should do... Later, when it comes to read the array somewhere, it looks different:

void use_myArray()

{

uint32_t sum = 0;

for ( int i = 0; i < 10; i++ ) //one pass for every variable in the array

{

sum += ADC_Converted_Values[i] // <- "i" indicates, WHICH of the 10 variables in the array to read

}

here_i_call_another_function(sum/10); //Just to get an average of all these 10 values

}

In your function, it copies the temp_value into another int and then, you try to write it via snprintf into your array (str_tmp[100])... I´m not very used to "snprintf", but it seems, that in this case you write exactly one time one int (int tmp) into always the 1st place of your array ( which would be [0] ). The rest of your array is just "not even initialized, so absolutely unpredictable" junk. And that´s, what you send via UART ...

As i said: I´m also just learning that stuff, but that´s, what i would expect, when reading the code =) I would be glad, if somebody corrects me, if i´m wrong =)

/Charly

oh.. and before i forget that: a char array is [xx] times the size of a char (which is 8 bits). An "int" is regularly 16 or 32 bits long (depending on the platform etc.) ... So maybe, you should declare the array with the type, your sensor returns and write them directly into the array without the use of "int tmp" ?

chaaalyy
Senior II

I would give this a try: (And NO, i never used HAL_UART_Transmit or BSP_TSENSOR_Read so far, so syntax errors are possible !)

void StartDefaultTask(void *argument)

{

 /* USER CODE BEGIN 5 */

 /* Infinite loop */

 for(;;)

 {

    temp_value=BSP_TSENSOR_ReadTemp();

    HAL_UART_Transmit(&huart1, (uint8_t *) temp_value, sizeof(temp_value), 500); // why the use of an array ? Take the value and send it ...

    osDelay(700);

 }

 /* USER CODE END 5 */

}

The code looks ok, except the single escaped quote.

Suggest check one thing at a time. The most important prerequisite here is the tick for osDelay. Does looping osDelay alone work? You'd test that using the debugger.

Osdelay() with hal_uart_tramsmit() work Well but when i put in the task a function to read by a sensor the task do one cycle and after doesn't work. The osdelay work with 2 or 3 task Also . The task work with the Hal_Delay but not with the osDelay

I don't think is a problem of variable because i try the task only with the function to read the value from a sensor and this work with the Hal_Delay and doesn't work with osDelay

Then BSP_TSENSOR_ReadTemp is where the problem lies, or the default task's stack is too small.

I don't think is the stack because i Have no HardFault_Handler. Idk because with 2 sensor that i try Give the same problem with osdelay and work with Hal_Delay. Thank you a lot anyway