From 72a473184e511654f564ebd81158d5499e49b424 Mon Sep 17 00:00:00 2001 From: "Brian A. Ignacio" Date: Thu, 10 Oct 2024 16:50:24 +0800 Subject: [PATCH] add esp32c5 support (#155) * add esp32c5 support * update ci actions version * update crystal freq function * add stub ref in esp32c5 --- .github/workflows/ci.yml | 6 +- .github/workflows/pages.yml | 4 +- .github/workflows/publish.yml | 6 +- src/esploader.ts | 4 + src/targets/esp32c5.ts | 151 ++++++++++++++++++ .../stub_flasher/stub_flasher_32c5.json | 8 + 6 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 src/targets/esp32c5.ts create mode 100644 src/targets/stub_flasher/stub_flasher_32c5.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c3216eb6..5135c2d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,8 +12,8 @@ jobs: ci: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 16 registry-url: 'https://registry.npmjs.org' @@ -32,7 +32,7 @@ jobs: echo $ESPTOOLJS_VERSION echo "version=$ESPTOOLJS_VERSION" >> $GITHUB_OUTPUT - name: Upload npm package file - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4 with: name: esptool-js-${{ steps.version.outputs.version }}.tgz path: esptool-js-${{ steps.version.outputs.version }}.tgz \ No newline at end of file diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 37c2c0d0..ecb00ee7 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -25,9 +25,9 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Node - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: 18 - name: Install Node modules diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index aa168050..b29b3a23 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -7,8 +7,8 @@ jobs: publish: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 16 registry-url: "https://registry.npmjs.org" @@ -26,7 +26,7 @@ jobs: [ ${GITHUB_REF:11} = $(node -p "require('./package.json').version") ] echo "version=${GITHUB_REF:11}" >> $GITHUB_OUTPUT - name: Upload package file - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4 with: name: esptool-js-${{ steps.version.outputs.version }}.tgz path: esptool-js-${{ steps.version.outputs.version }}.tgz diff --git a/src/esploader.ts b/src/esploader.ts index 54a9399a..f60405ea 100644 --- a/src/esploader.ts +++ b/src/esploader.ts @@ -139,6 +139,10 @@ async function magic2Chip(magic: number): Promise { const { ESP32C6ROM } = await import("./targets/esp32c6.js"); return new ESP32C6ROM(); } + case 0x1101406f: { + const { ESP32C5ROM } = await import("./targets/esp32c5.js"); + return new ESP32C5ROM(); + } case 0xd7b73e80: { const { ESP32H2ROM } = await import("./targets/esp32h2.js"); return new ESP32H2ROM(); diff --git a/src/targets/esp32c5.ts b/src/targets/esp32c5.ts new file mode 100644 index 00000000..dc9cc285 --- /dev/null +++ b/src/targets/esp32c5.ts @@ -0,0 +1,151 @@ +import { ESPLoader } from "../esploader"; +import { ESP32C6ROM } from "./esp32c6"; +import ESP32C5_STUB from "./stub_flasher/stub_flasher_32c5.json"; + +export class ESP32C5ROM extends ESP32C6ROM { + public CHIP_NAME = "ESP32-C5"; + public IMAGE_CHIP_ID = 23; + + public EFUSE_BASE = 0x600b4800; + public EFUSE_BLOCK1_ADDR = this.EFUSE_BASE + 0x044; + public MAC_EFUSE_REG = this.EFUSE_BASE + 0x044; + public UART_CLKDIV_REG = 0x60000014; + + public TEXT_START = ESP32C5_STUB.text_start; + public ENTRY = ESP32C5_STUB.entry; + public DATA_START = ESP32C5_STUB.data_start; + public ROM_DATA = ESP32C5_STUB.data; + public ROM_TEXT = ESP32C5_STUB.text; + + public EFUSE_RD_REG_BASE = this.EFUSE_BASE + 0x030; // BLOCK0 read base address + + public EFUSE_PURPOSE_KEY0_REG = this.EFUSE_BASE + 0x34; + public EFUSE_PURPOSE_KEY0_SHIFT = 24; + public EFUSE_PURPOSE_KEY1_REG = this.EFUSE_BASE + 0x34; + public EFUSE_PURPOSE_KEY1_SHIFT = 28; + public EFUSE_PURPOSE_KEY2_REG = this.EFUSE_BASE + 0x38; + public EFUSE_PURPOSE_KEY2_SHIFT = 0; + public EFUSE_PURPOSE_KEY3_REG = this.EFUSE_BASE + 0x38; + public EFUSE_PURPOSE_KEY3_SHIFT = 4; + public EFUSE_PURPOSE_KEY4_REG = this.EFUSE_BASE + 0x38; + public EFUSE_PURPOSE_KEY4_SHIFT = 8; + public EFUSE_PURPOSE_KEY5_REG = this.EFUSE_BASE + 0x38; + public EFUSE_PURPOSE_KEY5_SHIFT = 12; + + public EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT_REG = this.EFUSE_RD_REG_BASE; + public EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT = 1 << 20; + + public EFUSE_SPI_BOOT_CRYPT_CNT_REG = this.EFUSE_BASE + 0x034; + public EFUSE_SPI_BOOT_CRYPT_CNT_MASK = 0x7 << 18; + + public EFUSE_SECURE_BOOT_EN_REG = this.EFUSE_BASE + 0x038; + public EFUSE_SECURE_BOOT_EN_MASK = 1 << 20; + + public IROM_MAP_START = 0x42000000; + public IROM_MAP_END = 0x42800000; + public DROM_MAP_START = 0x42800000; + public DROM_MAP_END = 0x43000000; + + public PCR_SYSCLK_CONF_REG = 0x60096110; + public PCR_SYSCLK_XTAL_FREQ_V = 0x7f << 24; + public PCR_SYSCLK_XTAL_FREQ_S = 24; + + public XTAL_CLK_DIVIDER = 1; + + public UARTDEV_BUF_NO = 0x4085f51c; // Variable in ROM .bss which indicates the port in use + + // Magic value for ESP32C5 + public CHIP_DETECT_MAGIC_VALUE = [0x1101406f]; + + public FLASH_FREQUENCY = { + "80m": 0xf, + "40m": 0x0, + "20m": 0x2, + }; + + public MEMORY_MAP = [ + [0x00000000, 0x00010000, "PADDING"], + [0x42800000, 0x43000000, "DROM"], + [0x40800000, 0x40860000, "DRAM"], + [0x40800000, 0x40860000, "BYTE_ACCESSIBLE"], + [0x4003a000, 0x40040000, "DROM_MASK"], + [0x40000000, 0x4003a000, "IROM_MASK"], + [0x42000000, 0x42800000, "IROM"], + [0x40800000, 0x40860000, "IRAM"], + [0x50000000, 0x50004000, "RTC_IRAM"], + [0x50000000, 0x50004000, "RTC_DRAM"], + [0x600fe000, 0x60100000, "MEM_INTERNAL2"], + ]; + + UF2_FAMILY_ID = 0xf71c0343; + + EFUSE_MAX_KEY = 5; + KEY_PURPOSES = { + 0: "USER/EMPTY", + 1: "ECDSA_KEY", + 2: "XTS_AES_256_KEY_1", + 3: "XTS_AES_256_KEY_2", + 4: "XTS_AES_128_KEY", + 5: "HMAC_DOWN_ALL", + 6: "HMAC_DOWN_JTAG", + 7: "HMAC_DOWN_DIGITAL_SIGNATURE", + 8: "HMAC_UP", + 9: "SECURE_BOOT_DIGEST0", + 10: "SECURE_BOOT_DIGEST1", + 11: "SECURE_BOOT_DIGEST2", + 12: "KM_INIT_KEY", + }; + + public async getPkgVersion(loader: ESPLoader): Promise { + const numWord = 2; + return ((await loader.readReg(this.EFUSE_BLOCK1_ADDR + 4 * numWord)) >> 26) & 0x07; + } + + public async getMinorChipVersion(loader: ESPLoader): Promise { + const numWord = 2; + return ((await loader.readReg(this.EFUSE_BLOCK1_ADDR + 4 * numWord)) >> 0) & 0x0f; + } + + public async getMajorChipVersion(loader: ESPLoader): Promise { + const numWord = 2; + return ((await loader.readReg(this.EFUSE_BLOCK1_ADDR + 4 * numWord)) >> 4) & 0x03; + } + + public async getChipDescription(loader: ESPLoader): Promise { + const pkgVer = await this.getPkgVersion(loader); + let desc: string; + if (pkgVer === 0) { + desc = "ESP32-C5"; + } else { + desc = "unknown ESP32-C5"; + } + const majorRev = await this.getMajorChipVersion(loader); + const minorRev = await this.getMinorChipVersion(loader); + return `${desc} (revision v${majorRev}.${minorRev})`; + } + + public async getCrystalFreq(loader: ESPLoader): Promise { + // The crystal detection algorithm of ESP32/ESP8266 + // works for ESP32-C5 as well. + const uartDiv = (await loader.readReg(this.UART_CLKDIV_REG)) & this.UART_CLKDIV_MASK; + const etsXtal = (loader.transport.baudrate * uartDiv) / 1000000 / this.XTAL_CLK_DIVIDER; + let normXtal; + if (etsXtal > 45) { + normXtal = 48; + } else if (etsXtal > 33) { + normXtal = 40; + } else { + normXtal = 26; + } + if (Math.abs(normXtal - etsXtal) > 1) { + loader.info("WARNING: Unsupported crystal in use"); + } + return normXtal; + } + + public async getCrystalFreqRomExpect(loader: ESPLoader) { + return ( + ((await loader.readReg(this.PCR_SYSCLK_CONF_REG)) & this.PCR_SYSCLK_XTAL_FREQ_V) >> this.PCR_SYSCLK_XTAL_FREQ_S + ); + } +} diff --git a/src/targets/stub_flasher/stub_flasher_32c5.json b/src/targets/stub_flasher/stub_flasher_32c5.json new file mode 100644 index 00000000..896e7009 --- /dev/null +++ b/src/targets/stub_flasher/stub_flasher_32c5.json @@ -0,0 +1,8 @@ +{ + "entry": 1082132164, + "text": "QREixCbCBsa39wBgEUc3BIRA2Mu39ABgEwQEANxAkYuR57JAIkSSREEBgoCIQBxAE3X1D4KX3bcBEbcHAGBOxoOphwBKyDcJhEAmylLEBs4izLcEAGB9WhMJCQDATBN09D8N4PJAYkQjqDQBQknSRLJJIkoFYYKAiECDJwkAE3X1D4KXfRTjGUT/yb8TBwAMlEGqh2MY5QCFR4XGI6AFAHlVgoAFR2OH5gAJRmONxgB9VYKAQgUTB7ANQYVjlecCiUecwfW3kwbADWMW1QCYwRMFAAyCgJMG0A19VWOV1wCYwRMFsA2CgLc1hUBBEZOFhboGxmE/Y0UFBrc3hUCThweyA6cHCAPWRwgTdfUPkwYWAMIGwYIjktcIMpcjAKcAA9dHCJFnk4cHBGMe9wI3t4RA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MTe3hECTCEcBglcDp4iwg6UNAB2MHY8+nLJXI6TosKqLvpUjoL0Ak4dKAZ2NAcWhZ2OX9QBahe/wb8sjoG0BCcTcRJnD409w92PfCwCTB3AMvbeFS7c9hUC3jIRAk40Nu5OMTAHpv+OdC5zcROOKB5yTB4AMqbeDp4sA45MHnO/wb9MJZRMFBXGX8H//54CgHO/w786X8H//54BgIVWyA6TLAOMPBJjv8O/QEwWAPpfwf//ngEAa7/CPzAKUUbLv8A/M9lBmVNZURlm2WSZalloGW/ZLZkzWTEZNtk0JYYKAAAA=", + "text_start": 1082130432, + "data": "FACEQG4KgEC+CoBAFguAQOQLgEBQDIBA/guAQDoJgECgC4BA4AuAQCoLgEDqCIBAXguAQOoIgEBICoBAjgqAQL4KgEAWC4BAWgqAQJ4JgEDOCYBAVgqAQKgOgEC+CoBAaA2AQGAOgEAqCIBAiA6AQCoIgEAqCIBAKgiAQCoIgEAqCIBAKgiAQCoIgEAqCIBABA2AQCoIgECGDYBAYA6AQA==", + "data_start": 1082469296, + "bss_start": 1082392576 +} \ No newline at end of file