mphalport.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "rtt_getchar.h"
  33. int mp_hal_stdin_rx_chr(void) {
  34. char ch;
  35. while (1) {
  36. ch = rtt_getchar();
  37. if (ch != (char)0xFF) {
  38. break;
  39. }
  40. MICROPY_EVENT_POLL_HOOK;
  41. rt_thread_delay(1);
  42. }
  43. return ch;
  44. }
  45. // Send string of given length
  46. void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
  47. rt_device_t console;
  48. console = rt_console_get_device();
  49. if (console) {
  50. rt_device_write(console, 0, str, len);
  51. }
  52. }
  53. void mp_hal_stdout_tx_strn_stream(const char *str, size_t len) {
  54. rt_kprintf("%.*s", len, str);
  55. }
  56. mp_uint_t mp_hal_ticks_us(void) {
  57. return rt_tick_get() * 1000000UL / RT_TICK_PER_SECOND;
  58. }
  59. mp_uint_t mp_hal_ticks_ms(void) {
  60. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  61. }
  62. mp_uint_t mp_hal_ticks_cpu(void) {
  63. return rt_tick_get();
  64. }
  65. void mp_hal_delay_us(mp_uint_t us) {
  66. rt_tick_t t0 = rt_tick_get(), t1, dt;
  67. uint64_t dtick = us * RT_TICK_PER_SECOND / 1000000L;
  68. while (1) {
  69. t1 = rt_tick_get();
  70. dt = t1 - t0;
  71. if (dt >= dtick) {
  72. break;
  73. }
  74. mp_handle_pending();
  75. }
  76. }
  77. void mp_hal_delay_ms(mp_uint_t ms) {
  78. rt_tick_t t0 = rt_tick_get(), t1, dt;
  79. uint64_t dtick = ms * RT_TICK_PER_SECOND / 1000L;
  80. while (1) {
  81. t1 = rt_tick_get();
  82. dt = t1 - t0;
  83. if (dt >= dtick) {
  84. break;
  85. }
  86. MICROPY_EVENT_POLL_HOOK;
  87. rt_thread_delay(1);
  88. }
  89. }