Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ports/micropython/micropython.mk
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ endif

SOURCES += $(LVMP_DIR)/src/lv_mem_core_micropython.c

# An stm32 firmware with single-precision floats links MicroPython's
# single-precision libm, which has no double sqrt() for LVGL's gesture code.
ifneq ($(findstring /ports/stm32,$(LVMP_PORT_DIR)),)
ifeq ($(MICROPY_FLOAT_IMPL),single)
SOURCES += $(LVMP_DIR)/src/lv_sqrt_single.c
endif
endif

$(if $(wildcard $(LVMP_C)),,$(error $(LVMP_C) not found. Run $(BINDINGS_DIR)/regenerate_all.sh --target micropython))

# -Wno-unused-function here is not enough on ports that append -Werror after py.mk
Expand Down
20 changes: 20 additions & 0 deletions ports/micropython/src/lv_sqrt_single.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2026 Brad Barnett
//
// SPDX-License-Identifier: MIT

// LVGL's gesture recogniser (src/indev/lv_indev_gesture.c) calls the double
// sqrt(). An stm32 firmware built with single-precision floats links only
// MicroPython's single-precision libm, which has sqrtf() and no sqrt(), so the
// link fails. Gesture distances are pixels: single precision is plenty.

#include "py/mpconfig.h"

#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT

float sqrtf(float x);

double sqrt(double x) {
return sqrtf((float)x);
}

#endif
8 changes: 8 additions & 0 deletions ports/micropython/tests/test_micropython_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ def test_fs_driver_is_frozen_from_the_one_copy():

def test_no_consumer_smoke_wrapper_remains():
assert not (ROOT / "tools" / "test_lvgl_unix.py").exists()


def test_stm32_single_float_builds_get_a_double_sqrt():
make = (ROOT / "micropython.mk").read_text()
assert "src/lv_sqrt_single.c" in make
assert "/ports/stm32" in make
shim = (ROOT / "src" / "lv_sqrt_single.c").read_text()
assert "MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT" in shim
Loading