Initial commit
[kk_librfid.git] / firmware / src / os / syscalls.c
blob18f755af8d77ba5f90726881059cc4f83d190ec6
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 /***********************************************************************/
13 #include <stdlib.h>
14 #include <reent.h>
15 #include <sys/stat.h>
17 #include <board.h>
19 static void my_putc(char c)
21 while (!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_DBGU));
22 AT91F_US_PutChar((AT91PS_USART)AT91C_BASE_DBGU, c);
25 static int my_kbhit( void )
27 if ((AT91F_US_RxReady((AT91PS_USART)AT91C_BASE_DBGU)) == 0) return 0;
28 else return 1;
31 static char my_getc( void )
33 return AT91F_US_GetChar((AT91PS_USART)AT91C_BASE_DBGU);
36 _ssize_t _read_r(
37 struct _reent *r,
38 int file,
39 void *ptr,
40 size_t len)
42 char c;
43 unsigned int i;
44 unsigned char *p;
46 p = (unsigned char*)ptr;
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;
57 *p++ = c;
58 ////// uart0_putc(c);
60 return len - i;
64 _ssize_t _write_r (
65 struct _reent *r,
66 int file,
67 const void *ptr,
68 size_t len)
70 int i;
71 const unsigned char *p;
73 p = (const unsigned char*) ptr;
75 for (i = 0; i < len; i++) {
76 if (*p == '\n' ) my_putc('\r');
77 my_putc(*p++);
80 return len;
84 int _close_r(
85 struct _reent *r,
86 int file)
88 return 0;
92 _off_t _lseek_r(
93 struct _reent *r,
94 int file,
95 _off_t ptr,
96 int dir)
98 return (_off_t)0; /* Always indicate we are at file beginning. */
102 int _fstat_r(
103 struct _reent *r,
104 int file,
105 struct stat *st)
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. */
113 return 0;
117 int isatty(int file); /* avoid warning */
119 int isatty(int file)
121 return 1;
125 #if 0
126 static void _exit (int n) {
127 label: goto label; /* endless loop */
129 #endif
132 /* "malloc clue function" from newlib-lpc/Keil-Demo/"generic" */
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). */
140 static char *heap_ptr; /* Points to current end of the heap. */
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.
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.
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.
156 void * _sbrk_r(
157 struct _reent *_s_r,
158 ptrdiff_t nbytes)
160 char *base; /* errno should be set to ENOMEM on error */
162 if (!heap_ptr) { /* Initialize if first time through. */
163 heap_ptr = end;
165 base = heap_ptr; /* Point to end of heap. */
166 heap_ptr += nbytes; /* Increase heap. */
168 return base; /* Return pointer to start of new heap area. */