hal_stub.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * I/O and interface portion of GDB stub
  3. *
  4. * File : hal_stub.c
  5. * This file is part of RT-Thread RTOS
  6. * COPYRIGHT (C) 2006, RT-Thread Develop Team
  7. *
  8. * The license and distribution terms for this file may be
  9. * found in the file LICENSE in this distribution or at
  10. * http://www.rt-thread.org/license/LICENSE
  11. *
  12. * Change Logs:
  13. * Date Author Notes
  14. * 2014-07-04 Wzyy2 first version
  15. */
  16. #include <rtthread.h>
  17. #include <rthw.h>
  18. #include "gdb_stub.h"
  19. #ifdef RT_USING_SERIAL
  20. #include <rtdevice.h>
  21. #endif
  22. rt_device_t gdb_dev = RT_NULL;
  23. static struct rt_serial_device *gdb_serial;
  24. char gdb_io_set;
  25. void gdb_uart_putc(char c);
  26. int gdb_uart_getc();
  27. /*if you want to use something instead of the serial,change it */
  28. struct gdb_io gdb_io_ops = {
  29. gdb_uart_getc,
  30. gdb_uart_putc
  31. };
  32. /**
  33. * @ingroup gdb_stub
  34. *
  35. * This function will get GDB stubs started, with a proper environment
  36. */
  37. void gdb_start()
  38. {
  39. if (gdb_dev == RT_NULL)
  40. rt_kprintf("GDB: no gdb_dev found,please set it first\n");
  41. else
  42. gdb_breakpoint();
  43. }
  44. /**
  45. * @ingroup gdb_stub
  46. *
  47. * This function sets the input device of gdb_stub.
  48. *
  49. * @param device_name the name of new input device.
  50. */
  51. void gdb_set_device(const char* device_name)
  52. {
  53. rt_device_t dev = RT_NULL;
  54. dev = rt_device_find(device_name);
  55. if(dev == RT_NULL){
  56. rt_kprintf("GDB: can not find device: %s\n", device_name);
  57. return;
  58. }
  59. /* open this device and set the new device */
  60. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM) == RT_EOK)
  61. {
  62. gdb_dev = dev;
  63. gdb_serial = (struct rt_serial_device *)gdb_dev;
  64. }
  65. }
  66. void gdb_uart_putc(char c)
  67. {
  68. #ifdef RT_GDB_DEBUG
  69. rt_kprintf("%c",c);
  70. #endif
  71. rt_device_write(gdb_dev, 0, &c, 1);
  72. }
  73. /* polling */
  74. int gdb_uart_getc()
  75. {
  76. int ch;
  77. #ifdef RT_USING_SERIAL
  78. ch = -1;
  79. do {
  80. ch = gdb_serial->ops->getc(gdb_serial);
  81. } while (ch == -1);
  82. #else
  83. rt_device_read(gdb_dev, 0, &ch, 1);
  84. #endif
  85. #ifdef RT_GDB_DEBUG
  86. rt_kprintf("%c",ch);
  87. #endif
  88. return ch;
  89. }