1 /***************************************************************************//**
2  * @file
3  * @brief Debug (DBG) Peripheral API
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 
31 #include "em_dbg.h"
32 
33 #if defined(CoreDebug_DHCSR_C_DEBUGEN_Msk)
34 
35 #include "sl_assert.h"
36 #include "em_cmu.h"
37 #include "em_gpio.h"
38 #include "em_msc.h"
39 
40 /***************************************************************************//**
41  * @addtogroup dbg DBG - Debug
42  * @brief Debug (DBG) Peripheral API
43  * @details
44  *  This module contains functions to control the DBG peripheral of Silicon
45  *  Labs 32-bit MCUs and SoCs. The Debug Interface is used to program and debug
46  *  Silicon Labs devices.
47  * @{
48  ******************************************************************************/
49 
50 /*******************************************************************************
51  **************************   GLOBAL FUNCTIONS   *******************************
52  ******************************************************************************/
53 
54 #if defined(GPIO_ROUTE_SWOPEN) || defined(GPIO_ROUTEPEN_SWVPEN) \
55   || defined(GPIO_TRACEROUTEPEN_SWVPEN)
56 /***************************************************************************//**
57  * @brief
58  *   Enable Serial Wire Output (SWO) pin.
59  *
60  * @details
61  *   The SWO pin (sometimes denoted SWV, serial wire viewer) allows for
62  *   miscellaneous output to be passed from the Cortex-M3 debug trace module to
63  *   an external debug probe. By default, the debug trace module and pin output
64  *   may be disabled.
65  *
66  *   Since the SWO pin is only useful when using a debugger, a suggested use
67  *   of this function during startup may be:
68  * @verbatim
69  * if (DBG_Connected())
70  * {
71  * DBG_SWOEnable(1);
72  * }
73  * @endverbatim
74  *   By checking if the debugger is attached, a setup leading to a higher energy
75  *   consumption when the debugger is attached can be avoided when not using
76  *   a debugger.
77  *
78  *   Another alternative may be to set the debugger tool chain to configure
79  *   the required setup (similar to the content of this function) by some
80  *   sort of toolchain scripting during its attach/reset procedure. In that
81  *   case, the above suggested code for enabling the SWO pin is not required
82  *   in the application.
83  *
84  * @param[in] location
85  *   A pin location used for SWO pin on the application in use.
86  ******************************************************************************/
DBG_SWOEnable(unsigned int location)87 void DBG_SWOEnable(unsigned int location)
88 {
89   int port;
90   int pin;
91 
92 #if defined(GPIO_SWV_PORT)
93 
94   port = GPIO_SWV_PORT;
95   pin = GPIO_SWV_PIN;
96 
97 #else
98   EFM_ASSERT(location < AFCHANLOC_MAX);
99   #if defined (AF_DBG_SWO_PORT)
100   port = AF_DBG_SWO_PORT(location);
101   pin  = AF_DBG_SWO_PIN(location);
102   #elif defined (AF_DBG_SWV_PORT)
103   port = AF_DBG_SWV_PORT(location);
104   pin  = AF_DBG_SWV_PIN(location);
105 
106   #else
107   #warning "AF debug port is not defined."
108   #endif
109 #endif
110 
111   /* Port/pin location not defined for the device. */
112   if ((pin < 0) || (port < 0)) {
113     EFM_ASSERT(0);
114     return;
115   }
116 
117   /* Ensure that the auxiliary clock going to the Cortex debug trace module is enabled. */
118 #if !defined(_SILICON_LABS_32B_SERIES_2)
119   CMU_OscillatorEnable(cmuOsc_AUXHFRCO, true, false);
120 #endif
121 
122   /* Set the selected pin location for the SWO pin and enable it. */
123   GPIO_DbgLocationSet(location);
124   GPIO_DbgSWOEnable(true);
125 
126   /* Configure the SWO pin for output. */
127   GPIO_PinModeSet((GPIO_Port_TypeDef)port, pin, gpioModePushPull, 0);
128 }
129 #endif
130 
131 /** @} (end addtogroup dbg) */
132 #endif /* defined( CoreDebug_DHCSR_C_DEBUGEN_Msk ) */
133