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