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