sidestep
[mTask.git] / int / com / src / syscalls / syscalls.c
1 /***********************************************************************/
2 /* */
3 /* SYSCALLS.C: System Calls for the newlib */
4 /* most of this is from newlib-lpc and a Keil-demo */
5 /* */
6 /* These are "reentrant functions" as needed by */
7 /* the WinARM-newlib-config, see newlib-manual. */
8 /* Collected and modified by Martin Thomas */
9 /* */
10 /***********************************************************************/
11
12
13 #include <stdlib.h>
14 #include <reent.h>
15 #include <errno.h>
16 #include <sys/stat.h>
17
18 _ssize_t _read_r(
19 struct _reent *r,
20 int file,
21 void *ptr,
22 size_t len)
23 {
24 /*
25 unsigned char *p;
26 while ( AT91F_US_RxReady((AT91PS_USART)AT91C_BASE_DBGU) == 0 ) {
27 ;
28 }
29
30 p = ptr;
31
32 *p= AT91F_US_GetChar((AT91PS_USART)AT91C_BASE_DBGU);
33 */
34 return (_ssize_t)1;
35 }
36
37
38 _ssize_t _write_r (
39 struct _reent *r,
40 int file,
41 const void *ptr,
42 size_t len)
43 {
44 /*
45 size_t todo;
46 const unsigned char *p;
47
48 todo = len;
49 p = ptr;
50
51 for( ; todo != 0; todo--) {
52 if ( *p == '\n' ) {
53 while (!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_DBGU)) {
54 ;
55 }
56 AT91F_US_PutChar((AT91PS_USART)AT91C_BASE_DBGU, '\r');
57 }
58 while (!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_DBGU)) {
59 ;
60 }
61 AT91F_US_PutChar((AT91PS_USART)AT91C_BASE_DBGU, *p++);
62 }
63 */
64 return (_ssize_t)len; // Number of bytes written.
65 }
66
67
68 int _close_r(
69 struct _reent *r,
70 int file)
71 {
72 return 0;
73 }
74
75
76 _off_t _lseek_r(
77 struct _reent *r,
78 int file,
79 _off_t ptr,
80 int dir)
81 {
82 return (_off_t)0; /* Always indicate we are at file beginning. */
83 }
84
85
86 int _fstat_r(
87 struct _reent *r,
88 int file,
89 struct stat *st)
90 {
91 /* Always set as character device. */
92 st->st_mode = S_IFCHR;
93 /* assigned to strong type with implicit */
94 /* signed/unsigned conversion. Required by */
95 /* newlib. */
96
97 return 0;
98 }
99
100
101 int _isatty(int file); /* avoid warning */
102
103 int _isatty(int file)
104 {
105 return 1;
106 }
107
108 void abort(void)
109 {
110 while(1);
111 }
112
113 #if 0
114 static void _exit (int n) {
115 label: goto label; /* endless loop */
116 }
117 #endif
118
119
120 #ifdef USE_CHIBIOS
121
122 void * _sbrk_r(struct _reent *r, int incr)
123 {
124 #if CH_USE_MEMCORE
125 void *p;
126
127 chDbgCheck(incr > 0, "_sbrk_r");
128
129 (void)r;
130 p = chCoreAlloc((size_t)incr);
131 if (p == NULL) {
132 __errno_r(r) = ENOMEM;
133 return (caddr_t)-1;
134 }
135 return (caddr_t)p;
136 #else
137 __errno_r(r) = ENOMEM;
138 return (caddr_t)-1;
139 #endif
140 }
141
142
143 #else
144 /**** Locally used variables. ****/
145 // mt: "cleaner": extern char* end;
146 extern char end[]; /* end is set in the linker command */
147 /* file and is the end of statically */
148 /* allocated data (thus start of heap). */
149
150 static char *heap_ptr; /* Points to current end of the heap. */
151
152 /************************** _sbrk_r *************************************
153 * Support function. Adjusts end of heap to provide more memory to
154 * memory allocator. Simple and dumb with no sanity checks.
155
156 * struct _reent *r -- re-entrancy structure, used by newlib to
157 * support multiple threads of operation.
158 * ptrdiff_t nbytes -- number of bytes to add.
159 * Returns pointer to start of new heap area.
160 *
161 * Note: This implementation is not thread safe (despite taking a
162 * _reent structure as a parameter).
163 * Since _s_r is not used in the current implementation,
164 * the following messages must be suppressed.
165 */
166 void * _sbrk_r(
167 struct _reent *_s_r,
168 ptrdiff_t nbytes)
169 {
170 char *base; /* errno should be set to ENOMEM on error */
171
172 if (!heap_ptr) { /* Initialize if first time through. */
173 heap_ptr = end;
174 }
175 base = heap_ptr; /* Point to end of heap. */
176 heap_ptr += nbytes; /* Increase heap. */
177
178 return base; /* Return pointer to start of new heap area. */
179 }
180 #endif