newlib.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <stm32f10x.h>
  8. #elif defined STM32F4
  9. # include <stm32f4xx.h>
  10. #endif
  11. #if defined STM32F4
  12. uint64_t virtualClock = 0;
  13. #endif
  14. extern uint32_t __get_MSP(void);
  15. #if defined STM32F1
  16. # define STDOUT_USART USART1
  17. # define STDERR_USART USART1
  18. # define STDIN_USART USART1
  19. #elif defined STM32F4
  20. # define STDOUT_USART USART3
  21. # define STDERR_USART USART3
  22. # define STDIN_USART USART3
  23. #endif
  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. #if defined STM32F1
  59. tv->tv_sec = RTC_GetCounter();
  60. tv->tv_usec = 0;
  61. #elif defined STM32F4
  62. tv->tv_sec = virtualClock / 1000;
  63. tv->tv_usec = (virtualClock % 1000) * 1000;
  64. #endif
  65. return 0;
  66. }
  67. int _isatty(int file)
  68. {
  69. switch (file)
  70. {
  71. case STDOUT_FILENO:
  72. case STDERR_FILENO:
  73. case STDIN_FILENO:
  74. return 1;
  75. default:
  76. //errno = ENOTTY;
  77. errno = EBADF;
  78. return 0;
  79. }
  80. }
  81. int _kill(int pid, int sig)
  82. {
  83. errno = EINVAL;
  84. return (-1);
  85. }
  86. int _link(char *old, char *new)
  87. {
  88. errno = EMLINK;
  89. return -1;
  90. }
  91. int _lseek(int file, int ptr, int dir)
  92. {
  93. return 0;
  94. }
  95. caddr_t _sbrk(int incr)
  96. {
  97. extern char _ebss;
  98. static char *heap_end= &_ebss;
  99. char *prev_heap_end;
  100. prev_heap_end = heap_end;
  101. char * stack = (char*) __get_MSP();
  102. if (heap_end + incr > stack)
  103. {
  104. _write(STDERR_FILENO, "Heap and stack collision\n", 25);
  105. errno = ENOMEM;
  106. return (caddr_t) - 1;
  107. //abort ();
  108. }
  109. heap_end += incr;
  110. return (caddr_t) prev_heap_end;
  111. }
  112. int _read(int file, char *ptr, int len)
  113. {
  114. switch (file)
  115. {
  116. case STDIN_FILENO:
  117. while (USART_GetFlagStatus(STDIN_USART, USART_FLAG_RXNE) == RESET);
  118. char c = (char)(USART_ReceiveData(STDIN_USART) & (uint16_t) 0x01FF);
  119. *ptr++ = c;
  120. return 1;
  121. break;
  122. default:
  123. errno = EBADF;
  124. return -1;
  125. }
  126. }
  127. int _stat(const char *filepath, struct stat *st)
  128. {
  129. st->st_mode = S_IFCHR;
  130. return 0;
  131. }
  132. clock_t _times(struct tms *buf)
  133. {
  134. return -1;
  135. }
  136. int _unlink(char *name)
  137. {
  138. errno = ENOENT;
  139. return -1;
  140. }
  141. int _wait(int *status)
  142. {
  143. errno = ECHILD;
  144. return -1;
  145. }
  146. int _write(int file, char *ptr, int len)
  147. {
  148. int n;
  149. switch (file)
  150. {
  151. case STDOUT_FILENO: /*stdout*/
  152. for (n = 0; n < len; n++)
  153. {
  154. while (USART_GetFlagStatus(STDOUT_USART, USART_FLAG_TC) == RESET);
  155. USART_SendData(STDOUT_USART, (uint8_t) * ptr++);
  156. }
  157. break;
  158. case STDERR_FILENO: /* stderr */
  159. for (n = 0; n < len; n++)
  160. {
  161. while (USART_GetFlagStatus(STDERR_USART, USART_FLAG_TC) == RESET);
  162. USART_SendData(STDERR_USART, (uint8_t) * ptr++);
  163. }
  164. break;
  165. default:
  166. errno = EBADF;
  167. return -1;
  168. }
  169. return len;
  170. }