1
2
3
7
8
9
10
11
12
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
75
76
77
78
79
83
84
85
86
87
88
89
90
91
92
93
94
96
97
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
144
145
146
147
148
152
153
154
155
159
160
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
200
201
202
203
204
205
206
213
214
215
216
217
221
222
223
224
225
226
227
228
232
233
237
238
242
243
247
248
249
250
251
252
253
257
258
259
260
261
262
263
264
268
269
270
271
280
281
282
283
287
288
289
290
291
292
293
294
295
296
297
301
302
303
304
305
306
307
308
312
313
317
318
319
323
324
328
329
330
334
335
339
340
341
345
346
347
351
352
353
357
358
359
360
364
365
372
373
382
383
384
385
386
387
388
389
390
412
413
414
415
416
417
422
423
424
425
426
427
428
429
430
431
432
437
438
442
443
446
447
448
449
456
457
458
459
463
464
465
470
471
472
473
474
475
476
477
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/time_support.h>
#include "assert.h"
#include <target/target.h>
#include <target/target_type.h>
#include <target/semihosting_common.h>
#include "esp_xtensa.h"
#include "esp_xtensa_semihosting.h"
7 includes
#define ESP32_S2_RTC_DATA_LOW 0x50000000
#define ESP32_S2_RTC_DATA_HIGH 0x50002000
#define ESP32_S2_DR_REG_LOW 0x3f400000
#define ESP32_S2_DR_REG_HIGH 0x3f4d3FFC
#define ESP32_S2_SYS_RAM_LOW 0x60000000UL
#define ESP32_S2_SYS_RAM_HIGH (ESP32_S2_SYS_RAM_LOW + 0x20000000UL)
#define ESP32_S2_WDT_WKEY_VALUE 0x50d83aa1
#define ESP32_S2_TIMG0_BASE 0x3f41F000
#define ESP32_S2_TIMG1_BASE 0x3f420000
#define ESP32_S2_TIMGWDT_CFG0_OFF 0x48
#define ESP32_S2_TIMGWDT_PROTECT_OFF 0x64
#define ESP32_S2_TIMG0WDT_CFG0 (ESP32_S2_TIMG0_BASE + ESP32_S2_TIMGWDT_CFG0_OFF)
#define ESP32_S2_TIMG1WDT_CFG0 (ESP32_S2_TIMG1_BASE + ESP32_S2_TIMGWDT_CFG0_OFF)
#define ESP32_S2_TIMG0WDT_PROTECT (ESP32_S2_TIMG0_BASE + ESP32_S2_TIMGWDT_PROTECT_OFF)
#define ESP32_S2_TIMG1WDT_PROTECT (ESP32_S2_TIMG1_BASE + ESP32_S2_TIMGWDT_PROTECT_OFF)
#define ESP32_S2_RTCCNTL_BASE 0x3f408000
#define ESP32_S2_RTCWDT_CFG_OFF 0x94
#define ESP32_S2_RTCWDT_PROTECT_OFF 0xAC
#define ESP32_S2_SWD_CONF_OFF 0xB0
#define ESP32_S2_SWD_WPROTECT_OFF 0xB4
#define ESP32_S2_RTC_CNTL_DIG_PWC_REG_OFF 0x8C
#define ESP32_S2_RTC_CNTL_DIG_PWC_REG (ESP32_S2_RTCCNTL_BASE + ESP32_S2_RTC_CNTL_DIG_PWC_REG_OFF)
#define ESP32_S2_RTCWDT_CFG (ESP32_S2_RTCCNTL_BASE + ESP32_S2_RTCWDT_CFG_OFF)
#define ESP32_S2_RTCWDT_PROTECT (ESP32_S2_RTCCNTL_BASE + ESP32_S2_RTCWDT_PROTECT_OFF)
#define ESP32_S2_SWD_CONF_REG (ESP32_S2_RTCCNTL_BASE + ESP32_S2_SWD_CONF_OFF)
#define ESP32_S2_SWD_WPROTECT_REG (ESP32_S2_RTCCNTL_BASE + ESP32_S2_SWD_WPROTECT_OFF)
#define ESP32_S2_SWD_AUTO_FEED_EN_M BIT(31)
#define ESP32_S2_SWD_WKEY_VALUE 0x8F1D312AU
#define ESP32_S2_OPTIONS0 (ESP32_S2_RTCCNTL_BASE + 0x0000)
#define ESP32_S2_SW_SYS_RST_M 0x80000000
#define ESP32_S2_SW_SYS_RST_V 0x1
#define ESP32_S2_SW_SYS_RST_S 31
#define ESP32_S2_SW_STALL_PROCPU_C0_M ((ESP32_S2_SW_STALL_PROCPU_C0_V) << (ESP32_S2_SW_STALL_PROCPU_C0_S))
#define ESP32_S2_SW_STALL_PROCPU_C0_V 0x3
#define ESP32_S2_SW_STALL_PROCPU_C0_S 2
#define ESP32_S2_SW_CPU_STALL (ESP32_S2_RTCCNTL_BASE + 0x00B8)
#define ESP32_S2_SW_STALL_PROCPU_C1_M ((ESP32_S2_SW_STALL_PROCPU_C1_V) << (ESP32_S2_SW_STALL_PROCPU_C1_S))
#define ESP32_S2_SW_STALL_PROCPU_C1_V 0x3FU
#define ESP32_S2_SW_STALL_PROCPU_C1_S 26
#define ESP32_S2_CLK_CONF (ESP32_S2_RTCCNTL_BASE + 0x0074)
#define ESP32_S2_CLK_CONF_DEF 0x1583218
#define ESP32_S2_STORE4 (ESP32_S2_RTCCNTL_BASE + 0x00BC)
#define ESP32_S2_STORE5 (ESP32_S2_RTCCNTL_BASE + 0x00C0)
#define ESP32_S2_DPORT_PMS_OCCUPY_3 0x3F4C10E0
#define ESP32_S2_TRACEMEM_BLOCK_SZ 0x4000
#define ESP32_S2_DR_REG_UART_BASE 0x3f400000
#define ESP32_S2_REG_UART_BASE(i) (ESP32_S2_DR_REG_UART_BASE + (i) * 0x10000)
#define ESP32_S2_UART_DATE_REG(i) (ESP32_S2_REG_UART_BASE(i) + 0x74)
48 definesstruct esp32s2_common {
struct esp_xtensa_common esp_xtensa;
...};
static int esp32s2_soc_reset(struct target *target);
static int esp32s2_disable_wdts(struct target *target);
static int esp32s2_assert_reset(struct target *target)
{
return ERROR_OK;
}{ ... }
static int esp32s2_deassert_reset(struct target *target)
{
struct xtensa *xtensa = target_to_xtensa(target);
LOG_TARGET_DEBUG(target, "begin");
int res = xtensa_deassert_reset(target);
if (res != ERROR_OK)
return res;
/* ... */
res = xtensa_smpbreak_write(xtensa, xtensa->smp_break);
if (res != ERROR_OK) {
LOG_ERROR("Failed to restore smpbreak (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int esp32s2_soft_reset_halt(struct target *target)
{
LOG_TARGET_DEBUG(target, "begin");
int res = esp32s2_soc_reset(target);
if (res != ERROR_OK)
return res;
return xtensa_soft_reset_halt(target);
}{ ... }
static int esp32s2_set_peri_reg_mask(struct target *target,
target_addr_t addr,
uint32_t mask,
uint32_t val)
{
uint32_t reg_val;
int res = target_read_u32(target, addr, ®_val);
if (res != ERROR_OK)
return res;
reg_val = (reg_val & (~mask)) | val;
res = target_write_u32(target, addr, reg_val);
if (res != ERROR_OK)
return res;
return ERROR_OK;
}{ ... }
static int esp32s2_stall_set(struct target *target, bool stall)
{
LOG_TARGET_DEBUG(target, "begin");
int res = esp32s2_set_peri_reg_mask(target,
ESP32_S2_SW_CPU_STALL,
ESP32_S2_SW_STALL_PROCPU_C1_M,
stall ? 0x21U << ESP32_S2_SW_STALL_PROCPU_C1_S : 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_SW_CPU_STALL (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = esp32s2_set_peri_reg_mask(target,
ESP32_S2_OPTIONS0,
ESP32_S2_SW_STALL_PROCPU_C0_M,
stall ? 0x2 << ESP32_S2_SW_STALL_PROCPU_C0_S : 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_OPTIONS0 (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static inline int esp32s2_stall(struct target *target)
{
return esp32s2_stall_set(target, true);
}{ ... }
static inline int esp32s2_unstall(struct target *target)
{
return esp32s2_stall_set(target, false);
}{ ... }
/* ... */
static int esp32s2_soc_reset(struct target *target)
{
int res;
struct xtensa *xtensa = target_to_xtensa(target);
LOG_DEBUG("start");
if (target->state != TARGET_HALTED) {
LOG_TARGET_DEBUG(target, "Target not halted before SoC reset, trying to halt it first");
xtensa_halt(target);
res = target_wait_state(target, TARGET_HALTED, 1000);
if (res != ERROR_OK) {
LOG_TARGET_DEBUG(target, "Couldn't halt target before SoC reset, trying to do reset-halt");
res = xtensa_assert_reset(target);
if (res != ERROR_OK) {
LOG_TARGET_ERROR(
target,
"Couldn't halt target before SoC reset! (xtensa_assert_reset returned %d)",
res);
return res;
}if (res != ERROR_OK) { ... }
alive_sleep(10);
xtensa_poll(target);
int reset_halt_save = target->reset_halt;
target->reset_halt = 1;
res = xtensa_deassert_reset(target);
target->reset_halt = reset_halt_save;
if (res != ERROR_OK) {
LOG_TARGET_ERROR(
target,
"Couldn't halt target before SoC reset! (xtensa_deassert_reset returned %d)",
res);
return res;
}if (res != ERROR_OK) { ... }
alive_sleep(10);
xtensa_poll(target);
xtensa_halt(target);
res = target_wait_state(target, TARGET_HALTED, 1000);
if (res != ERROR_OK) {
LOG_TARGET_ERROR(target, "Couldn't halt target before SoC reset");
return res;
}if (res != ERROR_OK) { ... }
}if (res != ERROR_OK) { ... }
}if (target->state != TARGET_HALTED) { ... }
assert(target->state == TARGET_HALTED);
res = target_write_u32(target, ESP32_S2_STORE4, 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_STORE4 (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_STORE5, 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_STORE5 (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_RTC_CNTL_DIG_PWC_REG, 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_RTC_CNTL_DIG_PWC_REG (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_CLK_CONF, ESP32_S2_CLK_CONF_DEF);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_CLK_CONF (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = esp32s2_stall(target);
if (res != ERROR_OK)
return res;
res = xtensa_smpbreak_write(xtensa, OCDDCR_RUNSTALLINEN);
if (res != ERROR_OK) {
LOG_ERROR("Failed to set smpbreak (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
xtensa->suppress_dsr_errors = true;
res = esp32s2_set_peri_reg_mask(target,
ESP32_S2_OPTIONS0,
ESP32_S2_SW_SYS_RST_M,
BIT(ESP32_S2_SW_SYS_RST_S));
xtensa->suppress_dsr_errors = false;
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_OPTIONS0 (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
alive_sleep(100);
int64_t timeout = timeval_ms() + 100;
while (target->state != TARGET_RESET && target->state != TARGET_RUNNING) {
alive_sleep(10);
xtensa_poll(target);
if (timeval_ms() >= timeout) {
LOG_TARGET_ERROR(target, "Timed out waiting for CPU to be reset, target state=%d",
target->state);
return ERROR_TARGET_TIMEOUT;
}if (timeval_ms() >= timeout) { ... }
}while (target->state != TARGET_RESET && target->state != TARGET_RUNNING) { ... }
xtensa_halt(target);
res = target_wait_state(target, TARGET_HALTED, 1000);
if (res != ERROR_OK) {
LOG_TARGET_ERROR(target, "Couldn't halt target before SoC reset");
return res;
}if (res != ERROR_OK) { ... }
res = esp32s2_unstall(target);
if (res != ERROR_OK)
return res;
res = esp32s2_disable_wdts(target);
if (res != ERROR_OK)
return res;
res = target_write_u32(target, ESP32_S2_DPORT_PMS_OCCUPY_3, 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_DPORT_PMS_OCCUPY_3 (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int esp32s2_disable_wdts(struct target *target)
{
int res = target_write_u32(target, ESP32_S2_TIMG0WDT_PROTECT, ESP32_S2_WDT_WKEY_VALUE);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_TIMG0WDT_PROTECT (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_TIMG0WDT_CFG0, 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_TIMG0WDT_CFG0 (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_TIMG1WDT_PROTECT, ESP32_S2_WDT_WKEY_VALUE);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_TIMG1WDT_PROTECT (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_TIMG1WDT_CFG0, 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_TIMG1WDT_CFG0 (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_RTCWDT_PROTECT, ESP32_S2_WDT_WKEY_VALUE);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_RTCWDT_PROTECT (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_RTCWDT_CFG, 0);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_RTCWDT_CFG (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_u32(target, ESP32_S2_SWD_WPROTECT_REG, ESP32_S2_SWD_WKEY_VALUE);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_SWD_WPROTECT_REG (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
uint32_t swd_conf_reg = 0;
res = target_read_u32(target, ESP32_S2_SWD_CONF_REG, &swd_conf_reg);
if (res != ERROR_OK) {
LOG_ERROR("Failed to read ESP32_S2_SWD_CONF_REG (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
swd_conf_reg |= ESP32_S2_SWD_AUTO_FEED_EN_M;
res = target_write_u32(target, ESP32_S2_SWD_CONF_REG, swd_conf_reg);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write ESP32_S2_SWD_CONF_REG (%d)!", res);
return res;
}if (res != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int esp32s2_arch_state(struct target *target)
{
return ERROR_OK;
}{ ... }
static int esp32s2_on_halt(struct target *target)
{
int ret = esp32s2_disable_wdts(target);
if (ret == ERROR_OK)
ret = esp_xtensa_on_halt(target);
return ret;
}{ ... }
static int esp32s2_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
{
int ret = xtensa_step(target, current, address, handle_breakpoints);
if (ret == ERROR_OK) {
esp32s2_on_halt(target);
target_call_event_callbacks(target, TARGET_EVENT_HALTED);
}if (ret == ERROR_OK) { ... }
return ret;
}{ ... }
static int esp32s2_poll(struct target *target)
{
enum target_state old_state = target->state;
int ret = esp_xtensa_poll(target);
if (ret != ERROR_OK)
return ret;
if (old_state != TARGET_HALTED && target->state == TARGET_HALTED) {
if (old_state == TARGET_DEBUG_RUNNING) {
target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
}if (old_state == TARGET_DEBUG_RUNNING) { ... } else {
if (esp_xtensa_semihosting(target, &ret) == SEMIHOSTING_HANDLED) {
struct esp_xtensa_common *esp_xtensa = target_to_esp_xtensa(target);
if (ret == ERROR_OK && esp_xtensa->semihost.need_resume) {
esp_xtensa->semihost.need_resume = false;
ret = target_resume(target, 1, 0, 1, 0);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to resume target");
return ret;
}if (ret != ERROR_OK) { ... }
}if (ret == ERROR_OK && esp_xtensa->semihost.need_resume) { ... }
return ret;
}if (esp_xtensa_semihosting(target, &ret) == SEMIHOSTING_HANDLED) { ... }
esp32s2_on_halt(target);
target_call_event_callbacks(target, TARGET_EVENT_HALTED);
}else { ... }
}if (old_state != TARGET_HALTED && target->state == TARGET_HALTED) { ... }
return ret;
}{ ... }
static int esp32s2_virt2phys(struct target *target,
target_addr_t virtual, target_addr_t *physical)
{
*physical = virtual;
return ERROR_OK;
}{ ... }
static int esp32s2_target_init(struct command_context *cmd_ctx, struct target *target)
{
int ret = esp_xtensa_target_init(cmd_ctx, target);
if (ret != ERROR_OK)
return ret;
return esp_xtensa_semihosting_init(target);
}{ ... }
static const struct xtensa_debug_ops esp32s2_dbg_ops = {
.queue_enable = xtensa_dm_queue_enable,
.queue_reg_read = xtensa_dm_queue_reg_read,
.queue_reg_write = xtensa_dm_queue_reg_write
...};
static const struct xtensa_power_ops esp32s2_pwr_ops = {
.queue_reg_read = xtensa_dm_queue_pwr_reg_read,
.queue_reg_write = xtensa_dm_queue_pwr_reg_write
...};
static const struct esp_semihost_ops esp32s2_semihost_ops = {
.prepare = esp32s2_disable_wdts
...};
static int esp32s2_target_create(struct target *target, Jim_Interp *interp)
{
struct xtensa_debug_module_config esp32s2_dm_cfg = {
.dbg_ops = &esp32s2_dbg_ops,
.pwr_ops = &esp32s2_pwr_ops,
.tap = target->tap,
.queue_tdi_idle = NULL,
.queue_tdi_idle_arg = NULL
...};
struct esp32s2_common *esp32 = calloc(1, sizeof(*esp32));
if (!esp32) {
LOG_ERROR("Failed to alloc memory for arch info!");
return ERROR_FAIL;
}if (!esp32) { ... }
int ret = esp_xtensa_init_arch_info(target, &esp32->esp_xtensa, &esp32s2_dm_cfg, &esp32s2_semihost_ops);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to init arch info!");
free(esp32);
return ret;
}if (ret != ERROR_OK) { ... }
target->state = TARGET_RUNNING;
target->debug_reason = DBG_REASON_NOTHALTED;
return ERROR_OK;
}{ ... }
static const struct command_registration esp32s2_command_handlers[] = {
{
.chain = xtensa_command_handlers,
...},
{
.name = "esp",
.usage = "",
.chain = esp32_apptrace_command_handlers,
...},
{
.name = "arm",
.mode = COMMAND_ANY,
.help = "ARM Command Group",
.usage = "",
.chain = semihosting_common_handlers
...},
COMMAND_REGISTRATION_DONE
...};
struct target_type esp32s2_target = {
.name = "esp32s2",
.poll = esp32s2_poll,
.arch_state = esp32s2_arch_state,
.halt = xtensa_halt,
.resume = xtensa_resume,
.step = esp32s2_step,
.assert_reset = esp32s2_assert_reset,
.deassert_reset = esp32s2_deassert_reset,
.soft_reset_halt = esp32s2_soft_reset_halt,
.virt2phys = esp32s2_virt2phys,
.mmu = xtensa_mmu_is_enabled,
.read_memory = xtensa_read_memory,
.write_memory = xtensa_write_memory,
.read_buffer = xtensa_read_buffer,
.write_buffer = xtensa_write_buffer,
.checksum_memory = xtensa_checksum_memory,
.get_gdb_arch = xtensa_get_gdb_arch,
.get_gdb_reg_list = xtensa_get_gdb_reg_list,
.run_algorithm = xtensa_run_algorithm,
.start_algorithm = xtensa_start_algorithm,
.wait_algorithm = xtensa_wait_algorithm,
.add_breakpoint = esp_xtensa_breakpoint_add,
.remove_breakpoint = esp_xtensa_breakpoint_remove,
.add_watchpoint = xtensa_watchpoint_add,
.remove_watchpoint = xtensa_watchpoint_remove,
.target_create = esp32s2_target_create,
.init_target = esp32s2_target_init,
.examine = xtensa_examine,
.deinit_target = esp_xtensa_target_deinit,
.commands = esp32s2_command_handlers,
...};