Skip to content

Commit

Permalink
Merge pull request #17 from adafruit/fix-s3-reset
Browse files Browse the repository at this point in the history
Fix s3 reset
  • Loading branch information
ladyada authored Mar 22, 2023
2 parents b5a2854 + 98ca14c commit 30d2ead
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/githubci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: '3.x'

- uses: actions/checkout@v3

- name: Run pre-commit
uses: pre-commit/[email protected]

- uses: actions/checkout@v3
with:
repository: adafruit/ci-arduino
Expand Down
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
#
# SPDX-License-Identifier: Unlicense

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: trailing-whitespace
- id: end-of-file-fixer

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v15.0.7
hooks:
- id: clang-format
exclude: ^(examples)
types_or: [c++, c, header]

- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
hooks:
- id: codespell
exclude: ^(tools/esp_stub.py)
args: [-w]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Used internally for making testers, to reduce code replication!

Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!

Written by Limor Fried for Adafruit Industries.
Written by Limor Fried for Adafruit Industries.
BSD license, check license.txt for more information
All text above must be included in any redistribution

Expand Down
2 changes: 1 addition & 1 deletion code-of-conduct.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Examples of unacceptable behavior by participants include:

The goal of the standards and moderation guidelines outlined here is to build
and maintain a respectful community. We ask that you don’t just aim to be
"technically unimpeachable", but rather try to be your best self.
"technically unimpeachable", but rather try to be your best self.

We value many things beyond technical expertise, including collaboration and
supporting others within our community. Providing a positive experience for
Expand Down
1 change: 0 additions & 1 deletion examples/Brain/program_esp32_uart/program_esp32_uart.ino
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,3 @@ void setup1() {
void loop1() {
yield();
}

Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,3 @@ void setup1() {
void loop1() {
yield();
}

2 changes: 1 addition & 1 deletion examples/Brain/program_rp2040_uf2/program_rp2040_uf2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void setup() {
// prepare SD Card
prepare_sd();

// wait for USB filesytem is mounted. USB host bit-banging and task is
// wait for USB filesystem is mounted. USB host bit-banging and task is
// processed on core1
while (!is_usbfs_mounted) delay(10);

Expand Down
2 changes: 1 addition & 1 deletion examples/I2C_Scan/I2C_Scan.ino
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ void loop() {
#endif

delay(3000); // wait 3 seconds
}
}
2 changes: 1 addition & 1 deletion examples/MCP4725/MCP4725.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void loop(void) {
Serial.println("I2C 0x63 not found");
}
Serial.println("I2C 0x63 found");

Serial.println("Test OK!");
//TB.beepNblink();
delay(500);
Expand Down
35 changes: 29 additions & 6 deletions src/Adafruit_TestBed_Brains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ Adafruit_TestBed_Brains::Adafruit_TestBed_Brains() {
_target_swdclk = 3;

dap = NULL;

esp32boot = NULL;
_esp32_flash_defl = false;
_esp32_chip_detect = 0;
_esp32s3_in_reset = false;
}

void Adafruit_TestBed_Brains::begin(void) {
Expand Down Expand Up @@ -139,6 +142,17 @@ void Adafruit_TestBed_Brains::targetReset(uint32_t reset_ms) {
digitalWrite(_target_rst, LOW);
delay(reset_ms);
digitalWrite(_target_rst, HIGH);

// Note: S3 has an USB-OTG errata
// https://www.espressif.com/sites/default/files/documentation/esp32-s3_errata_en.pdf
// which is walkarounded by idf/arduino-esp32 to always mux JTAG to USB for
// uploading and/or power on. Afterwards USB-OTG will be set up if selected
// so. However rp2040 USBH is running too fast and can actually retrieve
// device/configuration descriptor of JTAG before the OTG is fully setup.
// Mark this for application usage
if (_esp32_chip_detect == CHIP_DETECT_MAGIC_ESP32S3) {
_esp32s3_in_reset = true;
}
}

//--------------------------------------------------------------------+
Expand Down Expand Up @@ -406,15 +420,15 @@ bool Adafruit_TestBed_Brains::esp32_begin(ESP32BootROMClass *bootrom,
esp32boot = bootrom;

LCD_printf("Syncing ESP32");
bool ret = esp32boot->begin(baudrate);
_esp32_chip_detect = esp32boot->begin(baudrate);

if (ret) {
if (_esp32_chip_detect) {
LCD_printf("Synced OK");
return true;
} else {
LCD_printf_error("Sync failed!");
return false;
}

return ret;
}

void Adafruit_TestBed_Brains::esp32_end(bool reset_esp) {
Expand All @@ -433,6 +447,14 @@ void Adafruit_TestBed_Brains::esp32_end(bool reset_esp) {
esp32boot->end();
}

bool Adafruit_TestBed_Brains::esp32_s3_inReset(void) {
return _esp32s3_in_reset;
}

void Adafruit_TestBed_Brains::esp32_s3_clearReset(void) {
_esp32s3_in_reset = false;
}

size_t
Adafruit_TestBed_Brains::esp32_programFlashDefl(const esp32_zipfile_t *zfile,
uint32_t addr) {
Expand All @@ -447,7 +469,7 @@ Adafruit_TestBed_Brains::esp32_programFlashDefl(const esp32_zipfile_t *zfile,
esp32boot->md5Flash(addr, zfile->uncompressed_len, esp_md5);
Serial.printf("Flash MD5: ");
for (size_t i = 0; i < 16; i++) {
Serial.printf("%02X ", esp_md5[i]);
Serial.printf("%02X ", esp_md5[i]);
}
Serial.println();
if (0 == memcmp(zfile->md5, esp_md5, 16)) {
Expand Down Expand Up @@ -729,7 +751,8 @@ void Adafruit_TestBed_Brains::setColor(uint32_t color) {

void Adafruit_TestBed_Brains::lcd_write(uint8_t linenum, char linebuf[17]) {
// wait for semaphore to release
while (LCD_semaphore) yield();
while (LCD_semaphore)
yield();
LCD_semaphore = true;
// fill the rest with spaces
memset(linebuf + strlen(linebuf), ' ', 16 - strlen(linebuf));
Expand Down
5 changes: 5 additions & 0 deletions src/Adafruit_TestBed_Brains.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class Adafruit_TestBed_Brains : public Adafruit_TestBed {
// program flash with compressed using zipfile struct
size_t esp32_programFlashDefl(const esp32_zipfile_t *zfile, uint32_t addr);

bool esp32_s3_inReset(void);
void esp32_s3_clearReset(void);

//--------------------------------------------------------------------+
// Public Variables
//--------------------------------------------------------------------+
Expand Down Expand Up @@ -137,6 +140,8 @@ class Adafruit_TestBed_Brains : public Adafruit_TestBed {
int _target_swdclk;

bool _esp32_flash_defl;
uint32_t _esp32_chip_detect;
bool _esp32s3_in_reset;

void lcd_write(uint8_t linenum, char buf[17]);
};
Expand Down
18 changes: 6 additions & 12 deletions src/ESP32BootROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ enum {
ESP_WRITE_REG = 0x09,
ESP_READ_REG = 0x0A,

// Some comands supported by ESP32 and later chips ROM bootloader (or -8266 w/
// stub)
// Some commands supported by ESP32 and later chips ROM bootloader (or -8266
// with stub)
ESP_SPI_SET_PARAMS = 0x0B,
ESP_SPI_ATTACH = 0x0D,
ESP_READ_FLASH_SLOW = 0x0E, // ROM only, much slower than the stub flash read
Expand Down Expand Up @@ -144,12 +144,6 @@ enum {
USB_JTAG_SERIAL_PID = 0x1001
};

enum {
CHIP_DETECT_MAGIC_ESP32 = 0x00F01D83,
CHIP_DETECT_MAGIC_ESP32S2 = 0x000007C6,
CHIP_DETECT_MAGIC_ESP32S3 = 0x9,
};

static inline uint32_t div_ceil(uint32_t v, uint32_t d) {
return (v + d - 1) / d;
}
Expand Down Expand Up @@ -185,7 +179,7 @@ void ESP32BootROMClass::resetBootloader(void) {
digitalWrite(_gpio0Pin, HIGH);
}

int ESP32BootROMClass::begin(unsigned long baudrate) {
uint32_t ESP32BootROMClass::begin(unsigned long baudrate) {
_serial->begin(ESP_ROM_BAUD);

pinMode(_gpio0Pin, OUTPUT);
Expand Down Expand Up @@ -265,7 +259,7 @@ int ESP32BootROMClass::begin(unsigned long baudrate) {
}
}

return 1;
return chip_detect;
}

void ESP32BootROMClass::end() {
Expand Down Expand Up @@ -393,10 +387,10 @@ bool ESP32BootROMClass::dataFlashDefl(const void *data, uint32_t len) {
header[3] = 0;

command(ESP_FLASH_DEFL_DATA, header, sizeof(header), data, len);
DBG_PRINTF("FLASH_DEFL_DATA...%d", millis()-stamp);
DBG_PRINTF("FLASH_DEFL_DATA...%d", millis() - stamp);

bool b = response(ESP_FLASH_DEFL_DATA, 3000);
DBG_PRINTF(": %d\t", millis()-stamp);
DBG_PRINTF(": %d\t", millis() - stamp);

return (b == 0);
}
Expand Down
9 changes: 8 additions & 1 deletion src/ESP32BootROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

#define ESP32_DEFAULT_TIMEOUT 3000

enum {
CHIP_DETECT_MAGIC_ESP32 = 0x00F01D83,
CHIP_DETECT_MAGIC_ESP32S2 = 0x000007C6,
CHIP_DETECT_MAGIC_ESP32S3 = 0x9,
};

typedef struct {
uint32_t entry;
uint32_t text_start;
Expand All @@ -39,7 +45,8 @@ class ESP32BootROMClass {
public:
ESP32BootROMClass(HardwareSerial &hwSerial, int gpio0Pin, int resetnPin);

int begin(unsigned long baudrate);
// return chip detect magic if success, otherwise 0
uint32_t begin(unsigned long baudrate);
void end();

bool read_reg(uint32_t regAddr, uint32_t *regValue,
Expand Down
2 changes: 1 addition & 1 deletion tools/esp_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def print_array(payload):
def print_stub(mcu):
data = base64.b64decode(stub[mcu]["data"])
text = base64.b64decode(stub[mcu]["text"])

print("//------------- {} -------------//".format(mcu))
print ("const uint8_t _stub_%s_data[%d] = {" % (mcu, len(data)))
print_array(data)
Expand Down

0 comments on commit 30d2ead

Please sign in to comment.