Forum: Mikrocontroller und Digitale Elektronik ESP32 - Blink Example Verständnisfrage


von Nicola R. (pastulla)


Lesenswert?

Hallo zusammen,
ich habe eine kurze Frage zum Einführungsbeispiel blink unter ESP-IDF.

Wenn man sich diverse Tutorials zum Thema tasks bei freertos anschaut, 
dann müssten tasks doch immer initialisiert werden:

xTaskCreate(&hello_task, "hello_task", 2048, NULL, 5, NULL);
oder so ähnlich.

Warum passiert das im blink example nicht (led-strip part habe ich 
auskommentiert, da normale LED):
1
#include <stdio.h>
2
#include "freertos/FreeRTOS.h"
3
#include "freertos/task.h"
4
#include "driver/gpio.h"
5
#include "esp_log.h"
6
#include "led_strip.h"
7
#include "sdkconfig.h"
8
9
static const char *TAG = "example";
10
11
/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
12
   or you can edit the following line and set a number here.
13
*/
14
#define BLINK_GPIO CONFIG_BLINK_GPIO
15
16
static uint8_t s_led_state = 0;
17
static void blink_led(void)
18
19
{
20
    /* Set the GPIO level according to the state (LOW or HIGH)*/
21
    gpio_set_level(BLINK_GPIO, s_led_state);
22
}
23
24
static void configure_led(void)
25
{
26
    ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
27
    gpio_reset_pin(BLINK_GPIO);
28
    /* Set the GPIO as a push/pull output */
29
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
30
}
31
32
void app_main(void)
33
{
34
35
    /* Configure the peripheral according to the LED type */
36
    configure_led();
37
38
    while (1) {
39
        ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
40
        blink_led();
41
        /* Toggle the LED state */
42
        s_led_state = !s_led_state;
43
        vTaskDelay(200 / portTICK_PERIOD_MS);
44
    }
45
}

Passiert die Initialisierung in einem header oder in der sdkconfig?

Danke für kurzfristige Erhellung!

Nicola

von Arduino F. (Firma: Gast) (arduinof)


Lesenswert?

Nicola R. schrieb:
> Passiert die Initialisierung in einem header oder in der sdkconfig?

Weder noch!
in der "int main(void)"

: Bearbeitet durch User
von Vincent H. (vinci)


Lesenswert?

app_main selbst ist eine Funktion die innerhalb eines Tasks aufgerufen 
wird. Das geschieht im Startup Code von Espressif innerhalb des ESP-IDF 
Frameworks.
1
static void main_task(void* args)
2
{
3
    ESP_LOGI(MAIN_TAG, "Started on CPU%d", (int)xPortGetCoreID());
4
#if !CONFIG_FREERTOS_UNICORE
5
    // Wait for FreeRTOS initialization to finish on other core, before replacing its startup stack
6
    esp_register_freertos_idle_hook_for_cpu(other_cpu_startup_idle_hook_cb, !xPortGetCoreID());
7
    while (!s_other_cpu_startup_done) {
8
        ;
9
    }
10
    esp_deregister_freertos_idle_hook_for_cpu(other_cpu_startup_idle_hook_cb, !xPortGetCoreID());
11
#endif
12
13
    // [refactor-todo] check if there is a way to move the following block to esp_system startup
14
    heap_caps_enable_nonos_stack_heaps();
15
16
    // Now we have startup stack RAM available for heap, enable any DMA pool memory
17
#if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
18
    if (esp_psram_is_initialized()) {
19
        esp_err_t r = esp_psram_extram_reserve_dma_pool(CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL);
20
        if (r != ESP_OK) {
21
            ESP_LOGE(MAIN_TAG, "Could not reserve internal/DMA pool (error 0x%x)", r);
22
            abort();
23
        }
24
    }
25
#endif
26
27
    // Initialize TWDT if configured to do so
28
#if CONFIG_ESP_TASK_WDT_INIT
29
    esp_task_wdt_config_t twdt_config = {
30
        .timeout_ms = CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000,
31
        .idle_core_mask = 0,
32
#if CONFIG_ESP_TASK_WDT_PANIC
33
        .trigger_panic = true,
34
#endif
35
    };
36
#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
37
    twdt_config.idle_core_mask |= (1 << 0);
38
#endif
39
#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
40
    twdt_config.idle_core_mask |= (1 << 1);
41
#endif
42
    ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config));
43
#endif // CONFIG_ESP_TASK_WDT
44
45
    /*
46
    Note: Be careful when changing the "Calling app_main()" log below as multiple pytest scripts expect this log as a
47
    start-of-application marker.
48
    */
49
    ESP_LOGI(MAIN_TAG, "Calling app_main()");
50
    extern void app_main(void);
51
    app_main();
52
    ESP_LOGI(MAIN_TAG, "Returned from app_main()");
53
    vTaskDelete(NULL);
54
}

https://github.com/espressif/esp-idf/blob/master/components/freertos/app_startup.c

: Bearbeitet durch User
von Adam P. (adamap)


Lesenswert?

Nicola R. schrieb:
> Warum passiert das im blink example nicht

Da in diesem Beispiel keine Tasks verwendet werde(keine eigenen).

: Bearbeitet durch User
von Nicola R. (pastulla)


Lesenswert?

Ok, also ist es wie folgt:
Der einzige task, der im Beispiel vorkommt, ist
static void main_task(void* args). Dieser wird automatisch gestartet.
Alles andere sind quasi nur Funktionen.
Und wenn ich das ursprüngliche freertos verwenden würde, dann müsste ich 
mich um die Initialisierung von app_main selbst kümmern bzw. einen 
Startpunkt festlegen.
Richtig?

von Arduino F. (Firma: Gast) (arduinof)


Lesenswert?

Nicola R. schrieb:
> Und wenn ich das ursprüngliche freertos verwenden würde, dann müsste ich
> mich um die Initialisierung von app_main selbst kümmern bzw. einen
> Startpunkt festlegen.

Kann man so sehen.

Wenn du eine eigene main() erstellst, dann wird ihm wohl die 
vorkompilierte links liegen lassen, und du musst alles selber erledigen.
ungetestet

von Nicola R. (pastulla)


Lesenswert?

Alles klar, vielen Dank!

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.