Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 1 | /* |
Gabor Abonyi | a6a7f08 | 2023-11-22 10:27:43 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 ARM Limited |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 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 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <stdio.h> |
Antonio de Angelis | 529b494 | 2024-10-20 20:19:19 +0100 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | #include <stdint.h> |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 21 | #include "Driver_USART.h" |
| 22 | #include "target_cfg.h" |
Mate Toth-Pal | f8f773f | 2019-09-11 16:28:08 +0200 | [diff] [blame] | 23 | #include "device_cfg.h" |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 24 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 25 | /* Imports USART driver */ |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 26 | #if DOMAIN_NS == 1U |
| 27 | extern ARM_DRIVER_USART NS_DRIVER_STDIO; |
| 28 | #define STDIO_DRIVER NS_DRIVER_STDIO |
| 29 | #else |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 30 | extern ARM_DRIVER_USART TFM_DRIVER_STDIO; |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 31 | #define STDIO_DRIVER TFM_DRIVER_STDIO |
| 32 | #endif |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 33 | |
Antonio de Angelis | 529b494 | 2024-10-20 20:19:19 +0100 | [diff] [blame] | 34 | static bool is_initialized = false; |
| 35 | |
Antonio de Angelis | 4f02131 | 2024-11-09 21:02:33 +0000 | [diff] [blame] | 36 | int stdio_output_string(const char *str, uint32_t len) |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 37 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 38 | int32_t ret; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 39 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 40 | ret = STDIO_DRIVER.Send(str, len); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 41 | if (ret != ARM_DRIVER_OK) { |
| 42 | return 0; |
| 43 | } |
Antonio de Angelis | 529b494 | 2024-10-20 20:19:19 +0100 | [diff] [blame] | 44 | |
Antonio de Angelis | 699b328 | 2024-11-11 16:33:18 +0000 | [diff] [blame] | 45 | /* Busy wait after Send(). CMSIS mandates the Send() to be non-blocking, |
| 46 | * while TF-M's current implementation expects to block on Send(), i.e. |
| 47 | * polling the tx_busy itself in driver code. For this reason the below |
| 48 | * busy wait does not have any practical effect, but we keep it in place |
| 49 | * for those platforms which might decide to implement IRQ-based UART |
| 50 | */ |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 51 | while (STDIO_DRIVER.GetStatus().tx_busy); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 52 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 53 | return STDIO_DRIVER.GetTxCount(); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Antonio de Angelis | 529b494 | 2024-10-20 20:19:19 +0100 | [diff] [blame] | 56 | void stdio_is_initialized_reset(void) |
| 57 | { |
| 58 | is_initialized = false; |
| 59 | } |
| 60 | |
| 61 | bool stdio_is_initialized(void) |
| 62 | { |
| 63 | return is_initialized; |
| 64 | } |
Raef Coles | 9126154 | 2024-03-04 10:09:59 +0000 | [diff] [blame] | 65 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 66 | /* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 67 | #if defined(__ARMCC_VERSION) |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 68 | /* Struct FILE is implemented in stdio.h. Used to redirect printf to |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 69 | * STDIO_DRIVER |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 70 | */ |
| 71 | FILE __stdout; |
Gabor Abonyi | be220c9 | 2023-11-22 11:50:01 +0100 | [diff] [blame] | 72 | FILE __stderr; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 73 | /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */ |
| 74 | int fputc(int ch, FILE *f) |
| 75 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 76 | (void)f; |
| 77 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 78 | /* Send byte to USART */ |
Antonio de Angelis | 4f02131 | 2024-11-09 21:02:33 +0000 | [diff] [blame] | 79 | (void)stdio_output_string((const char *)&ch, 1); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 80 | |
| 81 | /* Return character written */ |
| 82 | return ch; |
| 83 | } |
Anton Komlev | c88e2ac | 2024-09-12 16:46:39 +0100 | [diff] [blame^] | 84 | |
| 85 | /* Redirect sdtio for PicoLib in LLVM toolchain |
| 86 | as per https://github.com/picolibc/picolibc/blob/main/doc/os.md |
| 87 | 'fputch()' named intentionally different from 'fputc()' from picolib */ |
| 88 | #elif defined(__clang_major__) |
| 89 | |
| 90 | int fputch(char ch, struct __file *f) |
| 91 | { |
| 92 | (void)f; |
| 93 | |
| 94 | /* Send byte to USART */ |
| 95 | (void)stdio_output_string((const char *)&ch, 1); |
| 96 | |
| 97 | /* Return character written */ |
| 98 | return ch; |
| 99 | } |
| 100 | |
| 101 | static FILE __stdio = FDEV_SETUP_STREAM(fputch, NULL, NULL, _FDEV_SETUP_WRITE); |
| 102 | FILE *const stdin = &__stdio; |
| 103 | __strong_reference(stdin, stdout); |
| 104 | __strong_reference(stdin, stderr); |
| 105 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 106 | #elif defined(__GNUC__) |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 107 | /* Redirects printf to STDIO_DRIVER in case of GNUARM */ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 108 | int _write(int fd, char *str, int len) |
| 109 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 110 | (void)fd; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 111 | |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 112 | /* Send string and return the number of characters written */ |
Antonio de Angelis | 4f02131 | 2024-11-09 21:02:33 +0000 | [diff] [blame] | 113 | return stdio_output_string(str, (uint32_t)len); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 114 | } |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 115 | #elif defined(__ICCARM__) |
| 116 | int putchar(int ch) |
| 117 | { |
| 118 | /* Send byte to USART */ |
Antonio de Angelis | 4f02131 | 2024-11-09 21:02:33 +0000 | [diff] [blame] | 119 | (void)stdio_output_string((const char *)&ch, 1); |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 120 | |
| 121 | /* Return character written */ |
| 122 | return ch; |
| 123 | } |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 124 | #endif |
| 125 | |
| 126 | void stdio_init(void) |
| 127 | { |
Antonio de Angelis | 14dc3f0 | 2024-11-06 11:51:41 +0000 | [diff] [blame] | 128 | int32_t ret = ARM_DRIVER_ERROR; |
| 129 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 130 | ret = STDIO_DRIVER.Initialize(NULL); |
Antonio de Angelis | 14dc3f0 | 2024-11-06 11:51:41 +0000 | [diff] [blame] | 131 | if (ret != ARM_DRIVER_OK) { |
| 132 | assert(0); |
| 133 | return; |
| 134 | } |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 135 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 136 | ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL); |
Antonio de Angelis | 14dc3f0 | 2024-11-06 11:51:41 +0000 | [diff] [blame] | 137 | if (ret != ARM_DRIVER_OK) { |
| 138 | assert(0); |
| 139 | return; |
| 140 | } |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 141 | |
Antonio de Angelis | 14dc3f0 | 2024-11-06 11:51:41 +0000 | [diff] [blame] | 142 | ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS, DEFAULT_UART_BAUDRATE); |
| 143 | if (ret != ARM_DRIVER_OK) { |
| 144 | assert(0); |
| 145 | return; |
| 146 | } |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 147 | |
Antonio de Angelis | 14dc3f0 | 2024-11-06 11:51:41 +0000 | [diff] [blame] | 148 | ret = STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1); |
| 149 | if (ret != ARM_DRIVER_OK) { |
| 150 | assert(0); |
| 151 | return; |
| 152 | } |
Raef Coles | 9126154 | 2024-03-04 10:09:59 +0000 | [diff] [blame] | 153 | |
Antonio de Angelis | 529b494 | 2024-10-20 20:19:19 +0100 | [diff] [blame] | 154 | is_initialized = true; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void stdio_uninit(void) |
| 158 | { |
Antonio de Angelis | 14dc3f0 | 2024-11-06 11:51:41 +0000 | [diff] [blame] | 159 | int32_t ret = ARM_DRIVER_ERROR; |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 160 | |
Antonio de Angelis | 699b328 | 2024-11-11 16:33:18 +0000 | [diff] [blame] | 161 | ret = STDIO_DRIVER.PowerControl(ARM_POWER_OFF); |
| 162 | /* FixMe: Still allow this function not to be implemented as in blocking |
| 163 | * mode there is not much that needs to be done when powering off |
| 164 | */ |
| 165 | if ((ret != ARM_DRIVER_OK) && (ret != ARM_DRIVER_ERROR_UNSUPPORTED)) { |
| 166 | assert(0); |
| 167 | return; |
| 168 | } |
| 169 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 170 | ret = STDIO_DRIVER.Uninitialize(); |
Antonio de Angelis | 14dc3f0 | 2024-11-06 11:51:41 +0000 | [diff] [blame] | 171 | if (ret != ARM_DRIVER_OK) { |
| 172 | assert(0); |
| 173 | return; |
| 174 | } |
Raef Coles | 9126154 | 2024-03-04 10:09:59 +0000 | [diff] [blame] | 175 | |
Antonio de Angelis | 529b494 | 2024-10-20 20:19:19 +0100 | [diff] [blame] | 176 | is_initialized = false; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 177 | } |