]> rtime.felk.cvut.cz Git - arc.git/blob - common/newlib_port.c
Added support for LCD.
[arc.git] / common / newlib_port.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 \r
17 #include <unistd.h>\r
18 #include <sys/stat.h>\r
19 #include <sys/types.h>\r
20 #include <errno.h>\r
21 #include <stdlib.h>\r
22 #include <string.h>\r
23 #include <stdio.h>\r
24 #include "Std_Types.h"\r
25 #include "Ramlog.h"\r
26 \r
27 #if defined(CFG_ARM_CM3)\r
28 #include "irq_types.h"\r
29 #include "core_cm3.h"\r
30 #endif\r
31 \r
32 #ifdef USE_TTY_TCF_STREAMS\r
33 #include "streams.h"\r
34 #endif\r
35 \r
36 \r
37 #if defined(CFG_ARM)\r
38 #define open    _open\r
39 #define exit    _exit\r
40 #define fstat   _fstat\r
41 #define getpid  _getpid\r
42 #define kill    _kill\r
43 #define close   _close\r
44 #define isatty  _isatty\r
45 #define sbrk    _sbrk\r
46 #define read    _read\r
47 #define write   _write\r
48 #define lseek   _lseek\r
49 #endif\r
50 \r
51 // Operation on Winidea terminal buffer\r
52 \r
53 \r
54 #define TWBUFF_SIZE 0x100\r
55 #define TRBUFF_SIZE 0x100\r
56 \r
57 \r
58 #define TBUFF_PTR 2\r
59 \r
60 #define TWBUFF_LEN (TWBUFF_SIZE+TBUFF_PTR)\r
61 #define TRBUFF_LEN (TRBUFF_SIZE+TBUFF_PTR)\r
62 #define TWBUFF_TPTR (g_TWBuffer[TWBUFF_SIZE+0])\r
63 #define TWBUFF_CPTR (g_TWBuffer[TWBUFF_SIZE+1])\r
64 #define TWBUFF_INC(n) ((n + 1)&(TWBUFF_SIZE-1))\r
65 #define TWBUFF_FULL() (TWBUFF_TPTR==((TWBUFF_CPTR-1)&(TWBUFF_SIZE-1)))\r
66 \r
67 #ifdef USE_TTY_WINIDEA\r
68 \r
69 #if defined(MC912DG128A)\r
70 static volatile unsigned char g_TWBuffer[TWBUFF_LEN];\r
71 static volatile unsigned char g_TRBuffer[TRBUFF_LEN];\r
72 static volatile char g_TConn __attribute__ ((section (".winidea_port")));\r
73 \r
74 #else\r
75 static volatile unsigned char g_TWBuffer[TWBUFF_LEN] __attribute__ ((aligned (0x100))); // Transmit to WinIDEA terminal\r
76 static volatile unsigned char g_TRBuffer[TRBUFF_LEN] __attribute__ ((aligned (0x100)));\r
77 static volatile char g_TConn __attribute__ ((section (".winidea_port")));\r
78 \r
79 #endif\r
80 \r
81 #endif\r
82 \r
83 #ifdef USE_TTY_CODE_COMPOSER\r
84 \r
85 #define _DTOPEN    (0xF0)\r
86 #define _DTCLOSE   (0xF1)\r
87 #define _DTREAD    (0xF2)\r
88 #define _DTWRITE   (0xF3)\r
89 #define _DTLSEEK   (0xF4)\r
90 #define _DTUNLINK  (0xF5)\r
91 #define _DTGETENV  (0xF6)\r
92 #define _DTRENAME  (0xF7)\r
93 #define _DTGETTIME (0xF8)\r
94 #define _DTGETCLK  (0xF9)\r
95 #define _DTSYNC    (0xFF)\r
96 \r
97 #define LOADSHORT(x,y,z)  { x[(z)]   = (unsigned short) (y); \\r
98                             x[(z)+1] = (unsigned short) (y) >> 8;  }\r
99 \r
100 #define UNLOADSHORT(x,z) ((short) ( (short) x[(z)] +             \\r
101                                    ((short) x[(z)+1] << 8)))\r
102 \r
103 #define PACKCHAR(val, base, byte) ( (base)[(byte)] = (val) )\r
104 \r
105 #define UNPACKCHAR(base, byte)    ( (base)[byte] )\r
106 \r
107 \r
108 static unsigned char parmbuf[8];\r
109 #define BUFSIZ 512\r
110 #define CC_BUFFER_SIZE ((BUFSIZ)+32)\r
111 volatile unsigned int _CIOBUF_[CC_BUFFER_SIZE] __attribute__ ((section (".cio")));\r
112 \r
113 /***************************************************************************/\r
114 /*                                                                         */\r
115 /*  WRITEMSG()  -  Sends the passed data and parameters on to the host.    */\r
116 /*                                                                         */\r
117 /***************************************************************************/\r
118 void writemsg(unsigned char  command,\r
119               register const unsigned char *parm,\r
120               register const          char *data,\r
121               unsigned int            length)\r
122 {\r
123    volatile unsigned char *p = (volatile unsigned char *)(_CIOBUF_+1);\r
124    unsigned int i;\r
125 \r
126    /***********************************************************************/\r
127    /* THE LENGTH IS WRITTEN AS A TARGET INT                               */\r
128    /***********************************************************************/\r
129    _CIOBUF_[0] = length;\r
130 \r
131    /***********************************************************************/\r
132    /* THE COMMAND IS WRITTEN AS A TARGET BYTE                             */\r
133    /***********************************************************************/\r
134    *p++ = command;\r
135 \r
136    /***********************************************************************/\r
137    /* PACK THE PARAMETERS AND DATA SO THE HOST READS IT AS BYTE STREAM    */\r
138    /***********************************************************************/\r
139    for (i = 0; i < 8; i++)      PACKCHAR(*parm++, p, i);\r
140    for (i = 0; i < length; i++) PACKCHAR(*data++, p, i+8);\r
141 \r
142    /***********************************************************************/\r
143    /* THE BREAKPOINT THAT SIGNALS THE HOST TO DO DATA TRANSFER            */\r
144    /***********************************************************************/\r
145    __asm("       .global C$$IO$$");\r
146    __asm("C$$IO$$: nop");\r
147 }\r
148 \r
149 /***************************************************************************/\r
150 /*                                                                         */\r
151 /*  READMSG()   -  Reads the data and parameters passed from the host.     */\r
152 /*                                                                         */\r
153 /***************************************************************************/\r
154 void readmsg(register unsigned char *parm,\r
155              register char          *data)\r
156 {\r
157    volatile unsigned char *p = (volatile unsigned char *)(_CIOBUF_+1);\r
158    unsigned int   i;\r
159    unsigned int   length;\r
160 \r
161    /***********************************************************************/\r
162    /* THE LENGTH IS READ AS A TARGET INT                                  */\r
163    /***********************************************************************/\r
164    length = _CIOBUF_[0];\r
165 \r
166    /***********************************************************************/\r
167    /* UNPACK THE PARAMETERS AND DATA                                      */\r
168    /***********************************************************************/\r
169    for (i = 0; i < 8; i++) *parm++ = UNPACKCHAR(p, i);\r
170    if (data != NULL)\r
171       for (i = 0; i < length; i++) *data++ = UNPACKCHAR(p, i+8);\r
172 }\r
173 \r
174 /****************************************************************************/\r
175 /* HOSTWRITE()  -  Pass the write command and its arguments to the host.    */\r
176 /****************************************************************************/\r
177 int HOSTwrite(int dev_fd, const char *buf, unsigned count)\r
178 {\r
179    int result;\r
180 \r
181    if (count > BUFSIZ) count = BUFSIZ;\r
182 \r
183    LOADSHORT(parmbuf,dev_fd,0);\r
184    LOADSHORT(parmbuf,count,2);\r
185    writemsg(_DTWRITE,parmbuf,(char *)buf,count);\r
186    readmsg(parmbuf,NULL);\r
187 \r
188    result = UNLOADSHORT(parmbuf,0);\r
189 \r
190    return result;\r
191 }\r
192 \r
193 #endif\r
194 \r
195 #ifdef USE_TTY_TMS570_KEIL\r
196 #include "GLCD.h"\r
197 #endif\r
198 \r
199 #define FILE_RAMLOG             3\r
200 \r
201 /*\r
202  * T32 stuff\r
203  */\r
204 \r
205 // This must be in un-cached space....\r
206 #ifdef USE_TTY_T32\r
207 static volatile char t32_outport __attribute__ ((section (".t32_outport")));\r
208 \r
209 void t32_writebyte(char c)\r
210 {\r
211         /* T32 can hang here for several reasons;\r
212          * - term.view e:address.offset(v.address(t32_outport)) e:0\r
213          */\r
214 \r
215         while (t32_outport != 0 ) ; /* wait until port is free */\r
216         t32_outport = c; /* send character */\r
217 }\r
218 #endif\r
219 /*\r
220  * clib support\r
221  */\r
222 \r
223 /* Do nothing */\r
224 int close( int fd ) {\r
225         (void)fd;\r
226         return (-1);\r
227 }\r
228 \r
229 char *__env[1] = { 0 };\r
230 char **environ = __env;\r
231 \r
232 \r
233 #include <errno.h>\r
234 #undef errno\r
235 extern int errno;\r
236 \r
237 \r
238 int execve(const char *path, char * const argv[], char * const envp[] ) {\r
239 //int execve(char *name, char **argv, char **env){\r
240         (void)path;\r
241         (void)argv;\r
242         (void)envp;\r
243         errno=ENOMEM;\r
244         return -1;\r
245 }\r
246 \r
247 int fork() {\r
248   errno=EAGAIN;\r
249   return -1;\r
250 }\r
251 \r
252 #include <sys/stat.h>\r
253 int fstat(int file, struct stat *st) {\r
254         (void)file;\r
255         st->st_mode = S_IFCHR;\r
256         return 0;\r
257 }\r
258 \r
259 /* Returns 1 if connected to a terminal. T32 can be a terminal\r
260  */\r
261 \r
262 int isatty( int fd )\r
263 {\r
264         (void)fd;\r
265         return 1;\r
266 }\r
267 \r
268 /*\r
269 int fstat( int fd,  struct stat *buf )\r
270 {\r
271   buf->st_mode = S_IFCHR;\r
272   buf->st_blksize = 0;\r
273 \r
274   return (0);\r
275 }\r
276 */\r
277 \r
278 /* reposition read/write file offset\r
279  * We can't seek, return error.*/\r
280 off_t lseek( int fd, off_t offset,int whence)\r
281 {\r
282         (void)fd;\r
283         (void)offset;\r
284         (void)whence;\r
285 \r
286         errno = ESPIPE;\r
287         return ((off_t)-1);\r
288 }\r
289 \r
290 int open(const char *name, int flags, int mode){\r
291         (void)name;\r
292         (void)flags;\r
293         (void)mode;\r
294 \r
295 #if defined(USE_RAMLOG)\r
296         if( strcmp(name,"ramlog") == 0 ) {\r
297                 return FILE_RAMLOG;\r
298         }\r
299 #endif\r
300 \r
301     return -1;\r
302 }\r
303 \r
304 int read( int fd, void *buf, size_t nbytes )\r
305 {\r
306         (void)fd;\r
307         (void)buf;\r
308         (void)nbytes;\r
309 #ifdef USE_TTY_WINIDEA\r
310         (void)g_TRBuffer[0];\r
311 #endif\r
312 \r
313         /* Only support write for now, return 0 read */\r
314         return 0;\r
315 }\r
316 \r
317 \r
318 int write(  int fd, const void *_buf, size_t nbytes)\r
319 {\r
320         //(void)fd;  // Normally 0- ?, 1-stdout, 2-stderr,\r
321                                 // Added 3-ramlog,\r
322 \r
323 \r
324         if( fd <= STDERR_FILENO ) {\r
325 #ifdef USE_TTY_WINIDEA\r
326                 if (g_TConn)\r
327                 {\r
328           char *buf = (char *)_buf;\r
329                   unsigned char nCnt,nLen;\r
330                   for(nCnt=0; nCnt<nbytes; nCnt++)\r
331                         {\r
332                         while(TWBUFF_FULL());\r
333                         nLen=TWBUFF_TPTR;\r
334                         g_TWBuffer[nLen]=buf[nCnt];\r
335                         nLen=TWBUFF_INC(nLen);\r
336                         TWBUFF_TPTR=nLen;\r
337                         }\r
338                 }\r
339 #endif\r
340 \r
341 #ifdef USE_TTY_T32\r
342                 char *buf = (char *)_buf;\r
343                 for (int i = 0; i < nbytes; i++) {\r
344                         if (*(buf + i) == '\n') {\r
345                                 t32_writebyte ('\r');\r
346 //                      t32_writebyte ('\n');\r
347                         }\r
348                         t32_writebyte (*(buf + i));\r
349                 }\r
350 #endif\r
351 \r
352 #ifdef USE_TTY_ARM_ITM\r
353                 char *buf = (char *)_buf;\r
354                 for (int i = 0; i < nbytes; i++) {\r
355                         ITM_SendChar(*(buf + i));\r
356                 }\r
357 #endif\r
358 \r
359 #ifdef USE_TTY_TCF_STREAMS\r
360                 char *buf = (char *)_buf;\r
361                 for (int i = 0; i < nbytes; i++) {\r
362                         TCF_TTY_SendChar(*(buf + i));\r
363                 }\r
364 #endif\r
365 \r
366 #ifdef USE_TTY_CODE_COMPOSER\r
367         HOSTwrite(fd, _buf, nbytes);\r
368 #endif\r
369 \r
370 #ifdef USE_TTY_TMS570_KEIL\r
371         for (int i = 0; i < nbytes; i++) {\r
372                 GLCD_PrintChar((_buf + i));\r
373         }\r
374 #endif\r
375 \r
376 #if defined(USE_RAMLOG)\r
377                 {\r
378                         char *buf = (char *)_buf;\r
379                         for (int i = 0; i < nbytes; i++) {\r
380                                 ramlog_chr (*(buf + i));\r
381                         }\r
382                 }\r
383 #endif\r
384 \r
385         }\r
386         else\r
387         {\r
388 #if defined(USE_RAMLOG)\r
389                 /* RAMLOG support */\r
390                 if(fd == FILE_RAMLOG) {\r
391                         char *buf = (char *)_buf;\r
392                         for (int i = 0; i < nbytes; i++) {\r
393                                 ramlog_chr (*(buf + i));\r
394                         }\r
395                 }\r
396 #endif\r
397         }\r
398 \r
399         return (nbytes);\r
400 }\r
401 \r
402 int arc_putchar(int fd, int c) {\r
403         char cc = c;\r
404         write( fd,&cc,1);\r
405 \r
406         return 0;\r
407 }\r
408 \r
409 /* If we use malloc and it runs out of memory it calls sbrk()\r
410  */\r
411 #if 1\r
412 \r
413 extern char _end[];\r
414 \r
415 //static char *curbrk = _end;\r
416 \r
417 #ifndef HEAPSIZE\r
418 #define HEAPSIZE 16000\r
419 #endif\r
420 \r
421 /*\r
422  * The heap sadly have alignment that depends on the pagesize that\r
423  * you compile malloc newlib with. From what I can tell from the\r
424  * code that is a pagesize of 4096.\r
425  */\r
426 \r
427 unsigned char _heap[HEAPSIZE] __attribute__((aligned (4)));\r
428 //__attribute__((section(".heap")));\r
429 \r
430 void * sbrk( ptrdiff_t incr )\r
431 {\r
432     static unsigned char *heap_end;\r
433     unsigned char *prev_heap_end;\r
434 \r
435 /* initialize */\r
436     if( heap_end == 0 )\r
437         heap_end = _heap;\r
438 \r
439         prev_heap_end = heap_end;\r
440 \r
441         if( heap_end + incr - _heap > HEAPSIZE ) {\r
442         /* heap overflow - announce on stderr */\r
443                 write( 2, "Heap overflow!\n", 15 );\r
444                 abort();\r
445         }\r
446 \r
447    heap_end += incr;\r
448 \r
449    return (caddr_t) prev_heap_end;\r
450 }\r
451 #else\r
452 void *sbrk(int inc )\r
453 {\r
454         /* We use our own malloc */\r
455         return (void *)(-1);\r
456 }\r
457 #endif\r
458 \r
459 int stat( const char *file, struct stat *st ) {\r
460 //int stat(char *file, struct stat *st) {\r
461         (void)file;\r
462         st->st_mode = S_IFCHR;\r
463         return 0;\r
464 }\r
465 \r
466 \r
467 int getpid() {\r
468   return 1;\r
469 }\r
470 \r
471 #include <errno.h>\r
472 #undef errno\r
473 extern int errno;\r
474 int kill(int pid, int sig){\r
475         (void)pid;\r
476         (void)sig;\r
477         errno=EINVAL;\r
478         return(-1);\r
479 }\r
480 \r
481 \r
482 /* Should not really be here, but .. */\r
483 \r
484 void _fini( void )\r
485 {\r
486 \r
487 }\r
488 \r
489 \r
490 void __init( void )\r
491 {\r
492 \r
493 }\r
494 #if defined(CFG_ARM)\r
495 void _exit( int status ) {\r
496 #ifdef USE_TTY_CODE_COMPOSER\r
497         __asm("        .global C$$EXIT");\r
498         __asm("C$$EXIT: nop");\r
499 #endif\r
500         while(1);\r
501 }\r
502 #endif\r
503 \r