1 /* -------------------------------- Arctic Core ------------------------------
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
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>.
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
14 * -------------------------------- Arctic Core ------------------------------*/
19 #include <sys/types.h>
24 #include "Std_Types.h"
27 #if defined(CFG_ARM_CM3)
28 #include "irq_types.h"
37 #define getpid _getpid
40 #define isatty _isatty
47 // Operation on Winidea terminal buffer
50 #define TWBUFF_SIZE 0x100
51 #define TRBUFF_SIZE 0x100
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)))
63 #ifdef USE_TTY_WINIDEA
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")));
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")));
85 // This must be in un-cached space....
87 static volatile char t32_outport __attribute__ ((section (".t32_outport")));
89 void t32_writebyte(char c)
91 /* T32 can hang here for several reasons;
92 * - term.view e:address.offset(v.address(t32_outport)) e:0
95 while (t32_outport != 0 ) ; /* wait until port is free */
96 t32_outport = c; /* send character */
104 int close( int fd ) {
109 char *__env[1] = { 0 };
110 char **environ = __env;
118 int execve(const char *path, char * const argv[], char * const envp[] ) {
119 //int execve(char *name, char **argv, char **env){
132 #include <sys/stat.h>
133 int fstat(int file, struct stat *st) {
135 st->st_mode = S_IFCHR;
139 /* Returns 1 if connected to a terminal. T32 can be a terminal
149 int fstat( int fd, struct stat *buf )
151 buf->st_mode = S_IFCHR;
158 /* reposition read/write file offset
159 * We can't seek, return error.*/
160 off_t lseek( int fd, off_t offset,int whence)
170 int open(const char *name, int flags, int mode){
175 #if defined(USE_RAMLOG)
176 if( strcmp(name,"ramlog") == 0 ) {
184 int read( int fd, void *buf, size_t nbytes )
189 #ifdef USE_TTY_WINIDEA
193 /* Only support write for now, return 0 read */
198 int write( int fd, const void *_buf, size_t nbytes)
200 char *buf = (char *)_buf;
201 //(void)fd; // Normally 0- ?, 1-stdout, 2-stderr,
205 if( fd <= STDERR_FILENO ) {
206 #ifdef USE_TTY_WINIDEA
209 unsigned char nCnt,nLen;
210 for(nCnt=0; nCnt<nbytes; nCnt++)
212 while(TWBUFF_FULL());
214 g_TWBuffer[nLen]=buf[nCnt];
215 nLen=TWBUFF_INC(nLen);
222 for (int i = 0; i < nbytes; i++) {
223 if (*(buf + i) == '\n') {
224 t32_writebyte ('\r');
225 // t32_writebyte ('\n');
227 t32_writebyte (*(buf + i));
231 #ifdef USE_TTY_ARM_ITM
232 for (int i = 0; i < nbytes; i++) {
233 ITM_SendChar(*(buf + i));
237 #if defined(USE_RAMLOG)
238 for (int i = 0; i < nbytes; i++) {
239 ramlog_chr (*(buf + i));
246 #if defined(USE_RAMLOG)
248 if(fd == FILE_RAMLOG) {
249 for (int i = 0; i < nbytes; i++) {
250 ramlog_chr (*(buf + i));
259 int arc_putchar(int fd, int c) {
266 /* If we use malloc and it runs out of memory it calls sbrk()
272 //static char *curbrk = _end;
275 #define HEAPSIZE 16000
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.
284 unsigned char _heap[HEAPSIZE] __attribute__((aligned (4)));
285 //__attribute__((section(".heap")));
287 void * sbrk( ptrdiff_t incr )
289 static unsigned char *heap_end;
290 unsigned char *prev_heap_end;
296 prev_heap_end = heap_end;
298 if( heap_end + incr - _heap > HEAPSIZE ) {
299 /* heap overflow - announce on stderr */
300 write( 2, "Heap overflow!\n", 15 );
306 return (caddr_t) prev_heap_end;
311 /* We use our own malloc */
316 int stat( const char *file, struct stat *st ) {
317 //int stat(char *file, struct stat *st) {
319 st->st_mode = S_IFCHR;
331 int kill(int pid, int sig){
339 /* Should not really be here, but .. */
352 void _exit( int status ) {