mpgetcharport.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "lib/utils/interrupt_char.h"
  31. #include "mpgetcharport.h"
  32. #define UART_FIFO_SIZE 256
  33. static struct rt_ringbuffer *rx_fifo = NULL;
  34. static rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size) = NULL;
  35. static rt_err_t getchar_rx_ind(rt_device_t dev, rt_size_t size) {
  36. uint8_t ch;
  37. rt_size_t i;
  38. rt_base_t int_lvl;
  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. int_lvl = rt_hw_interrupt_disable();
  46. rt_ringbuffer_put_force(rx_fifo, &ch, 1);
  47. rt_hw_interrupt_enable(int_lvl);
  48. }
  49. }
  50. }
  51. return RT_EOK;
  52. }
  53. void mp_getchar_init(void) {
  54. rt_base_t int_lvl;
  55. rt_device_t console;
  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 mp_getchar_deinit(void) {
  70. rt_base_t int_lvl;
  71. rt_device_t console;
  72. rt_ringbuffer_destroy(rx_fifo);
  73. int_lvl = rt_hw_interrupt_disable();
  74. console = rt_console_get_device();
  75. if (console && odev_rx_ind) {
  76. /* restore RX indicate */
  77. rt_device_set_rx_indicate(console, odev_rx_ind);
  78. }
  79. rt_hw_interrupt_enable(int_lvl);
  80. }
  81. int mp_getchar(void) {
  82. uint8_t ch;
  83. rt_base_t int_lvl;
  84. int_lvl = rt_hw_interrupt_disable();
  85. if (!rt_ringbuffer_getchar(rx_fifo, &ch)) {
  86. ch = 0xFF;
  87. }
  88. rt_hw_interrupt_enable(int_lvl);
  89. return ch;
  90. }