mpgetcharport.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <stdint.h>
  28. #include <rtthread.h>
  29. #include <rtdevice.h>
  30. #include <rthw.h>
  31. #include "lib/utils/interrupt_char.h"
  32. #include "mpgetcharport.h"
  33. #define UART_FIFO_SIZE 256
  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. rt_base_t int_lvl;
  40. for (i = 0; i < size; i++) {
  41. /* read a char */
  42. if (rt_device_read(dev, 0, &ch, 1)) {
  43. if (ch == mp_interrupt_char) {
  44. mp_keyboard_interrupt();
  45. } else {
  46. int_lvl = rt_hw_interrupt_disable();
  47. rt_ringbuffer_put_force(rx_fifo, &ch, 1);
  48. rt_hw_interrupt_enable(int_lvl);
  49. }
  50. }
  51. }
  52. return RT_EOK;
  53. }
  54. void mp_getchar_init(void) {
  55. rt_base_t int_lvl;
  56. rt_device_t console;
  57. /* create RX FIFO */
  58. rx_fifo = rt_ringbuffer_create(UART_FIFO_SIZE);
  59. /* created must success */
  60. RT_ASSERT(rx_fifo);
  61. int_lvl = rt_hw_interrupt_disable();
  62. console = rt_console_get_device();
  63. if (console) {
  64. /* backup RX indicate */
  65. odev_rx_ind = console->rx_indicate;
  66. rt_device_set_rx_indicate(console, getchar_rx_ind);
  67. }
  68. rt_hw_interrupt_enable(int_lvl);
  69. }
  70. void mp_getchar_deinit(void) {
  71. rt_base_t int_lvl;
  72. rt_device_t console;
  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 mp_getchar(void) {
  83. uint8_t ch;
  84. rt_base_t int_lvl;
  85. int_lvl = rt_hw_interrupt_disable();
  86. if (!rt_ringbuffer_getchar(rx_fifo, &ch)) {
  87. ch = 0xFF;
  88. }
  89. rt_hw_interrupt_enable(int_lvl);
  90. return ch;
  91. }