blob: 6e9277f3234232d665872574e7591fc6b37cf1b2 [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}
84#elif defined(__GNUC__)
Kevin Peng524e3ec2020-05-29 10:28:26 +080085/* Redirects printf to STDIO_DRIVER in case of GNUARM */
Edison Ai1c266ae2019-03-20 11:21:21 +080086int _write(int fd, char *str, int len)
87{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080088 (void)fd;
Edison Ai1c266ae2019-03-20 11:21:21 +080089
Mingyang Sun1ec6e262019-10-23 22:24:22 +080090 /* Send string and return the number of characters written */
Antonio de Angelis4f021312024-11-09 21:02:33 +000091 return stdio_output_string(str, (uint32_t)len);
Edison Ai1c266ae2019-03-20 11:21:21 +080092}
TTornblomc640e072019-06-14 14:33:51 +020093#elif defined(__ICCARM__)
94int putchar(int ch)
95{
96 /* Send byte to USART */
Antonio de Angelis4f021312024-11-09 21:02:33 +000097 (void)stdio_output_string((const char *)&ch, 1);
TTornblomc640e072019-06-14 14:33:51 +020098
99 /* Return character written */
100 return ch;
101}
Edison Ai1c266ae2019-03-20 11:21:21 +0800102#endif
103
104void stdio_init(void)
105{
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000106 int32_t ret = ARM_DRIVER_ERROR;
107
Kevin Peng524e3ec2020-05-29 10:28:26 +0800108 ret = STDIO_DRIVER.Initialize(NULL);
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000109 if (ret != ARM_DRIVER_OK) {
110 assert(0);
111 return;
112 }
Edison Ai1c266ae2019-03-20 11:21:21 +0800113
Kevin Peng524e3ec2020-05-29 10:28:26 +0800114 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000115 if (ret != ARM_DRIVER_OK) {
116 assert(0);
117 return;
118 }
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800119
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000120 ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS, DEFAULT_UART_BAUDRATE);
121 if (ret != ARM_DRIVER_OK) {
122 assert(0);
123 return;
124 }
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800125
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000126 ret = STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
127 if (ret != ARM_DRIVER_OK) {
128 assert(0);
129 return;
130 }
Raef Coles91261542024-03-04 10:09:59 +0000131
Antonio de Angelis529b4942024-10-20 20:19:19 +0100132 is_initialized = true;
Edison Ai1c266ae2019-03-20 11:21:21 +0800133}
134
135void stdio_uninit(void)
136{
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000137 int32_t ret = ARM_DRIVER_ERROR;
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800138
Antonio de Angelis699b3282024-11-11 16:33:18 +0000139 ret = STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
140 /* FixMe: Still allow this function not to be implemented as in blocking
141 * mode there is not much that needs to be done when powering off
142 */
143 if ((ret != ARM_DRIVER_OK) && (ret != ARM_DRIVER_ERROR_UNSUPPORTED)) {
144 assert(0);
145 return;
146 }
147
Kevin Peng524e3ec2020-05-29 10:28:26 +0800148 ret = STDIO_DRIVER.Uninitialize();
Antonio de Angelis14dc3f02024-11-06 11:51:41 +0000149 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 = false;
Edison Ai1c266ae2019-03-20 11:21:21 +0800155}