read.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * \file
  3. *
  4. * \brief System-specific implementation of the \ref _read function used by
  5. * the standard library.
  6. *
  7. * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * 4. This software may only be redistributed and used in connection with an
  27. * Atmel microcontroller product.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  32. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  33. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * \asf_license_stop
  42. *
  43. */
  44. /*
  45. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  46. */
  47. #include "compiler.h"
  48. /**
  49. * \defgroup group_common_utils_stdio Standard I/O (stdio)
  50. *
  51. * Common standard I/O driver that implements the stdio
  52. * read and write functions on AVR and SAM devices.
  53. *
  54. * \{
  55. */
  56. extern volatile void *volatile stdio_base;
  57. void (*ptr_get)(void volatile*, char*);
  58. // IAR common implementation
  59. #if ( defined(__ICCAVR32__) || defined(__ICCAVR__) || defined(__ICCARM__) )
  60. #include <yfuns.h>
  61. _STD_BEGIN
  62. #pragma module_name = "?__read"
  63. /*! \brief Reads a number of bytes, at most \a size, into the memory area
  64. * pointed to by \a buffer.
  65. *
  66. * \param handle File handle to read from.
  67. * \param buffer Pointer to buffer to write read bytes to.
  68. * \param size Number of bytes to read.
  69. *
  70. * \return The number of bytes read, \c 0 at the end of the file, or
  71. * \c _LLIO_ERROR on failure.
  72. */
  73. size_t __read(int handle, unsigned char *buffer, size_t size)
  74. {
  75. int nChars = 0;
  76. // This implementation only reads from stdin.
  77. // For all other file handles, it returns failure.
  78. if (handle != _LLIO_STDIN) {
  79. return _LLIO_ERROR;
  80. }
  81. for (; size > 0; --size) {
  82. ptr_get(stdio_base, (char*)buffer);
  83. buffer++;
  84. nChars++;
  85. }
  86. return nChars;
  87. }
  88. /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10
  89. * the implementation is empty to be compatible with old IAR version.
  90. */
  91. int __close(int handle)
  92. {
  93. UNUSED(handle);
  94. return 0;
  95. }
  96. /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10
  97. * the implementation is empty to be compatible with old IAR version.
  98. */
  99. int remove(const char* val)
  100. {
  101. UNUSED(val);
  102. return 0;
  103. }
  104. /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10
  105. * the implementation is empty to be compatible with old IAR version.
  106. */
  107. long __lseek(int handle, long val, int val2)
  108. {
  109. UNUSED(handle);
  110. UNUSED(val2);
  111. return val;
  112. }
  113. _STD_END
  114. // GCC AVR32 and SAM implementation
  115. #elif (defined(__GNUC__) && !XMEGA && !MEGA)
  116. int __attribute__((weak))
  117. _read (int file, char * ptr, int len); // Remove GCC compiler warning
  118. int __attribute__((weak))
  119. _read (int file, char * ptr, int len)
  120. {
  121. int nChars = 0;
  122. if (file != 0) {
  123. return -1;
  124. }
  125. for (; len > 0; --len) {
  126. ptr_get(stdio_base, ptr);
  127. ptr++;
  128. nChars++;
  129. }
  130. return nChars;
  131. }
  132. // GCC AVR implementation
  133. #elif (defined(__GNUC__) && (XMEGA || MEGA) )
  134. int _read (int *f); // Remove GCC compiler warning
  135. int _read (int *f)
  136. {
  137. char c;
  138. ptr_get(stdio_base,&c);
  139. return c;
  140. }
  141. #endif
  142. /**
  143. * \}
  144. */