227 lines
6.7 KiB
C
Executable File
227 lines
6.7 KiB
C
Executable File
/*
|
|
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
|
*
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "driver/usb_serial_jtag.h"
|
|
#include "sdkconfig.h"
|
|
#include "esp_log.h"
|
|
#include "esp_check.h"
|
|
#include "freertos/queue.h"
|
|
#include "driver/uart.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_system.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_event.h"
|
|
#include "nvs_flash.h"
|
|
|
|
#include "lwip/err.h"
|
|
#include "lwip/sockets.h"
|
|
#include "lwip/sys.h"
|
|
#include <lwip/netdb.h>
|
|
|
|
#include "esp32_port.h"
|
|
#include "esp_loader.h"
|
|
#include "flasher_common.h"
|
|
#include "driver/uart.h"
|
|
#include "driver/gpio.h"
|
|
|
|
#include "wifi_task.h"
|
|
#include "spiffs.h"
|
|
|
|
#include "bsp_flash.h"
|
|
#include "stream.h"
|
|
#include "esp_efuse.h"
|
|
#include "public.h"
|
|
|
|
#define WRITE_BLOCK 64
|
|
|
|
static const char *TAG = "main";
|
|
|
|
|
|
|
|
uint8_t* load_html2Ram(wifi_html_t html)
|
|
{
|
|
uint8_t* buff;
|
|
if(html.html_data != NULL) {
|
|
return html.html_data;
|
|
}else{
|
|
buff = malloc(html.html_size + 10);
|
|
Read_ESPFlash(0x41,html.html_addr,&buff[0],html.html_size);
|
|
// ESP_LOGI(TAG,"html:%s",buff);
|
|
return buff;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
extern uint8_t* home_html_data;
|
|
stream_data_t wifi_interface_data;
|
|
stream_data_t uart_interface_data;
|
|
|
|
void app_main(void)
|
|
{
|
|
|
|
esp_err_t err = nvs_flash_init();
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
ESP_ERROR_CHECK(nvs_flash_erase()); // 擦除 NVS 分区
|
|
err = nvs_flash_init(); // 重新初始化
|
|
}
|
|
ESP_ERROR_CHECK(err);
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
init_spiffs();
|
|
list_files_in_spiffs();
|
|
ESPFlash_Init();
|
|
stream_init(&uart_interface_data,1024*10);
|
|
|
|
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
|
gpio_config_t config = {
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
.pin_bit_mask = 1ULL << 8 | 1ULL << 7,
|
|
.pull_up_en = 1
|
|
};
|
|
gpio_config(&config);
|
|
gpio_set_level(8,0);
|
|
// gpio_set_level(18,1);
|
|
usb_serial2Uart(NULL);
|
|
xTaskCreate(&Read_CMD_task, "Read_CMD_task", 1024*50, NULL, 14, NULL);
|
|
#if 0
|
|
home_html.html_data = load_html2Ram(home_html);
|
|
wifi_config_html.html_data = load_html2Ram(wifi_config_html);
|
|
upload_ota_html.html_data = load_html2Ram(upload_ota_html);
|
|
// favicon_ico.html_data = load_html2Ram(favicon_ico);
|
|
// ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
|
// ESP_LOGI(TAG,"def_name:%s",Sys_wifi_config.def_AP_Name);
|
|
ESP_LOGI(TAG,"sizeof--Sys_wifi_config:%d",sizeof(wifi_Save_info_t));
|
|
Read_ESPFlash(0x40,0,&Sys_wifi_config,sizeof(wifi_Save_info_t));
|
|
ESP_LOGI(TAG,"wifi_ssid:%s",Sys_wifi_config.wifi_ssid);
|
|
ESP_LOGI(TAG,"wifi_password:%s",Sys_wifi_config.wifi_password);
|
|
if(Sys_wifi_config.wifi_mode != 0x55)
|
|
{
|
|
wifi_init_softap();
|
|
}else{
|
|
wifi_init_station();
|
|
}
|
|
#endif
|
|
|
|
// FILE* f = fopen("/spiffs/hello.txt", "w");
|
|
// if (f == NULL) {
|
|
// printf("Failed to open file for writing\n");
|
|
// return;
|
|
// }
|
|
// fprintf(f, "Hello World!\n");
|
|
// fclose(f);
|
|
|
|
// f = fopen("/spiffs/hello.txt", "r");
|
|
// char line[64];
|
|
// fgets(line, sizeof(line), f);
|
|
// fclose(f);
|
|
// printf("Read from file: '%s'\n", line);
|
|
|
|
|
|
|
|
|
|
uint8_t mac_wifi[6];
|
|
uint8_t mac_bt[6];
|
|
|
|
// 获取 Wi-Fi MAC 地址
|
|
esp_efuse_mac_get_default(mac_wifi);
|
|
ESP_LOGI("MAC Address", "Wi-Fi MAC: %02x:%02x:%02x:%02x:%02x:%02x",
|
|
mac_wifi[0], mac_wifi[1], mac_wifi[2], mac_wifi[3], mac_wifi[4], mac_wifi[5]);
|
|
|
|
// 获取蓝牙 MAC 地址
|
|
// esp_bt_dev_get_address(mac_bt);
|
|
// ESP_LOGI("MAC Address", "Bluetooth MAC: %02x:%02x:%02x:%02x:%02x:%02x",
|
|
// mac_bt[0], mac_bt[1], mac_bt[2], mac_bt[3], mac_bt[4], mac_bt[5]);
|
|
|
|
vTaskDelay(1000/ portTICK_PERIOD_MS);
|
|
// start_web_server();
|
|
// xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL);
|
|
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
|
// Document_feeder_test();
|
|
#if 0
|
|
|
|
uint8_t* load_buff = NULL;
|
|
const loader_esp32_config_t esp32c3_config = {
|
|
.baud_rate = 1152000,
|
|
.uart_port = UART_NUM_0,
|
|
.uart_rx_pin = GPIO_NUM_20,
|
|
.uart_tx_pin = GPIO_NUM_21,
|
|
.reset_trigger_pin = GPIO_NUM_9,
|
|
.gpio0_trigger_pin = GPIO_NUM_2,
|
|
};
|
|
|
|
if (loader_port_esp32_init(&esp32c3_config) != ESP_LOADER_SUCCESS) {
|
|
ESP_LOGE(TAG, "serial initialization failed.");
|
|
return;
|
|
}
|
|
|
|
if (connect_to_target(1152000) == ESP_LOADER_SUCCESS) {
|
|
|
|
ESP_LOGI(TAG, "connect_to_target initialization success.");
|
|
if(esp_get_free_heap_size() >200*1024)
|
|
{
|
|
load_buff = malloc(WRITE_BLOCK*1024+24);
|
|
if(load_buff == NULL)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if(esp_loader_get_target() == ESP32S3_CHIP)
|
|
{
|
|
ESP_LOGI(TAG,"Find ESP32S3");
|
|
ESP_LOGI(TAG, "Loading bin...");
|
|
// flash_binary()
|
|
uint32_t bin_size = 749504;
|
|
uint32_t write_addr = 0;
|
|
while (bin_size)
|
|
{
|
|
if(bin_size > WRITE_BLOCK*1024)
|
|
{
|
|
if(Read_ESPFlash(0x45,write_addr,&load_buff[0],WRITE_BLOCK*1024) == WRITE_BLOCK*1024)
|
|
{
|
|
flash_binary(&load_buff[0],WRITE_BLOCK*1024,write_addr);
|
|
write_addr+=WRITE_BLOCK*1024;
|
|
bin_size -= WRITE_BLOCK*1024;
|
|
ESP_LOGI(TAG,"Load : %ld --- %ld",write_addr,bin_size);
|
|
}
|
|
|
|
}else{
|
|
Read_ESPFlash(0x45,write_addr,&load_buff[0],bin_size);
|
|
flash_binary(&load_buff[0],bin_size,write_addr);
|
|
write_addr += bin_size;
|
|
bin_size = 0;
|
|
ESP_LOGI(TAG,"Load : %ld --- %ld",write_addr,bin_size);
|
|
break;
|
|
}
|
|
}
|
|
ESP_LOGI(TAG, "Loading bin success");
|
|
|
|
ESP_LOGI(TAG, "Done!");
|
|
esp_loader_reset_target();
|
|
|
|
// Delay for skipping the boot message of the targets
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
|
// Forward slave's serial output
|
|
ESP_LOGI(TAG, "********************************************");
|
|
ESP_LOGI(TAG, "*** Logs below are print from slave .... ***");
|
|
ESP_LOGI(TAG, "********************************************");
|
|
}
|
|
}else{
|
|
ESP_LOGE(TAG, "connect_to_target initialization failed.");
|
|
}
|
|
|
|
loader_port_esp32_deinit();
|
|
#endif
|
|
}
|
|
|