在Nucleo STM32板上设置SWV打印功能

时间:2018-06-28 08:55:58

标签: debugging arm stm32 nucleo

我正在使用Atollic Truestudio IDE(基本上是Eclipse)在各种STM32L4 Nucleo板上开发固件。到目前为止,由于使用了虚拟COM端口,因此我一直通过UART使用printf。

我想使用STM32 ITM迁移到printf。

更确切地说,我研究Nucleo-L4A6ZG。通过gdb服务器进行调试。

在Atollic上,我修改了Debug Configuration(调试配置)以使SWV具有80MHz的核心时钟。我已经按照STM32L4参考手册中所述修改了启动脚本。我不确定是否有必要,因为TrueStudio / Eclipse允许从GUI设置SWV,但这种方式似乎更容易:

# Set character encoding
set host-charset CP1252
set target-charset CP1252

# Reset to known state
monitor reset

# Load the program executable
load        

# Reset the chip to get to a known state. Remove "monitor reset" command 
#  if the code is not located at default address and does not run by reset. 
monitor reset

# Enable Debug connection in low power modes (DBGMCU->CR) + TPIU for SWV
set *0xE0042004 = (*0xE0042004) | 0x67

# Write 0xC5ACCE55 to the ITM Lock Access Register to unlock the write access to the ITM registers
set *0xE0000FB0 =0xC5ACCE55

# Write 0x00010005 to the ITM Trace Control Register to enable the ITM with Synchronous enabled and an ATB ID different from 0x00
set *0xE0000E80= 0x00010005

# Write 0x1 to the ITM Trace Enable Register to enable the Stimulus Port 0
set *0xE0000E00= (*0xE0000E00) | 0x1

#write 1 to ITM trace privilege register to unmask Stimulus ports 7:0
set *0xE0000E40= (*0xE0000E40) | 0x1



# Set a breakpoint at main().
tbreak main

# Run to the breakpoint.
continue

我修改了_write函数,如下所示:

static inline unsigned long ITM_SendChar (unsigned long ch)
{
  if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) &&      /* ITM enabled */
      ((ITM->TER & 1UL               ) != 0UL)   )     /* ITM Port #0 enabled */
  {
    while (ITM->PORT[0U].u32 == 0UL)
    {
      __asm("nop");
    }
    ITM->PORT[0U].u8 = (uint8_t)ch;
  }
  return (ch);
}

int _write(int file, char *ptr, int len)
{
    //return usart_write(platform_get_console(), (u8 *)ptr, len);
      int i=0;
      for(i=0 ; i<len ; i++)
        ITM_SendChar((*ptr++));
      return len;
}

逐步调试,我看到我到达了ITM->PORT[0U].u8 = (uint8_t)ch;

最后,我在IDE的SWV控制台中启动了跟踪,但是没有任何输出。

知道我想念的是什么吗? SWV的核心时钟呢?我不确定它对应什么。

3 个答案:

答案 0 :(得分:2)

我的Nucleo-F103RB面临类似的情况。进行此工作的原因是在CubeMX上选择了“跟踪异步”调试选项,而不是“串行线”。跟踪异步调试将PB3引脚指定为SWO引脚。

然后按如下所示设置调试配置: Project debug configuration to enable Serial Wire Viewer (SWV)

此外,我已经在main.c文件本身中定义了写函数,更改了syscalls.c中的定义将无效。

最后,在调试项目时,在“ Serial Wire Viewer设置”下仅启用(检查)ITM刺激端口上的端口0,如下所示: Serial Wire Viewer settings in Debug perpective

当我为时间戳和一些跟踪事件启用预分频器时,我注意到的一件事是,跟踪输出不会显示很多跟踪日志。

答案 1 :(得分:2)

任何其他人都发现了这一点-Nucleo开发板的Nucleo-32系列莫名其妙地没有将SWO引脚路由到MCU。 SWO引脚对于所有SWV功能都是必需的,因此它在设计上将不起作用。引脚数更高的Nucleo板似乎已经布线了。

亲自看看:

https://www.st.com/resource/en/user_manual/dm00231744-stm32-nucleo32-boards-mb1180-stmicroelectronics.pdf(Nucleo-32)

https://www.st.com/resource/en/user_manual/dm00105823-stm32-nucleo-64-boards-mb1136-stmicroelectronics.pdf(Nucleo-64)

答案 2 :(得分:0)

仅需提及,ITM printf可以在Keil IDE上完美运行。设置没什么特别的,只需实现我的第一篇文章中所示的ITM_SendChar函数并打开debug printf窗口即可。