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