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