EFM32 Jade Gecko 12 Software Documentation  efm32jg12-doc-5.1.2
retargetio.c
Go to the documentation of this file.
1 /***************************************************************************/
18 /***************************************************************************/
27 extern int RETARGET_ReadChar(void);
28 extern int RETARGET_WriteChar(char c);
29 
30 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
31 
32 #include <sys/stat.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "em_device.h"
37 
39 int fileno(FILE *);
42 int _close(int file);
43 int _fstat(int file, struct stat *st);
44 int _isatty(int file);
45 int _lseek(int file, int ptr, int dir);
46 int _read(int file, char *ptr, int len);
47 caddr_t _sbrk(int incr);
48 int _write(int file, const char *ptr, int len);
49 
50 extern char _end;
52 /**************************************************************************/
62 int _close(int file)
63 {
64  (void) file;
65  return 0;
66 }
67 
68 /**************************************************************************/
73 void _exit (int status)
74 {
75  (void) status;
76  while (1) {} /* Hang here forever... */
77 }
78 
79 /**************************************************************************/
92 int _fstat(int file, struct stat *st)
93 {
94  (void) file;
95  st->st_mode = S_IFCHR;
96  return 0;
97 }
98 
99 /**************************************************************************/
102 int _getpid(void)
103 {
104  return 1;
105 }
106 
107 /**************************************************************************/
117 int _isatty(int file)
118 {
119  (void) file;
120  return 1;
121 }
122 
123 /**************************************************************************/
128 int _kill(int pid, int sig)
129 {
130  (void)pid;
131  (void)sig;
132  return -1;
133 }
134 
135 /**************************************************************************/
151 int _lseek(int file, int ptr, int dir)
152 {
153  (void) file;
154  (void) ptr;
155  (void) dir;
156  return 0;
157 }
158 
159 /**************************************************************************/
175 int _read(int file, char *ptr, int len)
176 {
177  int c, rxCount = 0;
178 
179  (void) file;
180 
181  while (len--)
182  {
183  if ((c = RETARGET_ReadChar()) != -1)
184  {
185  *ptr++ = c;
186  rxCount++;
187  }
188  else
189  {
190  break;
191  }
192  }
193 
194  if (rxCount <= 0)
195  {
196  return -1; /* Error exit */
197  }
198 
199  return rxCount;
200 }
201 
202 /**************************************************************************/
212 caddr_t _sbrk(int incr)
213 {
214  static char *heap_end;
215  char *prev_heap_end;
216  static const char heaperr[] = "Heap and stack collision\n";
217 
218  if (heap_end == 0)
219  {
220  heap_end = &_end;
221  }
222 
223  prev_heap_end = heap_end;
224  if ((heap_end + incr) > (char*) __get_MSP())
225  {
226  _write(fileno(stdout), heaperr, strlen(heaperr));
227  exit(1);
228  }
229  heap_end += incr;
230 
231  return (caddr_t) prev_heap_end;
232 }
233 
234 /**************************************************************************/
250 int _write(int file, const char *ptr, int len)
251 {
252  int txCount;
253 
254  (void) file;
255 
256  for (txCount = 0; txCount < len; txCount++)
257  {
258  RETARGET_WriteChar(*ptr++);
259  }
260 
261  return len;
262 }
263 #endif /* !defined( __CROSSWORKS_ARM ) && defined( __GNUC__ ) */
264 
265 #if defined(__ICCARM__)
266 /*******************
267  *
268  * Copyright 1998-2003 IAR Systems. All rights reserved.
269  *
270  * $Revision: 38614 $
271  *
272  * This is a template implementation of the "__write" function used by
273  * the standard library. Replace it with a system-specific
274  * implementation.
275  *
276  * The "__write" function should output "size" number of bytes from
277  * "buffer" in some application-specific way. It should return the
278  * number of characters written, or _LLIO_ERROR on failure.
279  *
280  * If "buffer" is zero then __write should perform flushing of
281  * internal buffers, if any. In this case "handle" can be -1 to
282  * indicate that all handles should be flushed.
283  *
284  * The template implementation below assumes that the application
285  * provides the function "MyLowLevelPutchar". It should return the
286  * character written, or -1 on failure.
287  *
288  ********************/
289 
290 #include <yfuns.h>
291 #include <stdint.h>
292 
293 _STD_BEGIN
294 
295 /**************************************************************************/
301 static int TxBuf(uint8_t *buffer, int nbytes)
302 {
303  int i;
304 
305  for (i = 0; i < nbytes; i++)
306  {
307  RETARGET_WriteChar(*buffer++);
308  }
309  return nbytes;
310 }
311 
312 /*
313  * If the __write implementation uses internal buffering, uncomment
314  * the following line to ensure that we are called with "buffer" as 0
315  * (i.e. flush) when the application terminates.
316  */
317 
318 size_t __write(int handle, const unsigned char * buffer, size_t size)
319 {
320  /* Remove the #if #endif pair to enable the implementation */
321 
322  size_t nChars = 0;
323 
324  if (buffer == 0)
325  {
326  /*
327  * This means that we should flush internal buffers. Since we
328  * don't we just return. (Remember, "handle" == -1 means that all
329  * handles should be flushed.)
330  */
331  return 0;
332  }
333 
334  /* This template only writes to "standard out" and "standard err",
335  * for all other file handles it returns failure. */
336  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
337  {
338  return _LLIO_ERROR;
339  }
340 
341  /* Hook into USART1 transmit function here */
342  if (TxBuf((uint8_t *) buffer, size) != size)
343  return _LLIO_ERROR;
344  else
345  nChars = size;
346 
347  return nChars;
348 }
349 
350 size_t __read(int handle, unsigned char * buffer, size_t size)
351 {
352  /* Remove the #if #endif pair to enable the implementation */
353  int nChars = 0;
354 
355  /* This template only reads from "standard in", for all other file
356  * handles it returns failure. */
357  if (handle != _LLIO_STDIN)
358  {
359  return _LLIO_ERROR;
360  }
361 
362  for (/* Empty */; size > 0; --size)
363  {
364  int c = RETARGET_ReadChar();
365  if (c < 0)
366  break;
367 
368  *buffer++ = c;
369  ++nChars;
370  }
371 
372  return nChars;
373 }
374 
375 _STD_END
376 
377 #endif /* defined( __ICCARM__ ) */
378 
379 #if defined(__CROSSWORKS_ARM)
380 
381 /* Pass each of these function straight to the USART */
382 int __putchar(int ch)
383 {
384  return(RETARGET_WriteChar(ch));
385 }
386 
387 int __getchar(void)
388 {
389  return(RETARGET_ReadChar());
390 }
391 
392 #endif /* defined( __CROSSWORKS_ARM ) */
393 
394 #if defined(__CC_ARM)
395 /******************************************************************************/
396 /* RETARGET.C: 'Retarget' layer for target-dependent low level functions */
397 /******************************************************************************/
398 /* This file is part of the uVision/ARM development tools. */
399 /* Copyright (c) 2005-2006 Keil Software. All rights reserved. */
400 /* This software may only be used under the terms of a valid, current, */
401 /* end user licence from KEIL for a compatible version of KEIL software */
402 /* development tools. Nothing else gives you the right to use this software. */
403 /******************************************************************************/
404 
405 #include <stdio.h>
406 
407 /* #pragma import(__use_no_semihosting_swi) */
408 
409 struct __FILE
410 {
411  int handle;
412 };
413 
415 FILE __stdout;
416 
417 /**************************************************************************/
430 int fputc(int ch, FILE *f)
431 {
432  return(RETARGET_WriteChar(ch));
433 }
434 
435 /**************************************************************************/
445 int fgetc(FILE *f)
446 {
447  return(RETARGET_ReadChar());
448 }
449 
450 /**************************************************************************/
461 int ferror(FILE *f)
462 {
463  /* Your implementation of ferror */
464  return EOF;
465 }
466 
467 /**************************************************************************/
474 void _ttywrch(int ch)
475 {
476  RETARGET_WriteChar(ch);
477 }
478 
479 /**************************************************************************/
487 void _sys_exit(int return_code)
488 {
489  label: goto label; /* endless loop */
490 }
491 #endif /* defined( __CC_ARM ) */
492 
int RETARGET_ReadChar(void)
Receive a byte from USART/LEUART and put into global buffer.
int _lseek(int file, int ptr, int dir)
Set position in a file.
Definition: retargetio.c:151
int _read(int file, char *ptr, int len)
Read from a file.
Definition: retargetio.c:175
caddr_t _sbrk(int incr)
Increase heap.
Definition: retargetio.c:212
int _isatty(int file)
Query whether output stream is a terminal.
Definition: retargetio.c:117
int _kill(int pid, int sig)
Send signal to process.
Definition: retargetio.c:128
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
int _getpid(void)
Get process ID.
Definition: retargetio.c:102
int _write(int file, const char *ptr, int len)
Write to a file.
Definition: retargetio.c:250
int RETARGET_WriteChar(char c)
Transmit single byte to USART/LEUART.
static volatile int rxCount
void _exit(int status)
Exit the program.
Definition: retargetio.c:73
int _fstat(int file, struct stat *st)
Status of an open file.
Definition: retargetio.c:92
int _close(int file)
Close a file.
Definition: retargetio.c:62
char _end