|
1 | | -/* |
2 | | - Copyright (c) 2011 Arduino. All right reserved. |
3 | | -
|
4 | | - This library is free software; you can redistribute it and/or |
5 | | - modify it under the terms of the GNU Lesser General Public |
6 | | - License as published by the Free Software Foundation; either |
7 | | - version 2.1 of the License, or (at your option) any later version. |
8 | | -
|
9 | | - This library is distributed in the hope that it will be useful, |
10 | | - but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12 | | - See the GNU Lesser General Public License for more details. |
13 | | -
|
14 | | - You should have received a copy of the GNU Lesser General Public |
15 | | - License along with this library; if not, write to the Free Software |
16 | | - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 | | -*/ |
18 | | - |
19 | 1 | /** |
20 | | - * \file syscalls_sam3.c |
| 2 | + * \file syscalls_stm32.c |
21 | 3 | * |
22 | 4 | * Implementation of newlib syscall. |
23 | 5 | * |
24 | 6 | */ |
25 | 7 |
|
26 | | -/*---------------------------------------------------------------------------- |
27 | | - * Headers |
28 | | - *----------------------------------------------------------------------------*/ |
29 | | - |
30 | | - |
31 | | -#include "syscalls.h" |
32 | | - |
33 | | -#include <stdio.h> |
34 | | -#include <stdarg.h> |
35 | 8 | #if defined ( __GNUC__ ) /* GCC CS3 */ |
36 | | - #include <sys/types.h> |
37 | 9 | #include <sys/stat.h> |
38 | 10 | #endif |
| 11 | +#include <errno.h> |
| 12 | +#undef errno |
| 13 | +extern int errno; |
39 | 14 |
|
40 | | -#include "Arduino.h" |
| 15 | +extern size_t uart_debug_write(uint8_t *data, uint32_t size); |
41 | 16 |
|
42 | 17 | // Helper macro to mark unused parameters and prevent compiler warnings. |
43 | 18 | // Appends _UNUSED to the variable name to prevent accidentally using them. |
|
50 | 25 | #define UNUSED(x) x ## _UNUSED |
51 | 26 | #endif |
52 | 27 |
|
53 | | -/*---------------------------------------------------------------------------- |
54 | | - * Exported variables |
55 | | - *----------------------------------------------------------------------------*/ |
| 28 | +register char * stack_ptr asm("sp"); |
56 | 29 |
|
57 | | -#undef errno |
58 | | -extern int errno ; |
59 | | -extern int _end ; |
60 | | - |
61 | | -/*---------------------------------------------------------------------------- |
62 | | - * Exported functions |
63 | | - *----------------------------------------------------------------------------*/ |
64 | | -extern void _exit( int status ) ; |
65 | | -extern void _kill( int pid, int sig ) ; |
66 | | -extern int _getpid ( void ) ; |
67 | | - |
68 | | -extern caddr_t _sbrk ( int incr ) |
69 | | -{ |
70 | | - static unsigned char *heap = NULL ; |
71 | | - unsigned char *prev_heap ; |
72 | | - |
73 | | - if ( heap == NULL ) |
74 | | - { |
75 | | - heap = (unsigned char *)&_end ; |
76 | | - } |
77 | | - prev_heap = heap; |
| 30 | +caddr_t _sbrk( int incr ) { |
| 31 | + extern char _end; /* Defined by the linker */ |
| 32 | + static char *heap_end = NULL ; |
| 33 | + char *prev_heap_end ; |
78 | 34 |
|
79 | | - heap += incr ; |
| 35 | + if ( heap_end == NULL ) { |
| 36 | + heap_end = &_end ; |
| 37 | + } |
| 38 | + prev_heap_end = heap_end; |
80 | 39 |
|
81 | | - return (caddr_t) prev_heap ; |
82 | | -} |
| 40 | + if (heap_end + incr > stack_ptr) { |
| 41 | + /* Heap and stack collision */ |
| 42 | + errno = ENOMEM; |
| 43 | + return (caddr_t) -1; |
| 44 | + } |
83 | 45 |
|
84 | | -extern int link( UNUSED(char *cOld), UNUSED(char *cNew) ) |
85 | | -{ |
86 | | - return -1 ; |
| 46 | + heap_end += incr ; |
| 47 | + return (caddr_t) prev_heap_end ; |
87 | 48 | } |
88 | 49 |
|
89 | | -extern int _close( UNUSED(int file) ) |
90 | | -{ |
91 | | - return -1 ; |
| 50 | +__attribute__((weak)) |
| 51 | +int _close( UNUSED(int file) ) { |
| 52 | + return -1; |
92 | 53 | } |
93 | 54 |
|
94 | | -extern int _fstat( UNUSED(int file), struct stat *st ) |
95 | | -{ |
| 55 | +__attribute__((weak)) |
| 56 | +int _fstat( UNUSED(int file), struct stat *st ) { |
96 | 57 | st->st_mode = S_IFCHR ; |
97 | | - |
98 | | - return 0 ; |
| 58 | + return 0; |
99 | 59 | } |
100 | 60 |
|
101 | | -extern int _isatty( UNUSED(int file) ) |
102 | | -{ |
103 | | - return 1 ; |
| 61 | +__attribute__((weak)) |
| 62 | +int _isatty( UNUSED(int file) ) { |
| 63 | + return 1; |
104 | 64 | } |
105 | 65 |
|
106 | | -extern int _lseek( UNUSED(int file), UNUSED(int ptr), UNUSED(int dir) ) |
107 | | -{ |
108 | | - return 0 ; |
| 66 | +__attribute__((weak)) |
| 67 | +int _lseek( UNUSED(int file), UNUSED(int ptr), UNUSED(int dir) ) { |
| 68 | + return 0; |
109 | 69 | } |
110 | 70 |
|
111 | | -extern int _read(UNUSED(int file), UNUSED(char *ptr), UNUSED(int len) ) |
112 | | -{ |
113 | | - return 0 ; |
| 71 | +__attribute__((weak)) |
| 72 | +int _read( UNUSED(int file), UNUSED(char *ptr), UNUSED(int len) ) { |
| 73 | + return 0; |
114 | 74 | } |
115 | 75 |
|
116 | | -extern int _write( UNUSED(int file), char *ptr, int len ) |
117 | | -{ |
118 | | - uart_debug_write((uint8_t *)ptr, len); |
119 | | - |
120 | | - return len ; |
| 76 | +__attribute__((weak)) |
| 77 | +int _write( UNUSED(int file), char *ptr, int len ) { |
| 78 | + return uart_debug_write((uint8_t *)ptr, (uint32_t)len); |
121 | 79 | } |
122 | 80 |
|
123 | | -extern void _exit( int status ) |
124 | | -{ |
125 | | - printf( "Exiting with status %d.\n", status ) ; |
126 | | - |
| 81 | +__attribute__((weak)) |
| 82 | +void _exit( UNUSED(int status) ) { |
127 | 83 | for ( ; ; ) ; |
128 | 84 | } |
129 | 85 |
|
130 | | -extern void _kill( UNUSED(int pid), UNUSED(int sig) ) |
131 | | -{ |
132 | | - return ; |
| 86 | +__attribute__((weak)) |
| 87 | +int _kill( UNUSED(int pid), UNUSED(int sig) ) { |
| 88 | + errno = EINVAL; |
| 89 | + return -1; |
133 | 90 | } |
134 | 91 |
|
135 | | -extern int _getpid ( void ) |
136 | | -{ |
137 | | - return -1 ; |
| 92 | +__attribute__((weak)) |
| 93 | +int _getpid( void ) { |
| 94 | + return 1; |
138 | 95 | } |
0 commit comments