-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_main.c
136 lines (120 loc) · 4.21 KB
/
app_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include "esp32c3.h"
#include <stdio.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <esp_app_desc.h>
#include <esp_chip_info.h>
#include <esp_http_server.h>
#include <esp_flash.h>
#include <esp_system.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <soc/uart_pins.h>
#include "elf_loader/include/esp_elf.h"
int uart0_tx IRAM_BSS_ATTR = U0TXD_GPIO_NUM;
int uart0_rx IRAM_BSS_ATTR = U0RXD_GPIO_NUM;
int uart1_tx IRAM_BSS_ATTR = U1TXD_GPIO_NUM;
int uart1_rx IRAM_BSS_ATTR = U1RXD_GPIO_NUM;
esp_netif_t* ap_netif IRAM_BSS_ATTR;
esp_netif_t* sta_netif IRAM_BSS_ATTR;
httpd_handle_t httpd_server IRAM_BSS_ATTR;
// Application version info
const _SECTION_ATTR_IMPL(".rodata_desc", __LINE__) esp_app_desc_t esp_app_desc = {
.magic_word = ESP_APP_DESC_MAGIC_WORD,
.secure_version = 0,
.version = __XSTRING(ELF_LOADER_VER_MAJOR) "." __XSTRING(ELF_LOADER_VER_MINOR) "." __XSTRING(ELF_LOADER_VER_PATCH) " ",
.project_name = "esp32c3-elf",
.time = __TIME__,
.date = __DATE__,
.idf_ver = "v" __XSTRING(ESP_IDF_VERSION_MAJOR) "." __XSTRING(ESP_IDF_VERSION_MINOR) "." __XSTRING(ESP_IDF_VERSION_PATCH) " "
"(" "clang version " __XSTRING(__clang_major__) "." __XSTRING(__clang_minor__) "." __XSTRING(__clang_patchlevel__) ")"
};
httpd_handle_t* hap_httpd_get_handle();
int __real_hap_httpd_start(void);
int __wrap_hap_httpd_start(void)
{
httpd_handle_t* handle = hap_httpd_get_handle();
if (handle) {
extern httpd_handle_t httpd_server;
(*handle) = httpd_server;
return 0;
}
return __real_hap_httpd_start();
}
int mesh_sta_auth_expire_time(void)
{
return 0;
}
bool elf_read(void* buffer, size_t size, bool string, const void* data, size_t offset)
{
FILE* file = (FILE*)data;
fseek(file, offset, SEEK_SET);
if (string) {
return fgets(buffer, size, file) ? true : false;
}
return fread(buffer, 1, size, file) == size ? true : false;
}
void app_main(void)
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
uint32_t flash_size;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
(chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
(chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");
unsigned major_rev = chip_info.revision / 100;
unsigned minor_rev = chip_info.revision % 100;
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
if (esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
printf("Get flash size failed");
return;
}
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
/* Initialize Component */
extern void vfs_init(void);
extern void fs_init(void);
vfs_init();
fs_init();
/* Execute ELF */
esp_elf_t elf;
if (esp_elf_init(&elf) == 0) {
const char* filename = "main.elf";
struct stat st;
if (stat(filename, &st) == 0 && st.st_size > 0) {
FILE* file = fopen(filename, "rb");
if (file) {
int ret = esp_elf_relocate(&elf, elf_read, file);
fclose(file);
if (ret == 0) {
printf("Start to run ELF file\n");
esp_elf_request(&elf, 0, 0, NULL);
printf("Success to exit from ELF file\n");
}
else {
printf("Fail to relocate FILE file (%d)\n", ret);
}
}
}
esp_elf_deinit(&elf);
}
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}