1 /*
2  * FreeRTOS Kernel V11.1.0
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 
29 /*This file is prepared for Doxygen automatic documentation generation.*/
30 
31 /*! \file *********************************************************************
32 *
33 * \brief System-specific implementation of the \ref __write function used by
34 *         the standard library.
35 *
36 * - Compiler:           IAR EWAVR32
37 * - Supported devices:  All AVR32 devices with a USART module can be used.
38 * - AppNote:
39 *
40 * \author               Atmel Corporation (Now Microchip):
41 *                                        https://www.microchip.com \n
42 *                       Support and FAQ: https://www.microchip.com/support
43 *
44 ******************************************************************************/
45 
46 /*
47  * Copyright (c) 2007, Atmel Corporation All rights reserved.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions are met:
51  *
52  * 1. Redistributions of source code must retain the above copyright notice,
53  * this list of conditions and the following disclaimer.
54  *
55  * 2. Redistributions in binary form must reproduce the above copyright notice,
56  * this list of conditions and the following disclaimer in the documentation
57  * and/or other materials provided with the distribution.
58  *
59  * 3. The name of ATMEL may not be used to endorse or promote products derived
60  * from this software without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
63  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
64  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
65  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
66  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
67  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
68  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
69  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
71  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 
75 #include <yfuns.h>
76 #include <avr32/io.h>
77 #include "usart.h"
78 
79 
80 _STD_BEGIN
81 
82 
83 #pragma module_name = "?__write"
84 
85 
86 /*! Pointer to the base of the USART module instance to use for stdio. */
87 __no_init volatile avr32_usart_t * volatile stdio_usart_base;
88 
89 
90 /*! \brief Writes a number of bytes, at most \a size, from the memory area
91  *         pointed to by \a buffer.
92  *
93  * If \a buffer is zero then \ref __write performs flushing of internal buffers,
94  * if any. In this case, \a handle can be \c -1 to indicate that all handles
95  * should be flushed.
96  *
97  * \param handle File handle to write to.
98  * \param buffer Pointer to buffer to read bytes to write from.
99  * \param size Number of bytes to write.
100  *
101  * \return The number of bytes written, or \c _LLIO_ERROR on failure.
102  */
__write(int handle,const uint8_t * buffer,size_t size)103 size_t __write( int handle,
104                 const uint8_t * buffer,
105                 size_t size )
106 {
107     size_t nChars = 0;
108 
109     if( buffer == 0 )
110     {
111         /* This means that we should flush internal buffers. */
112         return 0;
113     }
114 
115     /* This implementation only writes to stdout and stderr. */
116     /* For all other file handles, it returns failure. */
117     if( ( handle != _LLIO_STDOUT ) && ( handle != _LLIO_STDERR ) )
118     {
119         return _LLIO_ERROR;
120     }
121 
122     for( ; size != 0; --size )
123     {
124         if( usart_putchar( stdio_usart_base, *buffer++ ) < 0 )
125         {
126             return _LLIO_ERROR;
127         }
128 
129         ++nChars;
130     }
131 
132     return nChars;
133 }
134 
135 
136 _STD_END
137