rtt_getchar.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Armink (armink.ztl@gmail.com)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <rtthread.h>
  28. #include <rtdevice.h>
  29. #include <rthw.h>
  30. #include "rtt_getchar.h"
  31. #include "lib/utils/interrupt_char.h"
  32. #define UART_FIFO_SIZE 256
  33. static struct rt_semaphore notice;
  34. static struct rt_ringbuffer *rx_fifo = NULL;
  35. static rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size) = NULL;
  36. static rt_err_t getchar_rx_ind(rt_device_t dev, rt_size_t size) {
  37. uint8_t ch;
  38. rt_size_t i;
  39. for (i = 0; i < size; i++) {
  40. /* read a char */
  41. if (rt_device_read(dev, 0, &ch, 1)) {
  42. if (ch == mp_interrupt_char) {
  43. mp_keyboard_interrupt();
  44. } else {
  45. rt_ringbuffer_put_force(rx_fifo, &ch, 1);
  46. rt_sem_release(&notice);
  47. }
  48. }
  49. }
  50. return RT_EOK;
  51. }
  52. void rtt_getchar_init(void) {
  53. rt_base_t int_lvl;
  54. rt_device_t console;
  55. rt_sem_init(&notice, "uart_notice", 0, RT_IPC_FLAG_FIFO);
  56. /* create RX FIFO */
  57. rx_fifo = rt_ringbuffer_create(UART_FIFO_SIZE);
  58. /* created must success */
  59. RT_ASSERT(rx_fifo);
  60. int_lvl = rt_hw_interrupt_disable();
  61. console = rt_console_get_device();
  62. if (console) {
  63. /* backup RX indicate */
  64. odev_rx_ind = console->rx_indicate;
  65. rt_device_set_rx_indicate(console, getchar_rx_ind);
  66. }
  67. rt_hw_interrupt_enable(int_lvl);
  68. }
  69. void rtt_getchar_deinit(void) {
  70. rt_base_t int_lvl;
  71. rt_device_t console;
  72. rt_sem_detach(&notice);
  73. rt_ringbuffer_destroy(rx_fifo);
  74. int_lvl = rt_hw_interrupt_disable();
  75. console = rt_console_get_device();
  76. if (console && odev_rx_ind) {
  77. /* restore RX indicate */
  78. rt_device_set_rx_indicate(console, odev_rx_ind);
  79. }
  80. rt_hw_interrupt_enable(int_lvl);
  81. }
  82. int rtt_getchar(void) {
  83. uint8_t ch;
  84. rt_sem_take(&notice, RT_WAITING_FOREVER);
  85. rt_ringbuffer_getchar(rx_fifo, &ch);
  86. return ch;
  87. }