|
| 1 | +/** |
| 2 | + * \file syscalls_stm32.c |
| 3 | + * |
| 4 | + * Implementation of newlib syscall. |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "stm32_def.h" |
| 9 | +#if defined ( __GNUC__ ) /* GCC CS3 */ |
| 10 | +#include <sys/stat.h> |
| 11 | +#endif |
| 12 | +#include <errno.h> |
| 13 | +#undef errno |
| 14 | +extern int errno; |
| 15 | + |
| 16 | +extern size_t uart_debug_write(uint8_t *data, uint32_t size); |
| 17 | + |
| 18 | +// Helper macro to mark unused parameters and prevent compiler warnings. |
| 19 | +// Appends _UNUSED to the variable name to prevent accidentally using them. |
| 20 | +#ifdef UNUSED |
| 21 | +#undef UNUSED |
| 22 | +#endif |
| 23 | +#ifdef __GNUC__ |
| 24 | +#define UNUSED(x) x ## _UNUSED __attribute__((__unused__)) |
| 25 | +#else |
| 26 | +#define UNUSED(x) x ## _UNUSED |
| 27 | +#endif |
| 28 | + |
| 29 | +__attribute__((weak)) |
| 30 | +caddr_t _sbrk(int incr) |
| 31 | +{ |
| 32 | + extern char _estack; /* Defined in the linker script */ |
| 33 | + extern char _Min_Stack_Size; /* Defined in the linker script */ |
| 34 | + extern char _end; /* Defined by the linker */ |
| 35 | + static char *heap_end = &_end ; |
| 36 | + char *prev_heap_end = heap_end; |
| 37 | + |
| 38 | + if (heap_end + incr > (char *)__get_MSP()) { |
| 39 | + /* Heap and stack collision */ |
| 40 | + errno = ENOMEM; |
| 41 | + return (caddr_t) -1; |
| 42 | + } |
| 43 | + /* Ensure to keep minimun stack size defined in the linker script */ |
| 44 | + if (heap_end + incr >= (char *)(&_estack - &_Min_Stack_Size)) { |
| 45 | + errno = ENOMEM; |
| 46 | + return (caddr_t) -1; |
| 47 | + } |
| 48 | + |
| 49 | + heap_end += incr ; |
| 50 | + return (caddr_t) prev_heap_end ; |
| 51 | +} |
| 52 | + |
| 53 | +__attribute__((weak)) |
| 54 | +int _close(UNUSED(int file)) |
| 55 | +{ |
| 56 | + return -1; |
| 57 | +} |
| 58 | + |
| 59 | +__attribute__((weak)) |
| 60 | +int _fstat(UNUSED(int file), struct stat *st) |
| 61 | +{ |
| 62 | + st->st_mode = S_IFCHR ; |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +__attribute__((weak)) |
| 67 | +int _isatty(UNUSED(int file)) |
| 68 | +{ |
| 69 | + return 1; |
| 70 | +} |
| 71 | + |
| 72 | +__attribute__((weak)) |
| 73 | +int _lseek(UNUSED(int file), UNUSED(int ptr), UNUSED(int dir)) |
| 74 | +{ |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +__attribute__((weak)) |
| 79 | +int _read(UNUSED(int file), UNUSED(char *ptr), UNUSED(int len)) |
| 80 | +{ |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +__attribute__((weak)) |
| 85 | +int _write(UNUSED(int file), char *ptr, int len) |
| 86 | +{ |
| 87 | +#ifdef HAL_UART_MODULE_ENABLED |
| 88 | + return uart_debug_write((uint8_t *)ptr, (uint32_t)len); |
| 89 | +#else |
| 90 | + (void)ptr; |
| 91 | + return len; |
| 92 | +#endif |
| 93 | +} |
| 94 | + |
| 95 | +__attribute__((weak)) |
| 96 | +void _exit(UNUSED(int status)) |
| 97 | +{ |
| 98 | + for (; ;) ; |
| 99 | +} |
| 100 | + |
| 101 | +__attribute__((weak)) |
| 102 | +int _kill(UNUSED(int pid), UNUSED(int sig)) |
| 103 | +{ |
| 104 | + errno = EINVAL; |
| 105 | + return -1; |
| 106 | +} |
| 107 | + |
| 108 | +__attribute__((weak)) |
| 109 | +int _getpid(void) |
| 110 | +{ |
| 111 | + return 1; |
| 112 | +} |
0 commit comments