write.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * \file
  3. *
  4. * \brief System-specific implementation of the \ref _write 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. * \addtogroup group_common_utils_stdio
  50. *
  51. * \{
  52. */
  53. volatile void *volatile stdio_base;
  54. int (*ptr_put)(void volatile*, char);
  55. #if ( defined(__ICCAVR32__) || defined(__ICCAVR__) || defined(__ICCARM__))
  56. #include <yfuns.h>
  57. _STD_BEGIN
  58. #pragma module_name = "?__write"
  59. /*! \brief Writes a number of bytes, at most \a size, from the memory area
  60. * pointed to by \a buffer.
  61. *
  62. * If \a buffer is zero then \ref __write performs flushing of internal buffers,
  63. * if any. In this case, \a handle can be \c -1 to indicate that all handles
  64. * should be flushed.
  65. *
  66. * \param handle File handle to write to.
  67. * \param buffer Pointer to buffer to read bytes to write from.
  68. * \param size Number of bytes to write.
  69. *
  70. * \return The number of bytes written, or \c _LLIO_ERROR on failure.
  71. */
  72. size_t __write(int handle, const unsigned char *buffer, size_t size)
  73. {
  74. size_t nChars = 0;
  75. if (buffer == 0) {
  76. // This means that we should flush internal buffers.
  77. return 0;
  78. }
  79. // This implementation only writes to stdout and stderr.
  80. // For all other file handles, it returns failure.
  81. if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR) {
  82. return _LLIO_ERROR;
  83. }
  84. for (; size != 0; --size) {
  85. if (ptr_put(stdio_base, *buffer++) < 0) {
  86. return _LLIO_ERROR;
  87. }
  88. ++nChars;
  89. }
  90. return nChars;
  91. }
  92. _STD_END
  93. #elif (defined(__GNUC__) && !XMEGA && !MEGA)
  94. int __attribute__((weak))
  95. _write (int file, const char *ptr, int len);
  96. int __attribute__((weak))
  97. _write (int file, const char *ptr, int len)
  98. {
  99. int nChars = 0;
  100. if ((file != 1) && (file != 2) && (file!=3)) {
  101. return -1;
  102. }
  103. for (; len != 0; --len) {
  104. if (ptr_put(stdio_base, *ptr++) < 0) {
  105. return -1;
  106. }
  107. ++nChars;
  108. }
  109. return nChars;
  110. }
  111. #elif (defined(__GNUC__) && (XMEGA || MEGA))
  112. int _write (char c, int *f);
  113. int _write (char c, int *f)
  114. {
  115. if (ptr_put(stdio_base, c) < 0) {
  116. return -1;
  117. }
  118. return 1;
  119. }
  120. #endif
  121. /**
  122. * \}
  123. */