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
85 /* Location MUST match NoICE configuration */
\r
86 #ifdef USE_TTY_NOICE
\r
87 static volatile char VUART_TX __attribute__ ((section (".noice_port")));
\r
88 static volatile char VUART_RX __attribute__ ((section (".noice_port")));
\r
89 volatile unsigned char START_VUART = 0;
\r
96 // This must be in un-cached space....
\r
98 static volatile char t32_outport __attribute__ ((section (".t32_outport")));
\r
100 void t32_writebyte(char c)
\r
102 /* T32 can hang here for several reasons;
\r
103 * - term.view e:address.offset(v.address(t32_outport)) e:0
\r
106 while (t32_outport != 0 ) ; /* wait until port is free */
\r
107 t32_outport = c; /* send character */
\r
115 int close( int fd ) {
\r
120 char *__env[1] = { 0 };
\r
121 char **environ = __env;
\r
129 int execve(const char *path, char * const argv[], char * const envp[] ) {
\r
130 //int execve(char *name, char **argv, char **env){
\r
143 #include <sys/stat.h>
\r
144 int fstat(int file, struct stat *st) {
\r
146 st->st_mode = S_IFCHR;
\r
150 /* Returns 1 if connected to a terminal. T32 can be a terminal
\r
153 int isatty( int fd )
\r
160 int fstat( int fd, struct stat *buf )
\r
162 buf->st_mode = S_IFCHR;
\r
163 buf->st_blksize = 0;
\r
169 /* reposition read/write file offset
\r
170 * We can't seek, return error.*/
\r
171 off_t lseek( int fd, off_t offset,int whence)
\r
178 return ((off_t)-1);
\r
181 int open(const char *name, int flags, int mode){
\r
186 #if defined(USE_RAMLOG)
\r
187 if( strcmp(name,"ramlog") == 0 ) {
\r
188 return FILE_RAMLOG;
\r
195 int read( int fd, void *buf, size_t nbytes )
\r
200 #ifdef USE_TTY_WINIDEA
\r
201 (void)g_TRBuffer[0];
\r
204 #ifdef USE_TTY_NOICE
\r
205 // Not tested at all
\r
207 while (VUART_RX != 0)
\r
215 /* Only support write for now, return 0 read */
\r
220 int write( int fd, const void *_buf, size_t nbytes)
\r
222 //(void)fd; // Normally 0- ?, 1-stdout, 2-stderr,
\r
226 if( fd <= STDERR_FILENO ) {
\r
227 #ifdef USE_TTY_NOICE
\r
228 char *buf1 = (char *)_buf;
\r
231 for (int i = 0; i < nbytes; i++) {
\r
235 while (VUART_TX != 0)
\r
242 while (VUART_TX != 0)
\r
251 #ifdef USE_TTY_WINIDEA
\r
254 char *buf = (char *)_buf;
\r
255 unsigned char nCnt,nLen;
\r
256 for(nCnt=0; nCnt<nbytes; nCnt++)
\r
258 while(TWBUFF_FULL());
\r
260 g_TWBuffer[nLen]=buf[nCnt];
\r
261 nLen=TWBUFF_INC(nLen);
\r
268 char *buf = (char *)_buf;
\r
269 for (int i = 0; i < nbytes; i++) {
\r
270 if (*(buf + i) == '\n') {
\r
271 t32_writebyte ('\r');
\r
272 // t32_writebyte ('\n');
\r
274 t32_writebyte (*(buf + i));
\r
278 #ifdef USE_TTY_ARM_ITM
\r
279 char *buf = (char *)_buf;
\r
280 for (int i = 0; i < nbytes; i++) {
\r
281 ITM_SendChar(*(buf + i));
\r
285 #ifdef USE_TTY_TCF_STREAMS
\r
286 char *buf = (char *)_buf;
\r
287 for (int i = 0; i < nbytes; i++) {
\r
288 TCF_TTY_SendChar(*(buf + i));
\r
292 #if defined(USE_RAMLOG)
\r
294 char *buf = (char *)_buf;
\r
295 for (int i = 0; i < nbytes; i++) {
\r
296 ramlog_chr (*(buf + i));
\r
304 #if defined(USE_RAMLOG)
\r
305 /* RAMLOG support */
\r
306 if(fd == FILE_RAMLOG) {
\r
307 char *buf = (char *)_buf;
\r
308 for (int i = 0; i < nbytes; i++) {
\r
309 ramlog_chr (*(buf + i));
\r
318 int arc_putchar(int fd, int c) {
\r
325 /* If we use malloc and it runs out of memory it calls sbrk()
\r
329 extern char _end[];
\r
331 //static char *curbrk = _end;
\r
334 #define HEAPSIZE 16000
\r
338 * The heap sadly have alignment that depends on the pagesize that
\r
339 * you compile malloc newlib with. From what I can tell from the
\r
340 * code that is a pagesize of 4096.
\r
343 unsigned char _heap[HEAPSIZE] __attribute__((aligned (4)));
\r
344 //__attribute__((section(".heap")));
\r
346 void * sbrk( ptrdiff_t incr )
\r
348 static unsigned char *heap_end;
\r
349 unsigned char *prev_heap_end;
\r
352 if( heap_end == 0 )
\r
355 prev_heap_end = heap_end;
\r
357 if( heap_end + incr - _heap > HEAPSIZE ) {
\r
358 /* heap overflow - announce on stderr */
\r
359 write( 2, "Heap overflow!\n", 15 );
\r
365 return (caddr_t) prev_heap_end;
\r
368 void *sbrk(int inc )
\r
370 /* We use our own malloc */
\r
371 return (void *)(-1);
\r
375 int stat( const char *file, struct stat *st ) {
\r
376 //int stat(char *file, struct stat *st) {
\r
378 st->st_mode = S_IFCHR;
\r
390 int kill(int pid, int sig){
\r
398 /* Should not really be here, but .. */
\r
406 void __init( void )
\r
410 #if defined(CFG_ARM)
\r
411 void _exit( int status ) {
\r