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 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 52 | /* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 53 | #if defined(__ARMCC_VERSION) |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 54 | /* Struct FILE is implemented in stdio.h. Used to redirect printf to |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 55 | * STDIO_DRIVER |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 56 | */ |
| 57 | FILE __stdout; |
Gabor Abonyi | be220c9 | 2023-11-22 11:50:01 +0100 | [diff] [blame^] | 58 | FILE __stderr; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 59 | /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */ |
| 60 | int fputc(int ch, FILE *f) |
| 61 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 62 | (void)f; |
| 63 | |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 64 | /* Send byte to USART */ |
Mingyang Sun | c6b458b | 2019-12-19 15:07:34 +0800 | [diff] [blame] | 65 | (void)stdio_output_string((const unsigned char *)&ch, 1); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 66 | |
| 67 | /* Return character written */ |
| 68 | return ch; |
| 69 | } |
| 70 | #elif defined(__GNUC__) |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 71 | /* Redirects printf to STDIO_DRIVER in case of GNUARM */ |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 72 | int _write(int fd, char *str, int len) |
| 73 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 74 | (void)fd; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 75 | |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 76 | /* Send string and return the number of characters written */ |
Mingyang Sun | c6b458b | 2019-12-19 15:07:34 +0800 | [diff] [blame] | 77 | return stdio_output_string((const unsigned char *)str, (uint32_t)len); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 78 | } |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 79 | #elif defined(__ICCARM__) |
| 80 | int putchar(int ch) |
| 81 | { |
| 82 | /* Send byte to USART */ |
Mingyang Sun | c6b458b | 2019-12-19 15:07:34 +0800 | [diff] [blame] | 83 | (void)stdio_output_string((const unsigned char *)&ch, 1); |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 84 | |
| 85 | /* Return character written */ |
| 86 | return ch; |
| 87 | } |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 88 | #endif |
| 89 | |
| 90 | void stdio_init(void) |
| 91 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 92 | int32_t ret; |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 93 | ret = STDIO_DRIVER.Initialize(NULL); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 94 | ASSERT_HIGH(ret); |
| 95 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 96 | ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 97 | ASSERT_HIGH(ret); |
| 98 | |
Joakim Andersson | bfeb3b9 | 2023-04-03 16:35:04 +0200 | [diff] [blame] | 99 | ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS, |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 100 | DEFAULT_UART_BAUDRATE); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 101 | ASSERT_HIGH(ret); |
TTornblom | ae97988 | 2020-04-24 13:39:23 +0200 | [diff] [blame] | 102 | (void)ret; |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 103 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 104 | (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void stdio_uninit(void) |
| 108 | { |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 109 | int32_t ret; |
| 110 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 111 | (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF); |
Mingyang Sun | 1ec6e26 | 2019-10-23 22:24:22 +0800 | [diff] [blame] | 112 | |
Kevin Peng | 524e3ec | 2020-05-29 10:28:26 +0800 | [diff] [blame] | 113 | ret = STDIO_DRIVER.Uninitialize(); |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 114 | ASSERT_HIGH(ret); |
TTornblom | ae97988 | 2020-04-24 13:39:23 +0200 | [diff] [blame] | 115 | (void)ret; |
Edison Ai | 1c266ae | 2019-03-20 11:21:21 +0800 | [diff] [blame] | 116 | } |