stubs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* See LICENSE of license details. */
  2. #include <stdint.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/times.h>
  8. #include <sys/time.h>
  9. #include <time.h>
  10. #include <nuclei_sdk_hal.h>
  11. #undef errno
  12. extern int errno;
  13. /* Key stub function for uart io via printf/scanf and heap management */
  14. #undef putchar
  15. // TODO Implement your own uart_write and uart_read for your debug uart device
  16. int putchar(int dat)
  17. {
  18. if (dat == '\n') {
  19. uart_write(SOC_DEBUG_UART, '\r');
  20. }
  21. uart_write(SOC_DEBUG_UART, dat);
  22. return dat;
  23. }
  24. __WEAK ssize_t _write(int fd, const void* ptr, size_t len)
  25. {
  26. if (!isatty(fd)) {
  27. return -1;
  28. }
  29. const uint8_t* writebuf = (const uint8_t*)ptr;
  30. for (size_t i = 0; i < len; i++) {
  31. putchar((int)writebuf[i]);
  32. }
  33. return len;
  34. }
  35. // #define UART_AUTO_ECHO
  36. #undef getchar
  37. int getchar(void)
  38. {
  39. int dat;
  40. dat = (int)uart_read(SOC_DEBUG_UART);
  41. #ifdef UART_AUTO_ECHO
  42. uart_write(SOC_DEBUG_UART, (uint8_t)dat);
  43. #endif
  44. return dat;
  45. }
  46. __WEAK ssize_t _read(int fd, void* ptr, size_t len)
  47. {
  48. if (fd != STDIN_FILENO) {
  49. return -1;
  50. }
  51. ssize_t cnt = 0;
  52. uint8_t* readbuf = (uint8_t*)ptr;
  53. for (cnt = 0; cnt < len; cnt ++) {
  54. readbuf[cnt] = getchar();
  55. /* Return partial buffer if we get EOL */
  56. if (readbuf[cnt] == '\n') {
  57. return cnt;
  58. }
  59. }
  60. return cnt;
  61. }
  62. __WEAK void* _sbrk(ptrdiff_t incr)
  63. {
  64. extern char __heap_start[];
  65. extern char __heap_end[];
  66. static char* curbrk = __heap_start;
  67. if ((curbrk + incr < __heap_start) || (curbrk + incr > __heap_end)) {
  68. return (void*)(-1);
  69. }
  70. curbrk += incr;
  71. return (void*)(curbrk - incr);
  72. }
  73. /* Other newlib stub functions, see https://sourceware.org/newlib/libc.html#Stubs */
  74. int errno;
  75. __WEAK void* __dso_handle = NULL;
  76. /* version of environ for no OS. */
  77. char *__env[1] = { 0 };
  78. char **environ = __env;
  79. /* Get resolution of clock. */
  80. __WEAK int clock_getres(clockid_t clock_id, struct timespec* res)
  81. {
  82. res->tv_sec = 0;
  83. res->tv_nsec = 1000000000 / SystemCoreClock;
  84. return 0;
  85. }
  86. __WEAK int _gettimeofday(struct timeval* tp, void* tzp)
  87. {
  88. uint64_t cycles;
  89. cycles = __get_rv_cycle();
  90. tp->tv_sec = cycles / SystemCoreClock;
  91. tp->tv_usec = (cycles % SystemCoreClock) * 1000000 / SystemCoreClock;
  92. return 0;
  93. }
  94. __WEAK int _isatty(int fd)
  95. {
  96. return 1;
  97. }
  98. __WEAK int _stat(char* file, struct stat* st)
  99. {
  100. st->st_mode = S_IFCHR;
  101. return 0;
  102. }
  103. __WEAK int _fstat(int file, struct stat* st)
  104. {
  105. if ((STDOUT_FILENO == file) || (STDERR_FILENO == file)) {
  106. st->st_mode = S_IFCHR;
  107. return 0;
  108. } else {
  109. errno = EBADF;
  110. return -1;
  111. }
  112. }
  113. __WEAK int _chown(const char *path, uid_t owner, gid_t group)
  114. {
  115. errno = ENOSYS;
  116. return -1;
  117. }
  118. __WEAK int _open(const char* name, int flags, int mode)
  119. {
  120. errno = ENOSYS;
  121. return -1;
  122. }
  123. __WEAK int _lseek(int file, int offset, int whence)
  124. {
  125. return 0;
  126. }
  127. __WEAK int _link(char* path1, char* path2)
  128. {
  129. errno = EMLINK;
  130. return -1;
  131. }
  132. __WEAK int _getpid(void)
  133. {
  134. return 1;
  135. }
  136. __WEAK int _close(int fd)
  137. {
  138. errno = EBADF;
  139. return -1;
  140. }
  141. __WEAK int _unlink(const char* path)
  142. {
  143. return -1;
  144. }
  145. __WEAK int _fork(void)
  146. {
  147. errno = ENOSYS;
  148. return -1;
  149. }
  150. __WEAK int _symlink(const char *path1, const char *path2)
  151. {
  152. errno = ENOSYS;
  153. return -1;
  154. }
  155. /* Supply a definition of errno if one not already provided. */
  156. __WEAK int _execve(char* name, char** argv, char** env)
  157. {
  158. errno = ENOMEM;
  159. return -1;
  160. }
  161. __WEAK int _readlink(const char *path, char *buf, size_t bufsize)
  162. {
  163. errno = ENOSYS;
  164. return -1;
  165. }
  166. __WEAK int _kill(int pid, int sig)
  167. {
  168. errno = EINVAL;
  169. return -1;
  170. }
  171. __WEAK void _exit(int fd)
  172. {
  173. while (1) {
  174. __WFI();
  175. }
  176. }
  177. __WEAK int _wait(int* status)
  178. {
  179. errno = ECHILD;
  180. return -1;
  181. }
  182. extern int _gettimeofday(struct timeval* tp, void* tzp);
  183. __WEAK clock_t _times(struct tms* buf)
  184. {
  185. static struct timeval t0;
  186. struct timeval t;
  187. long long utime;
  188. /* When called for the first time, initialize t0. */
  189. if (t0.tv_sec == 0 && t0.tv_usec == 0) {
  190. _gettimeofday(&t0, 0);
  191. }
  192. _gettimeofday(&t, 0);
  193. utime = (t.tv_sec - t0.tv_sec) * 1000000 + (t.tv_usec - t0.tv_usec);
  194. buf->tms_utime = utime * CLOCKS_PER_SEC / 1000000;
  195. buf->tms_stime = buf->tms_cstime = buf->tms_cutime = 0;
  196. return buf->tms_utime;
  197. }
  198. /* Set CLOCK to value TP. */
  199. __WEAK int clock_settime(clockid_t clock_id, const struct timespec* tp)
  200. {
  201. return -1;
  202. }
  203. /* Get current value of CLOCK and store it in tp. */
  204. __WEAK int clock_gettime(clockid_t clock_id, struct timespec* tp)
  205. {
  206. struct timeval tv;
  207. int retval = -1;
  208. retval = _gettimeofday(&tv, NULL);
  209. if (retval == 0) {
  210. (tp)->tv_sec = (&tv)->tv_sec;
  211. (tp)->tv_nsec = (&tv)->tv_usec * 1000;
  212. }
  213. return retval;
  214. }