Re: Can you enable printf on R4?

It not related to used UART number, it is just a software problem. While you added a printf routine to, say, HardwareSerial class, it will be available on any Serial.

However, keep in mind that printf code is quite complex and consists of many functions.

PS We done it in our WinnerMicro W80x arduino package project

a plain C printf?

maybe this can help

Hi Delta_G, maybe I didn't understand the question, anyway ...

... on TinyUSB I don't know, but if you want to enable the Arduiino UNO R4 debugging (BACKTRACE) also on Minima using "Serial1" , you can do it with a simple modification to main.c (... as I did):

#ifdef BACKTRACE_SUPPORT
   // "install" stacktrace print over Hardfault handler
   *(irq_vector_table + 3) = (uint32_t)Stacktrace_Handler;
   cm_backtrace_init(stringify(PROJECT_NAME), "RA", __DATE__ "\0");
#endif

   _init();
   initVariant();
   analogReference();
#ifndef NO_USB
   __USBStart();
   Serial.begin(115200);
#endif
   startAgt();
#if defined (BACKTRACE_SUPPORT) && defined (ARDUINO_UNOR4_MINIMA)
   /* gpb01 add support for ARDUINO UNO R4 MINIMA and WeAct BOARD */
   Serial1.begin ( 115200 );
   Serial1.println ( "BackTrace support enabled, setup() delayed ...");
   for ( uint8_t i = 15; i > 0; i-- ) {
	   R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);
	   Serial1.print ( i );
	   Serial1.print ( " " );
   }
   Serial1.println ( );
   Serial1.println ( " ... entering setup()" );
#endif
   setup();
   while (1)
   {
      loop();
   }
}

#ifdef BACKTRACE_SUPPORT
/* for printf compatibility */

#ifdef ARDUINO_UNOR4_MINIMA
   /* gpb01 add support for ARDUINO UNO R4 MINIMA and WeAct BOARD */
extern "C" __attribute__((weak)) int _write (int fhdl, const void *buf, size_t count) {
  (void)fhdl;
  return Serial1.write_raw((uint8_t*)buf, count);
}
#else
extern "C" __attribute__((weak)) int _write (int fhdl, const void *buf, size_t count) {
  (void)fhdl;
  return Serial.write_raw((uint8_t*)buf, count);
}
#endif

As you can see, I've added a simple delay with a message on "Serial1" to report that you're starting a program with "BACKTRACE" support, but of course if you don't need it, you can throw out the whole thing. :grin:

Next, you need to add a new definition for "Arduino UNO R4 Minima" in the "boards.txt" file, where you define the "BACKTRACE", just like for WiFi so, after, you can select if you want the standard MINIMA or the MINIMA with BACKTRACE.

I don't know if this is helpful or ... if I didn't understand the question ... :roll_eyes:

Guglielmo

mmm ... this is probably the case ...

... reading your question better, I may understand that you want to implement real "debugging" (... with trace, breakpoint, etc.) on MINIMA ... in which case my instructions are completely useless, sorry. :slightly_frowning_face:

Guglielmo

I found THIS on Reddit ... maybe you can try it and see if it works. :roll_eyes:

Guglielmo

@Delta_G did you see my comment?

In my project, I just use the following code for Serial1 on UNO R4:

#include <stdio.h>
#include <stdarg.h>

#define PRINTF_BUF_SIZE 512

int printf(const char* fmt, ...) {
  int len;
  char buf[PRINTF_BUF_SIZE];

  va_list arg_ptr;
  va_start(arg_ptr, fmt);
  len = vsnprintf(buf, PRINTF_BUF_SIZE, fmt, arg_ptr);
  va_end(arg_ptr);

  // output to the serial console through the 'Serial'
  len = Serial1.write((uint8_t*)buf, (size_t)(len + 1));

  return len;
}

I think that

 len = Serial1.write((uint8_t*)buf, (size_t)len);

is enough but the last character would be missing, so I use (size_t)(len + 1) instead of (size_t)len.

cf. Arduino Playground - Printf

Ok, I don't know if you still need it ... anyway, I tried the code and it works perfectly on Renesas extending the "Print" class (... and so Serial, Ethernet, WiFi, LiquidCrystal, etc.).

If you still need it:

  1. in Arduino UNO R4 core replace the platform.txt with this one: platform.txt (8.0 KB)
  2. replace Print.h with this one: Print.h (3.2 KB)
  3. replace Print.cpp with this one: Print.cpp (8.2 KB)

... now you have your printf().

Let me know ... :slight_smile:

Guglielmo

I tried the code in post #3, but ... the amount of flash used is impressive (over 70%) :open_mouth:

#include <Arduino.h>
#include <stdio.h>
#include <iostream>

using namespace std;

extern "C" {
   int _write ( int fd, char *ptr, int len ) {
      ( void ) fd;
      return Serial.write ( ptr, len );
   }
}

void setup() {

   Serial.begin ( 115200 );
   while ( !Serial );

   Serial.println ( "START" );

   printf ( "HERE WE ARE\r\n" );

   cout << "TEST COUT\r\n";
   cerr << "TEST ERR\r\n";
}

void loop() {

}

Sketch uses 190720 bytes (72%) of program storage space. Maximum is 262144 bytes.
Global variables use 12644 bytes (38%) of dynamic memory, leaving 20124 bytes for local variables. Maximum is 32768 bytes.

Guglielmo

without the iostream it is:
Sketch uses 53760 bytes (20%) of program storage space. Maximum is 262144 bytes.
Global variables use 4552 bytes (13%) of dynamic memory, leaving 28216 bytes for local variables. Maximum is 32768 bytes.

1 Like

YES ... the "monster" is <iostream> ... :open_mouth:

#include <Arduino.h>
#include <stdio.h>

using namespace std;

extern "C" {
   int _write ( int fd, char *ptr, int len ) {
      ( void ) fd;
      return Serial.write ( ptr, len );
   }
}

void setup() {

   Serial.begin ( 115200 );
   while ( !Serial );

   Serial.println ( "START" );

   printf ( "HERE WE ARE\r\n" );
   
}

void loop() {

}

Sketch uses 49952 bytes (19%) of program storage space. Maximum is 262144 bytes.
Global variables use 6704 bytes (20%) of dynamic memory, leaving 26064 bytes for local variables. Maximum is 32768 bytes.

Guglielmo