Martin Valik 3 лет назад
Родитель
Сommit
8d9de16fdc
7 измененных файлов с 56 добавлено и 32 удалено
  1. 3 2
      include/esp_loader.h
  2. 2 1
      private_include/esp_targets.h
  3. 1 1
      private_include/serial_comm.h
  4. 3 1
      src/esp_loader.c
  5. 38 17
      src/esp_targets.c
  6. 2 10
      src/serial_comm.c
  7. 7 0
      test/test.cpp

+ 3 - 2
include/esp_loader.h

@@ -58,8 +58,9 @@ typedef enum {
     ESP32S2_CHIP = 2,
     ESP32C3_CHIP = 3,
     ESP32S3_CHIP = 4,
-    ESP_MAX_CHIP = 5,
-    ESP_UNKNOWN_CHIP = 5
+    ESP32C2_CHIP = 5,
+    ESP_MAX_CHIP = 6,
+    ESP_UNKNOWN_CHIP = 6
 } target_chip_t;
 
 /**

+ 2 - 1
private_include/esp_targets.h

@@ -29,4 +29,5 @@ typedef struct {
 } target_registers_t;
 
 esp_loader_error_t loader_detect_chip(target_chip_t *target, const target_registers_t **regs);
-esp_loader_error_t loader_read_spi_config(target_chip_t target_chip, uint32_t *spi_config);
+esp_loader_error_t loader_read_spi_config(target_chip_t target_chip, uint32_t *spi_config);
+bool encryption_in_begin_flash_cmd(target_chip_t target);

+ 1 - 1
private_include/serial_comm.h

@@ -23,7 +23,7 @@
 extern "C" {
 #endif
 
-esp_loader_error_t loader_flash_begin_cmd(uint32_t offset, uint32_t erase_size, uint32_t block_size, uint32_t blocks_to_write, target_chip_t target);
+esp_loader_error_t loader_flash_begin_cmd(uint32_t offset, uint32_t erase_size, uint32_t block_size, uint32_t blocks_to_write, bool encryption);
 
 esp_loader_error_t loader_flash_data_cmd(const uint8_t *data, uint32_t size);
 

+ 3 - 1
src/esp_loader.c

@@ -249,8 +249,10 @@ esp_loader_error_t esp_loader_flash_start(uint32_t offset, uint32_t image_size,
 
     init_md5(offset, image_size);
 
+    bool encryption_in_cmd = encryption_in_begin_flash_cmd(s_target);
+
     loader_port_start_timer(timeout_per_mb(erase_size, ERASE_REGION_TIMEOUT_PER_MB));
-    return loader_flash_begin_cmd(offset, erase_size, block_size, blocks_to_write, s_target);
+    return loader_flash_begin_cmd(offset, erase_size, block_size, blocks_to_write, encryption_in_cmd);
 }
 
 

+ 38 - 17
src/esp_targets.c

@@ -25,6 +25,7 @@ typedef struct {
     uint32_t efuse_base;
     uint32_t chip_magic_value[MAX_MAGIC_VALUES];
     read_spi_config_t read_spi_config;
+    bool encryption_in_begin_flash_cmd;
 } esp_target_t;
 
 // This ROM address has a different value on each chip model
@@ -32,8 +33,7 @@ typedef struct {
 
 #define ESP8266_SPI_REG_BASE 0x60000200
 #define ESP32S2_SPI_REG_BASE 0x3f402000
-#define ESP32C3_SPI_REG_BASE 0x60002000
-#define ESP32S3_SPI_REG_BASE 0x60002000
+#define ESP32xx_SPI_REG_BASE 0x60002000
 #define ESP32_SPI_REG_BASE   0x3ff42000
 
 static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config);
@@ -92,13 +92,13 @@ static const esp_target_t esp_target[ESP_MAX_CHIP] = {
     // ESP32C3
     {
         .regs = {
-            .cmd  = ESP32C3_SPI_REG_BASE + 0x00,
-            .usr  = ESP32C3_SPI_REG_BASE + 0x18,
-            .usr1 = ESP32C3_SPI_REG_BASE + 0x1c,
-            .usr2 = ESP32C3_SPI_REG_BASE + 0x20,
-            .w0   = ESP32C3_SPI_REG_BASE + 0x58,
-            .mosi_dlen = ESP32C3_SPI_REG_BASE + 0x24,
-            .miso_dlen = ESP32C3_SPI_REG_BASE + 0x28,
+            .cmd  = ESP32xx_SPI_REG_BASE + 0x00,
+            .usr  = ESP32xx_SPI_REG_BASE + 0x18,
+            .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
+            .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
+            .w0   = ESP32xx_SPI_REG_BASE + 0x58,
+            .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
+            .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
         },
         .efuse_base = 0x60008800,
         .chip_magic_value = { 0x6921506f, 0x1b31506f },
@@ -108,17 +108,33 @@ static const esp_target_t esp_target[ESP_MAX_CHIP] = {
     // ESP32S3
     {
         .regs = {
-            .cmd  = ESP32C3_SPI_REG_BASE + 0x00,
-            .usr  = ESP32C3_SPI_REG_BASE + 0x18,
-            .usr1 = ESP32C3_SPI_REG_BASE + 0x1c,
-            .usr2 = ESP32C3_SPI_REG_BASE + 0x20,
-            .w0   = ESP32C3_SPI_REG_BASE + 0x58,
-            .mosi_dlen = ESP32C3_SPI_REG_BASE + 0x24,
-            .miso_dlen = ESP32C3_SPI_REG_BASE + 0x28,
+            .cmd  = ESP32xx_SPI_REG_BASE + 0x00,
+            .usr  = ESP32xx_SPI_REG_BASE + 0x18,
+            .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
+            .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
+            .w0   = ESP32xx_SPI_REG_BASE + 0x58,
+            .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
+            .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
         },
         .efuse_base = 0x60007000,
         .chip_magic_value = { 0x00000009, 0 },
-        .read_spi_config = spi_config_esp32xx, // !
+        .read_spi_config = spi_config_esp32xx,
+    },
+
+    // ESP32C2
+    {
+        .regs = {
+            .cmd  = ESP32xx_SPI_REG_BASE + 0x00,
+            .usr  = ESP32xx_SPI_REG_BASE + 0x18,
+            .usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
+            .usr2 = ESP32xx_SPI_REG_BASE + 0x20,
+            .w0   = ESP32xx_SPI_REG_BASE + 0x58,
+            .mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
+            .miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
+        },
+        .efuse_base = 0x60008800,
+        .chip_magic_value = { 0x6f51306f, 0 },
+        .read_spi_config = spi_config_esp32xx,
     },
 };
 
@@ -209,4 +225,9 @@ static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_
 
     *spi_config = pins;
     return ESP_LOADER_SUCCESS;
+}
+
+bool encryption_in_begin_flash_cmd(target_chip_t target)
+{
+    return target == ESP32_CHIP || target == ESP8266_CHIP;
 }

+ 2 - 10
src/serial_comm.c

@@ -236,21 +236,13 @@ static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value, voi
     return ESP_LOADER_SUCCESS;
 }
 
-static inline uint32_t encryption_field_size(target_chip_t target)
-{
-    return (target == ESP32S2_CHIP || 
-            target == ESP32C3_CHIP || 
-            target == ESP32S3_CHIP) ? 0 : sizeof(uint32_t);
-}
-
-
 esp_loader_error_t loader_flash_begin_cmd(uint32_t offset,
                                           uint32_t erase_size,
                                           uint32_t block_size,
                                           uint32_t blocks_to_write,
-                                          target_chip_t target)
+                                          bool encryption)
 {
-    uint32_t encryption_size = encryption_field_size(target);
+    uint32_t encryption_size = encryption ? sizeof(uint32_t) : 0;
 
     begin_command_t begin_cmd = {
         .common = {

+ 7 - 0
test/test.cpp

@@ -115,6 +115,7 @@ map<target_chip_t, uint32_t> chip_magic_value = {
     {ESP8266_CHIP,  0xfff0c101},
     {ESP32_CHIP,    0x00f01d83},
     {ESP32S2_CHIP,  0x000007c6},
+    {ESP32C2_CHIP,  0x6f51306f},
     {ESP32C3_CHIP,  0x6921506f},
     {ESP32S3_CHIP,  0x00000009},
 };
@@ -194,6 +195,12 @@ TEST_CASE( "Can detect attached target" )
         REQUIRE( esp_loader_get_target() == ESP32S2_CHIP );
     }
 
+    SECTION( "Can detect ESP32C2" ) {
+        queue_connect_response(ESP32C2_CHIP);
+        REQUIRE_SUCCESS( esp_loader_connect(&connect_config) );
+        REQUIRE( esp_loader_get_target() == ESP32C2_CHIP );
+    }
+
     SECTION( "Can detect ESP32C3" ) {
         queue_connect_response(ESP32C3_CHIP);
         REQUIRE_SUCCESS( esp_loader_connect(&connect_config) );