Home arrow Support arrow Forums

Luminary Micro Forums

digkleppe

Expert Boarder
Click here to see the profile of this user

2008/06/28 12:27

printf again

Hi
I nearly succeeded in getting printf to work. I supplied the missing routines.
Here I just simply write to a string.
BUT: printf gets called every loop , but only calls
write when it has produced 1024 chars!
grrr , where do I change that?

I am working with code red and use the newlib

by the way 1 :
I get the errorcode
BFD: printf.axf: warning: sh_link not set for section `.ARM.exidx' but this seems to be a bug in GCC

by the way 2:
sprintf works OK!

Code:

  #include <stdio.h> #include <sys/stat.h> char str[100]; char *cp str; void writechar(char ch); int main(void) {     int x 1;     while (1) {         cp str;         printf(" abc %d "x);         writechar(0);         x+= 1;     } } int _close(int file) {     return -1; } int _exit(int x) {     while (1)         ; } int _fstat(int filestruct stat *st) {     st->st_mode S_IFCHR;     return 0; } //lseek //   Set position in a file. Minimal implementation: int _lseek(int fileint ptrint dir) {     return 0; } int isatty(int file) {     return 1; } //read //    Read from a file. Minimal implementation: int _read(int filechar *ptrint len) {     return 0; } //sbrk //   Increase program data space. As malloc and related functions depend on this, it is useful to  have a working implementation. The following suffices for a standalone system; it exploits the  symbol end automatically defined by the GNU linker. caddr_t _sbrk(int incr) {     extern char end/* Defined by the linker */     static char *heap_end;     char *prev_heap_end;     if (heap_end == 0) {         heap_end = &end;     }     prev_heap_end heap_end;     heap_end += incr;     return (caddr_tprev_heap_end; } //getpid //    Process-ID; this is sometimes used to generate strings unlikely to conflict with other  processes. Minimal implementation, for a system without processes: int _getpid() {     return 1; } //kill //    Send a signal. Minimal implementation: #include <errno.h> #undef errno extern int errno; int _kill(int pidint sig) {     errno=EINVAL;     return (-1); } //  write //  Write a character to a file.  void writechar(char ch) {     *cp++ = ch;     if (cp == (str 100))         cp str; } int _write(int filechar *ptrint len) {     int todo;     for (todo 0todo lentodo++) {         writechar(*ptr++);     }     return len; }



Post edited by: digkleppe, at: 2008/06/28 12:33

Post edited by: digkleppe, at: 2008/06/28 12:37

login or register to reply

cb1

Platinum Boarder
Click here to see the profile of this user

2008/06/29 08:38

Re:printf again

Interesting - should not you call write() rather than writechar()? (as write() calls writechar() - and the reverse is not true)

Otherwise - temporarily remove the "if" w/in writechar() - and see if/how that effects the frequency of writes. Let us know how you make out.

login or register to reply

digkleppe

Expert Boarder
Click here to see the profile of this user

2008/06/29 14:39

Re:printf again

printf calls write calls writechar. I only call printf.
I think printf "thinks" it is writing to a disk or something, it calls write after buffering 1024 chars.

found work around:
call fflush(0) after printf
note:
using printf gives you 31 kb of code.

Dig

Post edited by: digkleppe, at: 2008/07/02 12:50

login or register to reply