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 | |
| 17 | #include "uart_stdout.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <stdio.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 | |
| 25 | #define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK) |
| 26 | |
| 27 | /* Imports USART driver */ |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 28 | #if DOMAIN_NS == 1U |
| 29 | extern ARM_DRIVER_USART NS_DRIVER_STDIO; |
| 30 | #define STDIO_DRIVER NS_DRIVER_STDIO |
| 31 | #else |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 32 | extern ARM_DRIVER_USART TFM_DRIVER_STDIO; |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 33 | #define STDIO_DRIVER TFM_DRIVER_STDIO |
| 34 | #endif |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 35 | |
Mingyang Sun | c6b458b | 2019-12-19 15:07:34 +0800 | [diff] [blame] | 36 | int stdio_output_string(const unsigned 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 | |
Gabor Abonyi | a6a7f08 | 2023-11-22 10:27:43 +0100 | [diff] [blame] | 40 | /* Add a busy wait before sending. */ |
| 41 | while (STDIO_DRIVER.GetStatus().tx_busy); |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 42 | ret = STDIO_DRIVER.Send(str, len); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 43 | if (ret != ARM_DRIVER_OK) { |
| 44 | return 0; |
| 45 | } |
| 46 | /* Add a busy wait after sending. */ |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 47 | while (STDIO_DRIVER.GetStatus().tx_busy); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 48 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 49 | return STDIO_DRIVER.GetTxCount(); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 50 | } |
| 51 | |
Raef Coles | 9126154 | 2024-03-04 10:09:59 +0000 | [diff] [blame] | 52 | uint32_t stdio_is_initialized = 0; |
| 53 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 54 | /* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 55 | #if defined(__ARMCC_VERSION) |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 56 | /* Struct FILE is implemented in stdio.h. Used to redirect printf to |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 57 | * STDIO_DRIVER |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 58 | */ |
| 59 | FILE __stdout; |
Gabor Abonyi | be220c9 | 2023-11-22 11:50:01 +0100 | [diff] [blame] | 60 | FILE __stderr; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 61 | /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */ |
| 62 | int fputc(int ch, FILE *f) |
| 63 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 64 | (void)f; |
| 65 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 66 | /* Send byte to USART */ |
Mingyang Sun | c6b458b | 2019-12-19 15:07:34 +0800 | [diff] [blame] | 67 | (void)stdio_output_string((const unsigned char *)&ch, 1); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 68 | |
| 69 | /* Return character written */ |
| 70 | return ch; |
| 71 | } |
| 72 | #elif defined(__GNUC__) |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 73 | /* Redirects printf to STDIO_DRIVER in case of GNUARM */ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 74 | int _write(int fd, char *str, int len) |
| 75 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 76 | (void)fd; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 77 | |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 78 | /* Send string and return the number of characters written */ |
Mingyang Sun | c6b458b | 2019-12-19 15:07:34 +0800 | [diff] [blame] | 79 | return stdio_output_string((const unsigned char *)str, (uint32_t)len); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 80 | } |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 81 | #elif defined(__ICCARM__) |
| 82 | int putchar(int ch) |
| 83 | { |
| 84 | /* Send byte to USART */ |
Mingyang Sun | c6b458b | 2019-12-19 15:07:34 +0800 | [diff] [blame] | 85 | (void)stdio_output_string((const unsigned char *)&ch, 1); |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 86 | |
| 87 | /* Return character written */ |
| 88 | return ch; |
| 89 | } |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 90 | #endif |
| 91 | |
| 92 | void stdio_init(void) |
| 93 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 94 | int32_t ret; |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 95 | ret = STDIO_DRIVER.Initialize(NULL); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 96 | ASSERT_HIGH(ret); |
| 97 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 98 | ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 99 | ASSERT_HIGH(ret); |
| 100 | |
Joakim Andersson | bfeb3b9 | 2023-04-03 16:35:04 +0200 | [diff] [blame] | 101 | ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS, |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 102 | DEFAULT_UART_BAUDRATE); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 103 | ASSERT_HIGH(ret); |
TTornblom | ae97988 | 2020-04-24 13:39:23 +0200 | [diff] [blame] | 104 | (void)ret; |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 105 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 106 | (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1); |
Raef Coles | 9126154 | 2024-03-04 10:09:59 +0000 | [diff] [blame] | 107 | |
| 108 | stdio_is_initialized = true; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void stdio_uninit(void) |
| 112 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 113 | int32_t ret; |
| 114 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 115 | (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 116 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 117 | ret = STDIO_DRIVER.Uninitialize(); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 118 | ASSERT_HIGH(ret); |
TTornblom | ae97988 | 2020-04-24 13:39:23 +0200 | [diff] [blame] | 119 | (void)ret; |
Raef Coles | 9126154 | 2024-03-04 10:09:59 +0000 | [diff] [blame] | 120 | |
| 121 | stdio_is_initialized = false; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 122 | } |