syscalls.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * newlib_stubs.c
  3. *
  4. * Created on: 2 Nov 2010
  5. * Author: nanoage.co.uk
  6. */
  7. #include <errno.h>
  8. #include <sys/stat.h>
  9. #include <sys/times.h>
  10. #include <sys/unistd.h>
  11. #include "stm32f4xx_usart.h"
  12. #include "stm32f4xx.h"
  13. #ifdef SWOTRACE
  14. /* Set STDIO_USART to 255 as flag that trace output on SWO pin used instead */
  15. #define STDIO_USART 255
  16. #else
  17. /* Set USART number */
  18. #define STDIO_USART 1 /* Disable UART stdio */
  19. #endif /* SWOTRACE */
  20. #ifndef STDOUT_USART
  21. #define STDOUT_USART STDIO_USART
  22. #endif
  23. #ifndef STDERR_USART
  24. #define STDERR_USART STDIO_USART
  25. #endif
  26. #ifndef STDIN_USART
  27. #define STDIN_USART STDIO_USART
  28. #endif
  29. #undef errno
  30. extern int errno;
  31. /* Variable to receive characters on SWO */
  32. volatile int32_t ITM_RxBuffer;
  33. /*
  34. environ
  35. A pointer to a list of environment variables and their values.
  36. For a minimal environment, this empty list is adequate:
  37. */
  38. char *__env[1] = { 0 };
  39. char **environ = __env;
  40. int _write(int file, char *ptr, int len);
  41. void _exit(int status) {
  42. _write(1, "exit", 4);
  43. while (1) {
  44. ;
  45. }
  46. }
  47. int _close(int file) {
  48. return -1;
  49. }
  50. /*
  51. execve
  52. Transfer control to a new process. Minimal implementation (for a system without processes):
  53. */
  54. int _execve(char *name, char **argv, char **env) {
  55. errno = ENOMEM;
  56. return -1;
  57. }
  58. /*
  59. fork
  60. Create a new process. Minimal implementation (for a system without processes):
  61. */
  62. int _fork() {
  63. errno = EAGAIN;
  64. return -1;
  65. }
  66. /*
  67. fstat
  68. Status of an open file. For consistency with other minimal implementations in these examples,
  69. all files are regarded as character special devices.
  70. The `sys/stat.h' header file required is distributed in the `include' subdirectory for this C library.
  71. */
  72. int _fstat(int file, struct stat *st) {
  73. st->st_mode = S_IFCHR;
  74. return 0;
  75. }
  76. /*
  77. getpid
  78. Process-ID; this is sometimes used to generate strings unlikely to conflict with other processes. Minimal implementation, for a system without processes:
  79. */
  80. int _getpid() {
  81. return 1;
  82. }
  83. /*
  84. isatty
  85. Query whether output stream is a terminal. For consistency with the other minimal implementations,
  86. */
  87. int _isatty(int file) {
  88. switch (file){
  89. case STDOUT_FILENO:
  90. case STDERR_FILENO:
  91. case STDIN_FILENO:
  92. return 1;
  93. default:
  94. //errno = ENOTTY;
  95. errno = EBADF;
  96. return 0;
  97. }
  98. }
  99. /*
  100. kill
  101. Send a signal. Minimal implementation:
  102. */
  103. int _kill(int pid, int sig) {
  104. errno = EINVAL;
  105. return (-1);
  106. }
  107. /*
  108. link
  109. Establish a new name for an existing file. Minimal implementation:
  110. */
  111. int _link(char *old, char *new) {
  112. errno = EMLINK;
  113. return -1;
  114. }
  115. /*
  116. lseek
  117. Set position in a file. Minimal implementation:
  118. */
  119. int _lseek(int file, int ptr, int dir) {
  120. return 0;
  121. }
  122. /*
  123. sbrk
  124. Increase program data space.
  125. Malloc and related functions depend on this
  126. */
  127. caddr_t _sbrk(int incr) {
  128. extern char _ebss; // Defined by the linker
  129. extern char __bss_end__;
  130. static char *heap_end;
  131. char *prev_heap_end;
  132. if (heap_end == 0) {
  133. //heap_end = &_ebss;
  134. heap_end = &__bss_end__;
  135. }
  136. prev_heap_end = heap_end;
  137. char * stack = (char*) __get_MSP();
  138. if (heap_end + incr > stack)
  139. {
  140. _write (STDERR_FILENO, "Heap and stack collision\n", 25);
  141. errno = ENOMEM;
  142. return (caddr_t) -1;
  143. //abort ();
  144. }
  145. heap_end += incr;
  146. return (caddr_t) prev_heap_end;
  147. }
  148. /*
  149. read
  150. Read a character to a file. `libc' subroutines will use this system routine for input from all files, including stdin
  151. Returns -1 on error or blocks until the number of characters have been read.
  152. */
  153. int _read(int file, char *ptr, int len) {
  154. int n;
  155. int num = 0;
  156. switch (file) {
  157. case STDIN_FILENO:
  158. for (n = 0; n < len; n++) {
  159. #if STDIN_USART == 1
  160. while ((USART1->SR & USART_FLAG_RXNE) == (uint16_t)RESET) {}
  161. char c = (char)(USART1->DR & (uint16_t)0x01FF);
  162. #elif STDIN_USART == 2
  163. while ((USART2->SR & USART_FLAG_RXNE) == (uint16_t) RESET) {}
  164. char c = (char) (USART2->DR & (uint16_t) 0x01FF);
  165. #elif STDIN_USART == 3
  166. while ((USART3->SR & USART_FLAG_RXNE) == (uint16_t)RESET) {}
  167. char c = (char)(USART3->DR & (uint16_t)0x01FF);
  168. #elif STDIN_USART == 255
  169. /* TODO Test this feature */
  170. char c = (char)ITM_ReceiveChar();
  171. #elif STDIN_USART == 0
  172. /* Return NULL */
  173. char c = '\0';
  174. #endif
  175. *ptr++ = c;
  176. num++;
  177. }
  178. break;
  179. default:
  180. errno = EBADF;
  181. return -1;
  182. }
  183. return num;
  184. }
  185. /*
  186. stat
  187. Status of a file (by name). Minimal implementation:
  188. int _EXFUN(stat,( const char *__path, struct stat *__sbuf ));
  189. */
  190. int _stat(const char *filepath, struct stat *st) {
  191. st->st_mode = S_IFCHR;
  192. return 0;
  193. }
  194. /*
  195. times
  196. Timing information for current process. Minimal implementation:
  197. */
  198. clock_t _times(struct tms *buf) {
  199. return -1;
  200. }
  201. /*
  202. unlink
  203. Remove a file's directory entry. Minimal implementation:
  204. */
  205. int _unlink(char *name) {
  206. errno = ENOENT;
  207. return -1;
  208. }
  209. /*
  210. wait
  211. Wait for a child process. Minimal implementation:
  212. */
  213. int _wait(int *status) {
  214. errno = ECHILD;
  215. return -1;
  216. }
  217. /*
  218. write
  219. Write a character to a file. `libc' subroutines will use this system routine for output to all files, including stdout
  220. Returns -1 on error or number of bytes sent
  221. */
  222. int _write(int file, char *ptr, int len) {
  223. int n;
  224. switch (file) {
  225. case STDOUT_FILENO: /*stdout*/
  226. for (n = 0; n < len; n++) {
  227. #if STDOUT_USART == 1
  228. while ((USART1->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  229. USART1->DR = (*ptr++ & (uint16_t)0x01FF);
  230. #elif STDOUT_USART == 2
  231. while ((USART2->SR & USART_FLAG_TC) == (uint16_t) RESET) {
  232. }
  233. USART2->DR = (*ptr++ & (uint16_t) 0x01FF);
  234. #elif STDOUT_USART == 3
  235. while ((USART3->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  236. USART3->DR = (*ptr++ & (uint16_t)0x01FF);
  237. #elif STDOUT_USART == 255
  238. ITM_SendChar(*ptr++ & (uint16_t)0x00FF);
  239. #endif
  240. }
  241. break;
  242. case STDERR_FILENO: /* stderr */
  243. for (n = 0; n < len; n++) {
  244. #if STDERR_USART == 1
  245. while ((USART1->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  246. USART1->DR = (*ptr++ & (uint16_t)0x01FF);
  247. #elif STDERR_USART == 2
  248. while ((USART2->SR & USART_FLAG_TC) == (uint16_t) RESET) {
  249. }
  250. USART2->DR = (*ptr++ & (uint16_t) 0x01FF);
  251. #elif STDERR_USART == 3
  252. while ((USART3->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  253. USART3->DR = (*ptr++ & (uint16_t)0x01FF);
  254. #elif STDERR_USART == 255
  255. ITM_SendChar(*ptr++ & (uint16_t)0x00FF);
  256. #endif
  257. }
  258. break;
  259. default:
  260. errno = EBADF;
  261. return -1;
  262. }
  263. return len;
  264. }