From 7ca06bc32794044095cf9e3b8e74fcde479bac42 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 6 Aug 2025 12:00:17 -0500 Subject: [PATCH] bricks/_common/mphalport: use same file for EV3 and STM32 Move the STM32 mphalport.c to the common directory so that it can be used for both the EV3 and STM32 ports. Eventually, all platforms should use the same file. The EV3 clock driver had to have a small change to make it work with the common mphalport.c. Also clean up some duplication with regard to thumb assembly code files while we are touching this. --- bricks/_common/arm_none_eabi.mk | 28 ++----- bricks/{_common_stm32 => _common}/mphalport.c | 0 bricks/ev3/mphalport.c | 82 ------------------- lib/pbio/drv/clock/clock_ev3.c | 11 +-- 4 files changed, 13 insertions(+), 108 deletions(-) rename bricks/{_common_stm32 => _common}/mphalport.c (100%) delete mode 100644 bricks/ev3/mphalport.c diff --git a/bricks/_common/arm_none_eabi.mk b/bricks/_common/arm_none_eabi.mk index 9b83b488a..2f8393053 100644 --- a/bricks/_common/arm_none_eabi.mk +++ b/bricks/_common/arm_none_eabi.mk @@ -253,24 +253,22 @@ PY_EXTRA_SRC_C += $(addprefix bricks/_common/,\ micropython.c \ ) -ifeq ($(PB_MCU_FAMILY),STM32) -PY_EXTRA_SRC_C += $(addprefix bricks/_common_stm32/,\ +# TODO: NXT should eventually use the same mphalport.c as well. +ifeq ($(PB_MCU_FAMILY),AT91SAM7) +PY_EXTRA_SRC_C += $(addprefix bricks/nxt/,\ mphalport.c \ ) - -ifeq ($(PB_MCU_SERIES),F0) -SRC_S += shared/runtime/gchelper_thumb1.s else -SRC_S += shared/runtime/gchelper_thumb2.s -endif -endif - -ifeq ($(PB_MCU_FAMILY),AT91SAM7) -PY_EXTRA_SRC_C += $(addprefix bricks/nxt/,\ +PY_EXTRA_SRC_C += $(addprefix bricks/_common/,\ mphalport.c \ ) +endif +# Not all MCUs support thumb2 instructions. +ifeq ($(PB_MCU_SERIES),$(filter $(PB_MCU_SERIES),AT91SAM7 F0 TIAM1808)) SRC_S += shared/runtime/gchelper_thumb1.s +else +SRC_S += shared/runtime/gchelper_thumb2.s endif # Skipping uart_irda_cir.c, gpio_v2.c, and hsi2c.c usbphyGS70.c, which @@ -316,14 +314,6 @@ EV3_SRC_S = $(addprefix lib/pbio/platform/ev3/,\ start.S \ ) -ifeq ($(PB_MCU_FAMILY),TIAM1808) -PY_EXTRA_SRC_C += $(addprefix bricks/ev3/,\ - mphalport.c \ - ) - -SRC_S += shared/runtime/gchelper_thumb1.s -endif - # STM32 Bluetooth stack BLUENRG_SRC_C = $(addprefix lib/BlueNRG-MS/hci/,\ diff --git a/bricks/_common_stm32/mphalport.c b/bricks/_common/mphalport.c similarity index 100% rename from bricks/_common_stm32/mphalport.c rename to bricks/_common/mphalport.c diff --git a/bricks/ev3/mphalport.c b/bricks/ev3/mphalport.c deleted file mode 100644 index 7f869fc36..000000000 --- a/bricks/ev3/mphalport.c +++ /dev/null @@ -1,82 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright (c) 2018-2021 The Pybricks Authors -// Copyright (c) 2013, 2014 Damien P. George - -#include - -#include - -#include -#include -#include -#include - -#include "py/runtime.h" -#include "py/mphal.h" -#include "py/mpconfig.h" -#include "py/stream.h" - -// Core delay function that does an efficient sleep and may switch thread context. -// We must have the GIL. -void mp_hal_delay_ms(mp_uint_t Delay) { - // Use systick counter to do the delay - uint32_t start = pbdrv_clock_get_ms(); - // Wraparound of tick is taken care of by 2's complement arithmetic. - do { - // This macro will execute the necessary idle behaviour. It may - // raise an exception, switch threads or enter sleep mode (waiting for - // (at least) the SysTick interrupt). - MICROPY_EVENT_POLL_HOOK - } while (pbdrv_clock_get_ms() - start < Delay); -} - -uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { - uintptr_t ret = 0; - - if ((poll_flags & MP_STREAM_POLL_RD) && pbsys_host_stdin_get_available()) { - ret |= MP_STREAM_POLL_RD; - } - - return ret; -} - -// Receive single character -int mp_hal_stdin_rx_chr(void) { - uint32_t size; - uint8_t c; - - // wait for rx interrupt - while (size = 1, pbsys_host_stdin_read(&c, &size) != PBIO_SUCCESS) { - MICROPY_EVENT_POLL_HOOK - } - - return c; -} - -// Send string of given length -mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { - size_t remaining = len; - - while (remaining) { - uint32_t size = remaining; - pbio_error_t err = pbsys_host_stdout_write((const uint8_t *)str, &size); - if (err == PBIO_SUCCESS) { - str += size; - remaining -= size; - } else if (err != PBIO_ERROR_AGAIN) { - // Ignoring error for now. This means stdout is lost if Bluetooth/USB - // is disconnected. - return len - remaining; - } - - MICROPY_EVENT_POLL_HOOK - } - - return len; -} - -void mp_hal_stdout_tx_flush(void) { - while (!pbsys_host_tx_is_idle()) { - MICROPY_EVENT_POLL_HOOK - } -} diff --git a/lib/pbio/drv/clock/clock_ev3.c b/lib/pbio/drv/clock/clock_ev3.c index 75dd19883..5ee800a23 100644 --- a/lib/pbio/drv/clock/clock_ev3.c +++ b/lib/pbio/drv/clock/clock_ev3.c @@ -1,10 +1,7 @@ -// SPDX-License-Identifier: MPL-1.0 +// SPDX-License-Identifier: MPL-1.0 AND MIT // Copyright (c) 2016 Tobias Schießl - -// SPDX-License-Identifier: MIT // Copyright (c) 2024 The Pybricks Authors - #include #if PBDRV_CONFIG_CLOCK_TIAM1808 @@ -35,7 +32,7 @@ static const uint32_t timer_us_division = auxclk_freq_hz / 1000000; /** * The current tick in milliseconds */ -volatile uint32_t systick_ms = 0; +volatile uint32_t pbdrv_clock_ticks; /** * The systick interrupt service routine (ISR) which will be called every millisecond. @@ -45,7 +42,7 @@ void systick_isr_C(void) { IntSystemStatusClear(SYS_INT_TINT12_0); TimerIntStatusClear(SOC_TMR_0_REGS, TMR_INTSTAT12_TIMER_NON_CAPT); - ++systick_ms; + pbdrv_clock_ticks++; etimer_request_poll(); pbio_os_request_poll(); @@ -88,7 +85,7 @@ uint32_t pbdrv_clock_get_us(void) { } uint32_t pbdrv_clock_get_ms(void) { - return systick_ms; + return pbdrv_clock_ticks; } uint32_t pbdrv_clock_get_100us(void) {