1 /**
2 * \file
3 *
4 * \brief System-specific implementation of the \ref _read 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 * \defgroup group_common_utils_stdio Standard I/O (stdio)
52 *
53 * Common standard I/O driver that implements the stdio
54 * read and write functions on AVR and SAM devices.
55 *
56 * \{
57 */
58
59 extern volatile void *volatile stdio_base;
60 void (*ptr_get)(void volatile*, char*);
61
62
63 // IAR common implementation
64 #if ( defined(__ICCAVR32__) || defined(__ICCAVR__) || defined(__ICCARM__) )
65
66 #include <yfuns.h>
67
68 _STD_BEGIN
69
70 #pragma module_name = "?__read"
71
72 /*! \brief Reads a number of bytes, at most \a size, into the memory area
73 * pointed to by \a buffer.
74 *
75 * \param handle File handle to read from.
76 * \param buffer Pointer to buffer to write read bytes to.
77 * \param size Number of bytes to read.
78 *
79 * \return The number of bytes read, \c 0 at the end of the file, or
80 * \c _LLIO_ERROR on failure.
81 */
__read(int handle,unsigned char * buffer,size_t size)82 size_t __read(int handle, unsigned char *buffer, size_t size)
83 {
84 int nChars = 0;
85 // This implementation only reads from stdin.
86 // For all other file handles, it returns failure.
87 if (handle != _LLIO_STDIN) {
88 return _LLIO_ERROR;
89 }
90 for (; size > 0; --size) {
91 ptr_get(stdio_base, (char*)buffer);
92 buffer++;
93 nChars++;
94 }
95 return nChars;
96 }
97
98 /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10
99 * the implementation is empty to be compatible with old IAR version.
100 */
__close(int handle)101 int __close(int handle)
102 {
103 UNUSED(handle);
104 return 0;
105 }
106
107 /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10
108 * the implementation is empty to be compatible with old IAR version.
109 */
remove(const char * val)110 int remove(const char* val)
111 {
112 UNUSED(val);
113 return 0;
114 }
115
116 /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10
117 * the implementation is empty to be compatible with old IAR version.
118 */
__lseek(int handle,long val,int val2)119 long __lseek(int handle, long val, int val2)
120 {
121 UNUSED(handle);
122 UNUSED(val2);
123 return val;
124 }
125
126 _STD_END
127
128 // GCC AVR32 and SAM implementation
129 #elif (defined(__GNUC__) && !XMEGA && !MEGA)
130
131 int __attribute__((weak))
132 _read (int file, char * ptr, int len); // Remove GCC compiler warning
133
134 int __attribute__((weak))
135 _read (int file, char * ptr, int len)
136 {
137 int nChars = 0;
138
139 if (file != 0) {
140 return -1;
141 }
142
143 for (; len > 0; --len) {
144 ptr_get(stdio_base, ptr);
145 ptr++;
146 nChars++;
147 }
148 return nChars;
149 }
150
151 // GCC AVR implementation
152 #elif (defined(__GNUC__) && (XMEGA || MEGA) )
153
154 int _read (int *f); // Remove GCC compiler warning
155
156 int _read (int *f)
157 {
158 char c;
159 ptr_get(stdio_base,&c);
160 return c;
161 }
162 #endif
163
164 /**
165 * \}
166 */
167
168