基于rtthread studio的STM32F407OTA升级(http篇)

基于rtthread studio的STM32F407OTA升级(http篇)

0、前提

0.1、已经完成联网配置

STM32F407VG联网笔记(基于rtthread studio)_LangLang_2020的博客-CSDN博客

0.2、已经完成ymodem_OTA升级

基于rtthread studio的STM32F407OTA升级(ymodem篇)_LangLang_2020的博客-CSDN博客

1、相关配置

基于rtthread studio的STM32F407OTA升级(http篇)_第1张图片

基于rtthread studio的STM32F407OTA升级(http篇)_第2张图片

基于rtthread studio的STM32F407OTA升级(http篇)_第3张图片

2、更改http_ota.c的文件内容

基于rtthread studio的STM32F407OTA升级(http篇)_第4张图片

将原文件中的内容替换为以下内容,具体细节可自行分析源码。


#include 
#include 
#include 
#include 
#include 

#include "webclient.h"
#include 

#define DBG_ENABLE
#define DBG_SECTION_NAME          "http_ota"
#ifdef OTA_DOWNLOADER_DEBUG
#define DBG_LEVEL                 DBG_LOG
#else
#define DBG_LEVEL                 DBG_INFO
#endif
#define DBG_COLOR
#include 

#ifdef PKG_USING_HTTP_OTA

#define HTTP_OTA_BUFF_LEN         4096
#define GET_HEADER_BUFSZ          1024
#define GET_RESP_BUFSZ            1024
#define HTTP_OTA_DL_DELAY         (10 * RT_TICK_PER_SECOND)

#define HTTP_OTA_URL              PKG_HTTP_OTA_URL

static void print_progress(size_t cur_size, size_t total_size)
{
     
    static unsigned char progress_sign[100 + 1];
    uint8_t i, per = cur_size * 100 / total_size;

    if (per > 100)
    {
     
        per = 100;
    }

    for (i = 0; i < 100; i++)
    {
     
        if (i < per)
        {
     
            progress_sign[i] = '=';
        }
        else if (per == i)
        {
     
            progress_sign[i] = '>';
        }
        else
        {
     
            progress_sign[i] = ' ';
        }
    }

    progress_sign[sizeof(progress_sign) - 1] = '\0';

    LOG_I("\033[2A");
    LOG_I("Download: [%s] %d%%", progress_sign, per);
}

static int http_ota_fw_download(const char* uri)
{
     
    int ret = 0, resp_status;
    int file_size = 0, length, total_length = 0;
    rt_uint8_t *buffer_read = RT_NULL;
    struct webclient_session* session = RT_NULL;
    const struct fal_partition * dl_part = RT_NULL;

    /* create webclient session and set header response size */
    session = webclient_session_create(GET_HEADER_BUFSZ);
    if (!session)
    {
     
        LOG_E("open uri failed.");
        ret = -RT_ERROR;
        goto __exit;
    }

    /* send GET request by default header */
    if ((resp_status = webclient_get(session, uri)) != 200)
    {
     
        LOG_E("webclient GET request failed, response(%d) error.", resp_status);
        ret = -RT_ERROR;
        goto __exit;
    }

    file_size = webclient_content_length_get(session);
    rt_kprintf("http file_size:%d\n",file_size);

    if (file_size == 0)
    {
     
        LOG_E("Request file size is 0!");
        ret = -RT_ERROR;
        goto __exit;
    }
    else if (file_size < 0)
    {
     
        LOG_E("webclient GET request type is chunked.");
        ret = -RT_ERROR;
        goto __exit;
    }

    /* Get download partition information and erase download partition data */
    if ((dl_part = fal_partition_find("download")) == RT_NULL)
    {
     
        LOG_E("Firmware download failed! Partition (%s) find error!", "download");
        ret = -RT_ERROR;
        goto __exit;
    }

    LOG_I("Start erase flash (%s) partition!", dl_part->name);

    if (fal_partition_erase(dl_part, 0, file_size) < 0)
    {
     
        LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
        ret = -RT_ERROR;
        goto __exit;
    }
    LOG_I("Erase flash (%s) partition success!", dl_part->name);

    buffer_read = web_malloc(HTTP_OTA_BUFF_LEN);
    if (buffer_read == RT_NULL)
    {
     
        LOG_E("No memory for http ota!");
        ret = -RT_ERROR;
        goto __exit;
    }
    memset(buffer_read, 0x00, HTTP_OTA_BUFF_LEN);

    LOG_I("OTA file size is (%d)", file_size);

    do
    {
     
        length = webclient_read(session, buffer_read, file_size - total_length > HTTP_OTA_BUFF_LEN ?
                            HTTP_OTA_BUFF_LEN : file_size - total_length);
        if (length > 0)
        {
     
            /* Write the data to the corresponding partition address */
            if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
            {
     
                LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
                ret = -RT_ERROR;
                goto __exit;
            }
            total_length += length;

            print_progress(total_length, file_size);
        }
        else
        {
     
            LOG_E("Exit: server return err (%d)!", length);
            ret = -RT_ERROR;
            goto __exit;
        }

    } while(total_length != file_size);

    ret = RT_EOK;

    if (total_length == file_size)
    {
     
        if (session != RT_NULL)
        {
     
            webclient_close(session);
            session = RT_NULL;
        }
        if (buffer_read != RT_NULL)
        {
     
            web_free(buffer_read);
            buffer_read = RT_NULL;
        }

        LOG_I("Download firmware to flash success.");
        LOG_I("System now will restart...");

        rt_thread_delay(rt_tick_from_millisecond(5));

        /* Reset the device, Start new firmware */
        extern void rt_hw_cpu_reset(void);
        rt_hw_cpu_reset();
    }

__exit:
    if (session != RT_NULL)
        webclient_close(session);
    if (buffer_read != RT_NULL)
        web_free(buffer_read);

    return ret;
}

void http_ota(uint8_t argc, char **argv)
{
     
    if (argc < 2)
    {
     
        rt_kprintf("using uri: " HTTP_OTA_URL "\n");
        http_ota_fw_download(HTTP_OTA_URL);
    }
    else
    {
     
        http_ota_fw_download(argv[1]);
    }
}
/**
 * msh />http_ota [url]
*/
MSH_CMD_EXPORT(http_ota, Use HTTP to download the firmware);

#endif

3、打包升级固件

打包生成.rbl文件

基于rtthread studio的STM32F407OTA升级(http篇)_第5张图片

4、打开webserver

[下载地址](MyWebServer - Download (softonic.com))

基于rtthread studio的STM32F407OTA升级(http篇)_第6张图片

基于rtthread studio的STM32F407OTA升级(http篇)_第7张图片

5、执行http_ota命令

基于rtthread studio的STM32F407OTA升级(http篇)_第8张图片

你可能感兴趣的:(STM32笔记,stm32,http,arm)