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) {}
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;
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
00264
00265 #if defined(__ICCARM__)
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
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
00314
00315
00316
00317
00318 size_t __write(int handle, const unsigned char * buffer, size_t size)
00319 {
00320
00321
00322 size_t nChars = 0;
00323
00324 if (buffer == 0)
00325 {
00326
00327
00328
00329
00330
00331 return 0;
00332 }
00333
00334
00335
00336 if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
00337 {
00338 return _LLIO_ERROR;
00339 }
00340
00341
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
00353 int nChars = 0;
00354
00355
00356
00357 if (handle != _LLIO_STDIN)
00358 {
00359 return _LLIO_ERROR;
00360 }
00361
00362 for (; 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
00378
00379 #if defined(__CROSSWORKS_ARM)
00380
00381
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
00393
00394 #if defined(__CC_ARM)
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 #include <stdio.h>
00406
00407
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
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;
00490 }
00491 #endif
00492