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