📄 retargetx.c
字号:
/*
This implements a 'retarget' layer for low-level IO. Typically, this would
contain your own target-dependent implementations of fputc(), ferror(), etc.
This example provides implementations of fputc(), ferror(), _sys_exit(),
_ttywrch() and __user_initial_stackheap().
Here, semihosting SWIs are used to display text onto the console of the host debugger.
This mechanism is portable across ARMulator, Angel, Multi-ICE and EmbeddedICE.
Alternatively, to output characters from the serial port of an ARM Development (PID) Board
(see serial.c), use '#define USE_SERIAL_PORT' or compile with '-DUSE_SERIAL_PORT'.
*/
#include <stdio.h>
#include <rt_misc.h>
#include "XsUartDrv.h"
#include "SitsangBrdDrv.h"
#include "UtilFunc.h"
#pragma import(__use_no_semihosting_swi)
/* #define USE_SERIAL_PORT */
#ifdef __thumb
/* Thumb Semihosting SWI */
#define SemiSWI 0xAB
#else
/* ARM Semihosting SWI */
#define SemiSWI 0x123456
#endif
/* Write a character */
__swi(SemiSWI) void _WriteC(unsigned op, char *c);
#define WriteC(c) _WriteC (0x3,c)
/* Exit */
__swi(SemiSWI) void _Exit(unsigned op, unsigned except);
//#define Exit() _Exit (0x18,0x20026)
#define Exit() //call nothing
struct __FILE { int handle; /* Add whatever you need here */};
FILE __stdout;
FILE __stdin; //when reimplement fgetc, __stdin must be redefined also
FILE __stderr;
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to a UART, or to the debugger console with SWI WriteC */
char tempch = ch;
//sendchar( &tempch );
//XsUartDrv_SendCharFF(&tempch); //ok
XsUartDrv_SendCharDef(&tempch);
return ch;
}
int fgetc(FILE *f)
{
char tempch;
int ret;
//ret = XsUartDrv_ReadUartFF(&tempch, 1);
ret = XsUartDrv_ReadUartDef(&tempch, 1, -1);
//when read a char, echo back to host
//XsUartDrv_SendCharDef(&tempch);
if(tempch == '\r')
{
XsUartDrv_SendStrZDef("\\r");
}
else if(tempch == '\n')
{
XsUartDrv_SendStrZDef("\\n");
}
else
XsUartDrv_SendCharDef(&tempch);
//return EOF;
return (int)tempch;
}
int ferror(FILE *f)
{ /* Your implementation of ferror */
return EOF;
}
void _sys_exit(int return_code)
{
Exit(); /* for debugging */
label: goto label; /* endless loop */
}
void _ttywrch(int ch)
{
char tempch = ch;
//sendchar( &tempch );
//XsUartDrv_SendCharFF(&tempch);
XsUartDrv_SendCharDef(&tempch);
}
/*
* This can be defined to override the standard memory models' way
* of determining where to put the initial stack and heap.
*
* The input parameters R0 and R2 contain nothing useful. The input
* parameters SP and SL are the values that were in SP and SL when
* the program began execution (so you can return them if you want
* to keep that stack).
*
* The two `limit' fields in the return structure are ignored if
* you are using the one-region memory model: the memory region is
* taken to be all the space between heap_base and stack_base.
*/
__value_in_regs struct __initial_stackheap __user_initial_stackheap(
unsigned R0, unsigned SP, unsigned R2, unsigned SL)
{
struct __initial_stackheap config;
config.heap_base = SP - 1024; //put heap base in the bottowm of STACK
config.stack_base = SP;
/*
To place heap_base directly above the ZI area, use e.g:
extern unsigned int Image$$ZI$$Limit;
config.heap_base = (unsigned int)&Image$$ZI$$Limit;
(or &Image$$region_name$$ZI$$Limit for scatterloaded images)
To specify the limits for the heap & stack, use e.g:
config.heap_limit = SL;
config.stack_limit = SL;
*/
return config;
}
void __rt_raise(int sig, int type)
{
LED_H_ALL_ON();
}
/*
* extra reimplement functions
*/
char * gets(char *buf)
{
register int c;
register char *s;
//for (s = buf; (c = getchar()) != '\n';)
//for (s = buf; (c = fgetc(&__stdin)) != '\n';)
for (s = buf; ((c = fgetc(&__stdin)) != '\n') && (c != '\r');)
{
if (c == EOF)
{
if (s == buf) {
return (NULL);
}
else {
break;
}
}
else
{
*s++ = c;
}
}
*s = 0;
return (buf);
}
//used by getsConsole, don't echo char to console
int GetcConsole()
{
char tempch;
int ret;
ret = XsUartDrv_ReadUartDef(&tempch, 1, -1);
//return EOF;
return (int)tempch;
}
char * GetsConsole(char *buf)
{
register int c;
register char *s;
char tempc;
//for (s = buf; (c = getchar()) != '\n';)
//for (s = buf; (c = fgetc(&__stdin)) != '\n';)
//for (s = buf; ((c = fgetc(&__stdin)) != '\n') && (c != '\r');)
for (s = buf; ((c = GetcConsole()) != '\n') && (c != '\r');)
{
if (c == EOF)
{
if (s == buf) {
return (NULL);
}
else {
break;
}
}
else if(c == 0x8) //back space
{
if(s > buf)
{
tempc = c;
XsUartDrv_SendCharDef(&tempc);
XsUartDrv_SendCharDef(" ");
XsUartDrv_SendCharDef(&tempc);
s--;
}
else
{
tempc = '\a';
XsUartDrv_SendCharDef(&tempc);;
}
}
else
{
//echo back
tempc = c;
XsUartDrv_SendCharDef(&tempc);
*s++ = c;
}
//PrintfUartFF("buf=%x,s=%x\r\n", buf, s);
}
*s = 0;
return (buf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -