newlib.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <errno.h>
  2. #include <sys/stat.h>
  3. #include <sys/times.h>
  4. #include <sys/unistd.h>
  5. #include <sys/time.h>
  6. #if defined STM32F1
  7. # include <stm32f1xx_hal.h>
  8. #elif defined STM32F2
  9. # include <stm32f2xx_hal.h>
  10. #elif defined STM32F4
  11. # include <stm32f4xx_hal.h>
  12. #elif defined STM32G0
  13. # include <stm32g0xx_hal.h>
  14. #endif
  15. extern uint32_t __get_MSP(void);
  16. extern UART_HandleTypeDef UART_Handle;
  17. extern uint64_t virtualTimer;
  18. #undef errno
  19. extern int errno;
  20. char *__env[1] = { 0 };
  21. char **environ = __env;
  22. int _write(int file, char *ptr, int len);
  23. void _exit(int status)
  24. {
  25. while (1);
  26. }
  27. int _close(int file)
  28. {
  29. return -1;
  30. }
  31. int _execve(char *name, char **argv, char **env)
  32. {
  33. errno = ENOMEM;
  34. return -1;
  35. }
  36. int _fork()
  37. {
  38. errno = EAGAIN;
  39. return -1;
  40. }
  41. int _fstat(int file, struct stat *st)
  42. {
  43. st->st_mode = S_IFCHR;
  44. return 0;
  45. }
  46. int _getpid()
  47. {
  48. return 1;
  49. }
  50. int _gettimeofday(struct timeval *tv, struct timezone *tz)
  51. {
  52. tv->tv_sec = virtualTimer / 1000;
  53. tv->tv_usec = (virtualTimer % 1000) * 1000;
  54. return 0;
  55. }
  56. int _isatty(int file)
  57. {
  58. switch (file)
  59. {
  60. case STDOUT_FILENO:
  61. case STDERR_FILENO:
  62. case STDIN_FILENO:
  63. return 1;
  64. default:
  65. //errno = ENOTTY;
  66. errno = EBADF;
  67. return 0;
  68. }
  69. }
  70. int _kill(int pid, int sig)
  71. {
  72. errno = EINVAL;
  73. return (-1);
  74. }
  75. int _link(char *old, char *new)
  76. {
  77. errno = EMLINK;
  78. return -1;
  79. }
  80. int _lseek(int file, int ptr, int dir)
  81. {
  82. return 0;
  83. }
  84. caddr_t _sbrk(int incr)
  85. {
  86. extern char _ebss;
  87. static char *heap_end= &_ebss;
  88. char *prev_heap_end;
  89. prev_heap_end = heap_end;
  90. char * stack = (char*) __get_MSP();
  91. if (heap_end + incr > stack)
  92. {
  93. _write(STDERR_FILENO, "Heap and stack collision\n", 25);
  94. errno = ENOMEM;
  95. return (caddr_t) - 1;
  96. //abort ();
  97. }
  98. heap_end += incr;
  99. return (caddr_t) prev_heap_end;
  100. }
  101. int _read(int file, char *ptr, int len)
  102. {
  103. switch (file)
  104. {
  105. case STDIN_FILENO:
  106. HAL_UART_Receive(&UART_Handle, (uint8_t *)ptr, 1, HAL_MAX_DELAY);
  107. return 1;
  108. default:
  109. errno = EBADF;
  110. return -1;
  111. }
  112. }
  113. int _stat(const char *filepath, struct stat *st)
  114. {
  115. st->st_mode = S_IFCHR;
  116. return 0;
  117. }
  118. clock_t _times(struct tms *buf)
  119. {
  120. return -1;
  121. }
  122. int _unlink(char *name)
  123. {
  124. errno = ENOENT;
  125. return -1;
  126. }
  127. int _wait(int *status)
  128. {
  129. errno = ECHILD;
  130. return -1;
  131. }
  132. int _write(int file, char *ptr, int len)
  133. {
  134. switch (file)
  135. {
  136. case STDOUT_FILENO: /*stdout*/
  137. HAL_UART_Transmit(&UART_Handle, (uint8_t*)ptr, len, HAL_MAX_DELAY);
  138. break;
  139. case STDERR_FILENO: /* stderr */
  140. HAL_UART_Transmit(&UART_Handle, (uint8_t*)ptr, len, HAL_MAX_DELAY);
  141. break;
  142. default:
  143. errno = EBADF;
  144. return -1;
  145. }
  146. return len;
  147. }