1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "soc/uart_periph.h"
8 #include "soc/gpio_periph.h"
9 #include "soc/soc.h"
10 #include "soc/usb_serial_jtag_struct.h"
11 #include "hal/usb_serial_jtag_ll.h"
12 #include "esp_gdbstub_common.h"
13 #include "sdkconfig.h"
14 
15 #define UART_NUM CONFIG_ESP_CONSOLE_UART_NUM
16 
17 #define GDBSTUB_MEM_REGION_COUNT 9
18 
19 #define UART_REG_FIELD_LEN 0x84
20 
21 typedef struct {
22     intptr_t lower;
23     intptr_t upper;
24 } mem_bound_t;
25 
26 static const mem_bound_t mem_region_table [GDBSTUB_MEM_REGION_COUNT] =
27 {
28     {SOC_DROM_LOW, SOC_DROM_HIGH},
29     {SOC_IROM_LOW, SOC_IROM_HIGH},
30     {SOC_IRAM_LOW, SOC_IRAM_HIGH},
31     {SOC_DRAM_LOW, SOC_DRAM_HIGH},
32     {SOC_IROM_MASK_LOW, SOC_IROM_MASK_HIGH},
33     {SOC_DROM_MASK_LOW, SOC_DROM_MASK_HIGH},
34     {SOC_RTC_IRAM_LOW, SOC_RTC_IRAM_HIGH},
35     // RTC DRAM and RTC DATA are identical with RTC IRAM, hence we skip them
36     // We shouldn't read the uart registers since it will disturb the debugging via UART,
37     // so skip UART part of the peripheral registers.
38     {DR_REG_UART_BASE + UART_REG_FIELD_LEN, SOC_PERIPHERAL_HIGH},
39     {SOC_DEBUG_LOW, SOC_DEBUG_HIGH},
40 };
41 
check_inside_valid_region(intptr_t addr)42 static inline bool check_inside_valid_region(intptr_t addr)
43 {
44     for (size_t i = 0; i < GDBSTUB_MEM_REGION_COUNT; i++) {
45         if (addr >= mem_region_table[i].lower && addr < mem_region_table[i].upper) {
46             return true;
47         }
48     }
49 
50     return false;
51 }
52 
esp_gdbstub_target_init()53 void esp_gdbstub_target_init()
54 {
55 }
56 
57 #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
58 
esp_gdbstub_getchar()59 int esp_gdbstub_getchar()
60 {
61     uint8_t c;
62     //retry the read until we succeed
63     while (usb_serial_jtag_ll_read_rxfifo(&c, 1)==0) ;
64     return c;
65 }
66 
esp_gdbstub_putchar(int c)67 void esp_gdbstub_putchar(int c)
68 {
69     uint8_t cc=c;
70     //retry the write until we succeed
71     while (usb_serial_jtag_ll_write_txfifo(&cc, 1)<1) ;
72 }
73 
esp_gdbstub_flush()74 void esp_gdbstub_flush()
75 {
76     usb_serial_jtag_ll_txfifo_flush();
77 }
78 
79 
80 #else
81 
82 //assume UART gdbstub channel
83 
esp_gdbstub_getchar()84 int esp_gdbstub_getchar()
85 {
86     while (REG_GET_FIELD(UART_STATUS_REG(UART_NUM), UART_RXFIFO_CNT) == 0) {
87         ;
88     }
89     return REG_READ(UART_FIFO_AHB_REG(UART_NUM));
90 }
91 
esp_gdbstub_putchar(int c)92 void esp_gdbstub_putchar(int c)
93 {
94     while (REG_GET_FIELD(UART_STATUS_REG(UART_NUM), UART_TXFIFO_CNT) >= 126) {
95         ;
96     }
97     REG_WRITE(UART_FIFO_AHB_REG(UART_NUM), c);
98 }
99 
esp_gdbstub_flush()100 void esp_gdbstub_flush()
101 {
102     //not needed for uart
103 }
104 
105 #endif
106 
esp_gdbstub_readmem(intptr_t addr)107 int esp_gdbstub_readmem(intptr_t addr)
108 {
109     if (!check_inside_valid_region(addr)) {
110         /* see esp_cpu_configure_region_protection */
111         return -1;
112     }
113     uint32_t val_aligned = *(uint32_t *)(addr & (~3));
114     uint32_t shift = (addr & 3) * 8;
115     return (val_aligned >> shift) & 0xff;
116 }
117 
esp_gdbstub_writemem(unsigned int addr,unsigned char data)118 int esp_gdbstub_writemem(unsigned int addr, unsigned char data)
119 {
120     if (!check_inside_valid_region(addr)) {
121         /* see esp_cpu_configure_region_protection */
122         return -1;
123     }
124 
125     int *i = (int *)(addr & (~3));
126     if ((addr & 3) == 0) {
127         *i = (*i & 0xffffff00) | (data << 0);
128     }
129     if ((addr & 3) == 1) {
130         *i = (*i & 0xffff00ff) | (data << 8);
131     }
132     if ((addr & 3) == 2) {
133         *i = (*i & 0xff00ffff) | (data << 16);
134     }
135     if ((addr & 3) == 3) {
136         *i = (*i & 0x00ffffff) | (data << 24);
137     }
138     return 0;
139 }
140