]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/m68k/generic/libs/misc/system_stub.c
It is 12 years after C99 standardization - switch to stdint.h and its types.
[sysless.git] / arch / m68k / generic / libs / misc / system_stub.c
1 /*******************************************************************
2   Components for embedded applications builded for
3   laboratory and medical instruments firmware  
4  
5   system_stub.c - system stub routines for system-less
6                   NEWLIB C library environment
7  
8   Copyright (C) 2001-2003 by Pavel Pisa pisa@cmp.felk.cvut.cz
9             (C) 2001-2003 by PiKRON Ltd. http://www.pikron.com
10
11  *******************************************************************/
12
13 #include <stdint.h>
14 #include <system_def.h>
15 #include <sys/stat.h>
16 #include <errno.h>
17
18 /***********************************************************/
19 #include <periph/qsm_rs232.h>
20
21 char inbyte()
22 {
23   int ch;
24   while((ch=qsm_rs232_recch())<0)
25     { /* wait for char */ ; }
26   return ch;
27 }
28
29
30 int outbyte(unsigned char ch)
31 {
32   int res;
33   while((res=qsm_rs232_sendch(ch))<0)
34     { /* wait for send */ ; }
35   return res;
36 }
37
38
39 /***********************************************************/
40
41 /*
42  * close -- We don't need to do anything, but pretend we did.
43  */
44 int close (int fd)
45 {
46   return (0);
47 }
48
49 /*
50  * fstat -- Since we have no file system, we just return an error.
51  */
52 int fstat(int fd,struct stat *buf)
53 {
54   buf->st_mode = S_IFCHR;       /* Always pretend to be a tty */
55   buf->st_blksize = 0;
56   return (0);
57 }
58
59 /*
60  * getpid -- only one process, so just return 1.
61  */
62 int getpid()
63 {
64   return 1;
65 }
66
67 /*
68  * isatty -- returns 1 if connected to a terminal device,
69  *           returns 0 if not. Since we're hooked up to a
70  *           serial port, we'll say yes _AND return a 1.
71  */
72 int isatty(int fd)
73 {
74   return (1);
75 }
76
77 /*
78  * kill -- go out via exit...
79  */
80 int kill(int pid,int sig)
81 {
82   if(pid == 1)
83     _exit(sig);
84   return 0;
85 }
86
87
88 /*
89  * lseek --  Since a serial port is non-seekable, we return an error.
90  */
91 off_t lseek(int fd, off_t offset, int whence)
92 {
93   errno = ESPIPE;
94   return ((off_t)-1);
95 }
96
97 /*
98  * open -- open a file descriptor. We don't have a filesystem, so
99  *         we return an error.
100  */
101 int open(const char *buf, int flags, int mode)
102 {
103   errno = EIO;
104   return (-1);
105 }
106
107 /*
108  * print -- do a raw print of a string
109  */
110 void print(char *ptr)
111 {
112   while (*ptr) {
113     outbyte (*ptr++);
114   }
115 }
116
117 /*
118  * putnum -- print a 32 bit number in hex
119  */
120 void putnum(unsigned int num)
121 {
122   char  buf[9];
123   int   cnt;
124   char  *ptr;
125   int   digit;
126
127   ptr = buf;
128   for (cnt = 7 ; cnt >= 0 ; cnt--) {
129     digit = (num >> (cnt * 4)) & 0xf;
130
131     if (digit <= 9)
132       *ptr++ = (char) ('0' + digit);
133     else
134       *ptr++ = (char) ('a' - 10 + digit);
135   }
136
137   *ptr = (char) 0;
138   print (buf);
139 }
140
141 /*
142  * read  -- read bytes from the serial port. Ignore fd, since
143  *          we only have stdin.
144  */
145 int read(int fd,char *buf,int nbytes)
146 {
147   int i = 0;
148
149   for (i = 0; i < nbytes; i++) {
150     *(buf + i) = inbyte();
151     if ((*(buf + i) == '\n') || (*(buf + i) == '\r')) {
152       (*(buf + i + 1)) = 0;
153       break;
154     }
155   }
156   return (i);
157 }
158
159 char *heap_ptr=0;
160 extern char _end;
161
162 /*
163  * sbrk -- changes heap size size. Get nbytes more
164  *         RAM. We just increment a pointer in what's
165  *         left of memory on the board.
166  */
167 char *sbrk(int nbytes)
168 {
169   char        *base;
170
171   if (!heap_ptr)
172   {
173     heap_ptr = (char *)&_end;
174     /* heap_ptr = (char *)0x70000; */ /* !!!!!!!!!!! */
175   }
176   base = heap_ptr;
177   heap_ptr += nbytes;
178
179   return base;
180 /* FIXME: We really want to make sure we don't run out of RAM, but this
181  *       isn't very portable.
182  */
183 #if 0
184   if ((RAMSIZE - heap_ptr - nbytes) >= 0) {
185     base = heap_ptr;
186     heap_ptr += nbytes;
187     return (base);
188   } else {
189     errno = ENOMEM;
190     return ((char *)-1);
191   }
192 #endif
193 }
194
195 /*
196  * stat -- Since we have no file system, we just return an error.
197  */
198 int stat(const char *path, struct stat *buf)
199 {
200   errno = EIO;
201   return (-1);
202 }
203
204 /*
205  * unlink -- since we have no file system,
206  *           we just return an error.
207  */
208 int unlink(char * path)
209 {
210   errno = EIO;
211   return (-1);
212 }
213
214 /*
215  * write -- write bytes to the serial port. Ignore fd, since
216  *          stdout and stderr are the same. Since we have no filesystem,
217  *          open will only return an error.
218  */
219 int write(int fd, char *buf, int nbytes)
220 {
221   int i;
222
223   for (i = 0; i < nbytes; i++) {
224     if (*(buf + i) == '\n') {
225       outbyte ('\r');
226     }
227     outbyte (*(buf + i));
228   }
229   return (nbytes);
230 }