Harald Welte | 361ed20 | 2019-02-24 21:15:39 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * |
| 4 | * \brief STDIO redirection |
| 5 | * |
| 6 | * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries. |
| 7 | * |
| 8 | * \asf_license_start |
| 9 | * |
| 10 | * \page License |
| 11 | * |
| 12 | * Subject to your compliance with these terms, you may use Microchip |
| 13 | * software and any derivatives exclusively with Microchip products. |
| 14 | * It is your responsibility to comply with third party license terms applicable |
| 15 | * to your use of third party software (including open source software) that |
| 16 | * may accompany Microchip software. |
| 17 | * |
| 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 19 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 20 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 22 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 23 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 24 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 25 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 26 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 27 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 28 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 29 | * |
| 30 | * \asf_license_stop |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | |
| 36 | #ifdef _UNIT_TEST_ |
| 37 | #undef fputc |
| 38 | #undef fgetc |
| 39 | #undef ferror |
| 40 | #define fputc ut_fputc |
| 41 | #define fgetc ut_fgetc |
| 42 | #define ferror ut_ferror |
| 43 | #endif |
| 44 | |
| 45 | #include <stdio_io.h> |
| 46 | |
| 47 | /* Disable semihosting */ |
| 48 | #if defined(__GNUC__) && (__ARMCOMPILER_VERSION > 6000000) /* Keil MDK with ARM Compiler 6 */ |
| 49 | __asm(".global __use_no_semihosting\n\t"); |
| 50 | #else |
| 51 | #pragma import(__use_no_semihosting_swi) |
| 52 | #endif |
| 53 | |
| 54 | #ifndef __GNUC__ |
| 55 | struct __FILE { |
| 56 | int handle; |
| 57 | }; |
| 58 | #endif |
| 59 | FILE __stdout; |
| 60 | FILE __stdin; |
| 61 | FILE __stderr; |
| 62 | |
| 63 | int fputc(int ch, FILE *f) |
| 64 | { |
| 65 | if ((f == stdout) || (f == stderr)) { |
| 66 | uint8_t tmp = (uint8_t)ch; |
| 67 | if (stdio_io_write(&tmp, 1) < 0) { |
| 68 | return EOF; |
| 69 | } |
| 70 | return ch; |
| 71 | } else { |
| 72 | return EOF; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int fgetc(FILE *f) |
| 77 | { |
| 78 | if (f == stdin) { |
| 79 | uint8_t tmp = 0; |
| 80 | if (stdio_io_read(&tmp, 1) < 0) { |
| 81 | return EOF; |
| 82 | } |
| 83 | return tmp; |
| 84 | } else { |
| 85 | return EOF; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void _ttywrch(int ch) |
| 90 | { |
| 91 | uint8_t tmp = (uint8_t)ch; |
| 92 | stdio_io_write(&tmp, 1); |
| 93 | } |
| 94 | |
| 95 | int ferror(FILE *f) |
| 96 | { |
| 97 | (void)f; |
| 98 | /* Your implementation of ferror */ |
| 99 | return EOF; |
| 100 | } |
| 101 | |
| 102 | void _sys_exit(int return_code) |
| 103 | { |
| 104 | (void)return_code; |
| 105 | while (1) { |
| 106 | }; /* endless loop */ |
| 107 | } |