Serial print for firmware development

Hello folks,

I am currently doing a project using particle photon. However, I may need to customize the original particle firmware code to fit in my project.

For developing application code, I am able to use simple Serial.printf(); / Serial.println(); to monitor the program states.

Similarity, I am wondering whether there is any methods for Serial.printing message in libray/system code, which can bring huge convenience for us!!

For example, when I debugging spark_protocol.cpp file, is there any method for me to print the states of variables on console??

For now I found a existing library called service_debug.h, which provides 4 possible ‘printing’ function, called DEBUG, WARN, ERROR, PANIC, INFO, which seems can be used to printing out message. However, I never did it success, it looks like I need to enable some macros…but just don’t know how to do that.

Any suggestions are welcome! :slight_smile:

You could give the Log feature a try

with just adding this line to your main project, you will get some system info logged out via USB Serial

SerialLogHandler logHandler;

You can also print your own log entries via something like

  Log.warn("This is a warning #%d", warnNum);

And when you don’t want the logging, just comment the logHandler line.

2 Likes

@ScruffR thanks for your response.

It did work for the already defined functions.

However if it really dosen’t work for my customized function, which is a little bit wierd.

For example, I implement a myfunction() {} in the spark_protocol.cpp file (in communication folder). And I want to print “hello world” when entering myfunction() {}, what I already do is:

  • put WARN(“Hello World”); in myfunction() {}; [firmware code]
  • as you said, put SerialLogHandler logHandler(LOG_LEVEL_ALL); in application.cpp [application code];

Am I missing anything? thanks!

Hi @chenc

How are you compiling your changes to spark_protocol.cpp?

Are you compiling locally using gcc and make? That will be the only way that works!

Thanks @bko !

Yes! everything is done locally. the way to compile is to use arm tool chain by using make file in firmware/module/ folder.

However the problem is that the print feature (using WARN(???)) did not really work. This is really important for debugging!

I have put what I have done in previous post. Do you have any ideas of what am I missing? Cheers!

Have you tried using the syntax @ScruffR pointed out:

Log.warn("This is a warning");

instead of the macro based WARN? I don’t know if the WARN macro is hooked up to this log or not.

Thanks @bko @ScruffR
not really, I think we can use it in application code, e.g. main.cpp, Instead of firmware code, such as communication stubs, protocols etc.

I guess we need to use some macros, just like how original code does. But for somehow, the extra WARN(…) a added do not really work. I think there must be some macro definition I need to modify, but couldn’t really find it :frowning:

You may want to include the respective header files.

But have you also seen the comment in service_debug.h

 * NOTE: This module is here for compatibility purposes, consider using functions and macros
 * defined in logging.h instead.

Cool! thanks for the hint, it works charming!