esp32-off-Line-tools/main/main.c
2025-04-14 19:27:42 +08:00

238 lines
7.1 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"
#include "esp_flash.h"
#include "esp_lcd_st7735.h"
#include "i2c_port.h"
#include "gui_guider.h"
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;
}
esp_timer_handle_t SysTick_timer;
extern int getBattPercentage(void);
void IRAM_ATTR SysTick_Handler(void)
{
static uint32_t SysTick_ms_cnt = 0;
static uint32_t SysTick_s_cnt = 0;
if(SysTick_ms_cnt % 1000 == 0)//1s
{
// xEventGroupSetBits(Printer_group,EVENT_GET_Batterylevel);
// lv_event_send(guider_ui.screen_label_1, LV_EVENT_CUSTOM_UPDATE, (void *)getBattPercentage());
SysTick_s_cnt +=1;
}
if ((SysTick_s_cnt % 10 == 0) && (SysTick_ms_cnt % 1000 == 0)) {
xEventGroupSetBits(System_eventGroup,EVENT_GET_BATTPERCENTAGE);
}
SysTick_ms_cnt ++;
}
void Timer_Config_Init()
{
esp_timer_create_args_t args = {
.callback = SysTick_Handler,
.arg = (void*)1,
.dispatch_method = ESP_TIMER_TASK,
.name = "Tiemr1",
.skip_unhandled_events = false,
};
esp_timer_create(&args, &SysTick_timer);
esp_timer_start_periodic(SysTick_timer, 1000);
// ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer_PrintLoop));
// gptimer_event_callbacks_t PrintLoop_cbs = {
// .on_alarm = PrintLoop_callback,
// };
// EventGrou_PrintLoop = xEventGroupCreate();
// ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer_PrintLoop, &PrintLoop_cbs, NULL));
// ESP_LOGI(TAG, "Enable timer");
// ESP_ERROR_CHECK(gptimer_enable(gptimer_PrintLoop));
// gptimer_alarm_config_t alarm_config_PrintLoop = {
// .alarm_count = 1000,
// .flags.auto_reload_on_alarm = false,
// };
// ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer_PrintLoop, &alarm_config_PrintLoop));
// ESP_ERROR_CHECK(gptimer_start(gptimer_PrintLoop));
}
extern uint8_t* home_html_data;
stream_data_t wifi_interface_data;
stream_data_t uart_interface_data;
Device_info_t off_line_tool;
EventGroupHandle_t System_eventGroup;
Sys_config_t System_Config;
extern void Axp202_task(void *pvParameter);
extern QueueHandle_t gpio_evt_queue;
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());
System_eventGroup = xEventGroupCreate();
xTaskCreate(&Gui_Task, "Gui_Task", 1024*30, NULL, 12, NULL);
init_spiffs();
list_files_in_spiffs("/spiffs",off_line_tool.directory, 10);
// ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", off_line_tool.directory[0].name, off_line_tool.directory[0].size);
// ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", off_line_tool.directory[1].name, off_line_tool.directory[1].size);
ESPFlash_Init();
stream_init(&uart_interface_data,1024*50);
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
ESP_ERROR_CHECK(i2c_master_init());
System_Config.def_target_chip = ESP32S2_CHIP;
#if CONFIG_IDF_TARGET_ESP32C3
usb_serial2Uart(NULL);
#endif
xTaskCreate(&Read_CMD_task, "Read_CMD_task", 1024*20, NULL, 14, NULL);
xTaskCreate(&usb_serial_task,"usb_serial_task",1024*10,NULL,22,NULL);
xTaskCreate(&Flasher_tool_task, "Flasher_tool_task", 1024*20, NULL, 10, NULL);
xTaskCreate(&Axp202_task,"Axp202_task",1024*5,NULL,8,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
esp_flash_t *chip = esp_flash_default_chip;
esp_err_t ret = esp_flash_read_id(chip, &off_line_tool.flash_id);
if (ret == ESP_OK) {
ESP_LOGI("FLASH", "Flash ID: 0x%lX", off_line_tool.flash_id);
} else {
ESP_LOGE("FLASH", "Failed to read flash ID, error=0x%x", ret);
}
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]);
memcpy(&off_line_tool.Device_MAC_WIFI[0],&mac_wifi[0],6);
sprintf((char*)&off_line_tool.Device_SN[0],"1234567890");
// 获取蓝牙 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 %d",esp_get_free_heap_size(),sizeof(Sys_config_t));
// Write_ESPFlash(0x40,0x00,&System_Config,sizeof(Sys_config_t));
Timer_Config_Init();
EventBits_t bits;
while (1)
{
bits = xEventGroupWaitBits(System_eventGroup, EVENT_GET_BATTPERCENTAGE ,pdFALSE,pdFALSE,portMAX_DELAY);
if(bits&EVENT_GET_BATTPERCENTAGE)
{
ESP_LOGI(TAG,"EVENT_GET_BATTPERCENTAGE");
lv_event_send(guider_ui.screen_label_1, LV_EVENT_CUSTOM_UPDATE, (void *)getBattPercentage());
xEventGroupClearBits(System_eventGroup,EVENT_GET_BATTPERCENTAGE);
}
}
}