blob: fb8ce18a9b05dbfd72268e4e37932373bb0bb54f [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
17#include "uart_stdout.h"
18
19#include <assert.h>
20#include <stdio.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
25#define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
26
27/* Imports USART driver */
Kevin Peng524e3ec2020-05-29 10:28:26 +080028#if DOMAIN_NS == 1U
29extern ARM_DRIVER_USART NS_DRIVER_STDIO;
30#define STDIO_DRIVER NS_DRIVER_STDIO
31#else
Edison Ai1c266ae2019-03-20 11:21:21 +080032extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
Kevin Peng524e3ec2020-05-29 10:28:26 +080033#define STDIO_DRIVER TFM_DRIVER_STDIO
34#endif
Edison Ai1c266ae2019-03-20 11:21:21 +080035
Mingyang Sunc6b458b2019-12-19 15:07:34 +080036int stdio_output_string(const unsigned 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
Gabor Abonyia6a7f082023-11-22 10:27:43 +010040 /* Add a busy wait before sending. */
41 while (STDIO_DRIVER.GetStatus().tx_busy);
Kevin Peng524e3ec2020-05-29 10:28:26 +080042 ret = STDIO_DRIVER.Send(str, len);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080043 if (ret != ARM_DRIVER_OK) {
44 return 0;
45 }
46 /* Add a busy wait after sending. */
Kevin Peng524e3ec2020-05-29 10:28:26 +080047 while (STDIO_DRIVER.GetStatus().tx_busy);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080048
Kevin Peng524e3ec2020-05-29 10:28:26 +080049 return STDIO_DRIVER.GetTxCount();
Edison Ai1c266ae2019-03-20 11:21:21 +080050}
51
Kevin Peng524e3ec2020-05-29 10:28:26 +080052/* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
Edison Ai1c266ae2019-03-20 11:21:21 +080053#if defined(__ARMCC_VERSION)
TTornblomc640e072019-06-14 14:33:51 +020054/* Struct FILE is implemented in stdio.h. Used to redirect printf to
Kevin Peng524e3ec2020-05-29 10:28:26 +080055 * STDIO_DRIVER
TTornblomc640e072019-06-14 14:33:51 +020056 */
57FILE __stdout;
Gabor Abonyibe220c92023-11-22 11:50:01 +010058FILE __stderr;
Edison Ai1c266ae2019-03-20 11:21:21 +080059/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
60int fputc(int ch, FILE *f)
61{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080062 (void)f;
63
Edison Ai1c266ae2019-03-20 11:21:21 +080064 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080065 (void)stdio_output_string((const unsigned char *)&ch, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +080066
67 /* Return character written */
68 return ch;
69}
70#elif defined(__GNUC__)
Kevin Peng524e3ec2020-05-29 10:28:26 +080071/* Redirects printf to STDIO_DRIVER in case of GNUARM */
Edison Ai1c266ae2019-03-20 11:21:21 +080072int _write(int fd, char *str, int len)
73{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080074 (void)fd;
Edison Ai1c266ae2019-03-20 11:21:21 +080075
Mingyang Sun1ec6e262019-10-23 22:24:22 +080076 /* Send string and return the number of characters written */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080077 return stdio_output_string((const unsigned char *)str, (uint32_t)len);
Edison Ai1c266ae2019-03-20 11:21:21 +080078}
TTornblomc640e072019-06-14 14:33:51 +020079#elif defined(__ICCARM__)
80int putchar(int ch)
81{
82 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080083 (void)stdio_output_string((const unsigned char *)&ch, 1);
TTornblomc640e072019-06-14 14:33:51 +020084
85 /* Return character written */
86 return ch;
87}
Edison Ai1c266ae2019-03-20 11:21:21 +080088#endif
89
90void stdio_init(void)
91{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080092 int32_t ret;
Kevin Peng524e3ec2020-05-29 10:28:26 +080093 ret = STDIO_DRIVER.Initialize(NULL);
Edison Ai1c266ae2019-03-20 11:21:21 +080094 ASSERT_HIGH(ret);
95
Kevin Peng524e3ec2020-05-29 10:28:26 +080096 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080097 ASSERT_HIGH(ret);
98
Joakim Anderssonbfeb3b92023-04-03 16:35:04 +020099 ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS,
Kevin Peng524e3ec2020-05-29 10:28:26 +0800100 DEFAULT_UART_BAUDRATE);
Edison Ai1c266ae2019-03-20 11:21:21 +0800101 ASSERT_HIGH(ret);
TTornblomae979882020-04-24 13:39:23 +0200102 (void)ret;
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800103
Kevin Peng524e3ec2020-05-29 10:28:26 +0800104 (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +0800105}
106
107void stdio_uninit(void)
108{
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800109 int32_t ret;
110
Kevin Peng524e3ec2020-05-29 10:28:26 +0800111 (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800112
Kevin Peng524e3ec2020-05-29 10:28:26 +0800113 ret = STDIO_DRIVER.Uninitialize();
Edison Ai1c266ae2019-03-20 11:21:21 +0800114 ASSERT_HIGH(ret);
TTornblomae979882020-04-24 13:39:23 +0200115 (void)ret;
Edison Ai1c266ae2019-03-20 11:21:21 +0800116}