STM32 printf 사용

DongHee Lim·2022년 4월 10일
0

STM32 함수

목록 보기
1/2

서론

기존에 사용하는 CubeIDE

새롭게 사용해보는 Keil MDK-ARM

생각보다 성격이 많이 달랐다.

정확히 어디가 다른지는 모르지만

그 중 printf 사용이 달라서 삽질을 많이 했다

기본적으로 printf를 사용하려면 standard Input Output 헤더를 추가해야한다.

#include <stdio.h>

CubeIDE printf

HAL Driver

#ifdef __cplusplus
extern "C" int _write(int32_t file, uint8_t *ptr, int32_t len) {
#else
int _write(int32_t file, uint8_t *ptr, int32_t len) {
#endif
    if( HAL_UART_Transmit(&huart3, ptr, len, 10) == HAL_OK ) return len;
    else return 0;
}

LL Driver

int _write(uint32_t file, uint8_t* ptr, uint32_t len )
{
	for(int i = 0; i < len; i++)
	{
		LL_USART_TransmitData8(USART3, *(ptr+i));
		HAL_Delay(1);
	}
	return len;
}

Keil MDK-ARM printf

/* We need to implement own __FILE struct */
/* FILE struct is used from __FILE */
struct __FILE {
    int dummy;
};
 
/* You need this if you want use printf */
/* Struct FILE is implemented in stdio.h */
FILE __stdout;


int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF); 

  return ch;
}

추가

keil MDK-ARM 내부의 Serial window 로 printf 를 확인하는 방법이 있다고 들었다.
ITM 을 추가하는 것으로 보이는데 내가 해보니 잘 안되어서 기존의 방법을 사용했다.

[Debug 방법 1 블로그]
[위 방법 공식 사이트]

profile
하고 싶은 것, 소유하고 싶은 것, 좋아하는 것

0개의 댓글