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