Skip to content

Commit 0c2b1d4

Browse files
committed
esp32 flasher: Fixes reading of flash chip ID after attach operation
Closes #52. This commit fixes the problem with programming ESP-WROOM-32 modules with 16MB flash. It can happen that after calling esp_rom_spiflash_attach flash remains busy for some time. So when flasher stub reads flash chip ID just after attach operation it gets 0xFF value. This fix makes flash stub waiting for flash to be ready before reading it's ID. Also it fixes situation when OpenOCD hangs up during onboard algorithm execution with one CPU beeing stalled and its DSR does not indicate that it is stopped (other CPU in exception handler).
1 parent 6db5f0b commit 0c2b1d4

File tree

8 files changed

+282
-254
lines changed

8 files changed

+282
-254
lines changed

contrib/loaders/flash/esp32/stub_flasher.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,26 @@ static uint32_t stub_flash_exec_usr_cmd(uint32_t cmd)
482482
return status_value;
483483
}
484484

485+
static void stub_flash_spi_wait_ready()
486+
{
487+
uint32_t status_value = ESP_ROM_SPIFLASH_BUSY_FLAG;
488+
489+
while (ESP_ROM_SPIFLASH_BUSY_FLAG == (status_value & ESP_ROM_SPIFLASH_BUSY_FLAG)) {
490+
WRITE_PERI_REG(PERIPHS_SPI_FLASH_STATUS, 0); // clear regisrter
491+
WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_RDSR);
492+
while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0);
493+
status_value = READ_PERI_REG(PERIPHS_SPI_FLASH_STATUS) & (g_rom_spiflash_chip.status_mask);
494+
}
495+
}
496+
485497
static uint32_t stub_flash_spi_cmd_run(uint32_t cmd, uint8_t data_bits[], uint32_t data_bits_num, uint32_t read_bits_num)
486498
{
487499
uint32_t old_spi_usr = READ_PERI_REG(PERIPHS_SPI_FLASH_USRREG);
488500
uint32_t old_spi_usr2 = READ_PERI_REG(PERIPHS_SPI_FLASH_USRREG2);
489501
uint32_t flags = SPI_USR_COMMAND;
502+
503+
stub_flash_spi_wait_ready();
504+
490505
if (read_bits_num > 0) {
491506
flags |= SPI_USR_MISO;
492507
WRITE_PERI_REG(PERIPHS_SPI_MISO_DLEN_REG, read_bits_num - 1);
@@ -763,6 +778,15 @@ static int stub_flash_handler(int cmd, va_list ap)
763778

764779
esp_rom_spiflash_attach(spiconfig, 0);
765780
uint32_t flash_size = stub_flash_get_size();
781+
if (flash_size == 0) {
782+
STUB_LOGE("Failed to get flash size!\n");
783+
ret = cmd == ESP32_STUB_CMD_FLASH_SIZE ? 0 : ESP32_STUB_ERR_FAIL;
784+
goto _flash_end;
785+
}
786+
if (cmd == ESP32_STUB_CMD_FLASH_SIZE) {
787+
ret = flash_size;
788+
goto _flash_end;
789+
}
766790
esp_rom_spiflash_config_param(g_rom_flashchip.device_id, flash_size,
767791
ESP32_FLASH_BLOCK_SIZE, ESP32_FLASH_SECTOR_SIZE, ESP32_FLASH_PAGE_SIZE, ESP32_FLASH_STATUS_MASK);
768792

@@ -786,9 +810,6 @@ static int stub_flash_handler(int cmd, va_list ap)
786810
case ESP32_STUB_CMD_FLASH_WRITE:
787811
ret = stub_flash_write(arg1, arg2, arg3, arg4);
788812
break;
789-
case ESP32_STUB_CMD_FLASH_SIZE:
790-
ret = stub_flash_get_size();
791-
break;
792813
case ESP32_STUB_CMD_FLASH_MAP_GET:
793814
ret = stub_flash_get_map(arg1, arg2);
794815
break;

contrib/loaders/flash/esp32/stub_flasher_code.inc

Lines changed: 242 additions & 235 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
0xaf, 0x09, 0x09, 0x40, 0xa7, 0x0a, 0x09, 0x40, 0x8e, 0x0a, 0x09, 0x40,
2-
0x98, 0x0a, 0x09, 0x40, 0xee, 0x0b, 0x09, 0x40, 0xf6, 0x0b, 0x09, 0x40,
3-
0x03, 0x0c, 0x09, 0x40, 0x7a, 0x0c, 0x09, 0x40, 0x00, 0x00, 0x04, 0x00,
1+
0x0a, 0x0a, 0x09, 0x40, 0x07, 0x0b, 0x09, 0x40, 0xe8, 0x0a, 0x09, 0x40,
2+
0xf8, 0x0a, 0x09, 0x40, 0x34, 0x0d, 0x09, 0x40, 0x4b, 0x0c, 0x09, 0x40,
3+
0x55, 0x0c, 0x09, 0x40, 0xca, 0x0c, 0x09, 0x40, 0x00, 0x00, 0x04, 0x00,
44
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x20, 0x00,
55
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define ESP32_STUB_BSS_SIZE 0x0000040UL
22

3-
#define ESP32_STUB_ENTRY_ADDR 0x04009085cUL
3+
#define ESP32_STUB_ENTRY_ADDR 0x04009089cUL
44

55
//#define ESP32_STUB_BUILD_IDF_REV 0905aa010

src/flash/nor/esp32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ static uint32_t esp32_get_size(struct flash_bank *bank)
247247
int ret = esp32_init_flasher_image(&flasher_image);
248248
if (ret != ERROR_OK) {
249249
LOG_ERROR("Faied to init flasher image (%d)!", ret);
250-
return ret;
250+
return 0;
251251
}
252252
ret = esp32_run_func_image(bank->target, &run, &flasher_image, 1, ESP32_STUB_CMD_FLASH_SIZE);
253253
image_close(&flasher_image.image);
254254
if (ret != ERROR_OK) {
255255
LOG_ERROR("Faied to run flasher stub (%d)!", ret);
256-
return ret;
256+
return 0;
257257
}
258258
size = run.ret_code;
259259
if (size == 0) {

src/target/esp32.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,9 @@ static int xtensa_poll(struct target *target)
16251625
bool algo_stopped = false;
16261626
if (target->state == TARGET_DEBUG_RUNNING) {
16271627
if (cores_num == ESP32_CPU_COUNT) {
1628-
algo_stopped = (dsr[0] & OCDDSR_STOPPED) && (dsr[1] & OCDDSR_STOPPED);
1628+
/* algo can be run on any CPU while other one can be stalled by SW run on target, in this case OCDDSR_STOPPED will not be set for other CPU;
1629+
situation when target is in TARGET_DEBUG_RUNNING and both CPUs are stalled is impossible (one must run algo). */
1630+
algo_stopped = (dsr[0] & (OCDDSR_STOPPED|OCDDSR_RUNSTALLSAMPLE)) && (dsr[1] & (OCDDSR_STOPPED|OCDDSR_RUNSTALLSAMPLE));
16291631
} else {
16301632
algo_stopped = (dsr[0] & OCDDSR_STOPPED);
16311633
}
@@ -2156,8 +2158,7 @@ static int esp32_algo_run(struct target *target, struct esp32_algo_image *image,
21562158
&run->priv.stub.ainfo);
21572159
if (retval != ERROR_OK) {
21582160
LOG_ERROR("Faied to wait algorithm (%d)!", retval);
2159-
target_halt(target);
2160-
target_wait_state(target, TARGET_HALTED, ESP32_TARGET_STATE_TMO);
2161+
// target has been forced to stop in target_wait_algorithm()
21612162
}
21622163
#if ESP32_STUB_STACK_DEBUG
21632164
retval = esp32_stub_check_stack(target, run->priv.stub.stack_addr - run->stack_size, run->stack_size, ESP32_STUB_STACK_DEBUG);

testing/esp/debug_backend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def start(toolch, oocd_path, oocd_tcl_dir, oocd_cfg_files):
3434
raise e
3535

3636
def stop():
37-
# _gdb_inst.disconnect()
3837
_oocd_inst.stop()
3938
_oocd_inst.join()
4039

@@ -300,9 +299,9 @@ def target_reset(self, action='halt'):
300299
def target_download(self):
301300
raise NotImplementedError('target_download')
302301

303-
def target_program(self, file_name, off, actions='verify'):
302+
def target_program(self, file_name, off, actions='verify', tmo=30):
304303
# actions can be any or both of 'verify reset'
305-
self.monitor_run('program_esp32 %s %s 0x%x' % (file_name, actions, off), 30)
304+
self.monitor_run('program_esp32 %s %s 0x%x' % (file_name, actions, off), tmo)
306305

307306
def exec_file_set(self, file_path):
308307
# -file-exec-and-symbols file

testing/esp/test_flasher.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ def test_big_binary(self):
3434
for i in range(size):
3535
fbin.write(os.urandom(1024))
3636
fbin.close()
37-
self.gdb.monitor_run('program_esp32 %s 0x%x' % (fname1, ESP32_APP_FLASH_OFF + ESP32_APP_FLASH_SZ), tmo=40)
38-
# since we can not get result from OpenOCD (output parsing seems not tp be good idea),
39-
# we nee to read written flash and compare data manually
37+
self.gdb.target_program(fname1, ESP32_APP_FLASH_OFF + ESP32_APP_FLASH_SZ, actions='', tmo=130)
38+
# since we can not get result from OpenOCD (output parsing seems not to be good idea),
39+
# we need to read written flash and compare data manually
4040
fhnd,fname2 = tempfile.mkstemp()
4141
fbin = os.fdopen(fhnd, 'wb')
4242
fbin.close()
43-
self.gdb.monitor_run('flash read_bank 0 %s 0x%x %d' % (fname2, ESP32_APP_FLASH_OFF + ESP32_APP_FLASH_SZ, size*1024), tmo=30)
43+
self.gdb.monitor_run('flash read_bank 0 %s 0x%x %d' % (fname2, ESP32_APP_FLASH_OFF + ESP32_APP_FLASH_SZ, size*1024), tmo=120)
4444
self.assertTrue(filecmp.cmp(fname1, fname2))
4545

4646

0 commit comments

Comments
 (0)