1 /*******************************************************************************
\r
2 * (c) Copyright 2009-2013 Microsemi SoC Products Group. All rights reserved.
\r
4 * Stubs for Newlib system calls.
\r
6 * SVN $Revision: 5269 $
\r
7 * SVN $Date: 2013-03-21 20:53:38 +0000 (Thu, 21 Mar 2013) $
\r
10 #include <sys/unistd.h>
\r
11 #include <sys/stat.h>
\r
12 #include <sys/times.h>
\r
15 /*==============================================================================
\r
16 * Redirection of standard output to a SmartFusion2 MSS UART.
\r
17 *------------------------------------------------------------------------------
\r
18 * A default implementation for the redirection of the output of printf() to a
\r
19 * UART is provided at the bottom of this file. This redirection is enabled by
\r
20 * adding the symbol/define MICROSEMI_STDIO_THRU_MMUART0 or
\r
21 * MICROSEMI_STDIO_THRU_MMUART0 to your project settings and specifying the baud
\r
22 * rate using the MICROSEMI_STDIO_BAUD_RATE define.
\r
24 #ifdef MICROSEMI_STDIO_THRU_MMUART0
\r
25 #ifndef MICROSEMI_STDIO_THRU_UART
\r
26 #define MICROSEMI_STDIO_THRU_UART
\r
28 #endif /* MICROSEMI_STDIO_THRU_MMUART0 */
\r
30 #ifdef MICROSEMI_STDIO_THRU_MMUART1
\r
31 #ifndef MICROSEMI_STDIO_THRU_UART
\r
32 #define MICROSEMI_STDIO_THRU_UART
\r
34 #endif /* MICROSEMI_STDIO_THRU_MMUART1 */
\r
37 * Select which MMUART will be used for stdio and what baud rate will be used.
\r
38 * Default to 57600 baud if no baud rate is specified using the
\r
39 * MICROSEMI_STDIO_BAUD_RATE #define.
\r
41 #ifdef MICROSEMI_STDIO_THRU_UART
\r
42 #include "../../drivers/mss_uart/mss_uart.h"
\r
44 #ifndef MICROSEMI_STDIO_BAUD_RATE
\r
45 #define MICROSEMI_STDIO_BAUD_RATE MSS_UART_57600_BAUD
\r
48 #ifdef MICROSEMI_STDIO_THRU_MMUART0
\r
49 static mss_uart_instance_t * const gp_my_uart = &g_mss_uart0;
\r
51 static mss_uart_instance_t * const gp_my_uart = &g_mss_uart1;
\r
54 /*------------------------------------------------------------------------------
\r
55 * Global flag used to indicate if the UART driver needs to be initialized.
\r
57 static int g_stdio_uart_init_done = 0;
\r
59 #endif /* MICROSEMI_STDIO_THRU_UART */
\r
61 /*==============================================================================
\r
62 * Environment variables.
\r
63 * A pointer to a list of environment variables and their values. For a minimal
\r
64 * environment, this empty list is adequate:
\r
66 char *__env[1] = { 0 };
\r
67 char **environ = __env;
\r
69 /*==============================================================================
\r
72 int _close(int file)
\r
77 /*==============================================================================
\r
78 * Transfer control to a new process.
\r
80 int _execve(char *name, char **argv, char **env)
\r
86 /*==============================================================================
\r
87 * Exit a program without cleaning up files.
\r
89 void _exit( int code )
\r
91 /* Should we force a system reset? */
\r
98 /*==============================================================================
\r
99 * Create a new process.
\r
107 /*==============================================================================
\r
108 * Status of an open file.
\r
110 int _fstat(int file, struct stat *st)
\r
112 st->st_mode = S_IFCHR;
\r
116 /*==============================================================================
\r
124 /*==============================================================================
\r
125 * Query whether output stream is a terminal.
\r
127 int _isatty(int file)
\r
132 /*==============================================================================
\r
135 int _kill(int pid, int sig)
\r
141 /*==============================================================================
\r
142 * Establish a new name for an existing file.
\r
144 int _link(char *old, char *new)
\r
150 /*==============================================================================
\r
151 * Set position in a file.
\r
153 int _lseek(int file, int ptr, int dir)
\r
158 /*==============================================================================
\r
161 int _open(const char *name, int flags, int mode)
\r
166 /*==============================================================================
\r
167 * Read from a file.
\r
169 int _read(int file, char *ptr, int len)
\r
174 /*==============================================================================
\r
175 * Write to a file. libc subroutines will use this system routine for output to
\r
176 * all files, including stdout
\97so if you need to generate any output, for
\r
177 * example to a serial port for debugging, you should make your minimal write
\r
178 * capable of doing this.
\r
180 int _write_r( void * reent, int file, char * ptr, int len )
\r
182 #ifdef MICROSEMI_STDIO_THRU_UART
\r
183 /*--------------------------------------------------------------------------
\r
184 * Initialize the UART driver if it is the first time this function is
\r
187 if(!g_stdio_uart_init_done)
\r
189 MSS_UART_init(gp_my_uart,
\r
190 MICROSEMI_STDIO_BAUD_RATE,
\r
191 MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY);
\r
193 g_stdio_uart_init_done = 1;
\r
196 /*--------------------------------------------------------------------------
\r
197 * Output text to the UART.
\r
199 MSS_UART_polled_tx(gp_my_uart, (uint8_t *)ptr, len);
\r
202 #else /* MICROSEMI_STDIO_THRU_UART */
\r
204 #endif /* MICROSEMI_STDIO_THRU_UART */
\r
207 /*==============================================================================
\r
208 * Increase program data space. As malloc and related functions depend on this,
\r
209 * it is useful to have a working implementation. The following suffices for a
\r
210 * standalone system; it exploits the symbol _end automatically defined by the
\r
213 caddr_t _sbrk(int incr)
\r
215 extern char _end; /* Defined by the linker */
\r
216 static char *heap_end;
\r
217 char *prev_heap_end;
\r
225 prev_heap_end = heap_end;
\r
226 asm volatile ("MRS %0, msp" : "=r" (stack_ptr) );
\r
227 if (heap_end + incr > stack_ptr)
\r
229 _write_r ((void *)0, 1, "Heap and stack collision\n", 25);
\r
234 return (caddr_t) prev_heap_end;
\r
237 /*==============================================================================
\r
238 * Status of a file (by name).
\r
240 int _stat(char *file, struct stat *st)
\r
242 st->st_mode = S_IFCHR;
\r
246 /*==============================================================================
\r
247 * Timing information for current process.
\r
249 int _times(struct tms *buf)
\r
254 /*==============================================================================
\r
255 * Remove a file's directory entry.
\r
257 int _unlink(char *name)
\r
263 /*==============================================================================
\r
264 * Wait for a child process.
\r
266 int _wait(int *status)
\r