lowsrc.c 16 KB

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