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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SWO.h Source File

SWO.h

00001 /* mbed SWO Library
00002  *  Copyright (c) 2014, v01: WH. Ported from Segger example
00003  *                      v02: WH. Added Class with Stream support
00004  *                2017, v03: WH,PS. Added stream claim for stdout, proposed by Pavel Sorejs
00005  *
00006  * Simple implementation for tracing via Serial Wire Output(SWO) for Cortex-M processors.
00007  * It can be used with Host PC software such as ST-LINK Utility or Segger J-Link SWO viewer.
00008  * This sample implementation ensures that output via SWO is enabled in order to guarantee
00009  * that the application does not hang.
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining a copy
00012  * of this software and associated documentation files (the "Software"), to deal
00013  * in the Software without restriction, including without limitation the rights
00014  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015  * copies of the Software, and to permit persons to whom the Software is
00016  * furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included in
00019  * all copies or substantial portions of the Software.
00020  *
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027  * THE SOFTWARE.
00028  */
00029  
00030 #ifndef MBED_SWO_H
00031 #define MBED_SWO_H
00032 
00033 //
00034 // This is the Class implementation
00035 //
00036 
00037 /**
00038  * @code
00039  * #include "mbed.h"
00040  * #include "SWO.h"
00041  *
00042  * DigitalOut myled(LED1); 
00043  *
00044  * Serial pc(SERIAL_TX, SERIAL_RX);
00045  *
00046  * SWO_Channel SWO();
00047  *
00048  * int main() {
00049  *   pc.printf("Hello World\n\r"); 
00050  *
00051  *   SWO.printf("\r\nHello World from SWO\r\n");
00052  *   SWO.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
00053  * 
00054  *   while(1) {
00055  *     myled = 1; // LED is ON
00056  *     wait(0.2); // 200 ms
00057  *     myled = 0; // LED is OFF
00058  *     wait(1.0); // 1 sec
00059  *   
00060  *     SWO.putc('#');    
00061  *   }
00062  * }
00063  * @endcode
00064  */
00065 
00066 /** An SWO interface for debugging that supports Stream
00067  *
00068  * @brief Currently works on nucleo ST-LINK using ST-Link Utility and other devices that support SWD/SWO using Segger SWO viewer 
00069  *
00070  */
00071 class SWO_Channel : public Stream {
00072 
00073 public:
00074   /** Create an SWO interface for debugging that supports Stream
00075    *
00076    * @param const char *name Channel name (default = none)   
00077    */
00078   SWO_Channel(const char *name=NULL);  
00079 
00080  /**
00081   * Function: claim
00082   *
00083   * Redirect a stream to this SWO object
00084   *
00085   * Important: A name parameter must have been added when creating the SWO object:
00086   *
00087   * @code
00088   * #include "SWO.h"
00089   * ...
00090   * SWO_Channel pc("modser");
00091   * 
00092   * int main() {
00093   *   pc.claim();            // capture <stdout>
00094   *   pc.printf("Uses the SWO library\r\n");
00095   *   printf("So does this!\r\n");
00096   * }
00097   * @endcode
00098   *
00099   * @ingroup API
00100   * @param FILE *stream The stream to redirect (default = stdout)
00101   * @return true if succeeded, else false
00102   */  
00103   bool claim(FILE *stream = stdout);
00104   
00105 #if DOXYGEN_ONLY
00106   /** Write a character to the display
00107     *
00108     * @param c The character to write to the display
00109     */
00110   int putc(int c);
00111 
00112   /** Write a formatted string to the display
00113     *
00114     * @param format A printf-style format string, followed by the
00115     *               variables to use in formatting the string.
00116     */
00117   int printf(const char* format, ...);   
00118 #endif
00119 
00120 protected:
00121   // Stream implementation functions
00122   virtual int _putc(int value);
00123   virtual int _getc();
00124 
00125 private:
00126 
00127 };
00128 
00129 
00130 //
00131 //This is the classic implementation
00132 //
00133 
00134 /**
00135  * @code
00136  * #include "mbed.h"
00137  * #include "SWO.h"
00138  *
00139  * DigitalOut myled(LED1); 
00140  *
00141  * Serial pc(SERIAL_TX, SERIAL_RX);
00142  *
00143  * int main() {
00144  *   pc.printf("Hello World\n\r"); 
00145  *
00146  *   SWO_PrintString("\r\nHello World from SWO\r\n");
00147  *   char message[64];
00148  *   sprintf(message, "CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
00149  *   SWO_PrintString(message);      
00150  * 
00151  *   while(1) {
00152  *     myled = 1; // LED is ON
00153  *     wait(0.2); // 200 ms
00154  *     myled = 0; // LED is OFF
00155  *     wait(1.0); // 1 sec
00156  *   
00157  *     SWO_PrintString("#");    
00158  *   }
00159  * }
00160  * @endcode
00161  */
00162 
00163 // Prototypes
00164 
00165 /**
00166 * @brief 
00167 *   Checks if SWO is set up. If it is not, return,
00168 *    to avoid program hangs if no debugger is connected.
00169 *   If it is set up, print a character to the ITM_STIM register
00170 *    in order to provide data for SWO.
00171 * @param c The Character to be printed.
00172 * @notes   Additional checks for device specific registers can be added.
00173 */
00174 void SWO_PrintChar  (char c);
00175 
00176 
00177 /**
00178 *
00179 * SWO_PrintString()
00180 *
00181 * @brief Print a string via SWO.
00182 * @param *s The string to be printed.
00183 */
00184 void SWO_PrintString(const char *s);
00185 
00186 #endif