EC600U_esp32_iap_uart/EC600U_uart1/bootloader/port/esp32flash.c

176 lines
4.1 KiB
C
Raw Normal View History

2024-02-05 17:39:56 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <string.h>
#include "../../../EC600U_t2n/include/ModbusS.h"
#include "../../inc/esp32flash.h"
#include "../../inc/esp32common.h"
#include "ql_fs.h"
#include "ql_log.h"
#define LOGI(msg, ...) QL_LOG(QL_LOG_LEVEL_INFO, "esp32flash", msg, ##__VA_ARGS__)
#define DOWNLOAD_FILENAME "SD:pile_cm.bin"
#define HIGHER_BAUDRATE 460800
extern uint16_t gWordVar[];
#if 0
int ESP32DownloadFile(char *filename, int32_t address)
{
LOGD("ESP32DownloadFile start file=%s,address=0x%x", filename, address);
// 检查文件是否存在
QFILE ufd = ql_fopen(filename, "r"); // int32_t ufd = LSAPI_FS_Open(filename, LSAPI_FS_O_RDONLY, 0x0);
if (ufd <= 0)
{
LOGE("ufd < 0 failed, ret:%d ,filename: %s\n", ufd, filename);
return -3;
}
// 检查文件大小
int image_size = ql_fseek(ufd, 0, QL_SEEK_END); // int32_t image_size = LSAPI_FS_Seek(ufd, 0, SEEK_END);读取文件大小
if (image_size <= 0)
{
LOGE("file size <= 0, image_size:%d\n", image_size);
if (ufd > 0)
{
ql_fclose(ufd); // LSAPI_FS_Close(ufd);
}
return -4;
}
LOGI("open filename: %s succes and size:%d\n", filename, image_size);
int32_t blocksize = 8 * 1024;
uint8_t *buffer = NULL;
buffer = (uint8_t *)malloc(blocksize); // buffer = (int8_t *)LSAPI_OSI_Malloc(blocksize);
if (buffer == NULL)
{
LOGD("loader_port_m5700_init, size:%d\n", image_size);
goto exit0;
}
// esp32_ota_log(LOG_TAG "come here right now\n");
// 初始化
esp_loader_error_t err;
if (loader_port_m5700_init(115200) != ESP_LOADER_SUCCESS)
{
LOGE("serial initialization failed.\n");
goto exit0;
}
LOGI(" connect_to_target\n");
err = connect_to_target(HIGHER_BAUDRATE);
if (err == ESP_LOADER_SUCCESS)
{
LOGI(" connect_to_target ok \n");
err = ESP32_flash_start(address, image_size, 1024);
if (err != ESP_LOADER_SUCCESS)
{
LOGE(" ESP32_flash_start err = %d\n", err);
goto exit2;
}
ql_fseek(ufd, 0, SEEK_SET); // LSAPI_FS_Seek(ufd, 0, SEEK_SET);
int offset = 0;
while (1)
{
// esp32_ota_log(LOG_TAG "come in LSAPI_FS_Read and blocksize is %d\n", blocksize);
int read_block = ql_fread((void *)buffer, blocksize, 1, ufd); // LSAPI_FS_Read(ufd, (void *)buffer, blocksize);
// LSAPI_Log_Debug(LOG_TAG " read_block is %d\n", read_block);
if (read_block > 0)
{
// esp32_ota_log(LDEBUG, LOG_TAG "read block size = %d\n", read_block);
err = ESP32_flash_writeblock(buffer, read_block);
offset += read_block;
LOGI("Progress: %d %%\n", offset * 100 / image_size);
gWordVar[REG_UPDATE_STATUS] = offset * 100 / image_size;
if (err != ESP_LOADER_SUCCESS)
{
LOGE("ESP32_flash_writeblock err = %d\n", err);
goto exit2;
}
if (read_block < blocksize)
{
break;
}
}
else
{
LOGE("LSAPI_FS_Read failed. ret:%d\n", read_block);
break;
}
}
#if MD5_ENABLED
err = ESP32_flash_verify();
if (err != ESP_LOADER_SUCCESS)
{
LOGE("ESP32_flash_verify err=%d\n", err);
goto exit2;
}
#endif
}
else
{
LOGE("connect_to_target failed. ret:%d\n", err);
}
exit2:
loader_port_ec600_deinit();
// exit1:
// ql_free(buffer); //LSAPI_OSI_Free(buffer);
// buffer = NULL;
exit0:
ql_fclose(ufd); // LSAPI_FS_Close(ufd);
return 0;
}
int is_dowload = 0;
void start_download(void *param)
{
osiThreadExit(); // LSAPI_OSI_ThreadExit();
}
#endif
uint8_t flash_to_esp32(void)
{
LOGI("flash_to_esp32 start");
QFILE ufd = ql_fopen(DOWNLOAD_FILENAME, "r");
if (ufd <= 0){
return 1;
}
int image_size = ql_fseek(ufd, 0, QL_SEEK_END);
if (image_size <= 0){
ql_fclose(ufd);
return 2;
}
LOGI("open filename: %s succes, size = %d", DOWNLOAD_FILENAME, image_size);
int32_t blocksize = 8 * 1024;
uint8_t *buffer = (uint8_t *)malloc(blocksize);
if (buffer == NULL){
ql_fclose(ufd);
return 3;
}
LOGI("malloc buffer 8192 end");
esp_loader_error_t err = 0;
err = connect_to_target(HIGHER_BAUDRATE);
if(err != 0){
LOGI("connect_to_target fail, err = %d",err);
ql_fclose(ufd);
return 4;
}
LOGI("connect_to_target end");
if(ql_fclose(ufd)!= QL_FILE_OK){
return 10;
}
LOGI("flash_to_esp32 end");
return 0;
}