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