반응형
Eclipse IDE 환경에서 printf() 함수 사용시에는 아래와 같이 hwconfig.c 에서 정의해야 한다.
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE // for printf use USART1
{
// Write a character to the USART
if( ch == '\n')
{
USART_SendData(USART1, '\r');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, '\n');
}
else
{
USART_SendData(USART1, (uint8_t) ch);
}
// Loop until the end of transmission
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
GETCHAR_PROTOTYPE // for printf use USART1
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){};
return USART_ReceiveData(USART1);
}
그리고 특정 source file에 printf() 함수를 추가하고 build를 진행하면 아래와 같이 compile error가 발생하는데, 이 부분은 gcc compile system의 standard I/O function call에 따른 이슈로 인하여 발생되는 문제이며, 해결 방안은 syscalls.c 파일을 프로젝트에 추가하여 함께 build 하면 된다. (참고로 IAR compiler 환경에서는 해당 printf() 함수 사용 할 경우 optimization linking 및 compile 되어 build image가 생성된다.)
# compile error case
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-exit.o): In function `exit':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/stdlib/../../../../../newlib/libc/stdlib/exit.c:70: undefined reference to `_exit'
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/sbrkr.c:58: undefined reference to `_sbrk'
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-writer.o): In function `_write_r':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/writer.c:58: undefined reference to `_write'
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-closer.o): In function `_close_r':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/closer.c:53: undefined reference to `_close'
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-fstatr.o): In function `_fstat_r':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/fstatr.c:62: undefined reference to `_fstat'
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-isattyr.o): In function `_isatty_r':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/isattyr.c:58: undefined reference to `_isatty'
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-lseekr.o): In function `_lseek_r':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/lseekr.c:58: undefined reference to `_lseek'
/usr/lib/gcc/arm-none-eabi/4.9.1/../../../../arm-none-eabi/lib/libc.a(lib_a-readr.o): In function `_read_r':
/build/arm-none-eabi-gcc/src/gcc-4.9.1/build/arm-none-eabi/newlib/libc/reent/../../../../../newlib/libc/reent/readr.c:58: undefined reference to `_read'
collect2: error: ld returned 1 exit status
# syscall.c 새로 정의
#include <_ansi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <errno.h>
#include <reent.h>
#include <unistd.h>
#include <sys/wait.h>
#define FreeRTOS
#define MAX_STACK_SIZE 0x2000
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
#ifndef FreeRTOS
register char * stack_ptr asm("sp");
#endif
caddr_t _sbrk(int incr)
{
extern char end asm("end");
static char *heap_end;
char *prev_heap_end,*min_stack_ptr;
if (heap_end == 0)
heap_end = &end;
prev_heap_end = heap_end;
#ifdef FreeRTOS
/* Use the NVIC offset register to locate the main stack pointer. */
min_stack_ptr = (char*)(*(unsigned int *)*(unsigned int *)0xE000ED08);
/* Locate the STACK bottom address */
min_stack_ptr -= MAX_STACK_SIZE;
if (heap_end + incr > min_stack_ptr)
#else
if (heap_end + incr > stack_ptr)
#endif
{
// write(1, "Heap and stack collision\n", 25);
// abort();
errno = ENOMEM;
return (caddr_t) -1;
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
/*
* _gettimeofday primitive (Stub function)
* */
int _gettimeofday (struct timeval * tp, struct timezone * tzp)
{
/* Return fixed data for the timezone. */
if (tzp)
{
tzp->tz_minuteswest = 0;
tzp->tz_dsttime = 0;
}
return 0;
}
void initialise_monitor_handles()
{
}
int _getpid(void)
{
return 1;
}
int _kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
void _exit (int status)
{
_kill(status, -1);
while (1) {}
}
int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar( *ptr++ );
}
return len;
}
int _close(int file)
{
return -1;
}
int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file)
{
return 1;
}
int _lseek(int file, int ptr, int dir)
{
return 0;
}
int _read(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}
int _open(char *path, int flags, ...)
{
/* Pretend like we always fail */
return -1;
}
int _wait(int *status)
{
errno = ECHILD;
return -1;
}
int _unlink(char *name)
{
errno = ENOENT;
return -1;
}
int _times(struct tms *buf)
{
return -1;
}
int _stat(char *file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _link(char *old, char *new)
{
errno = EMLINK;
return -1;
}
int _fork(void)
{
errno = EAGAIN;
return -1;
}
int _execve(char *name, char **argv, char **env)
{
errno = ENOMEM;
return -1;
}
결론적으로 상용 컴파일러를 사용하지 않고, Eclipse IDE와 같은 무료 프로그램으로 충분히 compile를 할 수 있다는 것에 고무적인 결과이다. 조만간 STM32F103x processor를 사용한 hardware platform 기반 Eclipse IDE 환경에서의 setup guide post를 남겨보도록 하겠다.
참고로 일반적으로 잘 알려진 디버깅용 printf() 함수 사용과 비슷한 구현을 아래와 같이 공유한다.
void zPrintf(USART_TypeDef* usart_p, char *fmt, ...)
{
va_list arg_ptr;
char LocalText[64];
uint8_t cnt;
for(cnt=0 ; cnt<64 ; cnt++)
{
LocalText[cnt] = 0x00;
}
va_start(arg_ptr, fmt);
vsprintf(LocalText, fmt, arg_ptr);
va_end(arg_ptr);
for(cnt=0 ; cnt<64 ; cnt++)
{
if(LocalText[cnt] != 0)
{
USART_SendData(usart_p, LocalText[cnt]);
while(USART_GetFlagStatus(usart_p, USART_FLAG_TXE) == RESET);
if(LocalText[cnt] == KEYBOARD_LineFeed)
{
USART_SendData(usart_p, KEYBOARD_CarryRet);
while(USART_GetFlagStatus(usart_p, USART_FLAG_TXE) == RESET);
}
}
else break;
}
}
반응형
'Engineer > 소프트웨어 정보' 카테고리의 다른 글
[STM32F10x] software I2C example code (2) | 2020.02.04 |
---|---|
STM32 ARM series 이클립스(Eclipse) 내 GCC 컴파일 개발 환경 구축 (0) | 2020.01.29 |
Magnetic Sensor Evaluation Demo Program [MFC Version] (0) | 2011.03.08 |
Characteristics of Great Programmers (0) | 2010.10.01 |
Method and device for calibrating a magnetic sensor (0) | 2010.08.19 |