Skip to content

feat: Add auto-detect ESP chip via security info and improve test coverage#12

Merged
deadprogram merged 1 commit intotinygo-org:mainfrom
gandarez:feat/auto-detect-esp
Mar 19, 2026
Merged

feat: Add auto-detect ESP chip via security info and improve test coverage#12
deadprogram merged 1 commit intotinygo-org:mainfrom
gandarez:feat/auto-detect-esp

Conversation

@gandarez
Copy link
Contributor

@gandarez gandarez commented Mar 13, 2026

  • Refactor chip detection from magic-register-only approach to use GET_SECURITY_INFO command with ChipID fallback via readReg.
  • Add security_info.go for parsing security info responses (12/20 byte variants) and security flag decoding.
  • Expand protocol layer with securityInfo command support and improve test coverage across flasher, protocol, and security_info packages.

This PR is essential to get esp32c6 support in this PR

@deadprogram
Copy link
Member

I was just able to do a little testing on my machine. With this PR, the auto-detection no longer works on esp8266 or esp32.

@deadprogram
Copy link
Member

@gandarez with this patch to your PR, I was able to flash esp3266 as well as esp32s3 boards:

diff --git a/pkg/espflasher/flasher.go b/pkg/espflasher/flasher.go
index 136b86a..cd9eab8 100644
--- a/pkg/espflasher/flasher.go
+++ b/pkg/espflasher/flasher.go
@@ -256,30 +256,29 @@ synced:
 // detectChip identifies the connected ESP chip.
 func (f *Flasher) detectChip() (*chipDef, error) {
        si, err := f.readSecurityInfo()
-       if err != nil {
-               return nil, fmt.Errorf("detect chip: %w", err)
-       }
-
-       for _, def := range chipDefs {
-               if def.UsesMagicValue {
-                       continue
-               }
+       if err == nil {
+               for _, def := range chipDefs {
+                       if def.UsesMagicValue {
+                               continue
+                       }
 
-               if si.ChipID != nil && *si.ChipID == uint32(def.ImageChipID) {
-                       def.SecureDownloadMode = si.ParsedFlags.SecureDownloadEnable
-                       return def, nil
+                       if si.ChipID != nil && *si.ChipID == uint32(def.ImageChipID) {
+                               def.SecureDownloadMode = si.ParsedFlags.SecureDownloadEnable
+                               return def, nil
+                       }
                }
        }
 
-       var magic uint32
-
-       // Try to read the chip magic value to verify the chip type (ESP8266, ESP32, ESP32-S2)
-       magic, err = f.conn.readReg(chipDetectMagicRegAddr)
-       if err != nil {
+       // Otherwise, try to read the chip magic value to verify the chip type (ESP8266, ESP32, ESP32-S2)
+       magic, err := f.conn.readReg(chipDetectMagicRegAddr)
+       switch {
+       case err != nil && si != nil:
                // Only ESP32-S2 does not support chip id detection
                // and supports secure download mode
                chipDefs[ChipESP32S2].SecureDownloadMode = si.ParsedFlags.SecureDownloadEnable
                return chipDefs[ChipESP32S2], nil
+       case err != nil:
+               return nil, fmt.Errorf("detect chip: %w", err)
        }
 
        // Check magic value for older chips (ESP8266, ESP32, ESP32-S2)

@deadprogram
Copy link
Member

@gandarez did you see my proposed patch to this PR? What do you think?

@gandarez
Copy link
Contributor Author

@deadprogram wdyt the proposed changes in this commit? Idea is to log when read security fails and fall though.

@deadprogram
Copy link
Member

@deadprogram wdyt the proposed changes in this commit? Idea is to log when read security fails and fall though.

Can you please add that to this PR so I can try it out?

@deadprogram
Copy link
Member

@deadprogram wdyt the proposed changes in this commit? Idea is to log when read security fails and fall though.

Can you please add that to this PR so I can try it out?

I just saw you already did that. Trying again now...

@deadprogram
Copy link
Member

@gandarez please see my small wording change. It does in fact appear to do what you were saying and worked on my esp8266 and esp32 boards, as well as esp32-s3.

@gandarez gandarez force-pushed the feat/auto-detect-esp branch from 784c320 to cdc2460 Compare March 19, 2026 12:47
@gandarez
Copy link
Contributor Author

@deadprogram change made and commits squashed

@gandarez gandarez force-pushed the feat/auto-detect-esp branch from cdc2460 to 8c7d789 Compare March 19, 2026 12:56
…erage

Refactor chip detection from magic-register-only approach to use
GET_SECURITY_INFO command with ChipID fallback via readReg. Add
security_info.go for parsing security info responses (12/20 byte
variants) and security flag decoding. Expand protocol layer with
securityInfo command support and improve test coverage across
flasher, protocol, and security_info packages.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gandarez gandarez force-pushed the feat/auto-detect-esp branch from 8c7d789 to 1368642 Compare March 19, 2026 16:13
@deadprogram
Copy link
Member

Thank you very much for the additions @gandarez now merging...

@deadprogram deadprogram merged commit 636e9df into tinygo-org:main Mar 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants