read.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT AND BSD-3-Clause
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*This file is prepared for Doxygen automatic documentation generation.*/
  29. /*! \file *********************************************************************
  30. *
  31. * \brief System-specific implementation of the \ref __read function used by
  32. the standard library.
  33. *
  34. * - Compiler: IAR EWAVR32
  35. * - Supported devices: All AVR32 devices with a USART module can be used.
  36. * - AppNote:
  37. *
  38. * \author Atmel Corporation (Now Microchip):
  39. * https://www.microchip.com \n
  40. * Support and FAQ: https://www.microchip.com/support/
  41. *
  42. ******************************************************************************/
  43. /*
  44. * Copyright (c) 2007, Atmel Corporation All rights reserved.
  45. *
  46. * Redistribution and use in source and binary forms, with or without
  47. * modification, are permitted provided that the following conditions are met:
  48. *
  49. * 1. Redistributions of source code must retain the above copyright notice,
  50. * this list of conditions and the following disclaimer.
  51. *
  52. * 2. Redistributions in binary form must reproduce the above copyright notice,
  53. * this list of conditions and the following disclaimer in the documentation
  54. * and/or other materials provided with the distribution.
  55. *
  56. * 3. The name of ATMEL may not be used to endorse or promote products derived
  57. * from this software without specific prior written permission.
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
  60. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  61. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
  62. * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
  63. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  64. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  65. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  66. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  67. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  68. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  69. */
  70. #include <yfuns.h>
  71. #include <avr32/io.h>
  72. #include "usart.h"
  73. _STD_BEGIN
  74. #pragma module_name = "?__read"
  75. extern volatile avr32_usart_t *volatile stdio_usart_base;
  76. /*! \brief Reads a number of bytes, at most \a size, into the memory area
  77. * pointed to by \a buffer.
  78. *
  79. * \param handle File handle to read from.
  80. * \param buffer Pointer to buffer to write read bytes to.
  81. * \param size Number of bytes to read.
  82. *
  83. * \return The number of bytes read, \c 0 at the end of the file, or
  84. * \c _LLIO_ERROR on failure.
  85. */
  86. size_t __read(int handle, uint8_t *buffer, size_t size)
  87. {
  88. int nChars = 0;
  89. // This implementation only reads from stdin.
  90. // For all other file handles, it returns failure.
  91. if (handle != _LLIO_STDIN)
  92. {
  93. return _LLIO_ERROR;
  94. }
  95. for (; size > 0; --size)
  96. {
  97. int c = usart_getchar(stdio_usart_base);
  98. if (c < 0)
  99. break;
  100. *buffer++ = c;
  101. ++nChars;
  102. }
  103. return nChars;
  104. }
  105. _STD_END