retargetio.c

Go to the documentation of this file.
00001 /***************************************************************************/
00036 /***************************************************************************/
00045 extern int RETARGET_ReadChar(void);
00046 extern int RETARGET_WriteChar(char c);
00047 
00048 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
00049 
00050 #include <sys/stat.h>
00051 #include <stdio.h>
00052 #include <stdlib.h>
00053 #include <string.h>
00054 #include "em_device.h"
00055 
00057 int fileno(FILE *);
00060 int _close(int file);
00061 int _fstat(int file, struct stat *st);
00062 int _isatty(int file);
00063 int _lseek(int file, int ptr, int dir);
00064 int _read(int file, char *ptr, int len);
00065 caddr_t _sbrk(int incr);
00066 int _write(int file, const char *ptr, int len);
00067 
00068 extern char _end;                 
00070 /**************************************************************************/
00080 int _close(int file)
00081 {
00082   (void) file;
00083   return 0;
00084 }
00085 
00086 /**************************************************************************/
00091 void _exit (int status)
00092 {
00093   (void) status;
00094   while (1) {}      /* Hang here forever... */
00095 }
00096 
00097 /**************************************************************************/
00110 int _fstat(int file, struct stat *st)
00111 {
00112   (void) file;
00113   st->st_mode = S_IFCHR;
00114   return 0;
00115 }
00116 
00117 /**************************************************************************/
00120 int _getpid(void)
00121 {
00122   return 1;
00123 }
00124 
00125 /**************************************************************************/
00135 int _isatty(int file)
00136 {
00137   (void) file;
00138   return 1;
00139 }
00140 
00141 /**************************************************************************/
00146 int _kill(int pid, int sig)
00147 {
00148   (void)pid;
00149   (void)sig;
00150   return -1;
00151 }
00152 
00153 /**************************************************************************/
00169 int _lseek(int file, int ptr, int dir)
00170 {
00171   (void) file;
00172   (void) ptr;
00173   (void) dir;
00174   return 0;
00175 }
00176 
00177 /**************************************************************************/
00193 int _read(int file, char *ptr, int len)
00194 {
00195   int c, rxCount = 0;
00196 
00197   (void) file;
00198 
00199   while (len--)
00200   {
00201     if ((c = RETARGET_ReadChar()) != -1)
00202     {
00203       *ptr++ = c;
00204       rxCount++;
00205     }
00206     else
00207     {
00208       break;
00209     }
00210   }
00211 
00212   if (rxCount <= 0)
00213   {
00214     return -1;                        /* Error exit */
00215   }
00216 
00217   return rxCount;
00218 }
00219 
00220 /**************************************************************************/
00230 caddr_t _sbrk(int incr)
00231 {
00232   static char       *heap_end;
00233   char              *prev_heap_end;
00234   static const char heaperr[] = "Heap and stack collision\n";
00235 
00236   if (heap_end == 0)
00237   {
00238     heap_end = &_end;
00239   }
00240 
00241   prev_heap_end = heap_end;
00242   if ((heap_end + incr) > (char*) __get_MSP())
00243   {
00244     _write(fileno(stdout), heaperr, strlen(heaperr));
00245     exit(1);
00246   }
00247   heap_end += incr;
00248 
00249   return (caddr_t) prev_heap_end;
00250 }
00251 
00252 /**************************************************************************/
00268 int _write(int file, const char *ptr, int len)
00269 {
00270   int txCount;
00271 
00272   (void) file;
00273 
00274   for (txCount = 0; txCount < len; txCount++)
00275   {
00276     RETARGET_WriteChar(*ptr++);
00277   }
00278 
00279   return len;
00280 }
00281 #endif /* !defined( __CROSSWORKS_ARM ) && defined( __GNUC__ ) */
00282 
00283 #if defined(__ICCARM__)
00284 /*******************
00285  *
00286  * Copyright 1998-2003 IAR Systems.  All rights reserved.
00287  *
00288  * $Revision: 38614 $
00289  *
00290  * This is a template implementation of the "__write" function used by
00291  * the standard library.  Replace it with a system-specific
00292  * implementation.
00293  *
00294  * The "__write" function should output "size" number of bytes from
00295  * "buffer" in some application-specific way.  It should return the
00296  * number of characters written, or _LLIO_ERROR on failure.
00297  *
00298  * If "buffer" is zero then __write should perform flushing of
00299  * internal buffers, if any.  In this case "handle" can be -1 to
00300  * indicate that all handles should be flushed.
00301  *
00302  * The template implementation below assumes that the application
00303  * provides the function "MyLowLevelPutchar".  It should return the
00304  * character written, or -1 on failure.
00305  *
00306  ********************/
00307 
00308 #include <yfuns.h>
00309 #include <stdint.h>
00310 
00311 _STD_BEGIN
00312 
00313 /**************************************************************************/
00319 static int TxBuf(uint8_t *buffer, int nbytes)
00320 {
00321   int i;
00322 
00323   for (i = 0; i < nbytes; i++)
00324   {
00325     RETARGET_WriteChar(*buffer++);
00326   }
00327   return nbytes;
00328 }
00329 
00330 /*
00331  * If the __write implementation uses internal buffering, uncomment
00332  * the following line to ensure that we are called with "buffer" as 0
00333  * (i.e. flush) when the application terminates.
00334  */
00335 
00336 size_t __write(int handle, const unsigned char * buffer, size_t size)
00337 {
00338   /* Remove the #if #endif pair to enable the implementation */
00339 
00340   size_t nChars = 0;
00341 
00342   if (buffer == 0)
00343   {
00344     /*
00345      * This means that we should flush internal buffers.  Since we
00346      * don't we just return.  (Remember, "handle" == -1 means that all
00347      * handles should be flushed.)
00348      */
00349     return 0;
00350   }
00351 
00352   /* This template only writes to "standard out" and "standard err",
00353    * for all other file handles it returns failure. */
00354   if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
00355   {
00356     return _LLIO_ERROR;
00357   }
00358 
00359   /* Hook into USART1 transmit function here */
00360   if (TxBuf((uint8_t *) buffer, size) != size)
00361     return _LLIO_ERROR;
00362   else
00363     nChars = size;
00364 
00365   return nChars;
00366 }
00367 
00368 size_t __read(int handle, unsigned char * buffer, size_t size)
00369 {
00370   /* Remove the #if #endif pair to enable the implementation */
00371   int nChars = 0;
00372 
00373   /* This template only reads from "standard in", for all other file
00374    * handles it returns failure. */
00375   if (handle != _LLIO_STDIN)
00376   {
00377     return _LLIO_ERROR;
00378   }
00379 
00380   for (/* Empty */; size > 0; --size)
00381   {
00382     int c = RETARGET_ReadChar();
00383     if (c < 0)
00384       break;
00385 
00386     *buffer++ = c;
00387     ++nChars;
00388   }
00389 
00390   return nChars;
00391 }
00392 
00393 _STD_END
00394 
00395 #endif /* defined( __ICCARM__ ) */
00396 
00397 #if defined(__CROSSWORKS_ARM)
00398 
00399 /* Pass each of these function straight to the USART */
00400 int __putchar(int ch)
00401 {
00402   return(RETARGET_WriteChar(ch));
00403 }
00404 
00405 int __getchar(void)
00406 {
00407   return(RETARGET_ReadChar());
00408 }
00409 
00410 #endif /* defined( __CROSSWORKS_ARM ) */
00411 
00412 #if defined(__CC_ARM)
00413 /******************************************************************************/
00414 /* RETARGET.C: 'Retarget' layer for target-dependent low level functions      */
00415 /******************************************************************************/
00416 /* This file is part of the uVision/ARM development tools.                    */
00417 /* Copyright (c) 2005-2006 Keil Software. All rights reserved.                */
00418 /* This software may only be used under the terms of a valid, current,        */
00419 /* end user licence from KEIL for a compatible version of KEIL software       */
00420 /* development tools. Nothing else gives you the right to use this software.  */
00421 /******************************************************************************/
00422 
00423 #include <stdio.h>
00424 
00425 /* #pragma import(__use_no_semihosting_swi) */
00426 
00427 struct __FILE
00428 {
00429   int handle;
00430 };
00431 
00433 FILE __stdout;
00434 
00435 /**************************************************************************/
00448 int fputc(int ch, FILE *f)
00449 {
00450   return(RETARGET_WriteChar(ch));
00451 }
00452 
00453 /**************************************************************************/
00463 int fgetc(FILE *f)
00464 {
00465   return(RETARGET_ReadChar());
00466 }
00467 
00468 /**************************************************************************/
00479 int ferror(FILE *f)
00480 {
00481   /* Your implementation of ferror */
00482   return EOF;
00483 }
00484 
00485 /**************************************************************************/
00492 void _ttywrch(int ch)
00493 {
00494   RETARGET_WriteChar(ch);
00495 }
00496 
00497 /**************************************************************************/
00505 void _sys_exit(int return_code)
00506 {
00507  label:  goto label; /* endless loop */
00508 }
00509 #endif /* defined( __CC_ARM ) */
00510