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