]> git.sur5r.net Git - freertos/blob - Demo/RX600_RX62N-RSK_Renesas/RTOSDemo/Renesas-Files/lowsrc.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / RX600_RX62N-RSK_Renesas / RTOSDemo / Renesas-Files / lowsrc.c
1 /***********************************************************************/\r
2 /*                                                                     */\r
3 /*  FILE        :lowsrc.c                                              */\r
4 /*  DATE        :Wed, Jun 16, 2010                                     */\r
5 /*  DESCRIPTION :Program of I/O Stream                                 */\r
6 /*  CPU TYPE    :Other                                                 */\r
7 /*                                                                     */\r
8 /*  This file is generated by Renesas Project Generator (Ver.4.50).    */\r
9 /*  NOTE:THIS IS A TYPICAL EXAMPLE.                                    */\r
10 /*                                                                     */\r
11 /***********************************************************************/\r
12                   \r
13 \r
14 \r
15 /*********************************************************************\r
16 *\r
17 * Device     : RX\r
18 *\r
19 * File Name  : lowsrc.c\r
20 *\r
21 * Abstract   : Program of I/O Stream.\r
22 *\r
23 * History    : 1.00  (2009-08-07)\r
24 *\r
25 * NOTE       : THIS IS A TYPICAL EXAMPLE.\r
26 *\r
27 * Copyright(c) 2009 Renesas Technology Corp.\r
28 *               And Renesas Solutions Corp.,All Rights Reserved. \r
29 *\r
30 *********************************************************************/\r
31 \r
32 #include <string.h>\r
33 #include <stdio.h>\r
34 #include <stddef.h>\r
35 #include "lowsrc.h"\r
36 \r
37 /* file number */\r
38 #define STDIN  0                    /* Standard input (console)        */\r
39 #define STDOUT 1                    /* Standard output (console)       */\r
40 #define STDERR 2                    /* Standard error output (console) */\r
41 \r
42 #define FLMIN  0                    /* Minimum file number     */\r
43 #define _MOPENR 0x1\r
44 #define _MOPENW 0x2\r
45 #define _MOPENA 0x4\r
46 #define _MTRUNC 0x8\r
47 #define _MCREAT 0x10\r
48 #define _MBIN   0x20\r
49 #define _MEXCL  0x40\r
50 #define _MALBUF 0x40\r
51 #define _MALFIL 0x80\r
52 #define _MEOF   0x100\r
53 #define _MERR   0x200\r
54 #define _MLBF   0x400\r
55 #define _MNBF   0x800\r
56 #define _MREAD  0x1000\r
57 #define _MWRITE 0x2000\r
58 #define _MBYTE  0x4000\r
59 #define _MWIDE  0x8000\r
60 /* File Flags */\r
61 #define O_RDONLY 0x0001 /* Read only                                       */\r
62 #define O_WRONLY 0x0002 /* Write only                                      */\r
63 #define O_RDWR   0x0004 /* Both read and Write                             */\r
64 #define O_CREAT  0x0008 /* A file is created if it is not existed          */\r
65 #define O_TRUNC  0x0010 /* The file size is changed to 0 if it is existed. */\r
66 #define O_APPEND 0x0020 /* The position is set for next reading/writing    */\r
67                         /* 0: Top of the file 1: End of file               */\r
68 \r
69 /* Special character code */\r
70 #define CR 0x0d                     /* Carriage return */\r
71 #define LF 0x0a                     /* Line feed       */\r
72 \r
73 #if defined( __RX )\r
74 const long _nfiles = IOSTREAM; /* The number of files for input/output files */\r
75 #else\r
76 const int _nfiles = IOSTREAM;  /* The number of files for input/output files */\r
77 #endif\r
78 char flmod[IOSTREAM];          /* The location for the mode of opened file.  */\r
79 \r
80 unsigned char sml_buf[IOSTREAM];\r
81 \r
82 #define FPATH_STDIN     "C:\\stdin"\r
83 #define FPATH_STDOUT    "C:\\stdout"\r
84 #define FPATH_STDERR    "C:\\stderr"\r
85 \r
86 /* H8 Normal mode ,SH and RX */\r
87 #if defined( __2000N__ ) || defined( __2600N__ ) || defined( __300HN__ ) || defined( _SH )\r
88 /* Output one character to standard output */\r
89 extern void charput(char);\r
90 /* Input one character from standard input */\r
91 extern char charget(void);\r
92 /* Output one character to the file        */\r
93 extern char fcharput(char, unsigned char);\r
94 /* Input one character from the file       */\r
95 extern char fcharget(char*, unsigned char);\r
96 /* Open the file */\r
97 extern char fileopen(char*, unsigned char, unsigned char*);\r
98 /* Close the file */\r
99 extern char fileclose(unsigned char);\r
100 /* Move the file offset */\r
101 extern char fpseek(unsigned char, long, unsigned char);\r
102 /* Get the file offset */\r
103 extern char fptell(unsigned char, long*);\r
104 \r
105 /* RX */\r
106 #elif defined( __RX )\r
107 /* Output one character to standard output */\r
108 extern void charput(unsigned char);\r
109 /* Input one character from standard input */\r
110 extern unsigned char charget(void);\r
111 \r
112 /* H8 Advanced mode */\r
113 #elif defined( __2000A__ ) || defined( __2600A__ ) || defined( __300HA__ ) || defined( __H8SXN__ ) || defined( __H8SXA__ ) || defined( __H8SXM__ ) || defined( __H8SXX__ )\r
114 /* Output one character to standard output */\r
115 extern void charput(char);\r
116 /* Input one character from standard input */\r
117 extern char charget(void);\r
118 /* Output one character to the file        */\r
119 extern char fcharput(char, unsigned char);\r
120 /* Input one character from the file       */\r
121 extern char fcharget(char*, unsigned char);\r
122 /* Open the file */\r
123 /* Specified as the number of register which stored paramter is 3 */\r
124 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);\r
125 /* Close the file */\r
126 extern char fileclose(unsigned char);\r
127 /* Move the file offset */\r
128 extern char fpseek(unsigned char, long, unsigned char);\r
129 /* Get the file offset */\r
130 extern char fptell(unsigned char, long*);\r
131 \r
132 /* H8300 and H8300L */\r
133 #elif defined( __300__ ) || defined( __300L__ )\r
134 /* Output one character to standard output */\r
135 extern void charput(char);\r
136 /* Input one character from standard input */\r
137 extern char charget(void);\r
138 /* Output one character to the file        */\r
139 extern char fcharput(char, unsigned char);\r
140 /* Input one character from the file       */\r
141 extern char fcharget(char*, unsigned char);\r
142 /* Open the file */\r
143 /* Specified as the number of register which stored paramter is 3 */\r
144 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);\r
145 /* Close the file */\r
146 extern char fileclose(unsigned char);\r
147 /* Move the file offset */\r
148 /* Move the file offset */\r
149 extern char __regparam3 fpseek(unsigned char, long, unsigned char);\r
150 /* Get the file offset */\r
151 extern char fptell(unsigned char, long*);\r
152 #endif\r
153 \r
154 #include <stdio.h>\r
155 FILE *_Files[IOSTREAM]; // structure for FILE\r
156 char *env_list[] = {            // Array for environment variables(**environ)\r
157     "ENV1=temp01",\r
158     "ENV2=temp02",\r
159     "ENV9=end",\r
160     '\0'                        // Terminal for environment variables\r
161 };\r
162 \r
163 char **environ = env_list;\r
164 \r
165 /****************************************************************************/\r
166 /* _INIT_IOLIB                                                              */\r
167 /*  Initialize C library Functions, if necessary.                           */\r
168 /*  Define USES_SIMIO on Assembler Option.                                  */\r
169 /****************************************************************************/\r
170 void _INIT_IOLIB( void )\r
171 {\r
172     /* A file for standard input/output is opened or created. Each FILE     */\r
173     /* structure members are initialized by the library. Each _Buf member   */\r
174     /* in it is re-set the end of buffer pointer.                           */\r
175 \r
176     /* Standard Input File                                                  */\r
177     if( freopen( FPATH_STDIN, "r", stdin ) == NULL )\r
178         stdin->_Mode = 0xffff;  /* Not allow the access if it fails to open */\r
179     stdin->_Mode  = _MOPENR;            /* Read only attribute              */\r
180     stdin->_Mode |= _MNBF;              /* Non-buffering for data           */\r
181     stdin->_Bend = stdin->_Buf + 1;  /* Re-set pointer to the end of buffer */\r
182 \r
183     /* Standard Output File                                                 */\r
184     if( freopen( FPATH_STDOUT, "w", stdout ) == NULL ) \r
185         stdout->_Mode = 0xffff; /* Not allow the access if it fails to open */\r
186     stdout->_Mode |= _MNBF;             /* Non-buffering for data           */\r
187     stdout->_Bend = stdout->_Buf + 1;/* Re-set pointer to the end of buffer */\r
188     \r
189     /* Standard Error File                                                  */\r
190     if( freopen( FPATH_STDERR, "w", stderr ) == NULL )\r
191         stderr->_Mode = 0xffff; /* Not allow the access if it fails to open */\r
192     stderr->_Mode |= _MNBF;             /* Non-buffering for data           */\r
193     stderr->_Bend = stderr->_Buf + 1;/* Re-set pointer to the end of buffer */\r
194 }\r
195 \r
196 /****************************************************************************/\r
197 /* _CLOSEALL                                                                */\r
198 /****************************************************************************/\r
199 void _CLOSEALL( void )\r
200 {\r
201     long i;\r
202 \r
203     for( i=0; i < _nfiles; i++ )\r
204     {\r
205         /* Checks if the file is opened or not                               */\r
206         if( _Files[i]->_Mode & (_MOPENR | _MOPENW | _MOPENA ) )\r
207         fclose( _Files[i] );    /* Closes the file                           */\r
208     }\r
209 }\r
210 \r
211 /**************************************************************************/\r
212 /*       open:file open                                                   */\r
213 /*          Return value:File number (Pass)                               */\r
214 /*                       -1          (Failure)                            */\r
215 /**************************************************************************/\r
216 #if defined( __RX )\r
217 long open(const char *name,                  /* File name                 */\r
218      long  mode,                             /* Open mode                 */\r
219      long  flg)                              /* Open flag                 */\r
220 #else\r
221 int open(char *name,                         /* File name                 */\r
222      int  mode,                              /* Open mode                 */\r
223      int  flg)                               /* Open flag                 */\r
224 #endif\r
225 {\r
226 \r
227 \r
228     if( strcmp( name, FPATH_STDIN ) == 0 )      /* Standard Input file?   */\r
229     {\r
230         if( ( mode & O_RDONLY ) == 0 ) return -1;\r
231         flmod[STDIN] = mode;\r
232         return STDIN;\r
233     }\r
234     else if( strcmp( name, FPATH_STDOUT ) == 0 )/* Standard Output file?  */\r
235     {\r
236         if( ( mode & O_WRONLY ) == 0 ) return -1;\r
237         flmod[STDOUT] = mode;\r
238         return STDOUT;\r
239     }\r
240     else if(strcmp(name, FPATH_STDERR ) == 0 )  /* Standard Error file?   */\r
241     {\r
242         if( ( mode & O_WRONLY ) == 0 ) return -1;\r
243         flmod[STDERR] = mode;\r
244         return STDERR;\r
245     }\r
246     else return -1;                             /*Others                  */\r
247 }\r
248 \r
249 #if defined( __RX )\r
250 long close( long fileno )\r
251 #else\r
252 int close( int fileno )\r
253 #endif\r
254 {\r
255     return 1;\r
256 }\r
257 \r
258 /**************************************************************************/\r
259 /* write:Data write                                                       */\r
260 /*  Return value:Number of write characters (Pass)                        */\r
261 /*               -1                         (Failure)                     */\r
262 /**************************************************************************/\r
263 #if defined( __RX )\r
264 long write(long  fileno,             /* File number                       */\r
265       const unsigned char *buf,       /* The address of destination buffer */\r
266       long  count)                   /* The number of chacter to write    */\r
267 #else\r
268 int write(int  fileno,               /* File number                       */\r
269       char *buf,                     /* The address of destination buffer */\r
270       int  count)                    /* The number of chacter to write    */\r
271 #endif\r
272 {\r
273     long    i;                          /* A variable for counter         */\r
274     unsigned char    c;                 /* An output character            */\r
275 \r
276     /* Checking the mode of file , output each character                  */\r
277     /* Checking the attribute for Write-Only, Read-Only or Read-Write     */\r
278     if(flmod[fileno]&O_WRONLY || flmod[fileno]&O_RDWR)\r
279     {\r
280         if( fileno == STDIN ) return -1;            /* Standard Input     */\r
281         else if( (fileno == STDOUT) || (fileno == STDERR) ) \r
282                                                             /* Standard Error/output   */\r
283         {\r
284             for( i = count; i > 0; --i )\r
285             {\r
286                 c = *buf++;\r
287                 charput(c);\r
288             }\r
289             return count;        /*Return the number of written characters */\r
290         }\r
291         else return -1;                  /* Incorrect file number          */\r
292     }\r
293     else return -1;                      /* An error                       */\r
294 }\r
295 \r
296 #if defined( __RX )\r
297 long read( long fileno, unsigned char *buf, long count )\r
298 #else\r
299 int read( int fileno, char *buf, unsigned int count )\r
300 #endif\r
301 {\r
302            long i;\r
303 \r
304        /* Checking the file mode with the file number, each character is input and stored the buffer */\r
305 \r
306        if((flmod[fileno]&_MOPENR) || (flmod[fileno]&O_RDWR)){\r
307              for(i = count; i > 0; i--){\r
308                    *buf = charget();\r
309                    if(*buf==CR){              /* Replace the new line character */\r
310                          *buf = LF;\r
311                    }\r
312                    buf++;\r
313              }\r
314              return count;\r
315        }\r
316        else {\r
317              return -1;\r
318        }\r
319 }\r
320 \r
321 #if defined( __RX )\r
322 long lseek( long fileno, long offset, long base )\r
323 #else\r
324 long lseek( int fileno, long offset, int base )\r
325 #endif\r
326 {\r
327     return -1L;\r
328 }\r
329 \r