1 /*
2   Copyright (c) 2011 Arduino.  All right reserved.
3 
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8 
9   This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12   See the GNU Lesser General Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18 
19 /**
20   * \file syscalls_sam3.c
21   *
22   * Implementation of newlib syscall.
23   *
24   */
25 
26 /*----------------------------------------------------------------------------
27  *        Headers
28  *----------------------------------------------------------------------------*/
29 
30 
31 #include "syscalls.h"
32 
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include "sam.h"
36 #if defined (  __GNUC__  ) /* GCC CS3 */
37   #include <sys/types.h>
38   #include <sys/stat.h>
39 #endif
40 
41 // Helper macro to mark unused parameters and prevent compiler warnings.
42 // Appends _UNUSED to the variable name to prevent accidentally using them.
43 #ifdef __GNUC__
44 #  define UNUSED(x) x ## _UNUSED __attribute__((__unused__))
45 #else
46 #  define UNUSED(x) x ## _UNUSED
47 #endif
48 
49 /*----------------------------------------------------------------------------
50  *        Exported variables
51  *----------------------------------------------------------------------------*/
52 
53 #undef errno
54 extern int errno ;
55 extern int  _end ;
56 
57 /*----------------------------------------------------------------------------
58  *        Exported functions
59  *----------------------------------------------------------------------------*/
60 extern void _exit( int status ) ;
61 extern void _kill( int pid, int sig ) ;
62 extern int _getpid ( void ) ;
63 
_sbrk(int incr)64 extern caddr_t _sbrk ( int incr )
65 {
66     static unsigned char *heap = NULL ;
67     unsigned char *prev_heap ;
68 
69     if ( heap == NULL )
70     {
71         heap = (unsigned char *)&_end ;
72     }
73     prev_heap = heap;
74 
75     heap += incr ;
76 
77     return (caddr_t) prev_heap ;
78 }
79 
link(UNUSED (char * cOld),UNUSED (char * cNew))80 extern int link( UNUSED(char *cOld), UNUSED(char *cNew) )
81 {
82     return -1 ;
83 }
84 
_close(UNUSED (int file))85 extern int _close( UNUSED(int file) )
86 {
87     return -1 ;
88 }
89 
_fstat(UNUSED (int file),struct stat * st)90 extern int _fstat( UNUSED(int file), struct stat *st )
91 {
92     st->st_mode = S_IFCHR ;
93 
94     return 0 ;
95 }
96 
_isatty(UNUSED (int file))97 extern int _isatty( UNUSED(int file) )
98 {
99     return 1 ;
100 }
101 
_lseek(UNUSED (int file),UNUSED (int ptr),UNUSED (int dir))102 extern int _lseek( UNUSED(int file), UNUSED(int ptr), UNUSED(int dir) )
103 {
104     return 0 ;
105 }
106 
_read(UNUSED (int file),UNUSED (char * ptr),UNUSED (int len))107 extern int _read(UNUSED(int file), UNUSED(char *ptr), UNUSED(int len) )
108 {
109     return 0 ;
110 }
111 
_write(UNUSED (int file),char * ptr,int len)112 extern int _write( UNUSED(int file), char *ptr, int len )
113 {
114     int iIndex ;
115 
116 
117 //    for ( ; *ptr != 0 ; ptr++ )
118     for ( iIndex=0 ; iIndex < len ; iIndex++, ptr++ )
119     {
120 //        UART_PutChar( *ptr ) ;
121 
122 		// Check if the transmitter is ready
123 		  while ((UART->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
124 			;
125 
126 		  // Send character
127 		  UART->UART_THR = *ptr;
128     }
129 
130     return iIndex ;
131 }
132 
_exit(int status)133 extern void _exit( int status )
134 {
135     printf( "Exiting with status %d.\n", status ) ;
136 
137     for ( ; ; ) ;
138 }
139 
_kill(UNUSED (int pid),UNUSED (int sig))140 extern void _kill( UNUSED(int pid), UNUSED(int sig) )
141 {
142     return ;
143 }
144 
_getpid(void)145 extern int _getpid ( void )
146 {
147     return -1 ;
148 }
149