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