1 /*
2  * Copyright (c) 2020 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/uart.h>
9 #include <zephyr/sys/__assert.h>
10 
11 static const struct device *uart_dev;
12 
z_gdb_backend_init(void)13 int z_gdb_backend_init(void)
14 {
15 	int ret = 0;
16 	static const struct uart_config uart_cfg = {
17 		.baudrate = 115200,
18 		.parity = UART_CFG_PARITY_NONE,
19 		.stop_bits = UART_CFG_STOP_BITS_1,
20 		.data_bits = UART_CFG_DATA_BITS_8,
21 		.flow_ctrl = UART_CFG_FLOW_CTRL_NONE
22 	};
23 
24 #ifdef CONFIG_GDBSTUB_TRACE
25 	printk("gdbstub_serial:%s enter\n", __func__);
26 #endif
27 
28 	if (uart_dev == NULL) {
29 		uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_gdbstub_uart));
30 
31 		__ASSERT(device_is_ready(uart_dev), "uart device is not ready");
32 
33 		ret = uart_configure(uart_dev, &uart_cfg);
34 		__ASSERT(ret == 0, "Could not configure uart device");
35 	}
36 
37 #ifdef CONFIG_GDBSTUB_TRACE
38 	printk("gdbstub_serial:%s exit\n", __func__);
39 #endif
40 	return ret;
41 }
42 
z_gdb_putchar(unsigned char ch)43 void z_gdb_putchar(unsigned char ch)
44 {
45 	uart_poll_out(uart_dev, ch);
46 }
47 
z_gdb_getchar(void)48 unsigned char z_gdb_getchar(void)
49 {
50 	unsigned char ch;
51 
52 	while (uart_poll_in(uart_dev, &ch) < 0) {
53 	}
54 
55 	return ch;
56 }
57