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

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Thu Aug 24 18:15:02 2017 +0000
Parent:
3:e5af2e131b95
Commit message:
Added stream claim for stdout, proposed by Pavel Sorejs.

Changed in this revision

SWO.cpp Show annotated file Show diff for this revision Revisions of this file
SWO.h Show annotated file Show diff for this revision Revisions of this file
diff -r e5af2e131b95 -r 53de8ef789f3 SWO.cpp
--- a/SWO.cpp	Tue Dec 23 21:05:52 2014 +0000
+++ b/SWO.cpp	Thu Aug 24 18:15:02 2017 +0000
@@ -1,6 +1,7 @@
 /* mbed SWO Library
  *  Copyright (c) 2014, v01: WH. Ported from Segger example (www.segger.com)
  *                      v02: WH. Added Class with Stream support
+ *                2017, v03: WH,PS. Added stream claim for stdout, proposed by Pavel Sorejs
  *
  * Simple implementation for tracing via Serial Wire Output(SWO) for Cortex-M processors.
  * It can be used with Host PC software such as ST-LINK Utility or Segger J-Link SWO viewer.
@@ -35,9 +36,10 @@
 /** Create and SWO interface for debugging that supports Stream
   * @brief Currently works on nucleo ST-LINK using ST-Link Utility and other devices that support SWD/SWO using Segger SWO viewer
   */
-SWO_Channel::SWO_Channel () {
+SWO_Channel::SWO_Channel (const char *name) : Stream(name) {
   //May want to add initialisation stuff here  
 }
+ 
 
 /** Write a single character (Stream implementation)
   *
@@ -59,6 +61,35 @@
     return -1;
 }
 
+ /**
+  * Claim and redirect a stream to this SWO object
+  * Important: A name parameter must have been added when creating the SWO object.
+  *
+  * @param FILE *stream The stream to redirect (default = stdout)
+  * @return true if succeeded, else false
+  */  
+bool SWO_Channel::claim (FILE *stream) {
+    if ( FileBase::getName() == NULL) {
+        error("claim requires a name to be given in the instantiator of the SWO instance!\r\n");
+    }
+    
+    //Add '/' before name:
+    char *path = new char[strlen(FileBase::getName()) + 2];
+    sprintf(path, "/%s", FileBase::getName());
+    
+    if (freopen(path, "w", stream) == NULL) {
+        // Failed, should not happen
+        return false;
+    }
+    
+    delete(path);
+    
+    //No buffering
+    setvbuf(stream, NULL, _IONBF, 32);
+    return true;
+} 
+ 
+
 
 //
 //This is the classic implementation
diff -r e5af2e131b95 -r 53de8ef789f3 SWO.h
--- a/SWO.h	Tue Dec 23 21:05:52 2014 +0000
+++ b/SWO.h	Thu Aug 24 18:15:02 2017 +0000
@@ -1,6 +1,7 @@
 /* mbed SWO Library
  *  Copyright (c) 2014, v01: WH. Ported from Segger example
  *                      v02: WH. Added Class with Stream support
+ *                2017, v03: WH,PS. Added stream claim for stdout, proposed by Pavel Sorejs
  *
  * Simple implementation for tracing via Serial Wire Output(SWO) for Cortex-M processors.
  * It can be used with Host PC software such as ST-LINK Utility or Segger J-Link SWO viewer.
@@ -72,9 +73,35 @@
 public:
   /** Create an SWO interface for debugging that supports Stream
    *
+   * @param const char *name Channel name (default = none)   
    */
-  SWO_Channel();
+  SWO_Channel(const char *name=NULL);  
 
+ /**
+  * Function: claim
+  *
+  * Redirect a stream to this SWO object
+  *
+  * Important: A name parameter must have been added when creating the SWO object:
+  *
+  * @code
+  * #include "SWO.h"
+  * ...
+  * SWO_Channel pc("modser");
+  * 
+  * int main() {
+  *   pc.claim();            // capture <stdout>
+  *   pc.printf("Uses the SWO library\r\n");
+  *   printf("So does this!\r\n");
+  * }
+  * @endcode
+  *
+  * @ingroup API
+  * @param FILE *stream The stream to redirect (default = stdout)
+  * @return true if succeeded, else false
+  */  
+  bool claim(FILE *stream = stdout);
+  
 #if DOXYGEN_ONLY
   /** Write a character to the display
     *