123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- #include <errno.h>
- #include <sys/stat.h>
- #include <sys/times.h>
- #include <sys/unistd.h>
- #include "stm32f4xx_usart.h"
- #include "stm32f4xx.h"
- #ifdef SWOTRACE
- #define STDIO_USART 255
- #else
- #define STDIO_USART 1
- #endif
- #ifndef STDOUT_USART
- #define STDOUT_USART STDIO_USART
- #endif
- #ifndef STDERR_USART
- #define STDERR_USART STDIO_USART
- #endif
- #ifndef STDIN_USART
- #define STDIN_USART STDIO_USART
- #endif
- #undef errno
- extern int errno;
- volatile int32_t ITM_RxBuffer;
- char *__env[1] = { 0 };
- char **environ = __env;
- int _write(int file, char *ptr, int len);
- void _exit(int status) {
- _write(1, "exit", 4);
- while (1) {
- ;
- }
- }
- int _close(int file) {
- return -1;
- }
- int _execve(char *name, char **argv, char **env) {
- errno = ENOMEM;
- return -1;
- }
- int _fork() {
- errno = EAGAIN;
- return -1;
- }
- int _fstat(int file, struct stat *st) {
- st->st_mode = S_IFCHR;
- return 0;
- }
- int _getpid() {
- return 1;
- }
- int _isatty(int file) {
- switch (file){
- case STDOUT_FILENO:
- case STDERR_FILENO:
- case STDIN_FILENO:
- return 1;
- default:
-
- errno = EBADF;
- return 0;
- }
- }
- int _kill(int pid, int sig) {
- errno = EINVAL;
- return (-1);
- }
- int _link(char *old, char *new) {
- errno = EMLINK;
- return -1;
- }
- int _lseek(int file, int ptr, int dir) {
- return 0;
- }
- caddr_t _sbrk(int incr) {
- extern char _ebss;
- extern char __bss_end__;
- static char *heap_end;
- char *prev_heap_end;
- if (heap_end == 0) {
-
- heap_end = &__bss_end__;
- }
- prev_heap_end = heap_end;
- char * stack = (char*) __get_MSP();
- if (heap_end + incr > stack)
- {
- _write (STDERR_FILENO, "Heap and stack collision\n", 25);
- errno = ENOMEM;
- return (caddr_t) -1;
-
- }
- heap_end += incr;
- return (caddr_t) prev_heap_end;
- }
- int _read(int file, char *ptr, int len) {
- int n;
- int num = 0;
- switch (file) {
- case STDIN_FILENO:
- for (n = 0; n < len; n++) {
- #if STDIN_USART == 1
- while ((USART1->SR & USART_FLAG_RXNE) == (uint16_t)RESET) {}
- char c = (char)(USART1->DR & (uint16_t)0x01FF);
- #elif STDIN_USART == 2
- while ((USART2->SR & USART_FLAG_RXNE) == (uint16_t) RESET) {}
- char c = (char) (USART2->DR & (uint16_t) 0x01FF);
- #elif STDIN_USART == 3
- while ((USART3->SR & USART_FLAG_RXNE) == (uint16_t)RESET) {}
- char c = (char)(USART3->DR & (uint16_t)0x01FF);
- #elif STDIN_USART == 255
-
- char c = (char)ITM_ReceiveChar();
- #elif STDIN_USART == 0
-
- char c = '\0';
- #endif
- *ptr++ = c;
- num++;
- }
- break;
- default:
- errno = EBADF;
- return -1;
- }
- return num;
- }
- int _stat(const char *filepath, struct stat *st) {
- st->st_mode = S_IFCHR;
- return 0;
- }
- clock_t _times(struct tms *buf) {
- return -1;
- }
- int _unlink(char *name) {
- errno = ENOENT;
- return -1;
- }
- int _wait(int *status) {
- errno = ECHILD;
- return -1;
- }
- int _write(int file, char *ptr, int len) {
- int n;
- switch (file) {
- case STDOUT_FILENO:
- for (n = 0; n < len; n++) {
- #if STDOUT_USART == 1
- while ((USART1->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
- USART1->DR = (*ptr++ & (uint16_t)0x01FF);
- #elif STDOUT_USART == 2
- while ((USART2->SR & USART_FLAG_TC) == (uint16_t) RESET) {
- }
- USART2->DR = (*ptr++ & (uint16_t) 0x01FF);
- #elif STDOUT_USART == 3
- while ((USART3->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
- USART3->DR = (*ptr++ & (uint16_t)0x01FF);
- #elif STDOUT_USART == 255
- ITM_SendChar(*ptr++ & (uint16_t)0x00FF);
- #endif
- }
- break;
- case STDERR_FILENO:
- for (n = 0; n < len; n++) {
- #if STDERR_USART == 1
- while ((USART1->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
- USART1->DR = (*ptr++ & (uint16_t)0x01FF);
- #elif STDERR_USART == 2
- while ((USART2->SR & USART_FLAG_TC) == (uint16_t) RESET) {
- }
- USART2->DR = (*ptr++ & (uint16_t) 0x01FF);
- #elif STDERR_USART == 3
- while ((USART3->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
- USART3->DR = (*ptr++ & (uint16_t)0x01FF);
- #elif STDERR_USART == 255
- ITM_SendChar(*ptr++ & (uint16_t)0x00FF);
- #endif
- }
- break;
- default:
- errno = EBADF;
- return -1;
- }
- return len;
- }
|