Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 ARM Limited |
| 3 | * |
| 4 | * Licensed under the Apace License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apace.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "uart_stdout.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include "Driver_USART.h" |
| 23 | #include "target_cfg.h" |
| 24 | |
| 25 | #define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK) |
| 26 | |
| 27 | /* Imports USART driver */ |
| 28 | extern ARM_DRIVER_USART TFM_DRIVER_STDIO; |
| 29 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 30 | static void uart_putc(unsigned char c) |
| 31 | { |
| 32 | int32_t ret = ARM_DRIVER_OK; |
| 33 | |
| 34 | ret = TFM_DRIVER_STDIO.Send(&c, 1); |
| 35 | ASSERT_HIGH(ret); |
| 36 | } |
| 37 | |
| 38 | /* Redirects printf to TFM_DRIVER_STDIO in case of ARMCLANG*/ |
| 39 | #if defined(__ARMCC_VERSION) |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 40 | /* Struct FILE is implemented in stdio.h. Used to redirect printf to |
| 41 | * TFM_DRIVER_STDIO |
| 42 | */ |
| 43 | FILE __stdout; |
| 44 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 45 | /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */ |
| 46 | int fputc(int ch, FILE *f) |
| 47 | { |
| 48 | /* Send byte to USART */ |
| 49 | uart_putc(ch); |
| 50 | |
| 51 | /* Return character written */ |
| 52 | return ch; |
| 53 | } |
| 54 | #elif defined(__GNUC__) |
| 55 | /* Redirects printf to TFM_DRIVER_STDIO in case of GNUARM */ |
| 56 | int _write(int fd, char *str, int len) |
| 57 | { |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < len; i++) { |
| 61 | /* Send byte to USART */ |
| 62 | uart_putc(str[i]); |
| 63 | } |
| 64 | |
| 65 | /* Return the number of characters written */ |
| 66 | return len; |
| 67 | } |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 68 | #elif defined(__ICCARM__) |
| 69 | int putchar(int ch) |
| 70 | { |
| 71 | /* Send byte to USART */ |
| 72 | uart_putc(ch); |
| 73 | |
| 74 | /* Return character written */ |
| 75 | return ch; |
| 76 | } |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | void stdio_init(void) |
| 80 | { |
| 81 | int32_t ret = ARM_DRIVER_OK; |
| 82 | ret = TFM_DRIVER_STDIO.Initialize(NULL); |
| 83 | ASSERT_HIGH(ret); |
| 84 | |
| 85 | ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS, 115200); |
| 86 | ASSERT_HIGH(ret); |
| 87 | } |
| 88 | |
| 89 | void stdio_uninit(void) |
| 90 | { |
| 91 | int32_t ret = ARM_DRIVER_OK; |
| 92 | ret = TFM_DRIVER_STDIO.Uninitialize(); |
| 93 | ASSERT_HIGH(ret); |
| 94 | } |
| 95 | |