Skip to content

Commit fd03dc4

Browse files
committed
Add write commands for zlib compressed writing, cleanup and move test function to readme
1 parent 330f205 commit fd03dc4

File tree

3 files changed

+117
-63
lines changed

3 files changed

+117
-63
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@ Component for flashing ESP32 chips using an ESP32 chip.
44

55
This component is still in active development and has currently only been tested for programming the ESP32-C6. Many basic esptool functions are not yet supported.
66

7+
## Usage example
8+
9+
A proper usage example has yet to be written but the block of code below should give an idea.
10+
11+
```
12+
// Initialize
13+
uint32_t chip_id;
14+
esp_log_level_set(TAG, ESP_LOG_DEBUG);
15+
ESP_ERROR_CHECK(et2_detect(&chip_id));
16+
ESP_LOGI(TAG, "ESP32 detected; chip id 0x%08" PRIx32, chip_id);
17+
18+
// Start stub
19+
ESP_ERROR_CHECK(et2_run_stub());
20+
21+
// Write test data to flash
22+
uint8_t* dummy_data = malloc(4096);
23+
for (int i = 0; i < 4096; i++) dummy_data[i] = i;
24+
ESP_ERROR_CHECK(et2_cmd_flash_begin(4096 * 4, 0));
25+
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 0));
26+
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 1));
27+
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 2));
28+
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 3));
29+
ESP_ERROR_CHECK(et2_cmd_flash_finish(false));
30+
31+
// Read test data from flash
32+
memset(dummy_data, 0, 4096);
33+
ESP_ERROR_CHECK(et2_cmd_read_flash(0, 4096, dummy_data));
34+
printf("Data read:\r\n");
35+
for (size_t i = 0; i < 4096; i++) {
36+
printf("%02x", dummy_data[i]);
37+
}
38+
printf("\r\n");
39+
free(dummy_data);
40+
```
41+
742
## License
843

944
The contents of this repository are made available under the terms of the MIT license, see [LICENSE](LICENSE) for the full license text.

include/esptoolsquared.h

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,72 @@
77
#include <stdint.h>
88
#include "esp_system.h"
99

10-
// ESP flashing protocol commands.
10+
// ESP flashing protocol commands
1111
typedef enum {
12-
ET2_CMD_FLASH_BEGIN = 0x02,
13-
ET2_CMD_FLASH_DATA = 0x03,
14-
ET2_CMD_FLASH_END = 0x04,
15-
ET2_CMD_MEM_BEGIN = 0x05,
16-
ET2_CMD_MEM_END = 0x06,
17-
ET2_CMD_MEM_DATA = 0x07,
18-
ET2_CMD_SYNC = 0x08,
19-
ET2_CMD_WRITE_REG = 0x09,
20-
ET2_CMD_READ_REG = 0x0A,
21-
// Supported by ESP32-S2 and later.
22-
ET2_CMD_SEC_INFO = 0x14,
23-
// Supported by stub only
24-
ET2_CMD_ERASE_FLASH = 0xD0,
25-
ET2_CMD_ERASE_REGION = 0xD1,
26-
ET2_CMD_READ_FLASH = 0xD2,
27-
ET2_CMD_RUN_USER_CODE = 0xD3,
12+
ET2_CMD_FLASH_BEGIN = 0x02,
13+
ET2_CMD_FLASH_DATA = 0x03,
14+
ET2_CMD_FLASH_END = 0x04,
15+
ET2_CMD_MEM_BEGIN = 0x05,
16+
ET2_CMD_MEM_END = 0x06,
17+
ET2_CMD_MEM_DATA = 0x07,
18+
ET2_CMD_SYNC = 0x08,
19+
ET2_CMD_WRITE_REG = 0x09,
20+
ET2_CMD_READ_REG = 0x0A,
21+
ET2_CMD_SPI_SET_PARAMS = 0x0B,
22+
ET2_CMD_SPI_ATTACH = 0x0D,
23+
ET2_CMD_READ_FLASH_SLOW = 0x0E,
24+
ET2_CMD_CHANGE_BAUDRATE = 0x0F,
25+
ET2_CMD_DEFL_BEGIN = 0x10,
26+
ET2_CMD_DEFL_DATA = 0x11,
27+
ET2_CMD_DEFL_END = 0x12,
28+
ET2_CMD_SPI_FLASH_MD5 = 0x13,
29+
ET2_CMD_SEC_INFO = 0x14,
30+
ET2_CMD_ERASE_FLASH = 0xD0,
31+
ET2_CMD_ERASE_REGION = 0xD1,
32+
ET2_CMD_READ_FLASH = 0xD2,
33+
ET2_CMD_RUN_USER_CODE = 0xD3,
34+
ET2_CMD_FLASH_ENCRYPT_DATA = 0xD4,
2835
} et2_cmd_t;
2936

30-
// Set interface used to a UART.
37+
// Set interface used to a UART
3138
esp_err_t et2_setif_uart(uart_port_t uart);
32-
// Try to connect to and synchronize with the ESP32.
39+
40+
// Try to connect to and synchronize with the ESP32
3341
esp_err_t et2_sync();
34-
// Detect an ESP32 and, if present, read its chip ID.
35-
// If a pointer is NULL, the property is not read.
42+
43+
// Detect an ESP32 and, if present, read its chip ID
44+
// If a pointer is NULL, the property is not read
3645
esp_err_t et2_detect(uint32_t* chip_id);
37-
// Upload and start a flasher stub.
46+
47+
// Upload and start the flasher stub
3848
esp_err_t et2_run_stub();
3949

40-
// Write to a range of memory.
50+
// Write to a range of memory
4151
esp_err_t et2_mem_write(uint32_t addr, void const* wdata, uint32_t len);
42-
// Write to a range of FLASH.
43-
esp_err_t et2_flash_write(uint32_t flash_off, void const* wdata, uint32_t len);
4452

45-
// Send MEM_BEGIN command to initiate memory writes.
53+
// Write application to RAM (sending end command restarts the target into the loaded application)
4654
esp_err_t et2_cmd_mem_begin(uint32_t size, uint32_t blocks, uint32_t blocksize, uint32_t offset);
47-
// Send MEM_DATA command to send memory write payload.
4855
esp_err_t et2_cmd_mem_data(void const* data, uint32_t data_len, uint32_t seq);
49-
// Send MEM_END command to restart into application.
5056
esp_err_t et2_cmd_mem_end(uint32_t entrypoint);
51-
// Send FLASH_BEGIN command to initiate memory writes.
57+
58+
// Write uncompressed data to flash
5259
esp_err_t et2_cmd_flash_begin(uint32_t size, uint32_t offset);
53-
// Send FLASH_BLOCK command to send memory write payload.
5460
esp_err_t et2_cmd_flash_data(const uint8_t* data, uint32_t data_len, uint32_t seq);
55-
// Send FLASH_FINISH command to restart into application.
5661
esp_err_t et2_cmd_flash_finish(bool reboot);
5762

63+
// Write compressed data to flash
64+
esp_err_t et2_cmd_deflate_begin(uint32_t uncompressed_size, uint32_t compressed_size, uint32_t offset);
65+
esp_err_t et2_cmd_deflate_data(const uint8_t* data, uint32_t data_len, uint32_t seq);
66+
esp_err_t et2_cmd_deflate_finish(bool reboot);
67+
68+
// Read a register
5869
esp_err_t et2_cmd_read_reg(uint32_t address, uint32_t* out_value);
70+
71+
// Read uncompressed data from flash
5972
esp_err_t et2_cmd_read_flash(uint32_t offset, uint32_t length, uint8_t* out_data);
6073

61-
void et2_test(void);
74+
// Erase entire flash
75+
esp_err_t et2_cmd_erase_flash(void);
76+
77+
// Erase a region of flash
78+
esp_err_t et2_cmd_erase_region(uint32_t offset, uint32_t length);

src/esptoolsquared.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,37 +57,6 @@ static uint32_t et2_checksum(const uint8_t* data, uint32_t data_length, uint32_t
5757
return state;
5858
}
5959

60-
void et2_test(void) {
61-
uint32_t chip_id;
62-
esp_log_level_set(TAG, ESP_LOG_DEBUG);
63-
ESP_ERROR_CHECK(et2_detect(&chip_id));
64-
ESP_LOGI(TAG, "ESP32 detected; chip id 0x%08" PRIx32, chip_id);
65-
66-
ESP_ERROR_CHECK(et2_run_stub());
67-
68-
// Send FLASH_BEGIN command to initiate memory writes
69-
uint8_t* dummy_data = malloc(4096);
70-
71-
for (int i = 0; i < 4096; i++) dummy_data[i] = i;
72-
73-
ESP_ERROR_CHECK(et2_cmd_flash_begin(4096 * 4, 0));
74-
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 0));
75-
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 1));
76-
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 2));
77-
ESP_ERROR_CHECK(et2_cmd_flash_data(dummy_data, 4096, 3));
78-
ESP_ERROR_CHECK(et2_cmd_flash_finish(false));
79-
80-
uint32_t length = 4 * 1024;
81-
uint8_t* buffer = malloc(length);
82-
ESP_ERROR_CHECK(et2_cmd_read_flash(0, length, buffer));
83-
84-
printf("DATA READ FROM FLASH:\r\n");
85-
for (size_t i = 0; i < length; i++) {
86-
printf("%02x", buffer[i]);
87-
}
88-
printf("\r\n");
89-
}
90-
9160
esp_err_t et2_read_magic_reg(uint32_t* out_magic) {
9261
return et2_cmd_read_reg(0x40001000, out_magic);
9362
}
@@ -529,3 +498,36 @@ esp_err_t et2_cmd_flash_finish(bool reboot) {
529498
uint32_t params[] = {reboot ? 0 : 1};
530499
return et2_send_cmd_check(ET2_CMD_FLASH_END, 0, params, sizeof(params), NULL, NULL, NULL, NULL, NULL, 0);
531500
}
501+
502+
// Write compressed data to flash
503+
504+
esp_err_t et2_cmd_deflate_begin(uint32_t uncompressed_size, uint32_t compressed_size, uint32_t offset) {
505+
uint32_t num_blocks = (compressed_size + FLASH_WRITE_SIZE - 1) / FLASH_WRITE_SIZE;
506+
uint32_t erase_size = uncompressed_size;
507+
uint32_t params[] = {erase_size, num_blocks, FLASH_WRITE_SIZE, offset};
508+
return et2_send_cmd_check(ET2_CMD_DEFL_BEGIN, 0, params, sizeof(params), NULL, NULL, NULL, NULL, NULL, 0);
509+
}
510+
511+
esp_err_t et2_cmd_deflate_data(const uint8_t* data, uint32_t data_len, uint32_t seq) {
512+
uint32_t params[] = {data_len, seq, 0, 0};
513+
ESP_RETURN_ON_ERROR(
514+
et2_send_cmd_check(ET2_CMD_DEFL_DATA, 0, params, sizeof(params), NULL, NULL, NULL, NULL, data, data_len), TAG,
515+
"Failed to write to flash");
516+
return ESP_OK;
517+
}
518+
519+
esp_err_t et2_cmd_deflate_finish(bool reboot) {
520+
uint32_t params[] = {reboot ? 0 : 1};
521+
return et2_send_cmd_check(ET2_CMD_DEFL_END, 0, params, sizeof(params), NULL, NULL, NULL, NULL, NULL, 0);
522+
}
523+
524+
// Erase entire flash
525+
esp_err_t et2_cmd_erase_flash(void) {
526+
return et2_send_cmd_check(ET2_CMD_ERASE_FLASH, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, 0);
527+
}
528+
529+
// Erase a region of flash
530+
esp_err_t et2_cmd_erase_region(uint32_t offset, uint32_t length) {
531+
uint32_t params[] = {offset, length};
532+
return et2_send_cmd_check(ET2_CMD_ERASE_REGION, 0, params, sizeof(params), NULL, NULL, NULL, NULL, NULL, 0);
533+
}

0 commit comments

Comments
 (0)