Serial Wire Output (SWO) viewer for tracing purposes. Tested on F401 and ST-LINK Utility as well as for F103 and Segger J-Link SWO viewer.

Dependents:   WiFi_Scanner mbed_nucleo_swo DISCO-F429ZI_LCDTS_demo_richard TEST_SM_SPEED

Issue: SWO only available in main file

The SWO is instantiate in the main.c file. Then we can call to swo.printf(str) and the string str is shown. Then problem is when I need to print information (strings or chars) in other files, in those cases I can build the program but there is no output from those lines only the prints from main file.

1 comment:

23 Aug 2017

It is not an SWO issue and should be solved like like any other C/C++ case where you have an instantiation in one file and you want to use that variable in another file. You need to declare the variable as ''extern" in the second file to make it accessible. So, instantiate in main() as usual, and modify the second file as shown below:

#include "mbed.h"
#include "myfunction.h"

//The next include is needed to ensure that the compiler recognises ''SWO_Channel''
#include "SWO.h"
 
extern SWO_Channel SWO;  // SWO is instantiated in main(), we just reference it here
 
void myfunction() {
  SWO.printf("\r\nHello World from myfunction\n");
  //  .. your code
}

The compiler will now be happy with this second file and the linker will connect the ''external'' SWO variable to the one instantiated in main. Alternatively, you could pass the variable around as a parameter, but the solution above is easier.

What approach have you used to try SWO in other files?

I have successfully tested the solution shown above. It is included in the updated SWO test example.