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) {}
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;
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
00282
00283 #if defined(__ICCARM__)
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
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
00332
00333
00334
00335
00336 size_t __write(int handle, const unsigned char * buffer, size_t size)
00337 {
00338
00339
00340 size_t nChars = 0;
00341
00342 if (buffer == 0)
00343 {
00344
00345
00346
00347
00348
00349 return 0;
00350 }
00351
00352
00353
00354 if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
00355 {
00356 return _LLIO_ERROR;
00357 }
00358
00359
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
00371 int nChars = 0;
00372
00373
00374
00375 if (handle != _LLIO_STDIN)
00376 {
00377 return _LLIO_ERROR;
00378 }
00379
00380 for (; 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
00396
00397 #if defined(__CROSSWORKS_ARM)
00398
00399
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
00411
00412 #if defined(__CC_ARM)
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 #include <stdio.h>
00424
00425
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
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;
00508 }
00509 #endif
00510