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:
Sat Dec 20 22:30:12 2014 +0000
Revision:
1:bae4cff278f6
Parent:
0:0fd55660fc26
Child:
2:ef928f61a770
SWO lib first release

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 1:bae4cff278f6 3 *
wim 1:bae4cff278f6 4 * Simple implementation for tracing via Single Wire Output(SWO) for Cortex-M processors.
wim 1:bae4cff278f6 5 * It can be used with Host PC software such as ST-LINK Utility or Segger J-Link SWO viewer.
wim 1:bae4cff278f6 6 * This sample implementation ensures that output via SWO is enabled in order to guarantee
wim 1:bae4cff278f6 7 * that the application does not hang.
wim 1:bae4cff278f6 8 *
wim 1:bae4cff278f6 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 1:bae4cff278f6 10 * of this software and associated documentation files (the "Software"), to deal
wim 1:bae4cff278f6 11 * in the Software without restriction, including without limitation the rights
wim 1:bae4cff278f6 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 1:bae4cff278f6 13 * copies of the Software, and to permit persons to whom the Software is
wim 1:bae4cff278f6 14 * furnished to do so, subject to the following conditions:
wim 1:bae4cff278f6 15 *
wim 1:bae4cff278f6 16 * The above copyright notice and this permission notice shall be included in
wim 1:bae4cff278f6 17 * all copies or substantial portions of the Software.
wim 1:bae4cff278f6 18 *
wim 1:bae4cff278f6 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 1:bae4cff278f6 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 1:bae4cff278f6 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 1:bae4cff278f6 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 1:bae4cff278f6 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 1:bae4cff278f6 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 1:bae4cff278f6 25 * THE SOFTWARE.
wim 1:bae4cff278f6 26 */
wim 0:0fd55660fc26 27
wim 0:0fd55660fc26 28 #include "SWO.h"
wim 0:0fd55660fc26 29
wim 1:bae4cff278f6 30 /**
wim 1:bae4cff278f6 31 *
wim 1:bae4cff278f6 32 * Defines for Cortex-M debug unit
wim 1:bae4cff278f6 33 */
wim 0:0fd55660fc26 34 #define ITM_STIM_U32(n) (*(volatile unsigned int*)(0xE0000000+4*n)) // Stimulus Port n Register word access
wim 1:bae4cff278f6 35 #define ITM_STIM_U8(n) (*(volatile char*)(0xE0000000+n)) // Stimulus Port n Register byte access
wim 0:0fd55660fc26 36 //#define ITM_STIM_U32_0 (*(volatile unsigned int*)0xE0000000) // Stimulus Port 0 Register word access
wim 0:0fd55660fc26 37 //#define ITM_STIM_U8_0 (*(volatile char*)0xE0000000) // Stimulus Port 0 Register byte access
wim 0:0fd55660fc26 38 #define ITM_ENA (*(volatile unsigned int*)0xE0000E00) // Trace Enable Ports Register
wim 0:0fd55660fc26 39 #define ITM_TCR (*(volatile unsigned int*)0xE0000E80) // Trace control register
wim 0:0fd55660fc26 40
wim 1:bae4cff278f6 41 /**
wim 1:bae4cff278f6 42 *
wim 1:bae4cff278f6 43 * SWO_PrintChar()
wim 1:bae4cff278f6 44 *
wim 1:bae4cff278f6 45 * @brief
wim 1:bae4cff278f6 46 * Checks if SWO is set up. If it is not, return,
wim 1:bae4cff278f6 47 * to avoid program hangs if no debugger is connected.
wim 1:bae4cff278f6 48 * If it is set up, print a character to the ITM_STIM register
wim 1:bae4cff278f6 49 * in order to provide data for SWO.
wim 1:bae4cff278f6 50 * @param c The Character to be printed.
wim 1:bae4cff278f6 51 * @notes Additional checks for device specific registers can be added.
wim 1:bae4cff278f6 52 */
wim 1:bae4cff278f6 53 void SWO_PrintChar(char c) {
wim 0:0fd55660fc26 54
wim 0:0fd55660fc26 55 // Check if ITM_TCR.ITMENA is set
wim 0:0fd55660fc26 56 if ((ITM_TCR & 1) == 0) {
wim 0:0fd55660fc26 57 return;
wim 0:0fd55660fc26 58 }
wim 1:bae4cff278f6 59
wim 0:0fd55660fc26 60 // Check if stimulus port is enabled
wim 0:0fd55660fc26 61 if ((ITM_ENA & 1) == 0) {
wim 0:0fd55660fc26 62 return;
wim 0:0fd55660fc26 63 }
wim 0:0fd55660fc26 64
wim 1:bae4cff278f6 65 // Wait until STIMx FIFO is ready, then send data
wim 1:bae4cff278f6 66 while ((ITM_STIM_U8(0) & 1) == 0);
wim 1:bae4cff278f6 67 ITM_STIM_U8(0) = c;
wim 1:bae4cff278f6 68
wim 1:bae4cff278f6 69 // while ((ITM_STIM_U32(0) & 1) == 0);
wim 1:bae4cff278f6 70 // ITM_STIM_U32(0) = c;
wim 0:0fd55660fc26 71 }
wim 0:0fd55660fc26 72
wim 1:bae4cff278f6 73 /**
wim 1:bae4cff278f6 74 *
wim 1:bae4cff278f6 75 * SWO_PrintString()
wim 1:bae4cff278f6 76 *
wim 1:bae4cff278f6 77 * @brief Print a string via SWO.
wim 1:bae4cff278f6 78 * @param *s The string to be printed.
wim 1:bae4cff278f6 79 *
wim 1:bae4cff278f6 80 */
wim 0:0fd55660fc26 81 void SWO_PrintString(const char *s) {
wim 1:bae4cff278f6 82
wim 1:bae4cff278f6 83 // Print out characters until \0
wim 0:0fd55660fc26 84 while (*s) {
wim 0:0fd55660fc26 85 SWO_PrintChar(*s++);
wim 0:0fd55660fc26 86 }
wim 0:0fd55660fc26 87 }