ESP_LOGx or printf

username
Posts: 536
Joined: Thu May 03, 2018 1:18 pm

ESP_LOGx or printf

Postby username » Fri Nov 15, 2024 6:03 am

Ran into a situation where ESP_LOGI would not always spit out to the console, typically when A/D value is above 1000. Yet when I replaced it with a printf it always does. So now this has me a bit concerned using it in other code that should some critical event happen that I need to see it using ESP_LOGx I now have my doubts. Which is weird because this has never happened before to me, especially with such a simple program.

So I'm curious if printf has higher priority over ESP_LOGx.

If you look at the code below. I have 2 tasks running. Task1 is in charge of cycling a led. Task 2 is in charge of reading an external pot then converting it to the time I need for use in Task1.

Code: Select all


#define BPM_MIN 60   // Minimum beats per minute
#define BPM_MAX 140   // Maximum beats per minute

// Convert BPM to milliseconds per beat
#define DELAY_MIN_MS (60000 / BPM_MAX)  // 70 BPM -> ~857 ms
#define DELAY_MAX_MS (60000 / BPM_MIN)  // 30 BPM -> 2000 ms

volatile int pot_delay=2000;
//**************************************************************************
//  
//
//**************************************************************************
void Task1(void *parameter)
{
    ESP_LOGI(MAIN_TAG, "Task 1 Started");

    for (;;)
    {
        gpio_set_level(GPIO_NUM_15, LOW);
        vTaskDelay(pot_delay / portTICK_PERIOD_MS);

        gpio_set_level(GPIO_NUM_15, HIGH);
        vTaskDelay(pot_delay / portTICK_PERIOD_MS);
    }
}
//**************************************************************************
//
//
//**************************************************************************
void Task2(void *parameter)
{
    ESP_LOGI(MAIN_TAG, "Task 2 Started");

    int adc_raw[2][10];
    int adc_value;

    for (;;)
    {
        //-------------ADC1 Init---------------//
        adc_oneshot_unit_handle_t adc1_handle;
        adc_oneshot_unit_init_cfg_t init_config1 = {
            .unit_id = ADC_UNIT_1,
        };
        ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));

        //-------------ADC1 Config---------------//
        adc_oneshot_chan_cfg_t config = {
            .bitwidth = ADC_BITWIDTH_DEFAULT,
            .atten = EXAMPLE_ADC_ATTEN,
        };
        ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN0, &config));

        //-------------ADC1 Calibration Init---------------//
        adc_cali_handle_t adc1_cali_chan0_handle = NULL;
        bool do_calibration1_chan0 = example_adc_calibration_init(ADC_UNIT_1, EXAMPLE_ADC1_CHAN0, EXAMPLE_ADC_ATTEN, &adc1_cali_chan0_handle);

        if (do_calibration1_chan0)
        {
            while (true)
            {
                uint32_t avg_pot_delay=0;

                for(int x=0;x < 50;x++)
                {
                    adc_oneshot_read(adc1_handle, EXAMPLE_ADC1_CHAN0, &adc_raw[0][0]);
                    avg_pot_delay += adc_raw[0][0];
                   			
                    vTaskDelay(pdMS_TO_TICKS(10));   // added this to see if it would help with the ESP_LOGI issue
                }

                avg_pot_delay /= 50;
                adc_value = (int)avg_pot_delay;
                pot_delay =  DELAY_MAX_MS - ((adc_value * (DELAY_MAX_MS - DELAY_MIN_MS)) / 4096);

                // Calculate beats per minute based on delay
                int bpm = (60000 / pot_delay) / 2;

		// ESP_LOGI, will randomly not print for several cycles, yet I know this code is working because the 
		// POT is still adjusting the LED cycle time
		ESP_LOGI(MAIN_TAG,"ADC: %d: Delay: %d :BPM: %d\n", adc_value, pot_delay, bpm );
                
                // printf always prints
                //printf("ADC: %d: Delay: %d :BPM: %d\n", adc_value, pot_delay, bpm );

                vTaskDelay(pdMS_TO_TICKS(100));
            }
        }
        else
        {
            ESP_LOGI(MAIN_TAG, "Calibration Failed");
            vTaskDelay(pdMS_TO_TICKS(1000));
        }
    }
}

void app_main(void)
{
    // create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
    xTaskCreatePinnedToCore(
        Task1,                               // Task function.
        "Task1",                             // name of task.
        4000,                                // Stack size of task
        NULL,                               // parameter of the task
        10,                                   // priority of the task
        NULL], 				// Task handle to keep track of created task
        1);                                   	// core 0 or 1



    // create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
    xTaskCreatePinnedToCore(
        Task2,                                // Task function.
        "Task2",                              // name of task.
        4000,                                 // Stack size of task
        NULL,                                 // parameter of the task
        5,                                    // priority of the task
        NULL], // Task handle to keep track of created task
        0); 

}


aliarifat794
Posts: 198
Joined: Sun Jun 23, 2024 6:18 pm

Re: ESP_LOGx or printf

Postby aliarifat794 » Fri Nov 15, 2024 7:06 am

Increase the size of the log buffer in sdkconfig:

Code: Select all

CONFIG_LOG_BUFFER_SIZE=4096  // Default is often 1024; increase if needed.

username
Posts: 536
Joined: Thu May 03, 2018 1:18 pm

Re: ESP_LOGx or printf

Postby username » Fri Nov 15, 2024 7:33 am

What ESP_IDF version are you using? I am on v5.3.1 and CONFIG_LOG_BUFFER_SIZE is not in there.

Who is online

Users browsing this forum: pepgra and 162 guests