Skip to content

Commit 1ae2de5

Browse files
committed
feat: implement weak _write() hook for Print
Signed-off-by: Aymane Bahssain <aymane.bahssain@st.com>
1 parent 03a8daf commit 1ae2de5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

api/Print.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424

2525
#include "Print.h"
2626

27+
#ifdef ARDUINO_ARCH_STM32
28+
#include <unistd.h>
29+
#include "uart.h"
30+
#if defined (VIRTIO_LOG)
31+
#include "virtio_log.h"
32+
#endif
33+
#endif
34+
2735
using namespace arduino;
2836

2937
// Public Methods //////////////////////////////////////////////////////////////
@@ -248,6 +256,32 @@ size_t Print::println(const Printable& x)
248256
return n;
249257
}
250258

259+
#ifdef ARDUINO_ARCH_STM32
260+
extern "C" {
261+
__attribute__((weak))
262+
int _write(int file, char *ptr, int len)
263+
{
264+
switch (file) {
265+
case STDOUT_FILENO:
266+
case STDERR_FILENO:
267+
/* Used for core_debug() */
268+
#if defined (VIRTIO_LOG)
269+
virtio_log((uint8_t *)ptr, (uint32_t)len);
270+
#elif defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
271+
uart_debug_write((uint8_t *)ptr, (uint32_t)len);
272+
#endif
273+
break;
274+
case STDIN_FILENO:
275+
break;
276+
default:
277+
((class Print *)file)->write((uint8_t *)ptr, len);
278+
break;
279+
}
280+
return len;
281+
}
282+
}
283+
#endif
284+
251285
int Print::printf(const char *format, ...)
252286
{
253287
va_list ap;

0 commit comments

Comments
 (0)