mphalport.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2018 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 <string.h>
  28. #include <rtthread.h>
  29. #include <py/mpconfig.h>
  30. #include <py/runtime.h>
  31. #include "mphalport.h"
  32. #include "mpgetcharport.h"
  33. #include "mpputsnport.h"
  34. const char rtthread_help_text[] =
  35. "Welcome to MicroPython on RT-Thread!\n"
  36. "\n"
  37. "Control commands:\n"
  38. " CTRL-A -- on a blank line, enter raw REPL mode\n"
  39. " CTRL-B -- on a blank line, enter normal REPL mode\n"
  40. " CTRL-C -- interrupt a running program\n"
  41. " CTRL-D -- on a blank line, do a soft reset of the board\n"
  42. " CTRL-E -- on a blank line, enter paste mode\n"
  43. "\n"
  44. "For further help on a specific object, type help(obj)\n"
  45. ;
  46. int mp_hal_stdin_rx_chr(void) {
  47. char ch;
  48. while (1) {
  49. ch = mp_getchar();
  50. if (ch != (char)0xFF) {
  51. break;
  52. }
  53. MICROPY_EVENT_POLL_HOOK;
  54. rt_thread_delay(1);
  55. }
  56. return ch;
  57. }
  58. // Send string of given length
  59. void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
  60. mp_putsn(str, len);
  61. }
  62. void mp_hal_stdout_tx_strn_stream(const char *str, size_t len) {
  63. mp_putsn_stream(str, len);
  64. }
  65. mp_uint_t mp_hal_ticks_us(void) {
  66. return rt_tick_get() * 1000000UL / RT_TICK_PER_SECOND;
  67. }
  68. mp_uint_t mp_hal_ticks_ms(void) {
  69. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  70. }
  71. mp_uint_t mp_hal_ticks_cpu(void) {
  72. return rt_tick_get();
  73. }
  74. void mp_hal_delay_us(mp_uint_t us) {
  75. rt_tick_t t0 = rt_tick_get(), t1, dt;
  76. uint64_t dtick = us * RT_TICK_PER_SECOND / 1000000L;
  77. while (1) {
  78. t1 = rt_tick_get();
  79. dt = t1 - t0;
  80. if (dt >= dtick) {
  81. break;
  82. }
  83. mp_handle_pending();
  84. }
  85. }
  86. void mp_hal_delay_ms(mp_uint_t ms) {
  87. rt_tick_t t0 = rt_tick_get(), t1, dt;
  88. uint64_t dtick = ms * RT_TICK_PER_SECOND / 1000L;
  89. while (1) {
  90. t1 = rt_tick_get();
  91. dt = t1 - t0;
  92. if (dt >= dtick) {
  93. break;
  94. }
  95. MICROPY_EVENT_POLL_HOOK;
  96. rt_thread_delay(1);
  97. }
  98. }