1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "soc/uart_periph.h"
16 #include "soc/gpio_periph.h"
17 #include "esp_gdbstub_common.h"
18 #include "sdkconfig.h"
19 
20 #define UART_NUM CONFIG_ESP_CONSOLE_UART_NUM
21 
esp_gdbstub_target_init()22 void esp_gdbstub_target_init()
23 {
24 }
25 
esp_gdbstub_getchar()26 int esp_gdbstub_getchar()
27 {
28     while (REG_GET_FIELD(UART_STATUS_REG(UART_NUM), UART_RXFIFO_CNT) == 0) {
29         ;
30     }
31     return REG_READ(UART_FIFO_AHB_REG(UART_NUM));
32 }
33 
esp_gdbstub_putchar(int c)34 void esp_gdbstub_putchar(int c)
35 {
36     while (REG_GET_FIELD(UART_STATUS_REG(UART_NUM), UART_TXFIFO_CNT) >= 126) {
37         ;
38     }
39     REG_WRITE(UART_FIFO_AHB_REG(UART_NUM), c);
40 }
41 
esp_gdbstub_readmem(intptr_t addr)42 int esp_gdbstub_readmem(intptr_t addr)
43 {
44     if (addr < 0x20000000 || addr >= 0x80000000) {
45         /* see esp_cpu_configure_region_protection */
46         return -1;
47     }
48     uint32_t val_aligned = *(uint32_t *)(addr & (~3));
49     uint32_t shift = (addr & 3) * 8;
50     return (val_aligned >> shift) & 0xff;
51 }
52