xTaskCreate: Passing and reading structs in function loop
Posted: Thu Oct 05, 2023 7:28 am
Hello!
I started working with FreeRTOS and came across a nuisance that I can't overcome yet. The problem is that when passing the data structure to the task, the data comes out random when reading. Tell me, please, what is the problem?
So when I passing int, it works fine:
somewhere in function in app_main
console output:
BUT when I want to pass struct:
somewhere in function in app_main
output is wrong:
first run
other
What is going on and how can I avoid data substitution? Thanks!
I started working with FreeRTOS and came across a nuisance that I can't overcome yet. The problem is that when passing the data structure to the task, the data comes out random when reading. Tell me, please, what is the problem?
So when I passing int, it works fine:
Code: Select all
void task1_exec(void *pvParameters)
{
while (1)
{
int params = (int)pvParameters;
ESP_LOGW(ADC_TAG, "Run ntc termistor task: channel: %d", params);
vTaskDelay(pdMS_TO_TICKS(3000));
};
vTaskDelete(NULL);
}
Code: Select all
for (int i = 0; i < 4; i++)
{
if (adc[i].enabled == 1)
{
switch (adc[i].type)
{
case TYPE_NTC_THERMISTOR:
if (adc[i].config != NULL)
{
cJSON *config = cJSON_Parse(adc[i].config);
ntc_thermistor_config_t ntc_config = {
.channel = adc[i].channel,
.beta = cJSON_GetObjectItem(config, "coef")->valueint,
.width_bit = 12};
char *test_string = "Hello!";
xTaskCreate(task1_exec, "task1", STACK_SIZE, (void *)ntc_config.channel, 1, NULL);
}
break;
default:
break;
}
}
}
Code: Select all
W (63647) ADC: Run ntc termistor task: channel: 5
W (63657) ADC: Run ntc termistor task: channel: 4
W (63657) ADC: Run ntc termistor task: channel: 6
W (63667) ADC: Run ntc termistor task: channel: 7
Code: Select all
void task1_exec(void *pvParameters)
{
while (1)
{
ntc_thermistor_config_t params = *((ntc_thermistor_config_t *)pvParameters);
ESP_LOGW(ADC_TAG, "Run ntc termistor task: channel: %d, beta %d", params.channel, params.beta);
vTaskDelay(pdMS_TO_TICKS(3000));
};
vTaskDelete(NULL);
}
Code: Select all
for (int i = 0; i < 4; i++)
{
if (adc[i].enabled == 1)
{
switch (adc[i].type)
{
case TYPE_NTC_THERMISTOR:
if (adc[i].config != NULL)
{
cJSON *config = cJSON_Parse(adc[i].config);
ntc_thermistor_config_t ntc_config = {
.channel = adc[i].channel,
.beta = cJSON_GetObjectItem(config, "coef")->valueint,
.width_bit = 12};
xTaskCreate(task1_exec, "task1", STACK_SIZE, (void *)&ntc_config, 1, NULL);
}
break;
default:
break;
}
}
}
first run
Code: Select all
W (647) ADC: Run ntc termistor task: channel: 6, beta 3900
W (647) ADC: Run ntc termistor task: channel: 4, beta 3900
W (647) ADC: Run ntc termistor task: channel: 7, beta 3900
W (647) ADC: Run ntc termistor task: channel: 0, beta 396067
Code: Select all
W (51667) ADC: Run ntc termistor task: channel: 0, beta 397091
W (51667) ADC: Run ntc termistor task: channel: 0, beta 397091
W (54647) ADC: Run ntc termistor task: channel: 0, beta 397091
W (54657) ADC: Run ntc termistor task: channel: 0, beta 397091
W (54667) ADC: Run ntc termistor task: channel: 0, beta 397091
W (54667) ADC: Run ntc termistor task: channel: 0, beta 397091
W (57647) ADC: Run ntc termistor task: channel: 0, beta 397091