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
33 #if defined(CFG_ARM)
\r
36 #define fstat _fstat
\r
37 #define getpid _getpid
\r
39 #define close _close
\r
40 #define isatty _isatty
\r
43 #define write _write
\r
44 #define lseek _lseek
\r
47 // Operation on Winidea terminal buffer
\r
50 #define TWBUFF_SIZE 0x100
\r
51 #define TRBUFF_SIZE 0x100
\r
56 #define TWBUFF_LEN (TWBUFF_SIZE+TBUFF_PTR)
\r
57 #define TRBUFF_LEN (TRBUFF_SIZE+TBUFF_PTR)
\r
58 #define TWBUFF_TPTR (g_TWBuffer[TWBUFF_SIZE+0])
\r
59 #define TWBUFF_CPTR (g_TWBuffer[TWBUFF_SIZE+1])
\r
60 #define TWBUFF_INC(n) ((n + 1)&(TWBUFF_SIZE-1))
\r
61 #define TWBUFF_FULL() (TWBUFF_TPTR==((TWBUFF_CPTR-1)&(TWBUFF_SIZE-1)))
\r
63 #ifdef USE_TTY_WINIDEA
\r
65 #if defined(MC912DG128A)
\r
66 static volatile unsigned char g_TWBuffer[TWBUFF_LEN];
\r
67 static volatile unsigned char g_TRBuffer[TRBUFF_LEN];
\r
68 static volatile char g_TConn __attribute__ ((section (".winidea_port")));
\r
71 static volatile unsigned char g_TWBuffer[TWBUFF_LEN] __attribute__ ((aligned (0x100))); // Transmit to WinIDEA terminal
\r
72 static volatile unsigned char g_TRBuffer[TRBUFF_LEN] __attribute__ ((aligned (0x100)));
\r
73 static volatile char g_TConn __attribute__ ((section (".winidea_port")));
\r
79 #define FILE_RAMLOG 3
\r
85 // This must be in un-cached space....
\r
87 static volatile char t32_outport __attribute__ ((section (".t32_outport")));
\r
89 void t32_writebyte(char c)
\r
91 /* T32 can hang here for several reasons;
\r
92 * - term.view e:address.offset(v.address(t32_outport)) e:0
\r
95 while (t32_outport != 0 ) ; /* wait until port is free */
\r
96 t32_outport = c; /* send character */
\r
104 int close( int fd ) {
\r
109 char *__env[1] = { 0 };
\r
110 char **environ = __env;
\r
118 int execve(const char *path, char * const argv[], char * const envp[] ) {
\r
119 //int execve(char *name, char **argv, char **env){
\r
132 #include <sys/stat.h>
\r
133 int fstat(int file, struct stat *st) {
\r
135 st->st_mode = S_IFCHR;
\r
139 /* Returns 1 if connected to a terminal. T32 can be a terminal
\r
142 int isatty( int fd )
\r
149 int fstat( int fd, struct stat *buf )
\r
151 buf->st_mode = S_IFCHR;
\r
152 buf->st_blksize = 0;
\r
158 /* reposition read/write file offset
\r
159 * We can't seek, return error.*/
\r
160 off_t lseek( int fd, off_t offset,int whence)
\r
167 return ((off_t)-1);
\r
170 int open(const char *name, int flags, int mode){
\r
175 #if defined(USE_RAMLOG)
\r
176 if( strcmp(name,"ramlog") == 0 ) {
\r
177 return FILE_RAMLOG;
\r
184 int read( int fd, void *buf, size_t nbytes )
\r
189 #ifdef USE_TTY_WINIDEA
\r
190 (void)g_TRBuffer[0];
\r
193 /* Only support write for now, return 0 read */
\r
198 int write( int fd, const void *_buf, size_t nbytes)
\r
200 //(void)fd; // Normally 0- ?, 1-stdout, 2-stderr,
\r
204 if( fd <= STDERR_FILENO ) {
\r
205 #ifdef USE_TTY_WINIDEA
\r
208 char *buf = (char *)_buf;
\r
209 unsigned char nCnt,nLen;
\r
210 for(nCnt=0; nCnt<nbytes; nCnt++)
\r
212 while(TWBUFF_FULL());
\r
214 g_TWBuffer[nLen]=buf[nCnt];
\r
215 nLen=TWBUFF_INC(nLen);
\r
222 char *buf = (char *)_buf;
\r
223 for (int i = 0; i < nbytes; i++) {
\r
224 if (*(buf + i) == '\n') {
\r
225 t32_writebyte ('\r');
\r
226 // t32_writebyte ('\n');
\r
228 t32_writebyte (*(buf + i));
\r
232 #ifdef USE_TTY_ARM_ITM
\r
233 char *buf = (char *)_buf;
\r
234 for (int i = 0; i < nbytes; i++) {
\r
235 ITM_SendChar(*(buf + i));
\r
239 #if defined(USE_RAMLOG)
\r
241 char *buf = (char *)_buf;
\r
242 for (int i = 0; i < nbytes; i++) {
\r
243 ramlog_chr (*(buf + i));
\r
251 #if defined(USE_RAMLOG)
\r
252 /* RAMLOG support */
\r
253 if(fd == FILE_RAMLOG) {
\r
254 char *buf = (char *)_buf;
\r
255 for (int i = 0; i < nbytes; i++) {
\r
256 ramlog_chr (*(buf + i));
\r
265 int arc_putchar(int fd, int c) {
\r
272 /* If we use malloc and it runs out of memory it calls sbrk()
\r
276 extern char _end[];
\r
278 //static char *curbrk = _end;
\r
281 #define HEAPSIZE 16000
\r
285 * The heap sadly have alignment that depends on the pagesize that
\r
286 * you compile malloc newlib with. From what I can tell from the
\r
287 * code that is a pagesize of 4096.
\r
290 unsigned char _heap[HEAPSIZE] __attribute__((aligned (4)));
\r
291 //__attribute__((section(".heap")));
\r
293 void * sbrk( ptrdiff_t incr )
\r
295 static unsigned char *heap_end;
\r
296 unsigned char *prev_heap_end;
\r
299 if( heap_end == 0 )
\r
302 prev_heap_end = heap_end;
\r
304 if( heap_end + incr - _heap > HEAPSIZE ) {
\r
305 /* heap overflow - announce on stderr */
\r
306 write( 2, "Heap overflow!\n", 15 );
\r
312 return (caddr_t) prev_heap_end;
\r
315 void *sbrk(int inc )
\r
317 /* We use our own malloc */
\r
318 return (void *)(-1);
\r
322 int stat( const char *file, struct stat *st ) {
\r
323 //int stat(char *file, struct stat *st) {
\r
325 st->st_mode = S_IFCHR;
\r
337 int kill(int pid, int sig){
\r
345 /* Should not really be here, but .. */
\r
353 void __init( void )
\r
357 #if defined(CFG_ARM)
\r
358 void _exit( int status ) {
\r