SEGGER_RTT_Device.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * File : kservice.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-07-20 FlyLu the first version
  23. * 2018-07-20 Tanek use idle hook as receive
  24. */
  25. #include <rtdevice.h>
  26. #include <rthw.h>
  27. #include "SEGGER_RTT.h"
  28. static struct rt_serial_device segger_serial;
  29. static void serial1_idle(void)
  30. {
  31. rt_hw_serial_isr(&segger_serial, RT_SERIAL_EVENT_RX_IND);
  32. }
  33. static rt_err_t segger_control(struct rt_serial_device *serial, int cmd, void *arg)
  34. {
  35. static rt_bool_t sethook = RT_FALSE;
  36. RT_ASSERT(serial != RT_NULL);
  37. RT_ASSERT(arg != RT_NULL);
  38. switch (cmd)
  39. {
  40. case RT_DEVICE_CTRL_CLR_INT:
  41. if (sethook)
  42. {
  43. rt_thread_idle_delhook(serial1_idle);
  44. sethook = RT_FALSE;
  45. }
  46. break;
  47. case RT_DEVICE_CTRL_SET_INT:
  48. if (!sethook)
  49. {
  50. rt_thread_idle_sethook(serial1_idle);
  51. sethook = RT_TRUE;
  52. }
  53. break;
  54. }
  55. return RT_EOK;
  56. }
  57. static int segger_putc(struct rt_serial_device *serial, char c)
  58. {
  59. RT_ASSERT(serial != RT_NULL);
  60. return SEGGER_RTT_Write(0, &c, 1);
  61. }
  62. static int segger_getc(struct rt_serial_device *serial)
  63. {
  64. char ch;
  65. RT_ASSERT(serial != RT_NULL);
  66. if (SEGGER_RTT_Read(0, &ch, 1) == 1)
  67. {
  68. return ch;
  69. }
  70. else
  71. {
  72. return -1;
  73. }
  74. }
  75. int hw_segger_init(void)
  76. {
  77. static const struct rt_uart_ops segger_uart_ops =
  78. {
  79. RT_NULL,
  80. segger_control,
  81. segger_putc,
  82. segger_getc,
  83. };
  84. segger_serial.ops = &segger_uart_ops;
  85. #ifdef RT_USING_SERIAL_V1
  86. segger_serial.config.bufsz = RT_SERIAL_RB_BUFSZ;
  87. #endif
  88. /* register segger rtt as serial device */
  89. rt_hw_serial_register(&segger_serial,
  90. PKG_SERIAL_DEVICE_NAME,
  91. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  92. RT_NULL);
  93. return 0;
  94. }
  95. INIT_BOARD_EXPORT(hw_segger_init);