mpgetcharport.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "py/runtime.h"
  32. #include "lib/utils/interrupt_char.h"
  33. #include "mpgetcharport.h"
  34. #define UART_FIFO_SIZE 256
  35. static struct rt_ringbuffer *rx_fifo = NULL;
  36. static rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size) = NULL;
  37. static rt_err_t getchar_rx_ind(rt_device_t dev, rt_size_t size) {
  38. uint8_t ch;
  39. rt_size_t i;
  40. rt_base_t int_lvl;
  41. for (i = 0; i < size; i++) {
  42. /* read a char */
  43. if (rt_device_read(dev, 0, &ch, 1)) {
  44. if (ch == mp_interrupt_char) {
  45. mp_keyboard_interrupt();
  46. } else {
  47. int_lvl = rt_hw_interrupt_disable();
  48. rt_ringbuffer_put_force(rx_fifo, &ch, 1);
  49. rt_hw_interrupt_enable(int_lvl);
  50. }
  51. }
  52. }
  53. return RT_EOK;
  54. }
  55. void mp_getchar_init(void) {
  56. rt_base_t int_lvl;
  57. rt_device_t console;
  58. /* create RX FIFO */
  59. rx_fifo = rt_ringbuffer_create(UART_FIFO_SIZE);
  60. /* created must success */
  61. RT_ASSERT(rx_fifo);
  62. int_lvl = rt_hw_interrupt_disable();
  63. console = rt_console_get_device();
  64. if (console) {
  65. /* backup RX indicate */
  66. odev_rx_ind = console->rx_indicate;
  67. rt_device_set_rx_indicate(console, getchar_rx_ind);
  68. }
  69. rt_hw_interrupt_enable(int_lvl);
  70. }
  71. void mp_getchar_deinit(void) {
  72. rt_base_t int_lvl;
  73. rt_device_t console;
  74. rt_ringbuffer_destroy(rx_fifo);
  75. int_lvl = rt_hw_interrupt_disable();
  76. console = rt_console_get_device();
  77. if (console && odev_rx_ind) {
  78. /* restore RX indicate */
  79. rt_device_set_rx_indicate(console, odev_rx_ind);
  80. }
  81. rt_hw_interrupt_enable(int_lvl);
  82. }
  83. int mp_getchar(void) {
  84. uint8_t ch;
  85. rt_base_t int_lvl;
  86. int_lvl = rt_hw_interrupt_disable();
  87. if (!rt_ringbuffer_getchar(rx_fifo, &ch)) {
  88. ch = 0xFF;
  89. }
  90. rt_hw_interrupt_enable(int_lvl);
  91. return ch;
  92. }