diff --git a/bootloader_components/main/CMakeLists.txt b/bootloader_components/main/CMakeLists.txt new file mode 100644 index 0000000..7d29357 --- /dev/null +++ b/bootloader_components/main/CMakeLists.txt @@ -0,0 +1,12 @@ +idf_component_register(SRCS "bootloader.c" + REQUIRES bootloader bootloader_support) + +idf_build_get_property(target IDF_TARGET) + +set(target_folder "${target}") + +# Use the linker script files from the actual bootloader +set(scripts "${IDF_PATH}/components/bootloader/subproject/main/ld/${target_folder}/bootloader.ld" + "${IDF_PATH}/components/bootloader/subproject/main/ld/${target_folder}/bootloader.rom.ld") + +target_linker_script(${COMPONENT_LIB} INTERFACE "${scripts}") \ No newline at end of file diff --git a/bootloader_components/main/bootloader.c b/bootloader_components/main/bootloader.c new file mode 100644 index 0000000..6b0eafb --- /dev/null +++ b/bootloader_components/main/bootloader.c @@ -0,0 +1,65 @@ +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "sdkconfig.h" +#include "esp_log.h" +#include "bootloader_init.h" +#include "bootloader_utility.h" +#include "bootloader_common.h" + +static const char* TAG = "boot"; + +static int select_partition_number(bootloader_state_t* bs); + +/* + * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash. + * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset. + * We do have a stack, so we can do the initialization in C. + */ +void __attribute__((noreturn)) call_start_cpu0(void) { + // 1. Hardware initialization + if(bootloader_init() != ESP_OK) { + bootloader_reset(); + } + +#ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP + // If this boot is a wake up from the deep sleep then go to the short way, + // try to load the application which worked before deep sleep. + // It skips a lot of checks due to it was done before (while first boot). + bootloader_utility_load_boot_image_from_deep_sleep(); + // If it is not successful try to load an application as usual. +#endif + + // 2. Select the number of boot partition + bootloader_state_t bs = {0}; + int boot_index = select_partition_number(&bs); + if(boot_index == INVALID_INDEX) { + bootloader_reset(); + } + + // 2.1 Print a custom message! + ESP_LOGI(TAG, "Custom bootloader completed"); + + // 3. Load the app image for booting + bootloader_utility_load_boot_image(&bs, boot_index); +} + +// Select the number of boot partition +static int select_partition_number(bootloader_state_t* bs) { + // 1. Load partition table + if(!bootloader_utility_load_partition_table(bs)) { + ESP_LOGE(TAG, "load partition table error!"); + return INVALID_INDEX; + } + + // 2. Select the number of boot partition + return bootloader_utility_get_selected_boot_partition(bs); +} + +// Return global reent struct if any newlib functions are linked to bootloader +struct _reent* __getreent(void) { + return _GLOBAL_REENT; +} diff --git a/main/button_handling.c b/main/button_handling.c index c626d19..068248d 100644 --- a/main/button_handling.c +++ b/main/button_handling.c @@ -1,5 +1,6 @@ #include "button_handling.h" +#include "sdkconfig.h" #include #include #include "driver/gpio.h" @@ -7,6 +8,7 @@ #include "esp_err.h" #include "esp_log.h" #include "esp_timer.h" +#include "esp_mac.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/task.h" diff --git a/main/button_handling.h b/main/button_handling.h index eacbe31..02fb7b2 100644 --- a/main/button_handling.h +++ b/main/button_handling.h @@ -1,9 +1,5 @@ #pragma once -#include "driver/i2c.h" - -void IRAM_ATTR button_isr_handler(void* arg); - #ifdef __cplusplus extern "C" { #endif diff --git a/main/main.c b/main/main.c index 8478f16..1c19740 100644 --- a/main/main.c +++ b/main/main.c @@ -1,14 +1,19 @@ #include "setup.h" +#include "freertos/idf_additions.h" -#ifdef __cplusplus -extern "C" { -#endif -void app_main(void) { +void app_task(void* param) { setup(); while(1) { loop(); } } + +#ifdef __cplusplus +extern "C" { +#endif +void app_main(void) { + xTaskCreatePinnedToCore(app_task, "main_loop", 4096, NULL, 5, NULL, tskIDLE_PRIORITY + 1); +} #ifdef __cplusplus } #endif diff --git a/main/setup.c b/main/setup.c index 8aabf9a..bf9c4b2 100644 --- a/main/setup.c +++ b/main/setup.c @@ -3,7 +3,6 @@ #include #include #include "driver/gpio.h" -#include "driver/i2c.h" #include "esp_err.h" #include "esp_log.h" #include "esp_timer.h"