syscalls.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /* Includes */
  23. #include <sys/stat.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <signal.h>
  28. #include <time.h>
  29. #include <sys/time.h>
  30. #include <sys/times.h>
  31. /* Variables */
  32. extern int __io_putchar(int ch) __attribute__((weak));
  33. extern int __io_getchar(void) __attribute__((weak));
  34. char *__env[1] = { 0 };
  35. char **environ = __env;
  36. /* Functions */
  37. void initialise_monitor_handles()
  38. {
  39. }
  40. int _getpid(void)
  41. {
  42. return 1;
  43. }
  44. int _kill(int pid, int sig)
  45. {
  46. (void)pid;
  47. (void)sig;
  48. errno = EINVAL;
  49. return -1;
  50. }
  51. void _exit (int status)
  52. {
  53. _kill(status, -1);
  54. while (1) {} /* Make sure we hang here */
  55. }
  56. __attribute__((weak)) int _read(int file, char *ptr, int len)
  57. {
  58. (void)file;
  59. int DataIdx;
  60. for (DataIdx = 0; DataIdx < len; DataIdx++)
  61. {
  62. *ptr++ = __io_getchar();
  63. }
  64. return len;
  65. }
  66. __attribute__((weak)) int _write(int file, char *ptr, int len)
  67. {
  68. (void)file;
  69. int DataIdx;
  70. for (DataIdx = 0; DataIdx < len; DataIdx++)
  71. {
  72. __io_putchar(*ptr++);
  73. }
  74. return len;
  75. }
  76. int _close(int file)
  77. {
  78. (void)file;
  79. return -1;
  80. }
  81. int _fstat(int file, struct stat *st)
  82. {
  83. (void)file;
  84. st->st_mode = S_IFCHR;
  85. return 0;
  86. }
  87. int _isatty(int file)
  88. {
  89. (void)file;
  90. return 1;
  91. }
  92. int _lseek(int file, int ptr, int dir)
  93. {
  94. (void)file;
  95. (void)ptr;
  96. (void)dir;
  97. return 0;
  98. }
  99. int _open(char *path, int flags, ...)
  100. {
  101. (void)path;
  102. (void)flags;
  103. /* Pretend like we always fail */
  104. return -1;
  105. }
  106. int _wait(int *status)
  107. {
  108. (void)status;
  109. errno = ECHILD;
  110. return -1;
  111. }
  112. int _unlink(char *name)
  113. {
  114. (void)name;
  115. errno = ENOENT;
  116. return -1;
  117. }
  118. clock_t _times(struct tms *buf)
  119. {
  120. (void)buf;
  121. return -1;
  122. }
  123. int _stat(const char *file, struct stat *st)
  124. {
  125. (void)file;
  126. st->st_mode = S_IFCHR;
  127. return 0;
  128. }
  129. int _link(char *old, char *new)
  130. {
  131. (void)old;
  132. (void)new;
  133. errno = EMLINK;
  134. return -1;
  135. }
  136. int _fork(void)
  137. {
  138. errno = EAGAIN;
  139. return -1;
  140. }
  141. int _execve(char *name, char **argv, char **env)
  142. {
  143. (void)name;
  144. (void)argv;
  145. (void)env;
  146. errno = ENOMEM;
  147. return -1;
  148. }
  149. // --- Picolibc Specific Section ---
  150. #if defined(__PICOLIBC__)
  151. /**
  152. * @brief Picolibc helper function to output a character to a FILE stream.
  153. * This redirects the output to the low-level __io_putchar function.
  154. * @param c Character to write.
  155. * @param file FILE stream pointer (ignored).
  156. * @retval int The character written.
  157. */
  158. static int starm_putc(char c, FILE *file)
  159. {
  160. (void) file;
  161. __io_putchar(c);
  162. return c;
  163. }
  164. /**
  165. * @brief Picolibc helper function to input a character from a FILE stream.
  166. * This redirects the input from the low-level __io_getchar function.
  167. * @param file FILE stream pointer (ignored).
  168. * @retval int The character read, cast to an unsigned char then int.
  169. */
  170. static int starm_getc(FILE *file)
  171. {
  172. unsigned char c;
  173. (void) file;
  174. c = __io_getchar();
  175. return c;
  176. }
  177. // Define and initialize the standard I/O streams for Picolibc.
  178. // FDEV_SETUP_STREAM connects the starm_putc and starm_getc helper functions to a FILE structure.
  179. // _FDEV_SETUP_RW indicates the stream is for reading and writing.
  180. static FILE __stdio = FDEV_SETUP_STREAM(starm_putc,
  181. starm_getc,
  182. NULL,
  183. _FDEV_SETUP_RW);
  184. // Assign the standard stream pointers (stdin, stdout, stderr) to the initialized stream.
  185. // Picolibc uses these pointers for standard I/O operations (printf, scanf, etc.).
  186. FILE *const stdin = &__stdio;
  187. __strong_reference(stdin, stdout);
  188. __strong_reference(stdin, stderr);
  189. // Create strong aliases mapping standard C library function names (without underscore)
  190. // to the implemented system call stubs (with underscore). Picolibc uses these
  191. // standard names internally, so this linking is required.
  192. __strong_reference(_read, read);
  193. __strong_reference(_write, write);
  194. __strong_reference(_times, times);
  195. __strong_reference(_execve, execve);
  196. __strong_reference(_fork, fork);
  197. __strong_reference(_link, link);
  198. __strong_reference(_unlink, unlink);
  199. __strong_reference(_stat, stat);
  200. __strong_reference(_wait, wait);
  201. __strong_reference(_open, open);
  202. __strong_reference(_close, close);
  203. __strong_reference(_lseek, lseek);
  204. __strong_reference(_isatty, isatty);
  205. __strong_reference(_fstat, fstat);
  206. __strong_reference(_exit, exit);
  207. __strong_reference(_kill, kill);
  208. __strong_reference(_getpid, getpid);
  209. #endif //__PICOLIBC__