k82f_trace.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <stdint.h>
  2. #include "k82f_hal.h"
  3. #include "fsl_common.h"
  4. #include "fsl_gpio.h"
  5. #include "fsl_port.h"
  6. /* Similar trace info at:
  7. https://mcuoneclipse.com/2016/11/05/tutorial-getting-etm-instruction-trace-with-nxp-kinetis-arm-cortex-m4f/
  8. The K82F doesn't have the ETF or MCM register, so just turn on outputs is enough to be tracin' in the USA.
  9. */
  10. static void KinetisTrace_ConfigureGPIO(void) {
  11. uint32_t value;
  12. #define PORT_PCR_DSE_ENABLE (1<<6) /* Port Configuration Register, Drive Strength Enable (DSE) bit */
  13. #define PORT_PCR_MUX_ALTERNATE_4 (4<<8) /* Port Configuration Register, Alternate 4 function (mux as trace pin) */
  14. #define PORT_PCR_CONFIG_FOR_TRACE (PORT_PCR_DSE_ENABLE|PORT_PCR_DSE_ENABLE|PORT_PCR_MUX_ALTERNATE_4) /* for trace, mux it with function 5 and high drive strength */
  15. /* check and enable clocking of PORTE */
  16. value = SIM->SCGC5; /* read SIM_SCGC5 at 0x40048038 */
  17. if ((value & (1<<13)) == 0) { /* Bit13 in SCGC5 is the PortE clock gate control bit. Clock not already enabled? */
  18. SIM->SCGC5 |= (1<<13); /* Enabling clock gate for Port E */
  19. }
  20. value = SIM->SOPT2; /* SIM_SOPT2 at 0x40048004 */
  21. if ((value&(1<<12))==0) { /* Bit 12 enables the trace clock. Is the debug trace clock not already enabled? */
  22. SIM->SOPT2 |= (1<<12); /* Debug trace clock select = Core/system clock */
  23. }
  24. CLOCK_EnableClock(kCLOCK_PortA); //enable port a clock
  25. PORT_SetPinMux(PORTA, 16, kPORT_MuxAlt4);
  26. PORT_SetPinMux(PORTA, 15, kPORT_MuxAlt4);
  27. PORT_SetPinMux(PORTA, 14, kPORT_MuxAlt4);
  28. PORT_SetPinMux(PORTA, 13, kPORT_MuxAlt4);
  29. PORT_SetPinMux(PORTA, 12, kPORT_MuxAlt4);
  30. }
  31. void etmtrace_enable(void)
  32. {
  33. KinetisTrace_ConfigureGPIO();
  34. }