1 /* -------------------------------- Arctic Core ------------------------------
\r
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
\r
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
\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
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
14 * -------------------------------- Arctic Core ------------------------------*/
\r
18 #include <sys/stat.h>
\r
19 #include <sys/types.h>
\r
24 #include "Std_Types.h"
\r
27 #if defined(CFG_ARM_CM3)
\r
28 #include "irq_types.h"
\r
29 #include "core_cm3.h"
\r
32 #ifdef USE_TTY_TCF_STREAMS
\r
33 #include "streams.h"
\r
37 #if defined(CFG_ARM)
\r
40 #define fstat _fstat
\r
41 #define getpid _getpid
\r
43 #define close _close
\r
44 #define isatty _isatty
\r
47 #define write _write
\r
48 #define lseek _lseek
\r
51 // Operation on Winidea terminal buffer
\r
54 #define TWBUFF_SIZE 0x100
\r
55 #define TRBUFF_SIZE 0x100
\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
67 #ifdef USE_TTY_WINIDEA
\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
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
83 #define FILE_RAMLOG 3
\r
89 // This must be in un-cached space....
\r
91 static volatile char t32_outport __attribute__ ((section (".t32_outport")));
\r
93 void t32_writebyte(char c)
\r
95 /* T32 can hang here for several reasons;
\r
96 * - term.view e:address.offset(v.address(t32_outport)) e:0
\r
99 while (t32_outport != 0 ) ; /* wait until port is free */
\r
100 t32_outport = c; /* send character */
\r
108 int close( int fd ) {
\r
113 char *__env[1] = { 0 };
\r
114 char **environ = __env;
\r
122 int execve(const char *path, char * const argv[], char * const envp[] ) {
\r
123 //int execve(char *name, char **argv, char **env){
\r
136 #include <sys/stat.h>
\r
137 int fstat(int file, struct stat *st) {
\r
139 st->st_mode = S_IFCHR;
\r
143 /* Returns 1 if connected to a terminal. T32 can be a terminal
\r
146 int isatty( int fd )
\r
153 int fstat( int fd, struct stat *buf )
\r
155 buf->st_mode = S_IFCHR;
\r
156 buf->st_blksize = 0;
\r
162 /* reposition read/write file offset
\r
163 * We can't seek, return error.*/
\r
164 off_t lseek( int fd, off_t offset,int whence)
\r
171 return ((off_t)-1);
\r
174 int open(const char *name, int flags, int mode){
\r
179 #if defined(USE_RAMLOG)
\r
180 if( strcmp(name,"ramlog") == 0 ) {
\r
181 return FILE_RAMLOG;
\r
188 int read( int fd, void *buf, size_t nbytes )
\r
193 #ifdef USE_TTY_WINIDEA
\r
194 (void)g_TRBuffer[0];
\r
197 /* Only support write for now, return 0 read */
\r
202 int write( int fd, const void *_buf, size_t nbytes)
\r
204 //(void)fd; // Normally 0- ?, 1-stdout, 2-stderr,
\r
208 if( fd <= STDERR_FILENO ) {
\r
209 #ifdef USE_TTY_WINIDEA
\r
212 char *buf = (char *)_buf;
\r
213 unsigned char nCnt,nLen;
\r
214 for(nCnt=0; nCnt<nbytes; nCnt++)
\r
216 while(TWBUFF_FULL());
\r
218 g_TWBuffer[nLen]=buf[nCnt];
\r
219 nLen=TWBUFF_INC(nLen);
\r
226 char *buf = (char *)_buf;
\r
227 for (int i = 0; i < nbytes; i++) {
\r
228 if (*(buf + i) == '\n') {
\r
229 t32_writebyte ('\r');
\r
230 // t32_writebyte ('\n');
\r
232 t32_writebyte (*(buf + i));
\r
236 #ifdef USE_TTY_ARM_ITM
\r
237 char *buf = (char *)_buf;
\r
238 for (int i = 0; i < nbytes; i++) {
\r
239 ITM_SendChar(*(buf + i));
\r
243 #ifdef USE_TTY_TCF_STREAMS
\r
244 char *buf = (char *)_buf;
\r
245 for (int i = 0; i < nbytes; i++) {
\r
246 TCF_TTY_SendChar(*(buf + i));
\r
250 #if defined(USE_RAMLOG)
\r
252 char *buf = (char *)_buf;
\r
253 for (int i = 0; i < nbytes; i++) {
\r
254 ramlog_chr (*(buf + i));
\r
262 #if defined(USE_RAMLOG)
\r
263 /* RAMLOG support */
\r
264 if(fd == FILE_RAMLOG) {
\r
265 char *buf = (char *)_buf;
\r
266 for (int i = 0; i < nbytes; i++) {
\r
267 ramlog_chr (*(buf + i));
\r
276 int arc_putchar(int fd, int c) {
\r
283 /* If we use malloc and it runs out of memory it calls sbrk()
\r
287 extern char _end[];
\r
289 //static char *curbrk = _end;
\r
292 #define HEAPSIZE 16000
\r
296 * The heap sadly have alignment that depends on the pagesize that
\r
297 * you compile malloc newlib with. From what I can tell from the
\r
298 * code that is a pagesize of 4096.
\r
301 unsigned char _heap[HEAPSIZE] __attribute__((aligned (4)));
\r
302 //__attribute__((section(".heap")));
\r
304 void * sbrk( ptrdiff_t incr )
\r
306 static unsigned char *heap_end;
\r
307 unsigned char *prev_heap_end;
\r
310 if( heap_end == 0 )
\r
313 prev_heap_end = heap_end;
\r
315 if( heap_end + incr - _heap > HEAPSIZE ) {
\r
316 /* heap overflow - announce on stderr */
\r
317 write( 2, "Heap overflow!\n", 15 );
\r
323 return (caddr_t) prev_heap_end;
\r
326 void *sbrk(int inc )
\r
328 /* We use our own malloc */
\r
329 return (void *)(-1);
\r
333 int stat( const char *file, struct stat *st ) {
\r
334 //int stat(char *file, struct stat *st) {
\r
336 st->st_mode = S_IFCHR;
\r
348 int kill(int pid, int sig){
\r
356 /* Should not really be here, but .. */
\r
364 void __init( void )
\r
368 #if defined(CFG_ARM)
\r
369 void _exit( int status ) {
\r