Martin Valik 5 лет назад
Родитель
Сommit
1c0635e136
5 измененных файлов с 42 добавлено и 10 удалено
  1. 3 2
      include/esp_loader.h
  2. 23 5
      src/esp_targets.c
  3. 8 3
      src/serial_comm.c
  4. 1 0
      test/run_test.sh
  5. 7 0
      test/test.cpp

+ 3 - 2
include/esp_loader.h

@@ -56,8 +56,9 @@ typedef enum {
     ESP8266_CHIP = 0,
     ESP32_CHIP   = 1,
     ESP32S2_CHIP = 2,
-    ESP_MAX_CHIP = 3,
-    ESP_UNKNOWN_CHIP = 3 
+    ESP32C3_CHIP = 3,
+    ESP_MAX_CHIP = 4,
+    ESP_UNKNOWN_CHIP = 4
 } target_chip_t;
 
 /**

+ 23 - 5
src/esp_targets.c

@@ -30,10 +30,11 @@ typedef struct {
 
 #define ESP8266_SPI_REG_BASE 0x60000200
 #define ESP32S2_SPI_REG_BASE 0x3f402000
+#define ESP32C3_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);
-static esp_loader_error_t spi_config_esp32s2(uint32_t efuse_base, uint32_t *spi_config);
+static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_config);
 
 static const esp_target_t esp_target[ESP_MAX_CHIP] = {
 
@@ -82,8 +83,24 @@ static const esp_target_t esp_target[ESP_MAX_CHIP] = {
         },
         .efuse_base = 0x3f41A000,
         .chip_magic_value  = 0x000007c6,
-        .read_spi_config = spi_config_esp32s2,
-    }
+        .read_spi_config = spi_config_esp32xx,
+    },
+
+    // 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,
+        },
+        .efuse_base = 0x60008800,
+        .chip_magic_value = 0x6921506f,
+        .read_spi_config = spi_config_esp32xx,
+    },
 };
 
 const target_registers_t *get_esp_target_data(target_chip_t chip)
@@ -99,7 +116,7 @@ esp_loader_error_t loader_detect_chip(target_chip_t *target_chip, const target_r
     for (int chip = 0; chip < ESP_MAX_CHIP; chip++) {
         if (magic_value == esp_target[chip].chip_magic_value) {
             *target_chip = (target_chip_t)chip;
-            *target_data = (target_registers_t*)&esp_target[chip];
+            *target_data = (target_registers_t *)&esp_target[chip];
             return ESP_LOADER_SUCCESS;
         }
     }
@@ -154,7 +171,8 @@ static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_co
     return ESP_LOADER_SUCCESS;
 }
 
-static esp_loader_error_t spi_config_esp32s2(uint32_t efuse_base, uint32_t *spi_config)
+// Applies for esp32s2 and esp32c3
+static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_config)
 {
     *spi_config = 0;
 

+ 8 - 3
src/serial_comm.c

@@ -236,6 +236,11 @@ 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) ? 0 : sizeof(uint32_t);
+}
+
 
 esp_loader_error_t loader_flash_begin_cmd(uint32_t offset,
                                           uint32_t erase_size,
@@ -243,13 +248,13 @@ esp_loader_error_t loader_flash_begin_cmd(uint32_t offset,
                                           uint32_t blocks_to_write,
                                           target_chip_t target)
 {
-    size_t removeEncryption = target == ESP32S2_CHIP ? 0 : sizeof(uint32_t);
+    uint32_t encryption_size = encryption_field_size(target);
 
     begin_command_t begin_cmd = {
         .common = {
             .direction = WRITE_DIRECTION,
             .command = FLASH_BEGIN,
-            .size = CMD_SIZE(begin_cmd) - removeEncryption,
+            .size = CMD_SIZE(begin_cmd) - encryption_size,
             .checksum = 0
         },
         .erase_size = erase_size,
@@ -261,7 +266,7 @@ esp_loader_error_t loader_flash_begin_cmd(uint32_t offset,
 
     s_sequence_number = 0;
 
-    return send_cmd(&begin_cmd.common, sizeof(begin_cmd) - removeEncryption, NULL);
+    return send_cmd(&begin_cmd, sizeof(begin_cmd) - encryption_size, NULL);
 }
 
 

+ 1 - 0
test/run_test.sh

@@ -1,5 +1,6 @@
 #!/bin/bash
 
+set -e
 mkdir -p build && cd build
 
 if [ "$1" = "host" ]; then

+ 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},
+    {ESP32C3_CHIP,  0x6921506f},
 };
 
 void queue_connect_response(target_chip_t target = ESP32_CHIP)
@@ -193,6 +194,12 @@ TEST_CASE( "Can detect attached target" )
         REQUIRE( esp_loader_get_target() == ESP32S2_CHIP );
     }
 
+    SECTION( "Can detect ESP32C3" ) {
+        queue_connect_response(ESP32C3_CHIP);
+        REQUIRE_SUCCESS( esp_loader_connect(&connect_config) );
+        REQUIRE( esp_loader_get_target() == ESP32C3_CHIP );
+    }
+
     SECTION( "Can detect ESP8266" ) {
         queue_connect_response(ESP8266_CHIP);
         REQUIRE_SUCCESS( esp_loader_connect(&connect_config) );