📄 retarget.c
字号:
/*
** Copyright (C) ARM Limited, 2004. All rights reserved.
*/
/*
** This file contains re-implementations of functions whose
** C library implementations rely on semihosting.
** I/O is targeted to the Versatile serial port A.
*/
#include "system.h"
#if !SEMIHOSTING
/*
** Importing __use_no_semihosting_swi ensures that our image doesn't link
** with any C Library code that makes direct use of semihosting.
**
*/
#pragma import(__use_no_semihosting_swi)
/*
** Retargeted I/O
** ==============
** The following C library functions make use of semihosting
** to read or write characters to the debugger console: fputc(),
** fgetc(), and _ttywrch(). They must be retargeted to write to
** the Versatile AP UART. __backspace() must also be retargeted
** with this layer to enable scanf(). See the Compiler and
** Libraries Guide.
*/
/*
** These must be defined to avoid linking in stdio.o from the
** C Library
*/
struct __FILE { int handle; /* Add whatever you need here */};
FILE __stdout;
FILE __stdin;
/*
** __backspace must return the last char read to the stream
** fgetc() needs to keep a record of whether __backspace was
** called directly before it
*/
int last_char_read;
int backspace_called;
int fputc(int ch, FILE *f)
{
Uart_putc(ch);
return ch;
}
int fgetc(FILE *f)
{
unsigned char tempch;
/* if we just backspaced, then return the backspaced character */
/* otherwise output the next character in the stream */
if (backspace_called == TRUE)
{
backspace_called = FALSE;
return last_char_read;
}
tempch = Uart_getc();
Uart_putc(tempch);
switch(tempch){
case 0xd : tempch=0xa;
break;
case '\b' : Uart_putc(' ');
Uart_putc('\b');
break;
}
last_char_read = (int)tempch; /* backspace must return this value */
return tempch;
}
void _ttywrch(int ch)
{
Uart_putc(ch);
}
/*
** The effect of __backspace() should be to return the last character
** read from the stream, such that a subsequent fgetc() will
** return the same character again.
*/
int __backspace(FILE *f)
{
backspace_called = TRUE;
return 1;
}
/* END of Retargeted I/O */
/*
** Exception Signaling and Handling
** ================================
** The C library implementations of ferror() uses semihosting directly
** and must therefore be retargeted. This is a minimal reimplementation.
** _sys_exit() is called after the user's main() function has exited. The C library
** implementation uses semihosting to report to the debugger that the application has
** finished executing.
*/
int ferror(FILE *f)
{
return EOF;
}
void _sys_exit(int return_code)
{
while(1);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -