[ESP] ota 固件升级

cpp 复制代码
/*********************************
 *  esp32 OTA 固件升级
 *  输入参数:
 *      ota_buf:固件数据
 *      data_len:固件长度
 *  返回:
 *      0:升级成功 ,其他错误代码
 *********************************/
int esp_ota_update_process(uint8_t *ota_buf, uint32_t data_len)
{
    esp_err_t err = ESP_OK;
    esp_ota_handle_t update_handle = 0;
    const esp_partition_t *update_partition = NULL;
    update_partition = esp_ota_get_next_update_partition(NULL);
    printf("ota update_partition address = 0x%p \n", update_partition->address);
    uint32_t tick_record = esp_log_timestamp();
    err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
    if (err != ESP_OK)
    {
        printf("error:esp_ota_begin failed.code=%x.\n", err);
        return 1;
    }
    printf("esp_ota_begin,length=%ld bytes,take = %ld ms.\n", data_len, esp_log_timestamp() - tick_record);
    // 这个步骤可以分包处理,(本方法用了整包写入的方式).
    err = esp_ota_write(update_handle, ota_buf, data_len); // firmware_bin writing to ESP32 chip
    if (err != ESP_OK)
    {
        printf("error:ota_write,code=%d.\n", err);
        return 2;
    }

    // 调用此函数前,需要关闭看门狗
    err = esp_ota_end(update_handle);
    printf("esp_ota_end,ret=%d\n", err);
    if (err != ESP_OK)
    {
        printf("error:at esp_ota_end,code=%d.\n", err);
    }
    err = esp_ota_set_boot_partition(update_partition);
    printf("Reboot system, code=%d\n", err);
    esp_restart();
    return 0;
}

常用辅助ota测试的函数:(将需要ota的固件内置到esp32然后通过文件加载再通过ota函数升级)

cpp 复制代码
char* load_file_pt(const char* file_name)
{
    FILE *fp = fopen(file_name, "rb");
    if (fp == NULL)
    {
        printf("file [%s] open fail.\n", file_name);
        return NULL;
    }
    fseek(fp, 0L, SEEK_END);
    long file_len = ftell(fp);
    printf("file[%s],length = %ld bytes.\n",file_name,file_len);

    char *ota_file_data = (char *)malloc(file_len);
    if (ota_file_data == NULL)
    {
        printf("error:no mem to malloc!\n");
        fclose(fp);
        return NULL;
    }
    memset(ota_file_data, 0,file_len);
    rewind(fp);
    fread(ota_file_data, 1, file_len, fp);
    fclose(fp);

    return ota_file_data;
}
cpp 复制代码
/****************************************
* spi flash file system [for esp32]
******************************************/
void spiffs_init(void)
{
    esp_vfs_spiffs_conf_t conf = {
      .base_path = "/spiffs",
      .partition_label = NULL,
      .max_files = 5,
      .format_if_mount_failed = false
    };
    esp_err_t ret = esp_vfs_spiffs_register(&conf);
    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount or format filesystem");
        } else if (ret == ESP_ERR_NOT_FOUND) {
            ESP_LOGE(TAG, "Failed to find SPIFFS partition");
        } else {
            ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
        }
        return;
    }
    size_t total = 0, used = 0;
    ret = esp_spiffs_info(NULL, &total, &used);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
    } else {
        ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
    }
}
相关推荐
Uranus^2 分钟前
深入解析Spring Boot与微服务架构:从入门到实践
java·spring boot·spring cloud·微服务
风逸hhh3 分钟前
python打卡day30@浙大疏锦行
java·前端·python
珹洺7 分钟前
MyBatis实战指南(一)MyBatis入门基础与利用IDEA从零开始搭建你的第一个MyBatis系统
java·数据库·ide·intellij-idea·mybatis
搬码临时工10 分钟前
如何将内网的IP地址映射到外网?常见方法及详细步骤
运维·服务器·网络·tcp/ip·智能路由器·远程工作·异地访问
Bro_cat19 分钟前
在Java项目中集成Deepseek大语言模型实践指南
java·开发语言·人工智能·spring boot·语言模型
i1yo_kiki20 分钟前
idea部署本地仓库和连接放送远程仓库
java·ide·intellij-idea
ComPDFKit22 分钟前
使用 React PDF 构建 React.js PDF 查看器的指南
前端·react.js·pdf
asom2225 分钟前
Java 09Stream流与File类
java·开发语言
明长歌34 分钟前
HTML页面渲染过程
前端·html
孔明click3340 分钟前
Sa-Token v1.43.0 发布 🚀,新增 SSO 单设备注销、消息推送,多 Access-Token 并存能力
java·sa-token·springboot·登录·权限认证