libc_compat.c 3.0 KB

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