mirror of
https://github.com/micropython/micropython.git
synced 2025-07-21 04:51:12 +02:00
Some checks are pending
JavaScript code lint and formatting with Biome / eslint (push) Waiting to run
Check code formatting / code-formatting (push) Waiting to run
Check code size / build (push) Waiting to run
Check spelling with codespell / codespell (push) Waiting to run
Check commit message formatting / build (push) Waiting to run
Build docs / build (push) Waiting to run
Check examples / embedding (push) Waiting to run
Package mpremote / build (push) Waiting to run
.mpy file format and tools / test (push) Waiting to run
Build ports metadata / build (push) Waiting to run
cc3200 port / build (push) Waiting to run
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Waiting to run
esp32 port / build_idf (esp32_build_s3_c3) (push) Waiting to run
esp8266 port / build (push) Waiting to run
mimxrt port / build (push) Waiting to run
nrf port / build (push) Waiting to run
powerpc port / build (push) Waiting to run
qemu port / build_and_test_arm (push) Waiting to run
qemu port / build_and_test_rv32 (push) Waiting to run
renesas-ra port / build_renesas_ra_board (push) Waiting to run
rp2 port / build (push) Waiting to run
samd port / build (push) Waiting to run
stm32 port / build_stm32 (stm32_misc_build) (push) Waiting to run
stm32 port / build_stm32 (stm32_nucleo_build) (push) Waiting to run
stm32 port / build_stm32 (stm32_pyb_build) (push) Waiting to run
unix port / minimal (push) Waiting to run
unix port / reproducible (push) Waiting to run
unix port / standard (push) Waiting to run
unix port / standard_v2 (push) Waiting to run
unix port / coverage (push) Waiting to run
unix port / coverage_32bit (push) Waiting to run
unix port / nanbox (push) Waiting to run
unix port / float (push) Waiting to run
unix port / stackless_clang (push) Waiting to run
unix port / float_clang (push) Waiting to run
unix port / settrace (push) Waiting to run
unix port / settrace_stackless (push) Waiting to run
unix port / macos (push) Waiting to run
unix port / qemu_mips (push) Waiting to run
unix port / qemu_arm (push) Waiting to run
unix port / qemu_riscv64 (push) Waiting to run
webassembly port / build (push) Waiting to run
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (push) Waiting to run
windows port / build-mingw (i686, mingw32, dev) (push) Waiting to run
windows port / build-mingw (i686, mingw32, standard) (push) Waiting to run
windows port / build-mingw (x86_64, mingw64, dev) (push) Waiting to run
windows port / build-mingw (x86_64, mingw64, standard) (push) Waiting to run
windows port / cross-build-on-linux (push) Waiting to run
zephyr port / build (push) Waiting to run
Python code lint and formatting with ruff / ruff (push) Waiting to run
This fixes a regression introduced in commit
4247921c4e
, where this ring-buffer polling
was accidentally put inside the `#if MICROPY_HW_ESP_USB_SERIAL_JTAG`.
Signed-off-by: Andrew Leech <andrew@alelec.net>
269 lines
8.5 KiB
C
269 lines
8.5 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* Development of the code in this file was sponsored by Microbric Pty Ltd
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2014 Damien P. George
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/time.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_timer.h"
|
|
|
|
#include "py/obj.h"
|
|
#include "py/objstr.h"
|
|
#include "py/stream.h"
|
|
#include "py/mpstate.h"
|
|
#include "py/mphal.h"
|
|
#include "extmod/misc.h"
|
|
#include "shared/timeutils/timeutils.h"
|
|
#include "shared/runtime/pyexec.h"
|
|
#include "shared/tinyusb/mp_usbd.h"
|
|
#include "shared/tinyusb/mp_usbd_cdc.h"
|
|
#include "usb.h"
|
|
#include "usb_serial_jtag.h"
|
|
#include "uart.h"
|
|
|
|
#if MICROPY_PY_STRING_TX_GIL_THRESHOLD < 0
|
|
#error "MICROPY_PY_STRING_TX_GIL_THRESHOLD must be positive"
|
|
#endif
|
|
|
|
TaskHandle_t mp_main_task_handle;
|
|
|
|
static uint8_t stdin_ringbuf_array[260];
|
|
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
|
|
|
|
portMUX_TYPE mp_atomic_mux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
// Check the ESP-IDF error code and raise an OSError if it's not ESP_OK.
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_NORMAL
|
|
void check_esp_err_(esp_err_t code)
|
|
#else
|
|
void check_esp_err_(esp_err_t code, const char *func, const int line, const char *file)
|
|
#endif
|
|
{
|
|
if (code != ESP_OK) {
|
|
// map esp-idf error code to posix error code
|
|
uint32_t pcode = -code;
|
|
switch (code) {
|
|
case ESP_ERR_NO_MEM:
|
|
pcode = MP_ENOMEM;
|
|
break;
|
|
case ESP_ERR_TIMEOUT:
|
|
pcode = MP_ETIMEDOUT;
|
|
break;
|
|
case ESP_ERR_NOT_SUPPORTED:
|
|
pcode = MP_EOPNOTSUPP;
|
|
break;
|
|
}
|
|
// construct string object
|
|
mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
|
|
if (o_str == NULL) {
|
|
mp_raise_OSError(pcode);
|
|
return;
|
|
}
|
|
o_str->base.type = &mp_type_str;
|
|
#if MICROPY_ERROR_REPORTING > MICROPY_ERROR_REPORTING_NORMAL
|
|
char err_msg[64];
|
|
esp_err_to_name_r(code, err_msg, sizeof(err_msg));
|
|
vstr_t vstr;
|
|
vstr_init(&vstr, 80);
|
|
vstr_printf(&vstr, "0x%04X %s in function '%s' at line %d in file '%s'", code, err_msg, func, line, file);
|
|
o_str->data = (const byte *)vstr_null_terminated_str(&vstr);
|
|
#else
|
|
o_str->data = (const byte *)esp_err_to_name(code); // esp_err_to_name ret's ptr to const str
|
|
#endif
|
|
o_str->len = strlen((char *)o_str->data);
|
|
o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
|
|
// raise
|
|
mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(pcode), MP_OBJ_FROM_PTR(o_str)};
|
|
nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
|
|
}
|
|
}
|
|
|
|
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
|
|
uintptr_t ret = 0;
|
|
#if MICROPY_HW_ESP_USB_SERIAL_JTAG
|
|
usb_serial_jtag_poll_rx();
|
|
#endif
|
|
#if MICROPY_HW_USB_CDC
|
|
ret |= mp_usbd_cdc_poll_interfaces(poll_flags);
|
|
#endif
|
|
#if MICROPY_PY_OS_DUPTERM
|
|
ret |= mp_os_dupterm_poll(poll_flags);
|
|
#endif
|
|
// Check ringbuffer directly for uart and usj.
|
|
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
|
|
ret |= MP_STREAM_POLL_RD;
|
|
}
|
|
if (poll_flags & MP_STREAM_POLL_WR) {
|
|
ret |= MP_STREAM_POLL_WR;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int mp_hal_stdin_rx_chr(void) {
|
|
for (;;) {
|
|
#if MICROPY_HW_ESP_USB_SERIAL_JTAG
|
|
usb_serial_jtag_poll_rx();
|
|
#endif
|
|
#if MICROPY_HW_USB_CDC
|
|
mp_usbd_cdc_poll_interfaces(0);
|
|
#endif
|
|
int c = ringbuf_get(&stdin_ringbuf);
|
|
if (c != -1) {
|
|
return c;
|
|
}
|
|
MICROPY_EVENT_POLL_HOOK
|
|
}
|
|
}
|
|
|
|
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
|
|
// Only release the GIL if many characters are being sent
|
|
mp_uint_t ret = len;
|
|
bool did_write = false;
|
|
#if MICROPY_HW_ENABLE_UART_REPL || CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED
|
|
bool release_gil = len > MICROPY_PY_STRING_TX_GIL_THRESHOLD;
|
|
#if MICROPY_DEBUG_PRINTERS && MICROPY_DEBUG_VERBOSE && MICROPY_PY_THREAD_GIL
|
|
// If verbose debug output is enabled some strings are printed before the
|
|
// GIL mutex is set up. When that happens, no Python code is running and
|
|
// therefore the interpreter doesn't care about the GIL not being ready.
|
|
release_gil = release_gil && (MP_STATE_VM(gil_mutex).handle != NULL);
|
|
#endif
|
|
if (release_gil) {
|
|
MP_THREAD_GIL_EXIT();
|
|
}
|
|
#if MICROPY_HW_ESP_USB_SERIAL_JTAG
|
|
usb_serial_jtag_tx_strn(str, len);
|
|
did_write = true;
|
|
#endif
|
|
#if MICROPY_HW_ENABLE_UART_REPL
|
|
uart_stdout_tx_strn(str, len);
|
|
did_write = true;
|
|
#endif
|
|
if (release_gil) {
|
|
MP_THREAD_GIL_ENTER();
|
|
}
|
|
#endif // MICROPY_HW_ENABLE_UART_REPL || CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED
|
|
#if MICROPY_HW_USB_CDC
|
|
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
|
|
if (cdc_res > 0) {
|
|
did_write = true;
|
|
ret = MIN(cdc_res, ret);
|
|
}
|
|
#endif
|
|
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
|
|
if (dupterm_res >= 0) {
|
|
did_write = true;
|
|
ret = MIN((mp_uint_t)dupterm_res, ret);
|
|
}
|
|
return did_write ? ret : 0;
|
|
}
|
|
|
|
uint32_t mp_hal_ticks_ms(void) {
|
|
return esp_timer_get_time() / 1000;
|
|
}
|
|
|
|
uint32_t mp_hal_ticks_us(void) {
|
|
return esp_timer_get_time();
|
|
}
|
|
|
|
void mp_hal_delay_ms(uint32_t ms) {
|
|
uint64_t us = (uint64_t)ms * 1000ULL;
|
|
uint64_t dt;
|
|
uint64_t t0 = esp_timer_get_time();
|
|
for (;;) {
|
|
mp_handle_pending(true);
|
|
MICROPY_PY_SOCKET_EVENTS_HANDLER
|
|
MP_THREAD_GIL_EXIT();
|
|
uint64_t t1 = esp_timer_get_time();
|
|
dt = t1 - t0;
|
|
if (dt + portTICK_PERIOD_MS * 1000ULL >= us) {
|
|
// doing a vTaskDelay would take us beyond requested delay time
|
|
taskYIELD();
|
|
MP_THREAD_GIL_ENTER();
|
|
t1 = esp_timer_get_time();
|
|
dt = t1 - t0;
|
|
break;
|
|
} else {
|
|
ulTaskNotifyTake(pdFALSE, 1);
|
|
MP_THREAD_GIL_ENTER();
|
|
}
|
|
}
|
|
if (dt < us) {
|
|
// do the remaining delay accurately
|
|
mp_hal_delay_us(us - dt);
|
|
}
|
|
}
|
|
|
|
void mp_hal_delay_us(uint32_t us) {
|
|
// these constants are tested for a 240MHz clock
|
|
const uint32_t this_overhead = 5;
|
|
const uint32_t pend_overhead = 150;
|
|
|
|
// return if requested delay is less than calling overhead
|
|
if (us < this_overhead) {
|
|
return;
|
|
}
|
|
us -= this_overhead;
|
|
|
|
uint64_t t0 = esp_timer_get_time();
|
|
for (;;) {
|
|
uint64_t dt = esp_timer_get_time() - t0;
|
|
if (dt >= us) {
|
|
return;
|
|
}
|
|
if (dt + pend_overhead < us) {
|
|
// we have enough time to service pending events
|
|
// (don't use MICROPY_EVENT_POLL_HOOK because it also yields)
|
|
mp_handle_pending(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
uint64_t mp_hal_time_ns(void) {
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
uint64_t ns = tv.tv_sec * 1000000000ULL;
|
|
ns += (uint64_t)tv.tv_usec * 1000ULL;
|
|
return ns;
|
|
}
|
|
|
|
// Wake up the main task if it is sleeping.
|
|
void mp_hal_wake_main_task(void) {
|
|
xTaskNotifyGive(mp_main_task_handle);
|
|
}
|
|
|
|
// Wake up the main task if it is sleeping, to be called from an ISR.
|
|
void mp_hal_wake_main_task_from_isr(void) {
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
vTaskNotifyGiveFromISR(mp_main_task_handle, &xHigherPriorityTaskWoken);
|
|
if (xHigherPriorityTaskWoken == pdTRUE) {
|
|
portYIELD_FROM_ISR();
|
|
}
|
|
}
|