newlib.c 2.7 KB

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