]> rtime.felk.cvut.cz Git - arc.git/blob - common/newlib_port.c
Initial commit.
[arc.git] / common / newlib_port.c
1 /* -------------------------------- Arctic Core ------------------------------
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com
3  *
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>
5  *
6  * This source code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by the
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  * -------------------------------- Arctic Core ------------------------------*/
15
16 \r
17 #include <sys/stat.h>\r
18 #include <sys/types.h>\r
19 #include <errno.h>\r
20 #include <stdlib.h>\r
21 //#include "clibsupport_gcc.h"\r
22
23 #if defined(CFG_ARM)
24 #define open    _open
25 #define exit    _exit
26 #define fstat   _fstat
27 #define getpid  _getpid
28 #define kill    _kill
29 #define close   _close
30 #define isatty  _isatty
31 #define sbrk    _sbrk
32 #define read    _read
33 #define write   _write
34 #define lseek   _lseek
35 #endif
36 \r
37 // Operation on Winidea terminal buffer\r
38 #define TWBUFF_SIZE 0x100\r
39 #define TRBUFF_SIZE 0x100\r
40 \r
41 #define TBUFF_PTR 2\r
42 \r
43 #define TWBUFF_LEN (TWBUFF_SIZE+TBUFF_PTR)\r
44 #define TRBUFF_LEN (TRBUFF_SIZE+TBUFF_PTR)\r
45 #define TWBUFF_TPTR (g_TWBuffer[TWBUFF_SIZE+0])\r
46 #define TWBUFF_CPTR (g_TWBuffer[TWBUFF_SIZE+1])\r
47 #define TWBUFF_INC(n) ((n + 1)&(TWBUFF_SIZE-1))\r
48 #define TWBUFF_FULL() (TWBUFF_TPTR==((TWBUFF_CPTR-1)&(TWBUFF_SIZE-1)))\r
49 \r
50 #ifdef USE_WINIDEA_TERM\r
51 static volatile unsigned char g_TWBuffer[TWBUFF_LEN] __attribute__ ((aligned (0x100))); // Transmit to WinIDEA terminal\r
52 static volatile unsigned char g_TRBuffer[TRBUFF_LEN] __attribute__ ((aligned (0x100)));\r
53 static volatile char g_TConn __attribute__ ((section (".winidea_port")));\r
54 #endif\r
55
56 #define FILE_RAMLOG             3
57 \r
58 /*\r
59  * T32 stuff\r
60  */\r
61
62 // This must be in un-cached space....\r
63 static volatile char t32_outport __attribute__ ((section (".t32_outport")));\r
64 \r
65 \r
66 int arc_putchar(int c) {\r
67         char cc = c;\r
68         write( 1,&cc,1);\r
69 \r
70         return 0;\r
71 }\r
72 \r
73 void t32_writebyte(char c)\r
74 {
75         /* T32 can hang here for several reasons;
76          * - term.view e:address.offset(v.address(t32_outport)) e:0\r
77          */\r
78 \r
79         while (t32_outport != 0 ) ; /* wait until port is free */\r
80         t32_outport = c; /* send character */\r
81 }\r
82 \r
83 /*\r
84  * clib support\r
85  */\r
86 \r
87 /* Do nothing */\r
88 int close( int fd ) {\r
89         (void)fd;\r
90         return (-1);\r
91 }\r
92 \r
93 char *__env[1] = { 0 };\r
94 char **environ = __env;\r
95 \r
96 \r
97 #include <errno.h>\r
98 #undef errno\r
99 extern int errno;\r
100 \r
101 int execve(char *name, char **argv, char **env){\r
102         (void)name;\r
103         (void)argv;\r
104         (void)env;\r
105         errno=ENOMEM;\r
106         return -1;\r
107 }\r
108 \r
109 int fork() {\r
110   errno=EAGAIN;\r
111   return -1;\r
112 }\r
113 \r
114 #include <sys/stat.h>\r
115 int fstat(int file, struct stat *st) {\r
116         (void)file;\r
117         st->st_mode = S_IFCHR;\r
118         return 0;\r
119 }\r
120 \r
121 /* Returns 1 if connected to a terminal. T32 can be a terminal\r
122  */\r
123 \r
124 int isatty( int fd )\r
125 {\r
126         (void)fd;\r
127         return 1;\r
128 }\r
129 \r
130 /*\r
131 int fstat( int fd,  struct stat *buf )\r
132 {\r
133   buf->st_mode = S_IFCHR;\r
134   buf->st_blksize = 0;\r
135 \r
136   return (0);\r
137 }\r
138 */\r
139 \r
140 /* reposition read/write file offset\r
141  * We can't seek, return error.*/\r
142 off_t lseek( int fd, off_t offset,int whence)\r
143 {\r
144         (void)fd;\r
145         (void)offset;\r
146         (void)whence;\r
147 \r
148         errno = ESPIPE;\r
149         return ((off_t)-1);\r
150 }\r
151 \r
152 int open(const char *name, int flags, int mode){\r
153         (void)name;\r
154         (void)flags;\r
155         (void)mode;\r
156 \r
157         if( strcmp(name,"ramlog") == 0 ) {\r
158                 return FILE_RAMLOG;\r
159         }\r
160 \r
161     return -1;\r
162 }\r
163 \r
164 int read( int fd, char *buf, int nbytes )\r
165 {\r
166         (void)fd;\r
167         (void)buf;\r
168         (void)nbytes;\r
169 #ifdef USE_WINIDEA_TERM\r
170         (void)g_TRBuffer[0];\r
171 #endif\r
172 \r
173         /* Only support write for now, return 0 read */\r
174         return 0;\r
175 }\r
176 \r
177 \r
178 int write(  int fd, char *buf, int nbytes)\r
179 {\r
180         //(void)fd;  // Normally 0- ?, 1-stdout, 2-stderr,\r
181                                 // Added 3-ramlog,\r
182 \r
183         if( fd < 3 ) {\r
184 #ifdef USE_WINIDEA_TERM\r
185         if (g_TConn)\r
186         {\r
187           unsigned char nCnt,nLen;\r
188           for(nCnt=0; nCnt<nbytes; nCnt++)\r
189             {\r
190             while(TWBUFF_FULL());\r
191             nLen=TWBUFF_TPTR;\r
192             g_TWBuffer[nLen]=buf[nCnt];\r
193             nLen=TWBUFF_INC(nLen);\r
194             TWBUFF_TPTR=nLen;\r
195             }\r
196         }\r
197 #endif\r
198 \r
199 #ifdef USE_T32_TERM\r
200         for (int i = 0; i < nbytes; i++) {\r
201         if (*(buf + i) == '\n') {\r
202                 t32_writebyte ('\r');\r
203 //                      t32_writebyte ('\n');\r
204         }\r
205         t32_writebyte (*(buf + i));\r
206         }\r
207 #endif
208 #ifdef USE_ARM_ITM_TERM
209         for (int i = 0; i < nbytes; i++) {
210                 ITM_SendChar(*(buf + i));
211         }
212 #endif
213
214         }\r
215         else\r
216         {
217                 /* RAMLOG support */\r
218                 if(fd == FILE_RAMLOG) {\r
219                         for (int i = 0; i < nbytes; i++) {\r
220                                 ramlog_chr (*(buf + i));\r
221                         }\r
222                 }\r
223         }\r
224 \r
225         return (nbytes);\r
226 }\r
227 \r
228 /* If we use malloc and it runs out of memory it calls sbrk()\r
229  */\r
230 #if 1\r
231 \r
232 extern char _end[];\r
233 \r
234 //static char *curbrk = _end;\r
235 \r
236 #define HEAPSIZE 16000\r
237 unsigned char _heap[HEAPSIZE];\r
238 \r
239 caddr_t sbrk( int incr )\r
240 {\r
241     static unsigned char *heap_end;\r
242     unsigned char *prev_heap_end;\r
243 \r
244 /* initialize */\r
245     if( heap_end == 0 )\r
246         heap_end = _heap;\r
247 \r
248         prev_heap_end = heap_end;\r
249 \r
250         if( heap_end + incr - _heap > HEAPSIZE ) {\r
251         /* heap overflow - announce on stderr */\r
252                 write( 2, "Heap overflow!\n", 15 );\r
253                 abort();\r
254         }\r
255 \r
256    heap_end += incr;\r
257 \r
258    return (caddr_t) prev_heap_end;\r
259 }\r
260 #else\r
261 void *sbrk(int inc )\r
262 {\r
263         /* We use our own malloc */\r
264         return (void *)(-1);\r
265 }\r
266 #endif\r
267 \r
268 int stat( const char *file, struct stat *st ) {\r
269 //int stat(char *file, struct stat *st) {\r
270         (void)file;\r
271         st->st_mode = S_IFCHR;\r
272         return 0;\r
273 }\r
274 \r
275 \r
276 int getpid() {\r
277   return 1;\r
278 }\r
279 \r
280 #include <errno.h>\r
281 #undef errno\r
282 extern int errno;\r
283 int kill(int pid, int sig){\r
284         (void)pid;\r
285         (void)sig;\r
286         errno=EINVAL;\r
287         return(-1);\r
288 }\r
289 \r
290 \r
291 /* Should not really be here, but .. */\r
292 \r
293 void _fini( void )\r
294 {\r
295 \r
296 }\r
297 \r
298 \r
299 void __init( void )\r
300 {\r
301 \r
302 }\r
303 #if defined(CFG_ARM)\r
304 void _exit( int status ) {\r
305         while(1);\r
306 }\r
307 #endif\r
308 \r