Project

General

Profile

Download (3.85 KB) Statistics
| Branch: | Tag: | Revision:
1
/***********************************************************************/
2
/*                                                                     */
3
/*  SYSCALLS.C:  System Calls                                          */
4
/*  most of this is from newlib-lpc and a Keil-demo                    */
5
/*                                                                     */
6
/*  These are "reentrant functions" as needed by                       */
7
/*  the WinARM-newlib-config, see newlib-manual.                       */
8
/*  Collected and modified by Martin Thomas                            */
9
/*                                                                     */
10
/***********************************************************************/
11

    
12

    
13
#include <stdlib.h>
14
#include <reent.h>
15
#include <sys/stat.h>
16

    
17
#include <board.h>
18

    
19
static void my_putc(char c) 
20
{
21
	while (!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_DBGU));
22
	AT91F_US_PutChar((AT91PS_USART)AT91C_BASE_DBGU, c);
23
}
24

    
25
static int  my_kbhit( void ) 
26
{
27
	if ((AT91F_US_RxReady((AT91PS_USART)AT91C_BASE_DBGU)) == 0) return 0;
28
	else return 1;
29
}
30

    
31
static char my_getc( void )
32
{
33
	return AT91F_US_GetChar((AT91PS_USART)AT91C_BASE_DBGU);
34
}
35

    
36
_ssize_t _read_r(
37
    struct _reent *r, 
38
    int file, 
39
    void *ptr, 
40
    size_t len)
41
{
42
	char c;
43
	unsigned int  i;
44
	unsigned char *p;
45
	
46
	p = (unsigned char*)ptr;
47
	
48
	for (i = 0; i < len; i++) {
49
		// c = uart0Getch();
50
		// c = uart0GetchW();
51
		while ( !my_kbhit() ) ;
52
		c = (char) my_getc();
53
		if (c == 0x0D) {
54
			*p='\0';
55
			break;
56
		}
57
		*p++ = c;
58
		////// uart0_putc(c);
59
	}
60
	return len - i;
61
}
62

    
63

    
64
_ssize_t _write_r (
65
    struct _reent *r, 
66
    int file, 
67
    const void *ptr, 
68
    size_t len)
69
{
70
	int i;
71
	const unsigned char *p;
72
	
73
	p = (const unsigned char*) ptr;
74
	
75
	for (i = 0; i < len; i++) {
76
		if (*p == '\n' ) my_putc('\r');
77
		my_putc(*p++);
78
	}
79
	
80
	return len;
81
}
82

    
83

    
84
int _close_r(
85
    struct _reent *r, 
86
    int file)
87
{
88
	return 0;
89
}
90

    
91

    
92
_off_t _lseek_r(
93
    struct _reent *r, 
94
    int file, 
95
    _off_t ptr, 
96
    int dir)
97
{
98
	return (_off_t)0;	/*  Always indicate we are at file beginning.  */
99
}
100

    
101

    
102
int _fstat_r(
103
    struct _reent *r, 
104
    int file, 
105
    struct stat *st)
106
{
107
	/*  Always set as character device.				*/
108
	st->st_mode = S_IFCHR;
109
	/* assigned to strong type with implicit 	*/
110
	/* signed/unsigned conversion.  Required by 	*/
111
	/* newlib.					*/
112

    
113
	return 0;
114
}
115

    
116

    
117
int isatty(int file); /* avoid warning */
118

    
119
int isatty(int file)
120
{
121
	return 1;
122
}
123

    
124

    
125
#if 0
126
static void _exit (int n) {
127
label:  goto label; /* endless loop */
128
}
129
#endif 
130

    
131

    
132
/* "malloc clue function" from newlib-lpc/Keil-Demo/"generic" */
133

    
134
/**** Locally used variables. ****/
135
// mt: "cleaner": extern char* end;
136
extern char end[];              /*  end is set in the linker command 	*/
137
				/* file and is the end of statically 	*/
138
				/* allocated data (thus start of heap).	*/
139

    
140
static char *heap_ptr;		/* Points to current end of the heap.	*/
141

    
142
/************************** _sbrk_r *************************************
143
 * Support function. Adjusts end of heap to provide more memory to
144
 * memory allocator. Simple and dumb with no sanity checks.
145

    
146
 *  struct _reent *r -- re-entrancy structure, used by newlib to
147
 *                      support multiple threads of operation.
148
 *  ptrdiff_t nbytes -- number of bytes to add.
149
 *                      Returns pointer to start of new heap area.
150
 *
151
 *  Note:  This implementation is not thread safe (despite taking a
152
 *         _reent structure as a parameter).
153
 *         Since _s_r is not used in the current implementation, 
154
 *         the following messages must be suppressed.
155
 */
156
void * _sbrk_r(
157
    struct _reent *_s_r, 
158
    ptrdiff_t nbytes)
159
{
160
	char  *base;		/*  errno should be set to  ENOMEM on error  */
161

    
162
	if (!heap_ptr) {	/*  Initialize if first time through.  */
163
		heap_ptr = end;
164
	}
165
	base = heap_ptr;	/*  Point to end of heap.  */
166
	heap_ptr += nbytes;	/*  Increase heap.  */
167
	
168
	return base;		/*  Return pointer to start of new heap area.  */
169
}
(24-24/40)
Add picture from clipboard (Maximum size: 48.8 MB)