blob: fec035ada7ddc3354d0dba55fe8dfcd1fb75945b [file] [log] [blame]
Edison Ai1c266ae2019-03-20 11:21:21 +08001/*
Gabor Abonyia6a7f082023-11-22 10:27:43 +01002 * Copyright (c) 2017-2023 ARM Limited
Edison Ai1c266ae2019-03-20 11:21:21 +08003 *
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 Ai1c266ae2019-03-20 11:21:21 +080017#include <assert.h>
18#include <stdio.h>
Antonio de Angelis529b4942024-10-20 20:19:19 +010019#include <stdbool.h>
20#include <stdint.h>
Edison Ai1c266ae2019-03-20 11:21:21 +080021#include "Driver_USART.h"
22#include "target_cfg.h"
Mate Toth-Palf8f773f2019-09-11 16:28:08 +020023#include "device_cfg.h"
Edison Ai1c266ae2019-03-20 11:21:21 +080024
Edison Ai1c266ae2019-03-20 11:21:21 +080025/* Imports USART driver */
Kevin Peng524e3ec2020-05-29 10:28:26 +080026#if DOMAIN_NS == 1U
27extern ARM_DRIVER_USART NS_DRIVER_STDIO;
28#define STDIO_DRIVER NS_DRIVER_STDIO
29#else
Edison Ai1c266ae2019-03-20 11:21:21 +080030extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
Kevin Peng524e3ec2020-05-29 10:28:26 +080031#define STDIO_DRIVER TFM_DRIVER_STDIO
32#endif
Edison Ai1c266ae2019-03-20 11:21:21 +080033
Antonio de Angelis529b4942024-10-20 20:19:19 +010034static bool is_initialized = false;
35
Antonio de Angelis4f021312024-11-09 21:02:33 +000036int stdio_output_string(const char *str, uint32_t len)
Edison Ai1c266ae2019-03-20 11:21:21 +080037{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080038 int32_t ret;
Edison Ai1c266ae2019-03-20 11:21:21 +080039
Kevin Peng524e3ec2020-05-29 10:28:26 +080040 ret = STDIO_DRIVER.Send(str, len);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080041 if (ret != ARM_DRIVER_OK) {
42 return 0;
43 }
Antonio de Angelis529b4942024-10-20 20:19:19 +010044
Antonio de Angelis699b3282024-11-11 16:33:18 +000045 /* 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 Peng524e3ec2020-05-29 10:28:26 +080051 while (STDIO_DRIVER.GetStatus().tx_busy);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080052
Kevin Peng524e3ec2020-05-29 10:28:26 +080053 return STDIO_DRIVER.GetTxCount();
Edison Ai1c266ae2019-03-20 11:21:21 +080054}
55
Antonio de Angelis529b4942024-10-20 20:19:19 +010056void stdio_is_initialized_reset(void)
57{
58 is_initialized = false;
59}
60
61bool stdio_is_initialized(void)
62{
63 return is_initialized;
64}
Raef Coles91261542024-03-04 10:09:59 +000065
Kevin Peng524e3ec2020-05-29 10:28:26 +080066/* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
Edison Ai1c266ae2019-03-20 11:21:21 +080067#if defined(__ARMCC_VERSION)
TTornblomc640e072019-06-14 14:33:51 +020068/* Struct FILE is implemented in stdio.h. Used to redirect printf to
Kevin Peng524e3ec2020-05-29 10:28:26 +080069 * STDIO_DRIVER
TTornblomc640e072019-06-14 14:33:51 +020070 */
71FILE __stdout;
Gabor Abonyibe220c92023-11-22 11:50:01 +010072FILE __stderr;
Edison Ai1c266ae2019-03-20 11:21:21 +080073/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
74int fputc(int ch, FILE *f)
75{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080076 (void)f;
77
Edison Ai1c266ae2019-03-20 11:21:21 +080078 /* Send byte to USART */
Antonio de Angelis4f021312024-11-09 21:02:33 +000079 (void)stdio_output_string((const char *)&ch, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +080080
81 /* Return character written */
82 return ch;
83}
Anton Komlevc88e2ac2024-09-12 16:46:39 +010084
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
90int 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
101static FILE __stdio = FDEV_SETUP_STREAM(fputch, NULL, NULL, _FDEV_SETUP_WRITE);
102FILE *const stdin = &__stdio;
103__strong_reference(stdin, stdout);
104__strong_reference(stdin, stderr);
105
Edison Ai1c266ae2019-03-20 11:21:21 +0800106#elif defined(__GNUC__)
Kevin Peng524e3ec2020-05-29 10:28:26 +0800107/* Redirects printf to STDIO_DRIVER in case of GNUARM */
Edison Ai1c266ae2019-03-20 11:21:21 +0800108int _write(int fd, char *str, int len)
109{
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800110 (void)fd;
Edison Ai1c266ae2019-03-20 11:21:21 +0800111
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800112 /* Send string and return the number of characters written */
Antonio de Angelis4f021312024-11-09 21:02:33 +0000113 return stdio_output_string(str, (uint32_t)len);
Edison Ai1c266ae2019-03-20 11:21:21 +0800114}
TTornblomc640e072019-06-14 14:33:51 +0200115#elif defined(__ICCARM__)
116int putchar(int ch)
117{
118 /* Send byte to USART */
Antonio de Angelis4f021312024-11-09 21:02:33 +0000119 (void)stdio_output_string((const char *)&ch, 1);
TTornblomc640e072019-06-14 14:33:51 +0200120
121 /* Return character written */
122 return ch;
123}
Edison Ai1c266ae2019-03-20 11:21:21 +0800124#endif
125
126void stdio_init(void)
127{
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000128 int32_t ret = ARM_DRIVER_ERROR;
129
Kevin Peng524e3ec2020-05-29 10:28:26 +0800130 ret = STDIO_DRIVER.Initialize(NULL);
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000131 if (ret != ARM_DRIVER_OK) {
132 assert(0);
133 return;
134 }
Edison Ai1c266ae2019-03-20 11:21:21 +0800135
Kevin Peng524e3ec2020-05-29 10:28:26 +0800136 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000137 if (ret != ARM_DRIVER_OK) {
138 assert(0);
139 return;
140 }
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800141
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000142 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 Sun1ec6e262019-10-23 22:24:22 +0800147
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000148 ret = STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
149 if (ret != ARM_DRIVER_OK) {
150 assert(0);
151 return;
152 }
Raef Coles91261542024-03-04 10:09:59 +0000153
Antonio de Angelis529b4942024-10-20 20:19:19 +0100154 is_initialized = true;
Edison Ai1c266ae2019-03-20 11:21:21 +0800155}
156
157void stdio_uninit(void)
158{
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000159 int32_t ret = ARM_DRIVER_ERROR;
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800160
Antonio de Angelis699b3282024-11-11 16:33:18 +0000161 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 Peng524e3ec2020-05-29 10:28:26 +0800170 ret = STDIO_DRIVER.Uninitialize();
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000171 if (ret != ARM_DRIVER_OK) {
172 assert(0);
173 return;
174 }
Raef Coles91261542024-03-04 10:09:59 +0000175
Antonio de Angelis529b4942024-10-20 20:19:19 +0100176 is_initialized = false;
Edison Ai1c266ae2019-03-20 11:21:21 +0800177}