2 /* -------------------------------- Arctic Core ------------------------------
\r
3 * Arctic Core - the open source AUTOSAR platform http://arccore.com
\r
5 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
\r
7 * This source code is free software; you can redistribute it and/or modify it
\r
8 * under the terms of the GNU General Public License version 2 as published by the
\r
9 * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
\r
11 * This program is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
\r
15 * -------------------------------- Arctic Core ------------------------------*/
\r
17 /* ----------------------------[information]----------------------------------
\r
21 * Implements the glue between different clibs and ArcCore "system"
\r
23 * Implementation Notes:
\r
24 * Heavily inspired by http://neptune.billgatliff.com/newlib.html
\r
27 /* ----------------------------[includes]------------------------------------*/
\r
30 #include <sys/stat.h>
\r
31 #include <sys/types.h>
\r
37 #include "Std_Types.h"
\r
39 #include "device_serial.h"
\r
41 #if defined(USE_TTY_UDE)
\r
42 #include "serial_dbg_ude.h"
\r
45 /* ----------------------------[private define]------------------------------*/
\r
46 /* ----------------------------[private macro]-------------------------------*/
\r
47 /* ----------------------------[private typedef]-----------------------------*/
\r
48 /* ----------------------------[private function prototypes]-----------------*/
\r
49 /* ----------------------------[private variables]---------------------------*/
\r
51 #if defined(USE_TTY_T32)
\r
52 extern DeviceSerialType T32_Device;
\r
55 DeviceSerialType *deviceList[] = {
\r
56 #if defined(USE_TTY_T32)
\r
62 /* Global file descriptor to device list */
\r
63 DeviceSerialType *fileList[] = {
\r
64 #if defined(USE_TTY_T32)
\r
65 [0] = &T32_Device, /* stdin */
\r
66 [1] = &T32_Device, /* stdout */
\r
67 [2] = &T32_Device, /* stderr */
\r
68 #elif defined(USE_TTY_UDE)
\r
69 [0] = &T32_Device, /* stdin */
\r
70 [1] = &T32_Device, /* stdout */
\r
71 [2] = &T32_Device, /* stderr */
\r
77 /* Errno is made reentrant by using malloc and we don't want this. This is usually fixed
\r
78 * by undef errno and declaring it as extern int. It does not work when using GCC for
\r
79 * HC1X and this ifdef takes care of that.
\r
82 #if defined(__GNUC__) && defined(CFG_HC1X)
\r
88 /* ----------------------------[private functions]---------------------------*/
\r
89 /* ----------------------------[public functions]----------------------------*/
\r
93 * POSIX open function
\r
95 * Should probably support some O and S flags here
\r
96 * See http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html
\r
98 * @param name Name of the file to open
\r
99 * @param flags O_xx flags
\r
100 * @param mode S_xx modes.
\r
101 * @return The file descriptor or -1 if failed to open.
\r
103 int open(const char *name, int flags, int mode){
\r
107 for( i=0; i<sizeof(fileList)/sizeof(fileList[0]); i++ ) {
\r
108 if( strcmp(name,fileList[i]->name) == 0 ) {
\r
114 if( fileList[fd]->open != NULL ) {
\r
115 fileList[fd]->open(name,flags,mode);
\r
118 /* TODO: Set errno?! */
\r
126 #include <sys/stat.h>
\r
127 int fstat(int file, struct stat *st) {
\r
129 st->st_mode = S_IFCHR;
\r
137 int kill(int pid, int sig){
\r
145 int close( int fd ) {
\r
153 * @return 1 if connected to a terminal
\r
155 int isatty( int fd )
\r
161 /* If we use malloc and it runs out of memory it calls sbrk()
\r
164 #if defined(CFG_PPC)
\r
166 /* linker symbols */
\r
167 extern char _heap_start[]; // incomplete array to ensure not placed in small-data
\r
168 extern char _heap_end[];
\r
170 void * sbrk( ptrdiff_t incr )
\r
173 static char *nextAvailMemPtr = _heap_start;
\r
175 if( nextAvailMemPtr + incr > _heap_end) {
\r
176 write( 2, "Heap overflow!\n", 15 );
\r
179 prevEnd = nextAvailMemPtr;
\r
180 nextAvailMemPtr += incr;
\r
184 extern char _end[];
\r
186 //static char *curbrk = _end;
\r
189 #define HEAPSIZE 16000
\r
193 * The heap sadly have alignment that depends on the pagesize that
\r
194 * you compile malloc newlib with. From what I can tell from the
\r
195 * code that is a pagesize of 4096.
\r
198 unsigned char _heap[HEAPSIZE] __attribute__((aligned (4)));
\r
199 //__attribute__((section(".heap")));
\r
201 void * sbrk( ptrdiff_t incr )
\r
203 static unsigned char *heap_end;
\r
204 unsigned char *prev_heap_end;
\r
207 if( heap_end == 0 ){
\r
210 prev_heap_end = heap_end;
\r
212 if( heap_end + incr - _heap > HEAPSIZE ) {
\r
213 /* heap overflow - announce on stderr */
\r
214 write( 2, "Heap overflow!\n", 15 );
\r
220 return (caddr_t) prev_heap_end;
\r
226 int read( int fd, void *buf, size_t nbytes )
\r
229 Device_Get(fd)->read(buf,nbytes);
\r
237 * @param fd The file descriptor
\r
242 int write( int fd, const void *buf, size_t nbytes) {
\r
244 Device_Get(fd)->write(buf,nbytes);
\r
249 /* reposition read/write file offset
\r
250 * We can't seek, return error.*/
\r
251 off_t lseek( int fd, off_t offset,int whence)
\r
258 return ((off_t)-1);
\r
261 void __init( void )
\r
266 #if defined(CFG_ARM)
\r
267 void _exit( int status ) {
\r
268 #ifdef USE_TTY_CODE_COMPOSER
\r
269 __asm(" .global C$$EXIT");
\r
270 __asm("C$$EXIT: nop");
\r
273 ShutdownOS( E_OS_EXIT_ABORT );
\r