mphalport.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifdef PKG_USING_OPENMV_CP
  62. extern void serial_dbg_send_strn(const char *str, int len);
  63. serial_dbg_send_strn(str, len);
  64. #endif
  65. }
  66. void mp_hal_stdout_tx_strn_stream(const char *str, size_t len) {
  67. mp_putsn_stream(str, len);
  68. #ifdef PKG_USING_OPENMV_CP
  69. extern void serial_dbg_send_strn_cooked(const char *str, int len);
  70. serial_dbg_send_strn_cooked(str, len);
  71. #endif
  72. }
  73. mp_uint_t mp_hal_ticks_us(void) {
  74. return rt_tick_get() * 1000000UL / RT_TICK_PER_SECOND;
  75. }
  76. mp_uint_t mp_hal_ticks_ms(void) {
  77. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  78. }
  79. mp_uint_t mp_hal_ticks_cpu(void) {
  80. return rt_tick_get();
  81. }
  82. void mp_hal_delay_us(mp_uint_t us) {
  83. rt_tick_t t0 = rt_tick_get(), t1, dt;
  84. uint64_t dtick = us * RT_TICK_PER_SECOND / 1000000L;
  85. while (1) {
  86. t1 = rt_tick_get();
  87. dt = t1 - t0;
  88. if (dt >= dtick) {
  89. break;
  90. }
  91. mp_handle_pending(true);
  92. }
  93. }
  94. void mp_hal_delay_ms(mp_uint_t ms) {
  95. rt_tick_t t0 = rt_tick_get(), t1, dt;
  96. uint64_t dtick = ms * RT_TICK_PER_SECOND / 1000L;
  97. while (1) {
  98. t1 = rt_tick_get();
  99. dt = t1 - t0;
  100. if (dt >= dtick) {
  101. break;
  102. }
  103. MICROPY_EVENT_POLL_HOOK;
  104. rt_thread_delay(1);
  105. }
  106. }