bsp_trace.c

Go to the documentation of this file.
00001 /**************************************************************************/
00018 #include <stdbool.h>
00019 #include "em_device.h"
00020 #include "em_gpio.h"
00021 #include "em_cmu.h"
00022 #include "bsp_trace.h"
00023 #include "bsp.h"
00024 
00025 #if defined(BSP_ETM_TRACE) && defined( ETM_PRESENT )
00026 /**************************************************************************/
00030 void BSP_TraceEtmSetup(void)
00031 {
00032   /* Enable peripheral clocks */
00033   CMU->HFCORECLKEN0 |= CMU_HFCORECLKEN0_LE;
00034   CMU->HFPERCLKEN0  |= CMU_HFPERCLKEN0_GPIO;
00035   CMU->OSCENCMD      = CMU_OSCENCMD_AUXHFRCOEN;
00036 
00037   /* Wait until AUXHFRCO clock is ready */
00038   while (!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY)) ;
00039 
00040   /* Enable Port D, pins 3,4,5,6 for ETM Trace Data output */
00041   GPIO->P[3].MODEL = (GPIO->P[3].MODEL & ~_GPIO_P_MODEL_MODE3_MASK) | GPIO_P_MODEL_MODE3_PUSHPULL;
00042   GPIO->P[3].MODEL = (GPIO->P[3].MODEL & ~_GPIO_P_MODEL_MODE4_MASK) | GPIO_P_MODEL_MODE4_PUSHPULL;
00043   GPIO->P[3].MODEL = (GPIO->P[3].MODEL & ~_GPIO_P_MODEL_MODE5_MASK) | GPIO_P_MODEL_MODE5_PUSHPULL;
00044   GPIO->P[3].MODEL = (GPIO->P[3].MODEL & ~_GPIO_P_MODEL_MODE6_MASK) | GPIO_P_MODEL_MODE6_PUSHPULL;
00045 
00046   /* Enable Port D, pin 7 for DBG_TCLK */
00047   GPIO->P[3].MODEL = (GPIO->P[3].MODEL & ~_GPIO_P_MODEL_MODE7_MASK) | GPIO_P_MODEL_MODE7_PUSHPULL;
00048 
00049   /* Configure trace output for alternate location */
00050   GPIO->ROUTE = GPIO->ROUTE | GPIO_ROUTE_TCLKPEN | GPIO_ROUTE_TD0PEN | GPIO_ROUTE_TD1PEN
00051                 | GPIO_ROUTE_TD2PEN | GPIO_ROUTE_TD3PEN
00052                 | GPIO_ROUTE_ETMLOCATION_LOC0;
00053 }
00054 #endif
00055 
00056 #if defined( GPIO_ROUTE_SWOPEN )
00057 /**************************************************************************/
00063 void BSP_TraceSwoSetup(void)
00064 {
00065   /* Enable GPIO clock */
00066   CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;
00067 
00068   /* Enable Serial wire output pin */
00069   GPIO->ROUTE |= GPIO_ROUTE_SWOPEN;
00070 
00071   /* Set correct location */
00072   GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | BSP_TRACE_SWO_LOCATION;
00073 
00074   /* Enable output on correct pin. */
00075   TRACE_ENABLE_PINS();
00076 
00077   /* Enable debug clock AUXHFRCO */
00078   CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN;
00079 
00080   /* Wait until clock is ready */
00081   while (!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY)) ;
00082 
00083   // Check if trace already enabled, if so assume that an external debugger
00084   // is in control and skip further setup.
00085   if ( !( CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk ) ) {
00086     /* Enable trace in core debug */
00087     CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
00088 
00089     /* Enable PC and IRQ sampling output */
00090     DWT->CTRL = 0x400113FF;
00091 
00092     /* Set TPIU prescaler to 16. */
00093     TPI->ACPR = 0xf;
00094 
00095     /* Set protocol to NRZ */
00096     TPI->SPPR = 2;
00097 
00098     /* Disable continuous formatting */
00099     TPI->FFCR = 0x100;
00100 
00101     /* Unlock ITM and output data */
00102     ITM->LAR = 0xC5ACCE55;
00103     ITM->TCR = 0x10009;
00104   }
00105 }
00106 #endif
00107 
00108 
00109 #if defined( GPIO_ROUTE_SWOPEN )
00110 /**************************************************************************/
00116 bool BSP_TraceProfilerSetup(void)
00117 {
00118   volatile uint32_t *userData = (uint32_t *) USER_PAGE;
00119 
00120   /* Check magic "trace" word in user page */
00121   if (*userData == 0x00000000UL)
00122   {
00123     return false;
00124   }
00125   else
00126   {
00127     BSP_TraceSwoSetup();
00128     return true;
00129   }
00130 }
00131 #endif