mphalport.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Development of the code in this file was sponsored by Microbric Pty Ltd
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2014 Damien P. George
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include "freertos/FreeRTOS.h"
  31. #include "freertos/task.h"
  32. #include "rom/uart.h"
  33. #include "py/obj.h"
  34. #include "py/mpstate.h"
  35. #include "py/mphal.h"
  36. #include "extmod/misc.h"
  37. #include "lib/utils/pyexec.h"
  38. #include "mphalport.h"
  39. TaskHandle_t mp_main_task_handle;
  40. STATIC uint8_t stdin_ringbuf_array[256];
  41. ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array)};
  42. int mp_hal_stdin_rx_chr(void) {
  43. for (;;) {
  44. int c = ringbuf_get(&stdin_ringbuf);
  45. if (c != -1) {
  46. return c;
  47. }
  48. MICROPY_EVENT_POLL_HOOK
  49. ulTaskNotifyTake(pdFALSE, 1);
  50. }
  51. }
  52. void mp_hal_stdout_tx_str(const char *str) {
  53. mp_hal_stdout_tx_strn(str, strlen(str));
  54. }
  55. void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
  56. MP_THREAD_GIL_EXIT();
  57. for (uint32_t i = 0; i < len; ++i) {
  58. uart_tx_one_char(str[i]);
  59. }
  60. MP_THREAD_GIL_ENTER();
  61. mp_uos_dupterm_tx_strn(str, len);
  62. }
  63. // Efficiently convert "\n" to "\r\n"
  64. void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
  65. const char *last = str;
  66. while (len--) {
  67. if (*str == '\n') {
  68. if (str > last) {
  69. mp_hal_stdout_tx_strn(last, str - last);
  70. }
  71. mp_hal_stdout_tx_strn("\r\n", 2);
  72. ++str;
  73. last = str;
  74. } else {
  75. ++str;
  76. }
  77. }
  78. if (str > last) {
  79. mp_hal_stdout_tx_strn(last, str - last);
  80. }
  81. }
  82. uint32_t mp_hal_ticks_ms(void) {
  83. return esp_timer_get_time() / 1000;
  84. }
  85. uint32_t mp_hal_ticks_us(void) {
  86. return esp_timer_get_time();
  87. }
  88. void mp_hal_delay_ms(uint32_t ms) {
  89. uint64_t us = ms * 1000;
  90. uint64_t dt;
  91. uint64_t t0 = esp_timer_get_time();
  92. for (;;) {
  93. uint64_t t1 = esp_timer_get_time();
  94. dt = t1 - t0;
  95. if (dt + portTICK_PERIOD_MS * 1000 >= us) {
  96. // doing a vTaskDelay would take us beyond requested delay time
  97. break;
  98. }
  99. MICROPY_EVENT_POLL_HOOK
  100. ulTaskNotifyTake(pdFALSE, 1);
  101. }
  102. if (dt < us) {
  103. // do the remaining delay accurately
  104. mp_hal_delay_us(us - dt);
  105. }
  106. }
  107. void mp_hal_delay_us(uint32_t us) {
  108. // these constants are tested for a 240MHz clock
  109. const uint32_t this_overhead = 5;
  110. const uint32_t pend_overhead = 150;
  111. // return if requested delay is less than calling overhead
  112. if (us < this_overhead) {
  113. return;
  114. }
  115. us -= this_overhead;
  116. uint64_t t0 = esp_timer_get_time();
  117. for (;;) {
  118. uint64_t dt = esp_timer_get_time() - t0;
  119. if (dt >= us) {
  120. return;
  121. }
  122. if (dt + pend_overhead < us) {
  123. // we have enough time to service pending events
  124. // (don't use MICROPY_EVENT_POLL_HOOK because it also yields)
  125. mp_handle_pending();
  126. }
  127. }
  128. }
  129. // this function could do with improvements (eg use ets_delay_us)
  130. void mp_hal_delay_us_fast(uint32_t us) {
  131. uint32_t delay = ets_get_cpu_frequency() / 19;
  132. while (--us) {
  133. for (volatile uint32_t i = delay; i; --i) {
  134. }
  135. }
  136. }
  137. /*
  138. extern int mp_stream_errno;
  139. int *__errno() {
  140. return &mp_stream_errno;
  141. }
  142. */
  143. // Wake up the main task if it is sleeping
  144. void mp_hal_wake_main_task_from_isr(void) {
  145. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  146. vTaskNotifyGiveFromISR(mp_main_task_handle, &xHigherPriorityTaskWoken);
  147. if (xHigherPriorityTaskWoken == pdTRUE) {
  148. portYIELD_FROM_ISR();
  149. }
  150. }