1 | /** |
---|
2 | ****************************************************************************** |
---|
3 | * @file syscalls.c |
---|
4 | * @author Auto-generated by STM32CubeMX |
---|
5 | * @brief Minimal System calls file |
---|
6 | * |
---|
7 | * For more information about which c-functions |
---|
8 | * need which of these lowlevel functions |
---|
9 | * please consult the Newlib or Picolibc libc-manual |
---|
10 | ****************************************************************************** |
---|
11 | * @attention |
---|
12 | * |
---|
13 | * Copyright (c) 2020-2025 STMicroelectronics. |
---|
14 | * All rights reserved. |
---|
15 | * |
---|
16 | * This software is licensed under terms that can be found in the LICENSE file |
---|
17 | * in the root directory of this software component. |
---|
18 | * If no LICENSE file comes with this software, it is provided AS-IS. |
---|
19 | * |
---|
20 | ****************************************************************************** |
---|
21 | */ |
---|
22 | |
---|
23 | /* Includes */ |
---|
24 | #include <sys/stat.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <errno.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <signal.h> |
---|
29 | #include <time.h> |
---|
30 | #include <sys/time.h> |
---|
31 | #include <sys/times.h> |
---|
32 | |
---|
33 | |
---|
34 | /* Variables */ |
---|
35 | extern int __io_putchar(int ch) __attribute__((weak)); |
---|
36 | extern int __io_getchar(void) __attribute__((weak)); |
---|
37 | |
---|
38 | |
---|
39 | char *__env[1] = { 0 }; |
---|
40 | char **environ = __env; |
---|
41 | |
---|
42 | |
---|
43 | /* Functions */ |
---|
44 | void initialise_monitor_handles() |
---|
45 | { |
---|
46 | } |
---|
47 | |
---|
48 | int _getpid(void) |
---|
49 | { |
---|
50 | return 1; |
---|
51 | } |
---|
52 | |
---|
53 | int _kill(int pid, int sig) |
---|
54 | { |
---|
55 | (void)pid; |
---|
56 | (void)sig; |
---|
57 | errno = EINVAL; |
---|
58 | return -1; |
---|
59 | } |
---|
60 | |
---|
61 | void _exit (int status) |
---|
62 | { |
---|
63 | _kill(status, -1); |
---|
64 | while (1) {} /* Make sure we hang here */ |
---|
65 | } |
---|
66 | |
---|
67 | __attribute__((weak)) int _read(int file, char *ptr, int len) |
---|
68 | { |
---|
69 | (void)file; |
---|
70 | int DataIdx; |
---|
71 | |
---|
72 | for (DataIdx = 0; DataIdx < len; DataIdx++) |
---|
73 | { |
---|
74 | *ptr++ = __io_getchar(); |
---|
75 | } |
---|
76 | |
---|
77 | return len; |
---|
78 | } |
---|
79 | |
---|
80 | __attribute__((weak)) int _write(int file, char *ptr, int len) |
---|
81 | { |
---|
82 | (void)file; |
---|
83 | int DataIdx; |
---|
84 | |
---|
85 | for (DataIdx = 0; DataIdx < len; DataIdx++) |
---|
86 | { |
---|
87 | __io_putchar(*ptr++); |
---|
88 | } |
---|
89 | return len; |
---|
90 | } |
---|
91 | |
---|
92 | int _close(int file) |
---|
93 | { |
---|
94 | (void)file; |
---|
95 | return -1; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | int _fstat(int file, struct stat *st) |
---|
100 | { |
---|
101 | (void)file; |
---|
102 | st->st_mode = S_IFCHR; |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | int _isatty(int file) |
---|
107 | { |
---|
108 | (void)file; |
---|
109 | return 1; |
---|
110 | } |
---|
111 | |
---|
112 | int _lseek(int file, int ptr, int dir) |
---|
113 | { |
---|
114 | (void)file; |
---|
115 | (void)ptr; |
---|
116 | (void)dir; |
---|
117 | return 0; |
---|
118 | } |
---|
119 | |
---|
120 | int _open(char *path, int flags, ...) |
---|
121 | { |
---|
122 | (void)path; |
---|
123 | (void)flags; |
---|
124 | /* Pretend like we always fail */ |
---|
125 | return -1; |
---|
126 | } |
---|
127 | |
---|
128 | int _wait(int *status) |
---|
129 | { |
---|
130 | (void)status; |
---|
131 | errno = ECHILD; |
---|
132 | return -1; |
---|
133 | } |
---|
134 | |
---|
135 | int _unlink(char *name) |
---|
136 | { |
---|
137 | (void)name; |
---|
138 | errno = ENOENT; |
---|
139 | return -1; |
---|
140 | } |
---|
141 | |
---|
142 | clock_t _times(struct tms *buf) |
---|
143 | { |
---|
144 | (void)buf; |
---|
145 | return -1; |
---|
146 | } |
---|
147 | |
---|
148 | int _stat(const char *file, struct stat *st) |
---|
149 | { |
---|
150 | (void)file; |
---|
151 | st->st_mode = S_IFCHR; |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | int _link(char *old, char *new) |
---|
156 | { |
---|
157 | (void)old; |
---|
158 | (void)new; |
---|
159 | errno = EMLINK; |
---|
160 | return -1; |
---|
161 | } |
---|
162 | |
---|
163 | int _fork(void) |
---|
164 | { |
---|
165 | errno = EAGAIN; |
---|
166 | return -1; |
---|
167 | } |
---|
168 | |
---|
169 | int _execve(char *name, char **argv, char **env) |
---|
170 | { |
---|
171 | (void)name; |
---|
172 | (void)argv; |
---|
173 | (void)env; |
---|
174 | errno = ENOMEM; |
---|
175 | return -1; |
---|
176 | } |
---|
177 | |
---|
178 | // --- Picolibc Specific Section --- |
---|
179 | #if defined(__PICOLIBC__) |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief Picolibc helper function to output a character to a FILE stream. |
---|
183 | * This redirects the output to the low-level __io_putchar function. |
---|
184 | * @param c Character to write. |
---|
185 | * @param file FILE stream pointer (ignored). |
---|
186 | * @retval int The character written. |
---|
187 | */ |
---|
188 | static int starm_putc(char c, FILE *file) |
---|
189 | { |
---|
190 | (void) file; |
---|
191 | __io_putchar(c); |
---|
192 | return c; |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * @brief Picolibc helper function to input a character from a FILE stream. |
---|
197 | * This redirects the input from the low-level __io_getchar function. |
---|
198 | * @param file FILE stream pointer (ignored). |
---|
199 | * @retval int The character read, cast to an unsigned char then int. |
---|
200 | */ |
---|
201 | static int starm_getc(FILE *file) |
---|
202 | { |
---|
203 | unsigned char c; |
---|
204 | (void) file; |
---|
205 | c = __io_getchar(); |
---|
206 | return c; |
---|
207 | } |
---|
208 | |
---|
209 | // Define and initialize the standard I/O streams for Picolibc. |
---|
210 | // FDEV_SETUP_STREAM connects the starm_putc and starm_getc helper functions to a FILE structure. |
---|
211 | // _FDEV_SETUP_RW indicates the stream is for reading and writing. |
---|
212 | static FILE __stdio = FDEV_SETUP_STREAM(starm_putc, |
---|
213 | starm_getc, |
---|
214 | NULL, |
---|
215 | _FDEV_SETUP_RW); |
---|
216 | |
---|
217 | // Assign the standard stream pointers (stdin, stdout, stderr) to the initialized stream. |
---|
218 | // Picolibc uses these pointers for standard I/O operations (printf, scanf, etc.). |
---|
219 | FILE *const stdin = &__stdio; |
---|
220 | __strong_reference(stdin, stdout); |
---|
221 | __strong_reference(stdin, stderr); |
---|
222 | |
---|
223 | // Create strong aliases mapping standard C library function names (without underscore) |
---|
224 | // to the implemented system call stubs (with underscore). Picolibc uses these |
---|
225 | // standard names internally, so this linking is required. |
---|
226 | __strong_reference(_read, read); |
---|
227 | __strong_reference(_write, write); |
---|
228 | __strong_reference(_times, times); |
---|
229 | __strong_reference(_execve, execve); |
---|
230 | __strong_reference(_fork, fork); |
---|
231 | __strong_reference(_link, link); |
---|
232 | __strong_reference(_unlink, unlink); |
---|
233 | __strong_reference(_stat, stat); |
---|
234 | __strong_reference(_wait, wait); |
---|
235 | __strong_reference(_open, open); |
---|
236 | __strong_reference(_close, close); |
---|
237 | __strong_reference(_lseek, lseek); |
---|
238 | __strong_reference(_isatty, isatty); |
---|
239 | __strong_reference(_fstat, fstat); |
---|
240 | __strong_reference(_exit, exit); |
---|
241 | __strong_reference(_kill, kill); |
---|
242 | __strong_reference(_getpid, getpid); |
---|
243 | |
---|
244 | #endif //__PICOLIBC__ |
---|