]> git.sur5r.net Git - freertos/blob - Source/portable/IAR/AVR32_UC3/write.c
Add AVR32 port and demo files.
[freertos] / Source / portable / IAR / AVR32_UC3 / write.c
1 /* This source file is part of the ATMEL FREERTOS-0.9.0 Release */\r
2 \r
3 /*This file is prepared for Doxygen automatic documentation generation.*/\r
4 /*! \file *********************************************************************\r
5  *\r
6  * \brief System-specific implementation of the \ref __write function used by\r
7           the standard library.\r
8  *\r
9  * - Compiler:           IAR EWAVR32\r
10  * - Supported devices:  All AVR32 devices with a USART module can be used.\r
11  * - AppNote:\r
12  *\r
13  * \author               Atmel Corporation: http://www.atmel.com \n\r
14  *                       Support email: avr32@atmel.com\r
15  *\r
16  ******************************************************************************/\r
17 \r
18 /* Copyright (c) 2007, Atmel Corporation All rights reserved.\r
19  *\r
20  * Redistribution and use in source and binary forms, with or without\r
21  * modification, are permitted provided that the following conditions are met:\r
22  *\r
23  * 1. Redistributions of source code must retain the above copyright notice,\r
24  * this list of conditions and the following disclaimer.\r
25  *\r
26  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
27  * this list of conditions and the following disclaimer in the documentation\r
28  * and/or other materials provided with the distribution.\r
29  *\r
30  * 3. The name of ATMEL may not be used to endorse or promote products derived\r
31  * from this software without specific prior written permission.\r
32  *\r
33  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
35  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND\r
36  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,\r
37  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
38  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
39  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
42  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
43  */\r
44 \r
45 \r
46 #include <yfuns.h>\r
47 #include <avr32/iouc3a0512.h>\r
48 #include "usart.h"\r
49 \r
50 \r
51 _STD_BEGIN\r
52 \r
53 \r
54 #pragma module_name = "?__write"\r
55 \r
56 \r
57 //! Pointer to the base of the USART module instance to use for stdio.\r
58 __no_init volatile avr32_usart_t *volatile stdio_usart_base;\r
59 \r
60 \r
61 /*! \brief Writes a number of bytes, at most \a size, from the memory area\r
62  *         pointed to by \a buffer.\r
63  *\r
64  * If \a buffer is zero then \ref __write performs flushing of internal buffers,\r
65  * if any. In this case, \a handle can be \c -1 to indicate that all handles\r
66  * should be flushed.\r
67  *\r
68  * \param handle File handle to write to.\r
69  * \param buffer Pointer to buffer to read bytes to write from.\r
70  * \param size Number of bytes to write.\r
71  *\r
72  * \return The number of bytes written, or \c _LLIO_ERROR on failure.\r
73  */\r
74 size_t __write(int handle, const unsigned char *buffer, size_t size)\r
75 {\r
76   size_t nChars = 0;\r
77 \r
78   if (buffer == 0)\r
79   {\r
80     // This means that we should flush internal buffers.\r
81     return 0;\r
82   }\r
83 \r
84   // This implementation only writes to stdout and stderr.\r
85   // For all other file handles, it returns failure.\r
86   if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)\r
87   {\r
88     return _LLIO_ERROR;\r
89   }\r
90 \r
91   for (; size != 0; --size)\r
92   {\r
93     if (usart_putchar(stdio_usart_base, *buffer++) < 0)\r
94     {\r
95       return _LLIO_ERROR;\r
96     }\r
97 \r
98     ++nChars;\r
99   }\r
100 \r
101   return nChars;\r
102 }\r
103 \r
104 \r
105 _STD_END\r