STM32CubeIDE配置ITM调试

2021-04-19  本文已影响0人  望江樵夫

硬件需求

J-Link或者ST-Link,连接到了单片机的SWO管脚

Debug Configurations

image.png

打开调试窗口

进入到Debug页面后,点击 Windows-->show View --->SWV--->SWV ITM data console

配置Debug Trace

image.png image.png

启动调试

image.png

开始调试

image.png
在SWV ITM data console窗口查看printf打印的内容

增加接口文件

helper_functions.h

/***************************Copyright (c)********************************
** Copyright (c) 2021 Xingzhe Robot. All Rights Reserved.
**-----------------------------------------------------------------------
** Created by:          mhming
** Created date:        2021年3月20日
** Version:             v1.0
** Descriptions:        helper_functions.h
************************************************************************/

#ifndef SRC_HELPER_FUNCTIONS_H_
#define SRC_HELPER_FUNCTIONS_H_

#include "stm32l4xx_hal.h"
#include <stdio.h>

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(void)
/* With GCC, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int getc(FILE *f)
#endif /* __GNUC__ */

/**
 * Hacks
 */
#define SCANF_INIT_BUF() setvbuf(stdin, NULL, _IONBF, 0)    //set input  buffer to 0
#define PRINTF_INIT_BUF() setvbuf(stdout, NULL, _IONBF, 0)  //set output buffer to 0

/**
 * Cycle counter macros.
 * Can be used to count the number of cycles that a function takes to run
 */
//internal cycle counter functions
__STATIC_INLINE void __DWT_ResetTimer(void){
    //disable counter
    DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk;
    // reset cycle counter
    DWT->CYCCNT = 0;
    //enable trace and debug blocks (DWT, ITM, ETM, TPIU)
    CoreDebug->DEMCR |=  CoreDebug_DEMCR_TRCENA_Msk;
}
#define __DWT_START_TIMER() DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk
#define __DWT_STOP_TIMER()  DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk

// Returns the current number of cycles according to the internal cycle counter
#define __DWT_GET_CYCLES()  (uint32_t) DWT->CYCCNT


#endif /* SRC_HELPER_FUNCTIONS_H_ */

helper_functions.c

/***************************Copyright (c)********************************
** Copyright (c) 2021 Xingzhe Robot. All Rights Reserved.
**-----------------------------------------------------------------------
** Created by:          mhming
** Created date:        2021年3月20日
** Version:             v1.0
** Descriptions:        helper_functions.c
************************************************************************/

#include "helper_functions.h"
 /**
  * external variables and defines
  */

#define UART_HANDLE huart1 //Copy your handle name here
extern UART_HandleTypeDef UART_HANDLE;


//extern UART_HandleTypeDef UART_HANDLE;
volatile int32_t ITM_RxBuffer=ITM_RXBUFFER_EMPTY;

//Uncomment next line to use IT
#define USE_ITM
/**
 * Transmission and reception macros
 */

#ifndef USE_ITM
    static void transmit_char(char ch){
        HAL_UART_Transmit(&UART_HANDLE, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
    }
    static char receive_char(){
        char ch;
        HAL_UART_Receive(&UART_HANDLE, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
        return ch;
    }
#else
    static void transmit_char(char ch){
        ITM_SendChar((uint32_t)ch);
    }
    static char receive_char(){
        char ch = 0;
        ch = (char)ITM_ReceiveChar();
        return ch;
    }
#endif
/**
 * @brief PUTCHAR_PROTOTYPE function, called from printf
 * @param ch    : Char to be written to console
 * @return
 */
PUTCHAR_PROTOTYPE{

    transmit_char((char) ch);

    return 0;
}
/**
 * @brief GETCHAR_PROTOTYPE function, called from scanf
 * @return read out character
 */
GETCHAR_PROTOTYPE{
    char ch;

    ch = receive_char();
    transmit_char(ch);

    return (int)ch;
}
上一篇 下一篇

猜你喜欢

热点阅读