]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX63N-RSK_Renesas/RTOSDemo/Renesas-Files/board/rskrx63n/lowsrc.c
ad9f32ce45ee76266590fd725d108788f533e8fe
[freertos] / FreeRTOS / Demo / RX600_RX63N-RSK_Renesas / RTOSDemo / Renesas-Files / board / rskrx63n / lowsrc.c
1 /***********************************************************************************************************************\r
2 * DISCLAIMER\r
3 * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No \r
4 * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all \r
5 * applicable laws, including copyright laws. \r
6 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING\r
7 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, \r
8 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM \r
9 * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES \r
10 * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS \r
11 * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\r
12 * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of \r
13 * this software. By using this software, you agree to the additional terms and conditions found by accessing the \r
14 * following link:\r
15 * http://www.renesas.com/disclaimer \r
16 *\r
17 * Copyright (C) 2012 Renesas Electronics Corporation. All rights reserved.    \r
18 ***********************************************************************************************************************/\r
19 /***********************************************************************************************************************\r
20 * File Name        : lowsrc.c\r
21 * Description  : Functions to support stream I/O\r
22 ***********************************************************************************************************************/\r
23 /***********************************************************************************************************************\r
24 * History : DD.MM.YYYY Version  Description\r
25 *         : 26.10.2011 1.00     First Release\r
26 ***********************************************************************************************************************/\r
27 /***********************************************************************************************************************\r
28 Includes   <System Includes> , "Project Includes"\r
29 ***********************************************************************************************************************/\r
30 #include <string.h>\r
31 #include <stdio.h>\r
32 #include <stddef.h>\r
33 \r
34 /***********************************************************************************************************************\r
35 Macro definitions\r
36 ***********************************************************************************************************************/\r
37 /*Number of I/O Stream*/
38 #define IOSTREAM 20
39
40 /* file number */\r
41 #define STDIN  0                    /* Standard input (console)        */\r
42 #define STDOUT 1                    /* Standard output (console)       */\r
43 #define STDERR 2                    /* Standard error output (console) */\r
44 \r
45 #define FLMIN  0                    /* Minimum file number     */\r
46 #define _MOPENR 0x1\r
47 #define _MOPENW 0x2\r
48 #define _MOPENA 0x4\r
49 #define _MTRUNC 0x8\r
50 #define _MCREAT 0x10\r
51 #define _MBIN   0x20\r
52 #define _MEXCL  0x40\r
53 #define _MALBUF 0x40\r
54 #define _MALFIL 0x80\r
55 #define _MEOF   0x100\r
56 #define _MERR   0x200\r
57 #define _MLBF   0x400\r
58 #define _MNBF   0x800\r
59 #define _MREAD  0x1000\r
60 #define _MWRITE 0x2000\r
61 #define _MBYTE  0x4000\r
62 #define _MWIDE  0x8000\r
63 /* File Flags */\r
64 #define O_RDONLY 0x0001 /* Read only                                       */\r
65 #define O_WRONLY 0x0002 /* Write only                                      */\r
66 #define O_RDWR   0x0004 /* Both read and Write                             */\r
67 #define O_CREAT  0x0008 /* A file is created if it is not existed          */\r
68 #define O_TRUNC  0x0010 /* The file size is changed to 0 if it is existed. */\r
69 #define O_APPEND 0x0020 /* The position is set for next reading/writing    */\r
70                         /* 0: Top of the file 1: End of file               */\r
71 \r
72 /* Special character code */\r
73 #define CR 0x0d                     /* Carriage return */\r
74 #define LF 0x0a                     /* Line feed       */\r
75 \r
76 #if defined( __RX )\r
77 const long _nfiles = IOSTREAM; /* The number of files for input/output files */\r
78 #else\r
79 const int _nfiles = IOSTREAM;  /* The number of files for input/output files */\r
80 #endif\r
81 char flmod[IOSTREAM];          /* The location for the mode of opened file.  */\r
82 \r
83 unsigned char sml_buf[IOSTREAM];\r
84 \r
85 #define FPATH_STDIN     "C:\\stdin"\r
86 #define FPATH_STDOUT    "C:\\stdout"\r
87 #define FPATH_STDERR    "C:\\stderr"\r
88 \r
89 /* H8 Normal mode ,SH and RX */\r
90 #if defined( __2000N__ ) || defined( __2600N__ ) || defined( __300HN__ ) || defined( _SH )\r
91 /* Output one character to standard output */\r
92 extern void charput(char);\r
93 /* Input one character from standard input */\r
94 extern char charget(void);\r
95 /* Output one character to the file        */\r
96 extern char fcharput(char, unsigned char);\r
97 /* Input one character from the file       */\r
98 extern char fcharget(char*, unsigned char);\r
99 /* Open the file */\r
100 extern char fileopen(char*, unsigned char, unsigned char*);\r
101 /* Close the file */\r
102 extern char fileclose(unsigned char);\r
103 /* Move the file offset */\r
104 extern char fpseek(unsigned char, long, unsigned char);\r
105 /* Get the file offset */\r
106 extern char fptell(unsigned char, long*);\r
107 \r
108 /* RX */\r
109 #elif defined( __RX )\r
110 /* Output one character to standard output */\r
111 extern void charput(unsigned char);\r
112 /* Input one character from standard input */\r
113 extern unsigned char charget(void);\r
114 \r
115 /* H8 Advanced mode */\r
116 #elif defined( __2000A__ ) || defined( __2600A__ ) || defined( __300HA__ ) || defined( __H8SXN__ ) || defined( __H8SXA__ ) || defined( __H8SXM__ ) || defined( __H8SXX__ )\r
117 /* Output one character to standard output */\r
118 extern void charput(char);\r
119 /* Input one character from standard input */\r
120 extern char charget(void);\r
121 /* Output one character to the file        */\r
122 extern char fcharput(char, unsigned char);\r
123 /* Input one character from the file       */\r
124 extern char fcharget(char*, unsigned char);\r
125 /* Open the file */\r
126 /* Specified as the number of register which stored paramter is 3 */\r
127 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);\r
128 /* Close the file */\r
129 extern char fileclose(unsigned char);\r
130 /* Move the file offset */\r
131 extern char fpseek(unsigned char, long, unsigned char);\r
132 /* Get the file offset */\r
133 extern char fptell(unsigned char, long*);\r
134 \r
135 /* H8300 and H8300L */\r
136 #elif defined( __300__ ) || defined( __300L__ )\r
137 /* Output one character to standard output */\r
138 extern void charput(char);\r
139 /* Input one character from standard input */\r
140 extern char charget(void);\r
141 /* Output one character to the file        */\r
142 extern char fcharput(char, unsigned char);\r
143 /* Input one character from the file       */\r
144 extern char fcharget(char*, unsigned char);\r
145 /* Open the file */\r
146 /* Specified as the number of register which stored paramter is 3 */\r
147 extern char __regparam3 fileopen(char*, unsigned char, unsigned char*);\r
148 /* Close the file */\r
149 extern char fileclose(unsigned char);\r
150 /* Move the file offset */\r
151 /* Move the file offset */\r
152 extern char __regparam3 fpseek(unsigned char, long, unsigned char);\r
153 /* Get the file offset */\r
154 extern char fptell(unsigned char, long*);\r
155 #endif\r
156 \r
157 #include <stdio.h>\r
158 FILE *_Files[IOSTREAM]; // structure for FILE\r
159 char *env_list[] = {            // Array for environment variables(**environ)\r
160     "ENV1=temp01",\r
161     "ENV2=temp02",\r
162     "ENV9=end",\r
163     '\0'                        // Terminal for environment variables\r
164 };\r
165 \r
166 char **environ = env_list;\r
167 \r
168 /****************************************************************************/\r
169 /* _INIT_IOLIB                                                              */\r
170 /*  Initialize C library Functions, if necessary.                           */\r
171 /*  Define USES_SIMIO on Assembler Option.                                  */\r
172 /****************************************************************************/\r
173 void _INIT_IOLIB( void )\r
174 {\r
175     /* A file for standard input/output is opened or created. Each FILE     */\r
176     /* structure members are initialized by the library. Each _Buf member   */\r
177     /* in it is re-set the end of buffer pointer.                           */\r
178 \r
179     /* Standard Input File                                                  */\r
180     if( freopen( FPATH_STDIN, "r", stdin ) == NULL )\r
181         stdin->_Mode = 0xffff;  /* Not allow the access if it fails to open */\r
182     stdin->_Mode  = _MOPENR;            /* Read only attribute              */\r
183     stdin->_Mode |= _MNBF;              /* Non-buffering for data           */\r
184     stdin->_Bend = stdin->_Buf + 1;  /* Re-set pointer to the end of buffer */\r
185 \r
186     /* Standard Output File                                                 */\r
187     if( freopen( FPATH_STDOUT, "w", stdout ) == NULL ) \r
188         stdout->_Mode = 0xffff; /* Not allow the access if it fails to open */\r
189     stdout->_Mode |= _MNBF;             /* Non-buffering for data           */\r
190     stdout->_Bend = stdout->_Buf + 1;/* Re-set pointer to the end of buffer */\r
191     \r
192     /* Standard Error File                                                  */\r
193     if( freopen( FPATH_STDERR, "w", stderr ) == NULL )\r
194         stderr->_Mode = 0xffff; /* Not allow the access if it fails to open */\r
195     stderr->_Mode |= _MNBF;             /* Non-buffering for data           */\r
196     stderr->_Bend = stderr->_Buf + 1;/* Re-set pointer to the end of buffer */\r
197 }\r
198 \r
199 /****************************************************************************/\r
200 /* _CLOSEALL                                                                */\r
201 /****************************************************************************/\r
202 void _CLOSEALL( void )\r
203 {\r
204     long i;\r
205 \r
206     for( i=0; i < _nfiles; i++ )\r
207     {\r
208         /* Checks if the file is opened or not                               */\r
209         if( _Files[i]->_Mode & (_MOPENR | _MOPENW | _MOPENA ) )\r
210         fclose( _Files[i] );    /* Closes the file                           */\r
211     }\r
212 }\r
213 \r
214 /**************************************************************************/\r
215 /*       open:file open                                                   */\r
216 /*          Return value:File number (Pass)                               */\r
217 /*                       -1          (Failure)                            */\r
218 /**************************************************************************/\r
219 #if defined( __RX )\r
220 long open(const char *name,                  /* File name                 */\r
221      long  mode,                             /* Open mode                 */\r
222      long  flg)                              /* Open flag                 */\r
223 #else\r
224 int open(char *name,                         /* File name                 */\r
225      int  mode,                              /* Open mode                 */\r
226      int  flg)                               /* Open flag                 */\r
227 #endif\r
228 {\r
229 \r
230 \r
231     if( strcmp( name, FPATH_STDIN ) == 0 )      /* Standard Input file?   */\r
232     {\r
233         if( ( mode & O_RDONLY ) == 0 ) return -1;\r
234         flmod[STDIN] = mode;\r
235         return STDIN;\r
236     }\r
237     else if( strcmp( name, FPATH_STDOUT ) == 0 )/* Standard Output file?  */\r
238     {\r
239         if( ( mode & O_WRONLY ) == 0 ) return -1;\r
240         flmod[STDOUT] = mode;\r
241         return STDOUT;\r
242     }\r
243     else if(strcmp(name, FPATH_STDERR ) == 0 )  /* Standard Error file?   */\r
244     {\r
245         if( ( mode & O_WRONLY ) == 0 ) return -1;\r
246         flmod[STDERR] = mode;\r
247         return STDERR;\r
248     }\r
249     else return -1;                             /*Others                  */\r
250 }\r
251 \r
252 #if defined( __RX )\r
253 long close( long fileno )\r
254 #else\r
255 int close( int fileno )\r
256 #endif\r
257 {\r
258     return 1;\r
259 }\r
260 \r
261 /**************************************************************************/\r
262 /* write:Data write                                                       */\r
263 /*  Return value:Number of write characters (Pass)                        */\r
264 /*               -1                         (Failure)                     */\r
265 /**************************************************************************/\r
266 #if defined( __RX )\r
267 long write(long  fileno,             /* File number                       */\r
268       const unsigned char *buf,       /* The address of destination buffer */\r
269       long  count)                   /* The number of chacter to write    */\r
270 #else\r
271 int write(int  fileno,               /* File number                       */\r
272       char *buf,                     /* The address of destination buffer */\r
273       int  count)                    /* The number of chacter to write    */\r
274 #endif\r
275 {\r
276     long    i;                          /* A variable for counter         */\r
277     unsigned char    c;                 /* An output character            */\r
278 \r
279     /* Checking the mode of file , output each character                  */\r
280     /* Checking the attribute for Write-Only, Read-Only or Read-Write     */\r
281     if(flmod[fileno]&O_WRONLY || flmod[fileno]&O_RDWR)\r
282     {\r
283         if( fileno == STDIN ) return -1;            /* Standard Input     */\r
284         else if( (fileno == STDOUT) || (fileno == STDERR) ) \r
285                                                             /* Standard Error/output   */\r
286         {\r
287             for( i = count; i > 0; --i )\r
288             {\r
289                 c = *buf++;\r
290                 charput(c);\r
291             }\r
292             return count;        /*Return the number of written characters */\r
293         }\r
294         else return -1;                  /* Incorrect file number          */\r
295     }\r
296     else return -1;                      /* An error                       */\r
297 }\r
298 \r
299 #if defined( __RX )\r
300 long read( long fileno, unsigned char *buf, long count )\r
301 #else\r
302 int read( int fileno, char *buf, unsigned int count )\r
303 #endif\r
304 {\r
305            long i;\r
306 \r
307        /* Checking the file mode with the file number, each character is input and stored the buffer */\r
308 \r
309        if((flmod[fileno]&_MOPENR) || (flmod[fileno]&O_RDWR)){\r
310              for(i = count; i > 0; i--){\r
311                    *buf = charget();\r
312                    if(*buf==CR){              /* Replace the new line character */\r
313                          *buf = LF;\r
314                    }\r
315                    buf++;\r
316              }\r
317              return count;\r
318        }\r
319        else {\r
320              return -1;\r
321        }\r
322 }\r
323 \r
324 #if defined( __RX )\r
325 long lseek( long fileno, long offset, long base )\r
326 #else\r
327 long lseek( int fileno, long offset, int base )\r
328 #endif\r
329 {\r
330     return -1L;\r
331 }\r
332 \r