[c] code

Viewer

  1. /*-----------------------------------------------------------------------------
  2.  * Name:    retarget_io.c
  3.  * Purpose: Retarget I/O
  4.  * Rev.:    1.2.0
  5.  *-----------------------------------------------------------------------------*/
  6.  
  7. /* Copyright (c) 2013 - 2016 ARM LIMITED
  8.  
  9.    All rights reserved.
  10.    Redistribution and use in source and binary forms, with or without
  11.    modification, are permitted provided that the following conditions are met:
  12.    - Redistributions of source code must retain the above copyright
  13.      notice, this list of conditions and the following disclaimer.
  14.    - Redistributions in binary form must reproduce the above copyright
  15.      notice, this list of conditions and the following disclaimer in the
  16.      documentation and/or other materials provided with the distribution.
  17.    - Neither the name of ARM nor the names of its contributors may be used
  18.      to endorse or promote products derived from this software without
  19.      specific prior written permission.
  20.    *
  21.    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22.    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.    ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  25.    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26.    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27.    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28.    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29.    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30.    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31.    POSSIBILITY OF SUCH DAMAGE.
  32.    ---------------------------------------------------------------------------*/
  33.  
  34. #include <string.h>
  35. #include <stdint.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <assert.h>
  39. #include <rt_sys.h>
  40.  
  41. #include "RTE_Components.h"
  42.  
  43. #ifdef RTE_Compiler_IO_STDOUT_EVR
  44. #include "EventRecorder.h"
  45. #endif
  46.  
  47. #ifdef RTE_Compiler_IO_File_FS
  48. #include "rl_fs_lib.h"
  49. #endif
  50.  
  51.  
  52. #ifndef STDIN_ECHO
  53. #define STDIN_ECHO      0       /* STDIN: echo to STDOUT */
  54. #endif
  55. #ifndef STDOUT_CR_LF
  56. #define STDOUT_CR_LF    0       /* STDOUT: add CR for LF */
  57. #endif
  58. #ifndef STDERR_CR_LF
  59. #define STDERR_CR_LF    0       /* STDERR: add CR for LF */
  60. #endif
  61.  
  62.  
  63. #if (defined(RTE_Compiler_IO_TTY_ITM)    || \
  64.      defined(RTE_Compiler_IO_STDIN_ITM)  || \
  65.      defined(RTE_Compiler_IO_STDOUT_ITM) || \
  66.      defined(RTE_Compiler_IO_STDERR_ITM))
  67.  
  68. /* ITM registers */
  69. #define ITM_PORT0_U8          (*((volatile uint8_t  *)0xE0000000))
  70. #define ITM_PORT0_U32         (*((volatile uint32_t *)0xE0000000))
  71. #define ITM_TER               (*((volatile uint32_t *)0xE0000E00))
  72. #define ITM_TCR               (*((volatile uint32_t *)0xE0000E80))
  73.  
  74. #define ITM_TCR_ITMENA_Msk    (1UL << 0)
  75.  
  76. /*!< Value identifying \ref ITM_RxBuffer is ready for next character. */
  77. #define ITM_RXBUFFER_EMPTY    0x5AA55AA5
  78.  
  79. /*!< Variable to receive characters. */
  80. extern
  81. volatile int32_t ITM_RxBuffer;
  82. volatile int32_t ITM_RxBuffer = ITM_RXBUFFER_EMPTY;   
  83.  
  84. /** \brief  ITM Send Character
  85.  
  86.     The function transmits a character via the ITM channel 0, and
  87.     \li Just returns when no debugger is connected that has booked the output.
  88.     \li Is blocking when a debugger is connected, but the previous character
  89.         sent has not been transmitted.
  90.  
  91.     \param [in]     ch  Character to transmit.
  92.  
  93.     \returns            Character to transmit.
  94.  */
  95. int32_t ITM_SendChar (int32_t ch);
  96. int32_t ITM_SendChar (int32_t ch) {
  97.   if ((ITM_TCR & ITM_TCR_ITMENA_Msk) && /* ITM enabled */
  98.       (ITM_TER & (1UL << 0)        )) { /* ITM Port #0 enabled */
  99.     while (ITM_PORT0_U32 == 0);
  100.     ITM_PORT0_U8 = (uint8_t)ch;
  101.   }
  102.   return (ch);
  103. }
  104.  
  105. /** \brief  ITM Receive Character
  106.  
  107.     The function inputs a character via the external variable \ref ITM_RxBuffer.
  108.     This variable is monitored and altered by the debugger to provide input.
  109.  
  110.     \return             Received character.
  111.     \return         -1  No character pending.
  112.  */
  113. int32_t ITM_ReceiveChar (void);
  114. int32_t ITM_ReceiveChar (void) {
  115.   int32_t ch = -1;                      /* no character available */
  116.  
  117.   if (ITM_RxBuffer != ITM_RXBUFFER_EMPTY) {
  118.     ch = ITM_RxBuffer;
  119.     ITM_RxBuffer = ITM_RXBUFFER_EMPTY;  /* ready for next character */
  120.   }
  121.  
  122.   return (ch);
  123. }
  124.  
  125. #endif  /* RTE_Compiler_IO_STDxxx_ITM */
  126.  
  127.  
  128. /**
  129.   Get a character from the stdio
  130.  
  131.   \return     The next character from the input, or -1 on read error.
  132. */
  133. #if   defined(RTE_Compiler_IO_STDIN)
  134. #if   defined(RTE_Compiler_IO_STDIN_User)
  135. extern int stdin_getchar (void);
  136. #elif defined(RTE_Compiler_IO_STDIN_ITM)
  137. static int stdin_getchar (void) {
  138.   int32_t ch;
  139.  
  140.   do {
  141.     ch = ITM_ReceiveChar();
  142.   } while (ch == -1);
  143.   return (ch);
  144. }
  145. #elif defined(RTE_Compiler_IO_STDIN_BKPT)
  146. static int stdin_getchar (void) {
  147.   int32_t ch = -1;
  148.  
  149.   __asm("BKPT 0");
  150.   return (ch);
  151. }
  152. #endif
  153. #endif
  154.  
  155.  
  156. /**
  157.   Put a character to the stdout
  158.  
  159.   \param[in]   ch  Character to output
  160.   \return          The character written, or -1 on write error.
  161. */
  162. #if   defined(RTE_Compiler_IO_STDOUT)
  163. #if   defined(RTE_Compiler_IO_STDOUT_User)
  164. extern int stdout_putchar (int ch);
  165. #elif defined(RTE_Compiler_IO_STDOUT_ITM)
  166. static int stdout_putchar (int ch) {
  167.   return (ITM_SendChar(ch));
  168. }
  169. #elif defined(RTE_Compiler_IO_STDOUT_EVR)
  170. static int stdout_putchar (int ch) {
  171.   static uint32_t index = 0U;
  172.   static uint8_t  buffer[8];
  173.  
  174.   if (index >= 8U) {
  175.     return (-1);
  176.   }
  177.   buffer[index++] = (uint8_t)ch;
  178.   if ((index == 8U) || (ch == '\n')) {
  179.     EventRecordData(EventID(EventLevelOp, 0xFE, 0x00), buffer, index);
  180.     index = 0U;
  181.   }
  182.   return (ch);
  183. }
  184. #elif defined(RTE_Compiler_IO_STDOUT_BKPT)
  185. static int stdout_putchar (int ch) {
  186.   __asm("BKPT 0");
  187.   return (ch);
  188. }
  189. #endif
  190. #endif
  191.  
  192.  
  193. /**
  194.   Put a character to the stderr
  195.  
  196.   \param[in]   ch  Character to output
  197.   \return          The character written, or -1 on write error.
  198. */
  199. #if   defined(RTE_Compiler_IO_STDERR)
  200. #if   defined(RTE_Compiler_IO_STDERR_User)
  201. extern int stderr_putchar (int ch);
  202. #elif defined(RTE_Compiler_IO_STDERR_ITM)
  203. static int stderr_putchar (int ch) {
  204.   return (ITM_SendChar(ch));
  205. }
  206. #elif defined(RTE_Compiler_IO_STDERR_BKPT)
  207. static int stderr_putchar (int ch) {
  208.   __asm("BKPT 0");
  209.   return (ch);
  210. }
  211. #endif
  212. #endif
  213.  
  214.  
  215. #ifdef __MICROLIB
  216.  
  217.  
  218. #ifdef RTE_Compiler_IO_STDIN
  219. static int getchar_undo =  0;
  220. static int getchar_ch   = -1;
  221. #endif
  222.  
  223.  
  224. /**
  225.    Writes the character specified by c (converted to an unsigned char) to
  226.    the output stream pointed to by stream, at the position indicated by the
  227.    associated file position indicator (if defined), and advances the
  228.    indicator appropriately. If the file position indicator is not defined,
  229.    the character is appended to the output stream.
  230.  
  231.   \param[in] c       Character
  232.   \param[in] stream  Stream handle
  233.  
  234.   \return    The character written. If a write error occurs, the error
  235.              indicator is set and fputc returns EOF.
  236. */
  237. __attribute__((weak))
  238. int fputc (int c, FILE * stream) {
  239. #if (!defined(RTE_Compiler_IO_STDOUT) && !defined(RTE_Compiler_IO_STDERR))
  240.   (void)c;
  241.   (void)stream;
  242. #endif
  243.  
  244. #ifdef RTE_Compiler_IO_STDOUT
  245.   if (stream == &__stdout) {
  246. #if (STDOUT_CR_LF != 0)
  247.     if (== '\n') stdout_putchar('\r');
  248. #endif
  249.     return (stdout_putchar(c));
  250.   }
  251. #endif
  252.  
  253. #ifdef RTE_Compiler_IO_STDERR
  254.   if (stream == &__stderr) {
  255. #if (STDERR_CR_LF != 0)
  256.     if (== '\n') stderr_putchar('\r');
  257. #endif
  258.     return (stderr_putchar(c));
  259.   }
  260. #endif
  261.  
  262.   return (-1);
  263. }
  264.  
  265.  
  266. /**
  267.    Obtains the next character (if present) as an unsigned char converted to
  268.    an int, from the input stream pointed to by stream, and advances the
  269.    associated file position indicator (if defined).
  270.  
  271.   \param[in] stream  Stream handle
  272.  
  273.   \return    The next character from the input stream pointed to by stream.
  274.              If the stream is at end-of-file, the end-of-file indicator is
  275.              set and fgetc returns EOF. If a read error occurs, the error
  276.              indicator is set and fgetc returns EOF.
  277. */
  278. __attribute__((weak))
  279. int fgetc (FILE * stream) {
  280. #ifdef RTE_Compiler_IO_STDIN
  281.   int ch;
  282.  
  283.   if (stream == &__stdin) {
  284.     if (getchar_undo) {
  285.       ch = getchar_ch;
  286.       getchar_ch = -1;
  287.       getchar_undo = 0;
  288.       return (ch);
  289.     }
  290.     ch = stdin_getchar();
  291. #if (STDIN_ECHO != 0)
  292.     stdout_putchar(ch);
  293. #endif
  294.     getchar_ch = ch;
  295.     return (ch);
  296.   }
  297. #else
  298.   (void)stream;
  299. #endif
  300.  
  301.   return (-1);
  302. }
  303.  
  304.  
  305. /**
  306.    The function __backspace() is used by the scanf family of functions, and must
  307.    be re-implemented if you retarget the stdio arrangements at the fgetc() level.
  308.  
  309.   \param[in] stream  Stream handle
  310.  
  311.   \return    The value returned by __backspace() is either 0 (success) or EOF
  312.              (failure). It returns EOF only if used incorrectly, for example,
  313.              if no characters have been read from the stream. When used 
  314.              correctly, __backspace() must always return 0, because the scanf
  315.              family of functions do not check the error return.
  316. */
  317. __attribute__((weak))
  318. int __backspace(FILE *stream);
  319. int __backspace(FILE *stream) {
  320.  
  321. #ifdef RTE_Compiler_IO_STDIN
  322.   if (stream == &__stdin) {
  323.     if (getchar_ch != -1) {
  324.       getchar_undo = 1;
  325.       return (0);
  326.     }
  327.     return (-1);
  328.   }
  329. #else
  330.   (void)stream;
  331. #endif
  332.  
  333.   return (-1);
  334. }
  335.  
  336.  
  337. /**
  338.   Called from assert() and prints a message on stderr and calls abort().
  339.  
  340.   \param[in] expr  assert expression that was not TRUE
  341.   \param[in] file  source file of the assertion
  342.   \param[in] line  source line of the assertion
  343. */
  344. __attribute__((weak,noreturn))
  345. void __aeabi_assert (const char *expr, const char *file, int line) {
  346.   char str[12], *p;
  347.  
  348.   fputs("*** assertion failed: ", stderr);
  349.   fputs(expr, stderr);
  350.   fputs(", file ", stderr);
  351.   fputs(file, stderr);
  352.   fputs(", line ", stderr);
  353.  
  354.   p = str + sizeof(str);
  355.   *--= '\0';
  356.   *--= '\n';
  357.   while (line > 0) {
  358.     *--= '0' + (line % 10);
  359.     line /= 10;
  360.   }
  361.   fputs(p, stderr);
  362.  
  363.   abort();
  364. }
  365.  
  366.  
  367. __attribute__((weak))
  368. void abort(void) {
  369.   for (;;);
  370. }
  371.  
  372.  
  373. #else  /* __MICROLIB */
  374.  
  375.  
  376. #if (defined(RTE_Compiler_IO_STDIN)  || \
  377.      defined(RTE_Compiler_IO_STDOUT) || \
  378.      defined(RTE_Compiler_IO_STDERR) || \
  379.      defined(RTE_Compiler_IO_File))
  380. #define RETARGET_SYS
  381.  
  382. /* IO device file handles. */
  383. #define FH_STDIN    0x8001
  384. #define FH_STDOUT   0x8002
  385. #define FH_STDERR   0x8003
  386. // User defined ...
  387.  
  388. /* Standard IO device name defines. */
  389. const char __stdin_name[]  = ":STDIN";
  390. const char __stdout_name[] = ":STDOUT";
  391. const char __stderr_name[] = ":STDERR";
  392.  
  393. #endif
  394.  
  395.  
  396. /**
  397.   Defined in rt_sys.h, this function opens a file.
  398.  
  399.   The _sys_open() function is required by fopen() and freopen(). These
  400.   functions in turn are required if any file input/output function is to
  401.   be used.
  402.   The openmode parameter is a bitmap whose bits mostly correspond directly to
  403.   the ISO mode specification. Target-dependent extensions are possible, but
  404.   freopen() must also be extended.
  405.  
  406.   \param[in] name     File name
  407.   \param[in] openmode Mode specification bitmap
  408.  
  409.   \return    The return value is –1 if an error occurs.
  410. */
  411. #ifdef RETARGET_SYS
  412. __attribute__((weak))
  413. FILEHANDLE _sys_open (const char *name, int openmode) {
  414. #if (!defined(RTE_Compiler_IO_File))
  415.   (void)openmode;
  416. #endif
  417.  
  418.   if (name == NULL) {
  419.     return (-1);
  420.   }
  421.  
  422.   if (name[0] == ':') {
  423.     if (strcmp(name, ":STDIN") == 0) {
  424.       return (FH_STDIN);
  425.     }
  426.     if (strcmp(name, ":STDOUT") == 0) {
  427.       return (FH_STDOUT);
  428.     }
  429.     if (strcmp(name, ":STDERR") == 0) {
  430.       return (FH_STDERR);
  431.     }
  432.     return (-1);
  433.   }
  434.  
  435. #ifdef RTE_Compiler_IO_File
  436. #ifdef RTE_Compiler_IO_File_FS
  437.   return (__sys_open(name, openmode));
  438. #endif
  439. #else
  440.   return (-1);
  441. #endif
  442. }
  443. #endif
  444.  
  445.  
  446. /**
  447.   Defined in rt_sys.h, this function closes a file previously opened
  448.   with _sys_open().
  449.   
  450.   This function must be defined if any input/output function is to be used.
  451.  
  452.   \param[in] fh File handle
  453.  
  454.   \return    The return value is 0 if successful. A nonzero value indicates
  455.              an error.
  456. */
  457. #ifdef RETARGET_SYS
  458. __attribute__((weak))
  459. int _sys_close (FILEHANDLE fh) {
  460.  
  461.   switch (fh) {
  462.     case FH_STDIN:
  463.       return (0);
  464.     case FH_STDOUT:
  465.       return (0);
  466.     case FH_STDERR:
  467.       return (0);
  468.   }
  469.  
  470. #ifdef RTE_Compiler_IO_File
  471. #ifdef RTE_Compiler_IO_File_FS
  472.   return (__sys_close(fh));
  473. #endif
  474. #else
  475.   return (-1);
  476. #endif
  477. }
  478. #endif
  479.  
  480.  
  481. /**
  482.   Defined in rt_sys.h, this function writes the contents of a buffer to a file
  483.   previously opened with _sys_open().
  484.  
  485.   \note The mode parameter is here for historical reasons. It contains
  486.         nothing useful and must be ignored.
  487.  
  488.   \param[in] fh   File handle
  489.   \param[in] buf  Data buffer
  490.   \param[in] len  Data length
  491.   \param[in] mode Ignore this parameter
  492.  
  493.   \return    The return value is either:
  494.              - a positive number representing the number of characters not
  495.                written (so any nonzero return value denotes a failure of
  496.                some sort)
  497.              - a negative number indicating an error.
  498. */
  499. #ifdef RETARGET_SYS
  500. __attribute__((weak))
  501. int _sys_write (FILEHANDLE fh, const uint8_t *buf, uint32_t len, int mode) {
  502. #if (defined(RTE_Compiler_IO_STDOUT) || defined(RTE_Compiler_IO_STDERR))
  503.   int ch;
  504. #elif (!defined(RTE_Compiler_IO_File))
  505.   (void)buf;
  506.   (void)len;
  507. #endif
  508.   (void)mode;
  509.  
  510.   switch (fh) {
  511.     case FH_STDIN:
  512.       return (-1);
  513.     case FH_STDOUT:
  514. #ifdef RTE_Compiler_IO_STDOUT
  515.       for (; len; len--) {
  516.         ch = *buf++;
  517. #if (STDOUT_CR_LF != 0)
  518.         if (ch == '\n') stdout_putchar('\r');
  519. #endif
  520.         stdout_putchar(ch);
  521.       }
  522. #endif
  523.       return (0);
  524.     case FH_STDERR:
  525. #ifdef RTE_Compiler_IO_STDERR
  526.       for (; len; len--) {
  527.         ch = *buf++;
  528. #if (STDERR_CR_LF != 0)
  529.         if (ch == '\n') stderr_putchar('\r');
  530. #endif
  531.         stderr_putchar(ch);
  532.       }
  533. #endif
  534.       return (0);
  535.   }
  536.  
  537. #ifdef RTE_Compiler_IO_File
  538. #ifdef RTE_Compiler_IO_File_FS
  539.   return (__sys_write(fh, buf, len));
  540. #endif
  541. #else
  542.   return (-1);
  543. #endif
  544. }
  545. #endif
  546.  
  547.  
  548. /**
  549.   Defined in rt_sys.h, this function reads the contents of a file into a buffer.
  550.  
  551.   Reading up to and including the last byte of data does not turn on the EOF
  552.   indicator. The EOF indicator is only reached when an attempt is made to read
  553.   beyond the last byte of data. The target-independent code is capable of
  554.   handling:
  555.     - the EOF indicator being returned in the same read as the remaining bytes
  556.       of data that precede the EOF
  557.     - the EOF indicator being returned on its own after the remaining bytes of
  558.       data have been returned in a previous read.
  559.  
  560.   \note The mode parameter is here for historical reasons. It contains
  561.         nothing useful and must be ignored.
  562.  
  563.   \param[in] fh   File handle
  564.   \param[in] buf  Data buffer
  565.   \param[in] len  Data length
  566.   \param[in] mode Ignore this parameter
  567.  
  568.   \return     The return value is one of the following:
  569.               - The number of bytes not read (that is, len - result number of
  570.                 bytes were read).
  571.               - An error indication.
  572.               - An EOF indicator. The EOF indication involves the setting of
  573.                 0x80000000 in the normal result.
  574. */
  575. #ifdef RETARGET_SYS
  576. __attribute__((weak))
  577. int _sys_read (FILEHANDLE fh, uint8_t *buf, uint32_t len, int mode) {
  578. #ifdef RTE_Compiler_IO_STDIN
  579.   int ch;
  580. #elif (!defined(RTE_Compiler_IO_File))
  581.   (void)buf;
  582.   (void)len;
  583. #endif
  584.   (void)mode;
  585.  
  586.   switch (fh) {
  587.     case FH_STDIN:
  588. #ifdef RTE_Compiler_IO_STDIN
  589.       ch = stdin_getchar();
  590.       if (ch < 0) {
  591.         return ((int)(len | 0x80000000U));
  592.       }
  593.       *buf++ = (uint8_t)ch;
  594. #if (STDIN_ECHO != 0)
  595.       stdout_putchar(ch);
  596. #endif
  597.       len--;
  598.       return ((int)(len));
  599. #else
  600.       return ((int)(len | 0x80000000U));
  601. #endif
  602.     case FH_STDOUT:
  603.       return (-1);
  604.     case FH_STDERR:
  605.       return (-1);
  606.   }
  607.  
  608. #ifdef RTE_Compiler_IO_File
  609. #ifdef RTE_Compiler_IO_File_FS
  610.   return (__sys_read(fh, buf, len));
  611. #endif
  612. #else
  613.   return (-1);
  614. #endif
  615. }
  616. #endif
  617.  
  618.  
  619. /**
  620.   Defined in rt_sys.h, this function writes a character to the console. The
  621.   console might have been redirected. You can use this function as a last
  622.   resort error handling routine.
  623.   
  624.   The default implementation of this function uses semihosting.
  625.   You can redefine this function, or __raise(), even if there is no other
  626.   input/output. For example, it might write an error message to a log kept
  627.   in nonvolatile memory.
  628.  
  629.   \param[in] ch character to write
  630. */
  631. #if   defined(RTE_Compiler_IO_TTY)
  632. #if   defined(RTE_Compiler_IO_TTY_User)
  633. extern void ttywrch (int ch);
  634. __attribute__((weak))
  635. void _ttywrch (int ch) {
  636.   ttywrch(ch);
  637. }
  638. #elif defined(RTE_Compiler_IO_TTY_ITM)
  639. __attribute__((weak))
  640. void _ttywrch (int ch) {
  641.   ITM_SendChar(ch);
  642. }
  643. #elif defined(RTE_Compiler_IO_TTY_BKPT)
  644. __attribute__((weak))
  645. void _ttywrch (int ch) {
  646.   (void)ch;
  647.   __asm("BKPT 0");
  648. }
  649. #endif
  650. #endif
  651.  
  652.  
  653. /**
  654.   Defined in rt_sys.h, this function determines if a file handle identifies
  655.   a terminal.
  656.  
  657.   When a file is connected to a terminal device, this function is used to
  658.   provide unbuffered behavior by default (in the absence of a call to
  659.   set(v)buf) and to prohibit seeking.
  660.  
  661.   \param[in] fh File handle
  662.  
  663.   \return    The return value is one of the following values:
  664.              - 0:     There is no interactive device.
  665.              - 1:     There is an interactive device.
  666.              - other: An error occurred.
  667. */
  668. #ifdef RETARGET_SYS
  669. __attribute__((weak))
  670. int _sys_istty (FILEHANDLE fh) {
  671.  
  672.   switch (fh) {
  673.     case FH_STDIN:
  674.       return (1);
  675.     case FH_STDOUT:
  676.       return (1);
  677.     case FH_STDERR:
  678.       return (1);
  679.   }
  680.  
  681.   return (0);
  682. }
  683. #endif
  684.  
  685.  
  686. /**
  687.   Defined in rt_sys.h, this function puts the file pointer at offset pos from
  688.   the beginning of the file.
  689.  
  690.   This function sets the current read or write position to the new location pos
  691.   relative to the start of the current file fh.
  692.  
  693.   \param[in] fh  File handle
  694.   \param[in] pos File pointer offset
  695.  
  696.   \return    The result is:
  697.              - non-negative if no error occurs
  698.              - negative if an error occurs
  699. */
  700. #ifdef RETARGET_SYS
  701. __attribute__((weak))
  702. int _sys_seek (FILEHANDLE fh, long pos) {
  703. #if (!defined(RTE_Compiler_IO_File))
  704.   (void)pos;
  705. #endif
  706.  
  707.   switch (fh) {
  708.     case FH_STDIN:
  709.       return (-1);
  710.     case FH_STDOUT:
  711.       return (-1);
  712.     case FH_STDERR:
  713.       return (-1);
  714.   }
  715.  
  716. #ifdef RTE_Compiler_IO_File
  717. #ifdef RTE_Compiler_IO_File_FS
  718.   return (__sys_seek(fh, (uint32_t)pos));
  719. #endif
  720. #else
  721.   return (-1);
  722. #endif
  723. }
  724. #endif
  725.  
  726.  
  727. /**
  728.   Defined in rt_sys.h, this function returns the current length of a file.
  729.  
  730.   This function is used by _sys_seek() to convert an offset relative to the
  731.   end of a file into an offset relative to the beginning of the file.
  732.   You do not have to define _sys_flen() if you do not intend to use fseek().
  733.   If you retarget at system _sys_*() level, you must supply _sys_flen(),
  734.   even if the underlying system directly supports seeking relative to the
  735.   end of a file.
  736.  
  737.   \param[in] fh File handle
  738.  
  739.   \return    This function returns the current length of the file fh,
  740.              or a negative error indicator.
  741. */
  742. #ifdef RETARGET_SYS
  743. __attribute__((weak))
  744. long _sys_flen (FILEHANDLE fh) {
  745.  
  746.   switch (fh) {
  747.     case FH_STDIN:
  748.       return (0);
  749.     case FH_STDOUT:
  750.       return (0);
  751.     case FH_STDERR:
  752.       return (0);
  753.   }
  754.  
  755. #ifdef RTE_Compiler_IO_File
  756. #ifdef RTE_Compiler_IO_File_FS
  757.   return (__sys_flen(fh));
  758. #endif
  759. #else
  760.   return (0);
  761. #endif
  762. }
  763. #endif
  764.  
  765.  
  766. #endif  /* __MICROLIB */
  767.  

Editor

You can edit this paste and save as new:


File Description
  • code
  • Paste Code
  • 04 Feb-2024
  • 19.84 Kb
You can Share it: