我是靠谱客的博主 儒雅雪糕,最近开发中收集的这篇文章主要介绍esp32 作 MCU 端 使用 AT 命令对 esp8266 进行 OTA demo,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

AT+CUSTOTA=total_len,current_packet_len,offset,checksum

OK

MCU 收到 > 之后发送 data,当前数据写入到 FLASH 之后,打印 RECV OK,当接收到 total_len 所有数据之后,如果校验成功会打印 CUSTOTA OK,否则会打印 ERROR

total_len: 整个 bin 的大小
current_packet_len:当前包的大小
offset:当前包在 bin 中的偏移地址
checksum:当前包的 checksum
data:当前包的数据
NOTE:在当前的 demo 的 esp8266 的 app bin 文件烧录在 0x110000 的位置
/* UART Echo Example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "esp_spi_flash.h"
#include "string.h"


/**
 * This is an example which echos any data it receives on UART1 back to the sender,
 * with hardware flow control turned off. It does not use UART driver event queue.
 *
 * - Port: UART1
 * - Receive (Rx) buffer: on
 * - Transmit (Tx) buffer: off
 * - Flow control: off
 * - Event queue: off
 * - Pin assignment: see defines below
 */

#define ECHO_TEST_TXD  (GPIO_NUM_4)
#define ECHO_TEST_RXD  (GPIO_NUM_5)
#define ECHO_TEST_RTS  (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS  (UART_PIN_NO_CHANGE)

#define READ_BIN_MIN(A,B) ((A) < (B) ? (A):(B))

#define BUF_SIZE (1024)
#define BUF_SIZE_RECV (128)
#define BUF_SIZE_RECV_DELIMITER (1)
#define BUF_SIZE_RECV_RECV_OK (7)

static void echo_task()
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_NUM_1, &uart_config);
    uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
    
    uint8_t flash_read_data[BUF_SIZE] = { 0 };
    uint32_t bin_flash_length = 134704;
    uint32_t s_bin_flash_length = 134704;
    uint32_t to_read = 0;
    uint32_t read = 0;
    uint32_t offset = 0x110000;
    uint32_t bin_offset = 0;
    // Configure a temporary buffer for the incoming data
    char *data_recv = (char *) malloc(BUF_SIZE_RECV);
    uint8_t *data_recv_DELIMITER = (uint8_t *) malloc(BUF_SIZE_RECV_DELIMITER);
    uint8_t *data_recv_RECV_OK = (uint8_t *) malloc(BUF_SIZE_RECV_RECV_OK);
    char *data_command = (char *) malloc(BUF_SIZE);
    // char *data_send = (char *) malloc(BUF_SIZE);
    uint8_t data_send_length = 0;
    uint32_t checksum = 0;
    // static const uint8_t OK_DELIMITER[2] = {0x4F, 0x4B};   //OK
    static const char return_ok[] = "OK";
    // static const uint8_t DELIMITER = {0x3E};   //>
    static const char DELIMITER[] = ">";
    static const uint8_t test_demo[10] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};   // RECV OK
    static const char return_ok_and_input_sign[] = "OKrn>";  //4f 4b 0d 0a 3e
    static const char return_recv_ok[] = "RECV OK";   // RECV OK
    static const char return_custota[] = "CUSTOTA";   // RECV OK
    char *ret;
    int recv_data_len;
    int len = 0;

    #if 0 //test
    to_read = READ_BIN_MIN(bin_flash_length, sizeof(flash_read_data));
    read = spi_flash_read(offset, flash_read_data, to_read);
    if(read !=  ESP_OK) {
        // printf("read flash fail!n");
    } else {
        for(int data = 0; data < to_read; data++) {
            // printf("%02x ", flash_read_data[data]);
            checksum += flash_read_data[data];
            // printf("%d,%d ",data,checksum);
        }
        // printf("rn");
    }


    vTaskDelay(100);
    // uart_write_bytes(UART_NUM_1, (const char *) "--rn", strlen("--rn"));
    // data_send_length = snprintf(data_command, BUF_SIZE, "AT+OTA=%d,%d,%d,%drn", bin_flash_length, offset, to_read, checksum);
    data_send_length = snprintf(data_command, BUF_SIZE, "AT+CUSTOTA=%d,%d,%d,%drn", bin_flash_length, to_read, bin_offset, checksum);
    len = uart_write_bytes(UART_NUM_1, (const char *) "ATrn", strlen("ATrn"));
    vTaskDelay(10);
    len = uart_write_bytes(UART_NUM_1, (const char *) data_command, data_send_length);
    printf("%.*s", len, data_command);
    int  times = 0;
    while (1) {
        // len = uart_write_bytes(UART_NUM_1, (const char *) data_command, data_send_length);
        printf("times:%drn", times++);
        len = uart_read_bytes(UART_NUM_1, (const char *) data_recv, BUF_SIZE, 20 / portTICK_RATE_MS);
        if (len > 0) {
            printf("%.*srn", len, data_recv);
            for(int i=0; i<len; i++){
                printf("%02x ", data_recv[i]);
            }

            if(data_recv != NULL) {
                ret = strstr(data_recv, return_ok_and_input_sign);
                if (ret != NULL) {
                    // printf("ret:%srn", ret);
                    break;
                }
            }
        }
    }
    #endif

    #if 1
    // len = uart_write_bytes(UART_NUM_1, (const char *) "ATrn", strlen("ATrn"));
    
    
    while(bin_flash_length > 0) {
        vTaskDelay(10);
        checksum = 0;
        to_read = READ_BIN_MIN(bin_flash_length, sizeof(flash_read_data));
        read = spi_flash_read(offset, flash_read_data, to_read);
        if(read !=  ESP_OK) {
            printf("read flash fail!n");
        } 

        // checksum calculation
        for(int data = 0; data < to_read; data++) {
            // printf("%02x ", flash_read_data[data]);
            checksum += flash_read_data[data];
        }
        
        data_send_length = snprintf(data_command, BUF_SIZE, "AT+CUSTOTA=%d,%d,%d,%drn", 
                                                s_bin_flash_length, to_read, bin_offset, checksum);
        // data_send_length = snprintf(data_command, BUF_SIZE, "AT+CUSTOTA=10000,10,0,45rn");
        
        len = uart_write_bytes(UART_NUM_1, (const char *) data_command, data_send_length);

        printf("data_command:%.*s", len, data_command);

        while (1) {
            // len = uart_write_bytes(UART_NUM_1, (const char *) data_command, data_send_length);
            len = uart_read_bytes(UART_NUM_1, (const char *) data_recv, BUF_SIZE, 20 / portTICK_RATE_MS);
            if (len > 0) {
                printf("%.*srn", len, data_recv);

                //return value debug info
                // for(int i=0; i<len; i++){
                //     printf("%02x ", data_recv[i]);
                // }

                if(data_recv != NULL) {
                    ret = strstr(data_recv, return_ok_and_input_sign);
                    if (ret != NULL) {
                        // printf("return_OK_<_success!rn");test_demo
                        len = uart_write_bytes(UART_NUM_1, (const char *)flash_read_data, to_read);
                        // len = uart_write_bytes(UART_NUM_1, (const char *)test_demo, 10);
                        break;
                    }
                }
            }
        }

        // len = uart_write_bytes(UART_NUM_1, (const char *)flash_read_data, to_read);

        //debug info
        // {
        //     for(int i = 0; i < to_read; i++) {
        //         printf("%02x %d ", flash_read_data[i], i);
        //     }
        //     printf("rn");
        // }

        while (1) {
            // len = uart_write_bytes(UART_NUM_1, (const char *) data_command, data_send_length);
            len = uart_read_bytes(UART_NUM_1, (const char *) data_recv, BUF_SIZE, 20 / portTICK_RATE_MS);
            if (len > 0) {
                printf("%.*srn", len, data_recv);

                //return value debug
                // for(int i=0; i<len; i++){
                //     printf("%02x ", data_recv[i]);
                // }

                if(data_recv != NULL) {
                    ret = strstr(data_recv, return_recv_ok);
                    if (ret != NULL) {
                        printf("return_recv_ok success!rn");
                        break;
                    }
                }
            }
        }

        bin_flash_length -= to_read;
        offset += to_read;
        bin_offset += to_read;
    }
    
    while (1) {
        // len = uart_write_bytes(UART_NUM_1, (const char *) data_command, data_send_length);
        len = uart_read_bytes(UART_NUM_1, (const char *) data_recv, BUF_SIZE, 20 / portTICK_RATE_MS);
        if (len > 0) {
            printf("%.*srn", len, data_recv);

            //return value debug info
            // for(int i=0; i<len; i++){
            //     printf("%02x ", data_recv[i]);
            // }

            if(data_recv != NULL) {
                ret = strstr(data_recv, return_custota);
                if (ret != NULL) {
                    printf("custota:success!rn");
                    break;
                }
            }
        }
    }

    #endif
    // // printf("AT+OTA==OK!n");

    free(data_command);
    free(data_recv);
    
    while (1) {
        vTaskDelay(10);
    }
}

void app_main()
{
    xTaskCreate(echo_task, "uart_echo_task", 8192, NULL, 10, NULL);
}

最后

以上就是儒雅雪糕为你收集整理的esp32 作 MCU 端 使用 AT 命令对 esp8266 进行 OTA demo的全部内容,希望文章能够帮你解决esp32 作 MCU 端 使用 AT 命令对 esp8266 进行 OTA demo所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部