STM32F4单片机串口线调试无法正常工作

我正在使用STM32F4发现板–
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/data_brief/DM00037955.pdf

我正尝试使用ST Micro STLink软件中的串行线查看器通过“printf”类似的语句进行调试:http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/CD00262073.pdf

但是,尽管将系统时钟设置为168000000 Hz并将激励端口设置为“全部”,但我在SWO Viewer中看不到任何结果.我在芯片上运行的(相关)软件如下.此演示设置为基于按下用户按钮更改LED灯.

static uint8_t lastButtonStatus = RESET;

int main() {
    init();

    do {
        loop();
    } while (1);
}

void init() {
    initLeds();
    initButton();
}

void loop() {
    static uint32_t counter = 0;

    uint8_t currentButtonStatus = GPIO_ReadInputDataBit(GPIOA, USER_BUTTON);

    if (lastButtonStatus != currentButtonStatus
            && currentButtonStatus != RESET) {
        ++counter;
        GPIO_ResetBits(GPIOD, LEDS);
        GPIO_SetBits(GPIOD, LED[counter % 4]);
        // Test SWD output
        SWV_puts("hello from stm32f4\n");
        SWV_printfloat(1.98254, 2);
    }
    lastButtonStatus = currentButtonStatus;
}

以下是SWV_打印功能:

void SWV_puts(const char *s )
{
    while (*s) ITM_SendChar(*s++);

}

/**
  * @brief   This function sends numbers to the serial wire viewer.
  * @param  number: number to be displayed on SWV
  * @retval None
  */
void SWV_printnum(long number)
{
 unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
 unsigned int i = 0;

 //if number is 0
 if (number == 0)
 {
  ITM_SendChar('0'); //if number is zero
     return;
 }
 //account for negative numbers
 if (number < 0)
 {
  ITM_SendChar('-');
  number = number * -1;
 }
 while(number > 0)
 {
  buf[i++] = number % 10; //display in base 10
  number = number / 10;
  //NOTE: the effect of i++ means that the i variable will be at number of digits + 1
 }
 for(; i > 0; i--)
 {
  ITM_SendChar((char)('0' + buf[i-1]));
 }
}

/**
  * @brief  This function sends numbers to the serial wire viewer.
  * @param  number: number to be displayed on SWV
  * @param  digits: number of digits after decimal point
  * @retval None
  */
void SWV_printfloat(double number, int digits)
{
 int i = 0;
 //handle negative numbers
 if(number < 0.0)
 {
  ITM_SendChar('-');
  number = -number;
 }
 //round correctly so that uart_printfloat(1.999, 2) shows as "2.00"
 double rounding = 0.5;
 for(i = 0; i < digits; ++i) rounding = rounding / 10.0;
 number = number + rounding;

 //extract the integer part of the number and print it
 unsigned long int_part = (unsigned long) number;
 double remainder = (double)(number - (double)int_part);
 SWV_printnum(int_part); //print the integer part
 if(digits > 0) ITM_SendChar('.'); //print decimal pint
 int toprint;
 while(digits-- > 0)
 {
  remainder = remainder * 10.0;
  toprint = (int)remainder;
  SWV_printnum(toprint);
  remainder = remainder - toprint;
 }

}

我可以确认代码编译没有错误或警告谢谢你帮助我的SWD打印,所以我可以调试真正的程序=)

最佳答案 我已经在我的STMF4发现上尝试了你的代码(没有别的),它工作正常.

我认为你必须更新任何东西到最后版本:

STM32 ST/LINK utility v3.4.0
STLinkUSBDriver.dll v4.3.3.0
ST-LINK_CLI.exe v2.0.0
STM32F4 discovery board ST-LINK Firmware version : V2J21S0
STLink dongle 1.1.0.0 (10/12/2013)

祝好运

点赞