EC600U_esp32_iap_uart/EC600U_uart1/bootloader/port/esp32common.c
2024-02-05 17:39:56 +08:00

133 lines
4.1 KiB
C

#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include "../../inc/esp32common.h"
#include "../../inc/serial_io.h"
#include "../../inc/esp_loader.h"
#include "../../inc/EC600U_port.h"
#include "ql_log.h"
#define LOGD(msg, ...) QL_LOG(QL_LOG_LEVEL_DEBUG, "esp32common", msg, ##__VA_ARGS__)
#define LOGI(msg, ...) QL_LOG(QL_LOG_LEVEL_INFO, "esp32common", msg, ##__VA_ARGS__)
#define LOGW(msg, ...) QL_LOG(QL_LOG_LEVEL_WARN, "esp32common", msg, ##__VA_ARGS__)
#define LOGE(msg, ...) QL_LOG(QL_LOG_LEVEL_ERROR, "esp32common", msg, ##__VA_ARGS__)
esp_loader_error_t connect_to_target(uint32_t higher_baudrate)
{
esp_loader_connect_args_t connect_config = {
.sync_timeout = 100,
.trials = 50
};
LOGI("connect_to_target start");
esp_loader_error_t err = esp_loader_connect(&connect_config);
if (err != ESP_LOADER_SUCCESS){
// loader_port_delay_ms(1000);
// err = esp_loader_connect(&connect_config);
// if(err != ESP_LOADER_SUCCESS){
LOGI("Cannot connect to target. Error: %d", err);
return err;
// }
}
LOGI("esp_loader_connect end");
if (higher_baudrate && (esp_loader_get_target()!= ESP8266_CHIP))
{
err = esp_loader_change_baudrate(higher_baudrate);
if (err == ESP_LOADER_ERROR_UNSUPPORTED_FUNC)
{
LOGI("ESP8266 does not support change baudrate command");
return err;
}
else if (err != ESP_LOADER_SUCCESS)
{
LOGI("Unable to change baud rate on target");
return err;
}
else
{
err = loader_port_change_baudrate(higher_baudrate);
if (err != ESP_LOADER_SUCCESS)
{
LOGI("Unable to change baud rate.\n");
return err;
}
LOGI("Baudrate changed\n");
}
}
return ESP_LOADER_SUCCESS;
}
esp_loader_error_t ESP32_flash_start(uint32_t address, uint32_t image_size, uint32_t block_size)
{
LOGI("Erasing flash (this may take a while)...");
esp_loader_error_t err = esp_loader_flash_start(address, image_size, block_size);
if (err != ESP_LOADER_SUCCESS)
{
LOGI("esp_loader_flash_start fail, err = %d", err);
return err;
}
LOGI("Start programming\n");
return err;
}
esp_loader_error_t ESP32_flash_writeblock(const uint8_t *bin, size_t size)
{
esp_loader_error_t err;
static uint8_t payload[1024];
const uint8_t *bin_addr = bin;
//size_t binary_size = size;
size_t written = 0;
// LSAPI_Log_Debug(" ESP32_flash_writeblock size is %d.\n", size);
while (size > 0)
{
size_t to_read = MIN(size, sizeof(payload));
memcpy(payload, bin_addr, to_read);
err = esp_loader_flash_write(payload, to_read);
if (err != ESP_LOADER_SUCCESS)
{
//QL_LOG(QL_LOG_LEVEL_ERROR,LOG_TAG,"Packet could not be written! Error %d.\n", err);
LOGE("Packet could not be written! Error %d.\n", err);
return err;
}
size -= to_read;
bin_addr += to_read;
written += to_read;
// int progress = (int)(((float)written / binary_size) * 100);
// fflush(stdout);
};
return err;
}
esp_loader_error_t ESP32_flash_verify(void)
{
#if MD5_ENABLED
esp_loader_error_t ESP32_err;
//QL_LOG(QL_LOG_LEVEL_INFO,LOG_TAG,"come in to ESP32_flash_verify \n");
LOGI("come in to ESP32_flash_verify \n");
ESP32_err = esp_loader_flash_verify();
if (ESP32_err == ESP_LOADER_ERROR_UNSUPPORTED_FUNC)
{
//QL_LOG(QL_LOG_LEVEL_ERROR,LOG_TAG,"ESP32 does not support flash verify command.\n");
LOGE("ESP32 does not support flash verify command.\n");
return ESP32_err;
}
else if (ESP32_err != ESP_LOADER_SUCCESS)
{
//QL_LOG(QL_LOG_LEVEL_ERROR,LOG_TAG,"MD5 does not match. err: %d\n", ESP32_err);
LOGE("MD5 does not match. err: %d\n", ESP32_err);
return ESP32_err;
}
//QL_LOG(QL_LOG_LEVEL_INFO,LOG_TAG,"Flash verified\n");
LOGI("Flash verified\n");
#endif
return ESP_LOADER_SUCCESS;
}