From d0b1e5e2a8a6eba2244cc8e91c865c16525065cd Mon Sep 17 00:00:00 2001 From: Tamas Jozsi Date: Mon, 22 Dec 2025 12:17:13 +0100 Subject: [PATCH] core: serial: implement Serial.printf() --- cores/arduino/zephyrSerial.cpp | 11 +++++++++++ cores/arduino/zephyrSerial.h | 4 ++++ loader/Kconfig | 13 +++++++++++++ .../arduino_nano_matter_mgm240sd22vna.conf | 2 ++ 4 files changed, 30 insertions(+) diff --git a/cores/arduino/zephyrSerial.cpp b/cores/arduino/zephyrSerial.cpp index 2ebece7b9..dea82cb57 100644 --- a/cores/arduino/zephyrSerial.cpp +++ b/cores/arduino/zephyrSerial.cpp @@ -187,6 +187,17 @@ void arduino::ZephyrSerial::flush() { } } +#if CONFIG_ARDUINO_SERIAL_PRINTF +void arduino::ZephyrSerial::printf(const char *fmt, ...) { + char message[CONFIG_ARDUINO_SERIAL_PRINTF_BUFFER_SIZE]; + va_list args; + va_start(args, fmt); + vsnprintf(message, sizeof(message), fmt, args); + va_end(args); + this->write((uint8_t *)message, strlen(message)); +} +#endif + #if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)) #define FIRST_UART_INDEX 1 #else diff --git a/cores/arduino/zephyrSerial.h b/cores/arduino/zephyrSerial.h index 5e1d15fe7..06cf75141 100644 --- a/cores/arduino/zephyrSerial.h +++ b/cores/arduino/zephyrSerial.h @@ -89,6 +89,10 @@ class ZephyrSerial : public HardwareSerial { int peek(); int read(); +#if CONFIG_ARDUINO_SERIAL_PRINTF + void printf(const char *fmt, ...); +#endif + operator bool() { return true; } diff --git a/loader/Kconfig b/loader/Kconfig index f6ee63309..2fac391ff 100644 --- a/loader/Kconfig +++ b/loader/Kconfig @@ -17,3 +17,16 @@ config MAIN_STACK_REGION depends on CODE_DATA_RELOCATION help Specify the memory region for main stack. + +config ARDUINO_SERIAL_PRINTF + bool "Enable Serial.printf()" + default n + help + Enable the Serial.printf() function. + +config ARDUINO_SERIAL_PRINTF_BUFFER_SIZE + int "Serial.printf() buffer size" + depends on ARDUINO_SERIAL_PRINTF + default 128 + help + Set the buffer size for the Serial.printf() function. diff --git a/variants/arduino_nano_matter_mgm240sd22vna/arduino_nano_matter_mgm240sd22vna.conf b/variants/arduino_nano_matter_mgm240sd22vna/arduino_nano_matter_mgm240sd22vna.conf index 0abcbeddb..d84c12a03 100644 --- a/variants/arduino_nano_matter_mgm240sd22vna/arduino_nano_matter_mgm240sd22vna.conf +++ b/variants/arduino_nano_matter_mgm240sd22vna/arduino_nano_matter_mgm240sd22vna.conf @@ -35,3 +35,5 @@ CONFIG_STACK_CANARIES=n CONFIG_THREAD_ANALYZER=n CONFIG_SYS_HEAP_RUNTIME_STATS=n # CONFIG_LOG_DEFAULT_LEVEL=3 + +CONFIG_ARDUINO_SERIAL_PRINTF=y