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

Committer:
wim
Date:
Tue Dec 23 21:05:52 2014 +0000
Revision:
3:e5af2e131b95
Parent:
2:ef928f61a770
Child:
4:53de8ef789f3
Added Class SWO_Channel that supports Stream putc() and printf().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 1:bae4cff278f6 1 /* mbed SWO Library
wim 1:bae4cff278f6 2 * Copyright (c) 2014, v01: WH. Ported from Segger example
wim 3:e5af2e131b95 3 * v02: WH. Added Class with Stream support
wim 1:bae4cff278f6 4 *
wim 2:ef928f61a770 5 * Simple implementation for tracing via Serial Wire Output(SWO) for Cortex-M processors.
wim 1:bae4cff278f6 6 * It can be used with Host PC software such as ST-LINK Utility or Segger J-Link SWO viewer.
wim 1:bae4cff278f6 7 * This sample implementation ensures that output via SWO is enabled in order to guarantee
wim 1:bae4cff278f6 8 * that the application does not hang.
wim 1:bae4cff278f6 9 *
wim 1:bae4cff278f6 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 1:bae4cff278f6 11 * of this software and associated documentation files (the "Software"), to deal
wim 1:bae4cff278f6 12 * in the Software without restriction, including without limitation the rights
wim 1:bae4cff278f6 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 1:bae4cff278f6 14 * copies of the Software, and to permit persons to whom the Software is
wim 1:bae4cff278f6 15 * furnished to do so, subject to the following conditions:
wim 1:bae4cff278f6 16 *
wim 1:bae4cff278f6 17 * The above copyright notice and this permission notice shall be included in
wim 1:bae4cff278f6 18 * all copies or substantial portions of the Software.
wim 1:bae4cff278f6 19 *
wim 1:bae4cff278f6 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 1:bae4cff278f6 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 1:bae4cff278f6 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 1:bae4cff278f6 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 1:bae4cff278f6 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 1:bae4cff278f6 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 1:bae4cff278f6 26 * THE SOFTWARE.
wim 1:bae4cff278f6 27 */
wim 1:bae4cff278f6 28
wim 1:bae4cff278f6 29 #ifndef MBED_SWO_H
wim 1:bae4cff278f6 30 #define MBED_SWO_H
wim 0:0fd55660fc26 31
wim 3:e5af2e131b95 32 //
wim 3:e5af2e131b95 33 // This is the Class implementation
wim 3:e5af2e131b95 34 //
wim 3:e5af2e131b95 35
wim 3:e5af2e131b95 36 /**
wim 3:e5af2e131b95 37 * @code
wim 3:e5af2e131b95 38 * #include "mbed.h"
wim 3:e5af2e131b95 39 * #include "SWO.h"
wim 3:e5af2e131b95 40 *
wim 3:e5af2e131b95 41 * DigitalOut myled(LED1);
wim 3:e5af2e131b95 42 *
wim 3:e5af2e131b95 43 * Serial pc(SERIAL_TX, SERIAL_RX);
wim 3:e5af2e131b95 44 *
wim 3:e5af2e131b95 45 * SWO_Channel SWO();
wim 3:e5af2e131b95 46 *
wim 3:e5af2e131b95 47 * int main() {
wim 3:e5af2e131b95 48 * pc.printf("Hello World\n\r");
wim 3:e5af2e131b95 49 *
wim 3:e5af2e131b95 50 * SWO.printf("\r\nHello World from SWO\r\n");
wim 3:e5af2e131b95 51 * SWO.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
wim 3:e5af2e131b95 52 *
wim 3:e5af2e131b95 53 * while(1) {
wim 3:e5af2e131b95 54 * myled = 1; // LED is ON
wim 3:e5af2e131b95 55 * wait(0.2); // 200 ms
wim 3:e5af2e131b95 56 * myled = 0; // LED is OFF
wim 3:e5af2e131b95 57 * wait(1.0); // 1 sec
wim 3:e5af2e131b95 58 *
wim 3:e5af2e131b95 59 * SWO.putc('#');
wim 3:e5af2e131b95 60 * }
wim 3:e5af2e131b95 61 * }
wim 3:e5af2e131b95 62 * @endcode
wim 3:e5af2e131b95 63 */
wim 3:e5af2e131b95 64
wim 3:e5af2e131b95 65 /** An SWO interface for debugging that supports Stream
wim 3:e5af2e131b95 66 *
wim 3:e5af2e131b95 67 * @brief Currently works on nucleo ST-LINK using ST-Link Utility and other devices that support SWD/SWO using Segger SWO viewer
wim 3:e5af2e131b95 68 *
wim 3:e5af2e131b95 69 */
wim 3:e5af2e131b95 70 class SWO_Channel : public Stream {
wim 3:e5af2e131b95 71
wim 3:e5af2e131b95 72 public:
wim 3:e5af2e131b95 73 /** Create an SWO interface for debugging that supports Stream
wim 3:e5af2e131b95 74 *
wim 3:e5af2e131b95 75 */
wim 3:e5af2e131b95 76 SWO_Channel();
wim 3:e5af2e131b95 77
wim 3:e5af2e131b95 78 #if DOXYGEN_ONLY
wim 3:e5af2e131b95 79 /** Write a character to the display
wim 3:e5af2e131b95 80 *
wim 3:e5af2e131b95 81 * @param c The character to write to the display
wim 3:e5af2e131b95 82 */
wim 3:e5af2e131b95 83 int putc(int c);
wim 3:e5af2e131b95 84
wim 3:e5af2e131b95 85 /** Write a formatted string to the display
wim 3:e5af2e131b95 86 *
wim 3:e5af2e131b95 87 * @param format A printf-style format string, followed by the
wim 3:e5af2e131b95 88 * variables to use in formatting the string.
wim 3:e5af2e131b95 89 */
wim 3:e5af2e131b95 90 int printf(const char* format, ...);
wim 3:e5af2e131b95 91 #endif
wim 3:e5af2e131b95 92
wim 3:e5af2e131b95 93 protected:
wim 3:e5af2e131b95 94 // Stream implementation functions
wim 3:e5af2e131b95 95 virtual int _putc(int value);
wim 3:e5af2e131b95 96 virtual int _getc();
wim 3:e5af2e131b95 97
wim 3:e5af2e131b95 98 private:
wim 3:e5af2e131b95 99
wim 3:e5af2e131b95 100 };
wim 3:e5af2e131b95 101
wim 3:e5af2e131b95 102
wim 3:e5af2e131b95 103 //
wim 3:e5af2e131b95 104 //This is the classic implementation
wim 3:e5af2e131b95 105 //
wim 3:e5af2e131b95 106
wim 1:bae4cff278f6 107 /**
wim 1:bae4cff278f6 108 * @code
wim 1:bae4cff278f6 109 * #include "mbed.h"
wim 1:bae4cff278f6 110 * #include "SWO.h"
wim 1:bae4cff278f6 111 *
wim 1:bae4cff278f6 112 * DigitalOut myled(LED1);
wim 1:bae4cff278f6 113 *
wim 1:bae4cff278f6 114 * Serial pc(SERIAL_TX, SERIAL_RX);
wim 1:bae4cff278f6 115 *
wim 1:bae4cff278f6 116 * int main() {
wim 1:bae4cff278f6 117 * pc.printf("Hello World\n\r");
wim 1:bae4cff278f6 118 *
wim 1:bae4cff278f6 119 * SWO_PrintString("\r\nHello World from SWO\r\n");
wim 1:bae4cff278f6 120 * char message[64];
wim 1:bae4cff278f6 121 * sprintf(message, "CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
wim 1:bae4cff278f6 122 * SWO_PrintString(message);
wim 1:bae4cff278f6 123 *
wim 1:bae4cff278f6 124 * while(1) {
wim 1:bae4cff278f6 125 * myled = 1; // LED is ON
wim 1:bae4cff278f6 126 * wait(0.2); // 200 ms
wim 1:bae4cff278f6 127 * myled = 0; // LED is OFF
wim 1:bae4cff278f6 128 * wait(1.0); // 1 sec
wim 1:bae4cff278f6 129 *
wim 1:bae4cff278f6 130 * SWO_PrintString("#");
wim 1:bae4cff278f6 131 * }
wim 1:bae4cff278f6 132 * }
wim 1:bae4cff278f6 133 * @endcode
wim 1:bae4cff278f6 134 */
wim 1:bae4cff278f6 135
wim 1:bae4cff278f6 136 // Prototypes
wim 1:bae4cff278f6 137
wim 1:bae4cff278f6 138 /**
wim 1:bae4cff278f6 139 * @brief
wim 1:bae4cff278f6 140 * Checks if SWO is set up. If it is not, return,
wim 1:bae4cff278f6 141 * to avoid program hangs if no debugger is connected.
wim 1:bae4cff278f6 142 * If it is set up, print a character to the ITM_STIM register
wim 1:bae4cff278f6 143 * in order to provide data for SWO.
wim 1:bae4cff278f6 144 * @param c The Character to be printed.
wim 1:bae4cff278f6 145 * @notes Additional checks for device specific registers can be added.
wim 0:0fd55660fc26 146 */
wim 0:0fd55660fc26 147 void SWO_PrintChar (char c);
wim 1:bae4cff278f6 148
wim 1:bae4cff278f6 149
wim 1:bae4cff278f6 150 /**
wim 1:bae4cff278f6 151 *
wim 1:bae4cff278f6 152 * SWO_PrintString()
wim 1:bae4cff278f6 153 *
wim 1:bae4cff278f6 154 * @brief Print a string via SWO.
wim 1:bae4cff278f6 155 * @param *s The string to be printed.
wim 1:bae4cff278f6 156 */
wim 0:0fd55660fc26 157 void SWO_PrintString(const char *s);
wim 0:0fd55660fc26 158
wim 0:0fd55660fc26 159 #endif