esp32 烧录协议

esp32的rom固化了出场固件。

进入烧录模式后,esp32串口输出:

给esp32烧录固件的时候,需要和rom的bootloder进行通讯。通讯时,使用 SLIP 数据包帧进行双向数据传输。

每个 SLIP 数据包都以 0xC0 开始和结束。 在数据包中,所有出现的 0xC0 和 0xDB 分别替换为 0xDB 0xDC 和 0xDB 0xDD。 替换是在计算校验和和长度后进行的,因此数据包长度可能比下面的大小字段长。

在SLIP通讯协议基础上,衍生出了以下协议:

Command Packet

每个命令都是一个由主机发起的 SLIP 数据包, 数据包由一个header和一个可变长度的主体组成。 小端模式传输。

Byte

Name

Comment

0

Direction

Always 0x00 for requests

1

Command

Command identifier (see Commands).

2-3

Size

Length of Data field, in bytes.

4-7

Checksum

Simple checksum of part of the data field (only used for some commands, see Checksum).

8..n

Data

Variable length data payload (0-65535 bytes, as indicated by Size parameter). Usage depends on specific command.

esp32 rom 支持的命令类型有如下:

Byte

Name

Description

Input Data

Output Data

0x02

FLASH_BEGIN

Begin Flash Download

Four 32-bit words: size to erase, number of data packets, data size in one packet, flash offset.

0x03

FLASH_DATA

Flash Download Data

Four 32-bit words: data size, sequence number, 0, 0, then data. Uses Checksum.

0x04

FLASH_END

Finish Flash Download

One 32-bit word: 0 to reboot, 1 to run user code. Not necessary to send this command if you wish to stay in the loader

0x05

MEM_BEGIN

Begin RAM Download Start

Total size, number of data packets, data size in one packet, memory offset

0x06

MEM_END

Finish RAM Download

Two 32-bit words: execute flag, entry point address

0x07

MEM_DATA

RAM Download Data

Four 32-bit words: data size, sequence number, 0, 0, then data. Uses Checksum.

0x08

SYNC

Sync Frame

36 bytes: 0x07 0x07 0x12 0x20, followed by 32 x 0x55

0x09

WRITE_REG

Write 32-bit memory address

Four 32-bit words: address, value, mask and delay (in microseconds)

0x0a

READ_REG

Read 32-bit memory address

Address as 32-bit word

Read data as 32-bit word in value field.

0x0b

SPI_SET_PARAMS

Configure SPI flash

Six 32-bit words: id, total size in bytes, block size, sector size, page size, status mask.

0x0d

SPI_ATTACH

Attach SPI flash

32-bit word: Zero for normal SPI flash. A second 32-bit word (should be 0) is passed to ROM loader only.

0x0f

CHANGE_BAUDRATE

Change Baud rate

Two 32-bit words: new baud rate, 0 if we are talking to the ROM loader or the current/old baud rate if we are talking to the stub loader.

0x10

FLASH_DEFL_BEGIN

Begin compressed flash download

Four 32-bit words: uncompressed size, number of data packets, data packet size, flash offset. With stub loader the uncompressed size is exact byte count to be written, whereas on ROM bootloader it is rounded up to flash erase block size.

0x11

FLASH_DEFL_DATA

Compressed flash download data

Four 32-bit words: data size, sequence number, 0, 0, then data. Uses Checksum.

Error code 0xC1 on checksum error.

0x12

FLASH_DEFL_END

End compressed flash download

One 32-bit word: 0 to reboot, 1 to run user code. Not necessary to send this command if you wish to stay in the loader.

0x13

SPI_FLASH_MD5

Calculate MD5 of flash region

Four 32-bit words: address, size, 0, 0

Body contains 16 raw bytes of MD5 followed by 2 status bytes (stub loader) or 32 hex-coded ASCII (ROM loader) of calculated MD5

Response Packet

Byte

Name

Comment

0

Direction

Always 0x01 for responses

1

Command

Same value as Command identifier in the request packet that trigged the response

2-3

Size

Size of data field. At least the length of the Status Bytes (2 or 4 bytes, see below).

4-7

Value

Response value used by READ_REG command (see below). Zero otherwise.

8..n

Data

Variable length data payload. Length indicated by “Size” field.

The final bytes of the Data payload indicate command status

对于 ESP32 ROM最后四个字节被使用,但只有前两个字节包含状态信息:

Byte

Name

Comment

Size-4

Status

Status flag, success (0) or failure (1)

Size-3

Error

If Status 1, this indicates the type of error.

Size-2

Reserved

Size-1

Reserved

ROM Loader Errors 枚举

Value

Meaning

0x05

“Received message is invalid” (parameters or length field is invalid)

0x06

“Failed to act on received message”

0x07

“Invalid CRC in message”

0x08

“Flash write error” - after writing a block of data to flash, the ROM loader reads the value back and the 8-bit CRC is compared to the data read from flash. If they don’t match, this error is returned.

0x09

“Flash read error” - SPI read failed

0x0a

“Flash read length error” - SPI read request length is too long

0x0b

“Deflate error” (compressed uploads only)

reff:

https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/index.html

https://github.com/espressif/esp-serial-flasher

https://github.com/espressif/esptool

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ESP32是一个小型的Wi-Fi和蓝牙模块,可以使用ESP-IDF编写嵌入式C代码来控制它。要在ESP32上连接SFTP服务器,需要使用适当的SFTP客户端库。 有一些可用的SFTP客户端库,例如libssh和mbedtls。这些库可以用于在ESP32上实现SFTP客户端。 以下是一些步骤来连接SFTP服务器: 1. 首先配置ESP32的Wi-Fi连接,确保ESP32可以连接到Internet。 2. 下载并安装所选的SFTP客户端库,例如libssh或mbedtls。 3. 在ESP-IDF中编写代码来连接SFTP服务器。以下是一个基本的示例代码: ``` #include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_wifi.h" #include "esp_event_loop.h" #include "esp_log.h" #include <libssh/libssh.h> /* SFTP服务器的IP地址 */ #define SFTP_SERVER_IP "192.168.1.100" /* SFTP服务器的端口号 */ #define SFTP_SERVER_PORT 22 /* SFTP服务器的用户名和密码 */ #define SFTP_USERNAME "username" #define SFTP_PASSWORD "password" /* SFTP客户端连接句柄 */ ssh_session my_ssh_session; sftp_session my_sftp_session; /* 连接到SFTP服务器 */ void sftp_connect(void *pvParameters) { /* 初始化SSH会话 */ int rc; my_ssh_session = ssh_new(); if (my_ssh_session == NULL) { printf("Error creating SSH session.\n"); vTaskDelete(NULL); } /* 设置SSH会话选项 */ ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, SFTP_SERVER_IP); ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &SFTP_SERVER_PORT); ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, SFTP_USERNAME); ssh_options_set(my_ssh_session, SSH_OPTIONS_PASSWORD, SFTP_PASSWORD); /* 连接到SFTP服务器 */ rc = ssh_connect(my_ssh_session); if (rc != SSH_OK) { printf("Error connecting to SFTP server.\n"); ssh_free(my_ssh_session); vTaskDelete(NULL); } /* 认证 */ rc = ssh_userauth_password(my_ssh_session, NULL, SFTP_PASSWORD); if (rc != SSH_AUTH_SUCCESS) { printf("Error authenticating with SFTP server.\n"); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); vTaskDelete(NULL); } /* 初始化SFTP会话 */ my_sftp_session = sftp_new(my_ssh_session); if (my_sftp_session == NULL) { printf("Error creating SFTP session.\n"); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); vTaskDelete(NULL); } /* 连接到SFTP服务器 */ rc = sftp_init(my_sftp_session); if (rc != SSH_OK) { printf("Error connecting to SFTP server.\n"); sftp_free(my_sftp_session); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); vTaskDelete(NULL); } printf("Connected to SFTP server.\n"); vTaskDelete(NULL); } void app_main() { /* 配置Wi-Fi连接 */ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&cfg); esp_wifi_set_mode(WIFI_MODE_STA); wifi_config_t wifi_config = { .sta = { .ssid = "your_wifi_ssid", .password = "your_wifi_password" } }; esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_wifi_start(); /* 连接到SFTP服务器 */ xTaskCreate(&sftp_connect, "sftp_connect", 4096, NULL, 5, NULL); } ``` 在此示例代码中,我们使用libssh库连接到SFTP服务器。首先,我们初始化一个SSH会话,然后设置SSH选项以指定SFTP服务器的IP地址,端口号,用户名和密码。然后,我们连接到SFTP服务器并进行身份验证。最后,我们初始化SFTP会话并连接到SFTP服务器。 4. 编译和烧录ESP32应用程序。使用ESP-IDF的命令行界面工具进行编译和烧录。 5. 运行ESP32应用程序,它会连接到SFTP服务器并进行身份验证。如果一切正常,它将打印出“Connected to SFTP server.”消息。 这样,ESP32就可以使用SFTP协议连接到SFTP服务器了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值