retargetio.c

Go to the documentation of this file.
00001 /***************************************************************************/
00018 /***************************************************************************/
00027 extern int RETARGET_ReadChar(void);
00028 extern int RETARGET_WriteChar(char c);
00029 
00030 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
00031 
00032 #include <sys/stat.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include "em_device.h"
00037 
00039 int fileno(FILE *);
00042 int _close(int file);
00043 int _fstat(int file, struct stat *st);
00044 int _isatty(int file);
00045 int _lseek(int file, int ptr, int dir);
00046 int _read(int file, char *ptr, int len);
00047 caddr_t _sbrk(int incr);
00048 int _write(int file, const char *ptr, int len);
00049 
00050 extern char _end;                 
00052 /**************************************************************************/
00062 int _close(int file)
00063 {
00064   (void) file;
00065   return 0;
00066 }
00067 
00068 /**************************************************************************/
00073 void _exit (int status)
00074 {
00075   (void) status;
00076   while (1) {}      /* Hang here forever... */
00077 }
00078 
00079 /**************************************************************************/
00092 int _fstat(int file, struct stat *st)
00093 {
00094   (void) file;
00095   st->st_mode = S_IFCHR;
00096   return 0;
00097 }
00098 
00099 /**************************************************************************/
00102 int _getpid(void)
00103 {
00104   return 1;
00105 }
00106 
00107 /**************************************************************************/
00117 int _isatty(int file)
00118 {
00119   (void) file;
00120   return 1;
00121 }
00122 
00123 /**************************************************************************/
00128 int _kill(int pid, int sig)
00129 {
00130   (void)pid;
00131   (void)sig;
00132   return -1;
00133 }
00134 
00135 /**************************************************************************/
00151 int _lseek(int file, int ptr, int dir)
00152 {
00153   (void) file;
00154   (void) ptr;
00155   (void) dir;
00156   return 0;
00157 }
00158 
00159 /**************************************************************************/
00175 int _read(int file, char *ptr, int len)
00176 {
00177   int c, rxCount = 0;
00178 
00179   (void) file;
00180 
00181   while (len--)
00182   {
00183     if ((c = RETARGET_ReadChar()) != -1)
00184     {
00185       *ptr++ = c;
00186       rxCount++;
00187     }
00188     else
00189     {
00190       break;
00191     }
00192   }
00193 
00194   if (rxCount <= 0)
00195   {
00196     return -1;                        /* Error exit */
00197   }
00198 
00199   return rxCount;
00200 }
00201 
00202 /**************************************************************************/
00212 caddr_t _sbrk(int incr)
00213 {
00214   static char       *heap_end;
00215   char              *prev_heap_end;
00216   static const char heaperr[] = "Heap and stack collision\n";
00217 
00218   if (heap_end == 0)
00219   {
00220     heap_end = &_end;
00221   }
00222 
00223   prev_heap_end = heap_end;
00224   if ((heap_end + incr) > (char*) __get_MSP())
00225   {
00226     _write(fileno(stdout), heaperr, strlen(heaperr));
00227     exit(1);
00228   }
00229   heap_end += incr;
00230 
00231   return (caddr_t) prev_heap_end;
00232 }
00233 
00234 /**************************************************************************/
00250 int _write(int file, const char *ptr, int len)
00251 {
00252   int txCount;
00253 
00254   (void) file;
00255 
00256   for (txCount = 0; txCount < len; txCount++)
00257   {
00258     RETARGET_WriteChar(*ptr++);
00259   }
00260 
00261   return len;
00262 }
00263 #endif /* !defined( __CROSSWORKS_ARM ) && defined( __GNUC__ ) */
00264 
00265 #if defined(__ICCARM__)
00266 /*******************
00267  *
00268  * Copyright 1998-2003 IAR Systems.  All rights reserved.
00269  *
00270  * $Revision: 38614 $
00271  *
00272  * This is a template implementation of the "__write" function used by
00273  * the standard library.  Replace it with a system-specific
00274  * implementation.
00275  *
00276  * The "__write" function should output "size" number of bytes from
00277  * "buffer" in some application-specific way.  It should return the
00278  * number of characters written, or _LLIO_ERROR on failure.
00279  *
00280  * If "buffer" is zero then __write should perform flushing of
00281  * internal buffers, if any.  In this case "handle" can be -1 to
00282  * indicate that all handles should be flushed.
00283  *
00284  * The template implementation below assumes that the application
00285  * provides the function "MyLowLevelPutchar".  It should return the
00286  * character written, or -1 on failure.
00287  *
00288  ********************/
00289 
00290 #include <yfuns.h>
00291 #include <stdint.h>
00292 
00293 _STD_BEGIN
00294 
00295 /**************************************************************************/
00301 static int TxBuf(uint8_t *buffer, int nbytes)
00302 {
00303   int i;
00304 
00305   for (i = 0; i < nbytes; i++)
00306   {
00307     RETARGET_WriteChar(*buffer++);
00308   }
00309   return nbytes;
00310 }
00311 
00312 /*
00313  * If the __write implementation uses internal buffering, uncomment
00314  * the following line to ensure that we are called with "buffer" as 0
00315  * (i.e. flush) when the application terminates.
00316  */
00317 
00318 size_t __write(int handle, const unsigned char * buffer, size_t size)
00319 {
00320   /* Remove the #if #endif pair to enable the implementation */
00321 
00322   size_t nChars = 0;
00323 
00324   if (buffer == 0)
00325   {
00326     /*
00327      * This means that we should flush internal buffers.  Since we
00328      * don't we just return.  (Remember, "handle" == -1 means that all
00329      * handles should be flushed.)
00330      */
00331     return 0;
00332   }
00333 
00334   /* This template only writes to "standard out" and "standard err",
00335    * for all other file handles it returns failure. */
00336   if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
00337   {
00338     return _LLIO_ERROR;
00339   }
00340 
00341   /* Hook into USART1 transmit function here */
00342   if (TxBuf((uint8_t *) buffer, size) != size)
00343     return _LLIO_ERROR;
00344   else
00345     nChars = size;
00346 
00347   return nChars;
00348 }
00349 
00350 size_t __read(int handle, unsigned char * buffer, size_t size)
00351 {
00352   /* Remove the #if #endif pair to enable the implementation */
00353   int nChars = 0;
00354 
00355   /* This template only reads from "standard in", for all other file
00356    * handles it returns failure. */
00357   if (handle != _LLIO_STDIN)
00358   {
00359     return _LLIO_ERROR;
00360   }
00361 
00362   for (/* Empty */; size > 0; --size)
00363   {
00364     int c = RETARGET_ReadChar();
00365     if (c < 0)
00366       break;
00367 
00368     *buffer++ = c;
00369     ++nChars;
00370   }
00371 
00372   return nChars;
00373 }
00374 
00375 _STD_END
00376 
00377 #endif /* defined( __ICCARM__ ) */
00378 
00379 #if defined(__CROSSWORKS_ARM)
00380 
00381 /* Pass each of these function straight to the USART */
00382 int __putchar(int ch)
00383 {
00384   return(RETARGET_WriteChar(ch));
00385 }
00386 
00387 int __getchar(void)
00388 {
00389   return(RETARGET_ReadChar());
00390 }
00391 
00392 #endif /* defined( __CROSSWORKS_ARM ) */
00393 
00394 #if defined(__CC_ARM)
00395 /******************************************************************************/
00396 /* RETARGET.C: 'Retarget' layer for target-dependent low level functions      */
00397 /******************************************************************************/
00398 /* This file is part of the uVision/ARM development tools.                    */
00399 /* Copyright (c) 2005-2006 Keil Software. All rights reserved.                */
00400 /* This software may only be used under the terms of a valid, current,        */
00401 /* end user licence from KEIL for a compatible version of KEIL software       */
00402 /* development tools. Nothing else gives you the right to use this software.  */
00403 /******************************************************************************/
00404 
00405 #include <stdio.h>
00406 
00407 /* #pragma import(__use_no_semihosting_swi) */
00408 
00409 struct __FILE
00410 {
00411   int handle;
00412 };
00413 
00415 FILE __stdout;
00416 
00417 /**************************************************************************/
00430 int fputc(int ch, FILE *f)
00431 {
00432   return(RETARGET_WriteChar(ch));
00433 }
00434 
00435 /**************************************************************************/
00445 int fgetc(FILE *f)
00446 {
00447   return(RETARGET_ReadChar());
00448 }
00449 
00450 /**************************************************************************/
00461 int ferror(FILE *f)
00462 {
00463   /* Your implementation of ferror */
00464   return EOF;
00465 }
00466 
00467 /**************************************************************************/
00474 void _ttywrch(int ch)
00475 {
00476   RETARGET_WriteChar(ch);
00477 }
00478 
00479 /**************************************************************************/
00487 void _sys_exit(int return_code)
00488 {
00489  label:  goto label; /* endless loop */
00490 }
00491 #endif /* defined( __CC_ARM ) */
00492