/***********************************************************************/ /* */ /* SYSCALLS.C: System Calls Remapping */ /* most of this is from newlib-lpc and a Keil-demo */ /* */ /* these are "reentrant functions" as needed by */ /* the WinARM-newlib-config, see newlib-manual */ /* collected and modified by Kongfanbin */ /* TODO: some more work has to be done on this */ /***********************************************************************/ #include "uart0.h" #include #include #include // new code for _read_r provided by Alexey Shusharin - Thanks _ssize_t _read_r(struct _reent *r, int file, void *ptr, size_t len) { return 0; } _ssize_t _write_r ( struct _reent *r, int file, const void *ptr, size_t len) { int i; const unsigned char *p; p = (const unsigned char*) ptr; for (i = 0; i < len; i++) { if (*p == '\n' ) my_write_char('\r'); // my_write_char is a routine to send a char to uart, you should implement it in your system in order to use printf. also, you can do nothing here for just compiling ok like other routines in this file. my_write_char(*p++); } return len; } int _close_r( struct _reent *r, int file) { return 0; } _off_t _lseek_r( struct _reent *r, int file, _off_t ptr, int dir) { return (_off_t)0; /* Always indicate we are at file beginning. */ } int _fstat_r( struct _reent *r, int file, struct stat *st) { /* Always set as character device. */ st->st_mode = S_IFCHR; /* assigned to strong type with implicit */ /* signed/unsigned conversion. Required by */ /* newlib. */ return 0; } extern "C" int isatty(int file); /* avoid warning */ int isatty(int file) { volatile int i = 0; return i; } #if 0 static void _exit (int n) { label: goto label; /* endless loop */ } #endif /* "malloc clue function" */ /**** Locally used variables. ****/ extern char end[]; /* end is set in the linker command */ /* file and is the end of statically */ /* allocated data (thus start of heap). */ static char *heap_ptr; /* Points to current end of the heap. */ /************************** _sbrk_r *************************************/ /* Support function. Adjusts end of heap to provide more memory to */ /* memory allocator. Simple and dumb with no sanity checks. */ /* struct _reent *r -- re-entrancy structure, used by newlib to */ /* support multiple threads of operation. */ /* ptrdiff_t nbytes -- number of bytes to add. */ /* Returns pointer to start of new heap area. */ /* Note: This implementation is not thread safe (despite taking a */ /* _reent structure as a parameter). */ /* Since _s_r is not used in the current implementation, the following */ /* messages must be suppressed. */ void * _sbrk_r( struct _reent *_s_r, ptrdiff_t nbytes) { char *base; /* errno should be set to ENOMEM on error */ char *stack_ptr; if (!heap_ptr) { /* Initialize if first time through. */ heap_ptr = end; } #ifdef _TOS_ // added by Kongfanbin /* __sdram_end__ is a symbol defined in linker script */ extern char __sdram_end__[]; stack_ptr = __sdram_end__; //LOG_PRINT("__sdram_end_ = 0x%08x.\n", __sdram_end__); #else /* the limit of heap is current stack address(in this case, I suppose * inner sram is being used for both heap and stack, stack grow from top * to bottom, and heap grows from bottom to top, so i can only handle like * this) */ volatile char tmp; stack_ptr = &tmp - 16; /* leave some excess */ #endif if (heap_ptr + nbytes > stack_ptr) { return NULL; } // added by Kongfanbin(end) base = heap_ptr; /* Point to end of heap. */ heap_ptr += nbytes; /* Increase heap. */ return base; /* Return pointer to start of new heap area. */ } void abort(void) { while(1); } void _exit(void) { while(1); }