newlib.c 2.7 KB

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