]> rtime.felk.cvut.cz Git - arc.git/blob - common/newlib_port.c
Merged in from diagnostic-dev
[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
231 #ifdef USE_TTY_ARM_ITM
232                 for (int i = 0; i < nbytes; i++) {
233                         ITM_SendChar(*(buf + i));
234                 }
235 #endif
236
237 #if defined(USE_RAMLOG)
238                 for (int i = 0; i < nbytes; i++) {
239                         ramlog_chr (*(buf + i));
240                 }
241 #endif
242
243         }
244         else
245         {
246 #if defined(USE_RAMLOG)
247                 /* RAMLOG support */
248                 if(fd == FILE_RAMLOG) {
249                         for (int i = 0; i < nbytes; i++) {
250                                 ramlog_chr (*(buf + i));
251                         }
252                 }
253 #endif
254         }
255
256         return (nbytes);
257 }
258
259 int arc_putchar(int fd, int c) {
260         char cc = c;
261         write( fd,&cc,1);
262
263         return 0;
264 }
265
266 /* If we use malloc and it runs out of memory it calls sbrk()
267  */
268 #if 1
269
270 extern char _end[];
271
272 //static char *curbrk = _end;
273
274 #ifndef HEAPSIZE
275 #define HEAPSIZE 16000
276 #endif
277
278 /*
279  * The heap sadly have alignment that depends on the pagesize that
280  * you compile malloc newlib with. From what I can tell from the
281  * code that is a pagesize of 4096.
282  */
283
284 unsigned char _heap[HEAPSIZE] __attribute__((aligned (4)));
285 //__attribute__((section(".heap")));
286
287 void * sbrk( ptrdiff_t incr )
288 {
289     static unsigned char *heap_end;
290     unsigned char *prev_heap_end;
291
292 /* initialize */
293     if( heap_end == 0 )
294         heap_end = _heap;
295
296         prev_heap_end = heap_end;
297
298         if( heap_end + incr - _heap > HEAPSIZE ) {
299         /* heap overflow - announce on stderr */
300                 write( 2, "Heap overflow!\n", 15 );
301                 abort();
302         }
303
304    heap_end += incr;
305
306    return (caddr_t) prev_heap_end;
307 }
308 #else
309 void *sbrk(int inc )
310 {
311         /* We use our own malloc */
312         return (void *)(-1);
313 }
314 #endif
315
316 int stat( const char *file, struct stat *st ) {
317 //int stat(char *file, struct stat *st) {
318         (void)file;
319         st->st_mode = S_IFCHR;
320         return 0;
321 }
322
323
324 int getpid() {
325   return 1;
326 }
327
328 #include <errno.h>
329 #undef errno
330 extern int errno;
331 int kill(int pid, int sig){
332         (void)pid;
333         (void)sig;
334         errno=EINVAL;
335         return(-1);
336 }
337
338
339 /* Should not really be here, but .. */
340
341 void _fini( void )
342 {
343
344 }
345
346
347 void __init( void )
348 {
349
350 }
351 #if defined(CFG_ARM)
352 void _exit( int status ) {
353         while(1);
354 }
355 #endif
356