blob: 1797357a54c6ccde032d58f52e12fe804ee45907 [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;
Edison Ai1c266ae2019-03-20 11:21:21 +080058/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
59int fputc(int ch, FILE *f)
60{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080061 (void)f;
62
Edison Ai1c266ae2019-03-20 11:21:21 +080063 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080064 (void)stdio_output_string((const unsigned char *)&ch, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +080065
66 /* Return character written */
67 return ch;
68}
69#elif defined(__GNUC__)
Kevin Peng524e3ec2020-05-29 10:28:26 +080070/* Redirects printf to STDIO_DRIVER in case of GNUARM */
Edison Ai1c266ae2019-03-20 11:21:21 +080071int _write(int fd, char *str, int len)
72{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080073 (void)fd;
Edison Ai1c266ae2019-03-20 11:21:21 +080074
Mingyang Sun1ec6e262019-10-23 22:24:22 +080075 /* Send string and return the number of characters written */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080076 return stdio_output_string((const unsigned char *)str, (uint32_t)len);
Edison Ai1c266ae2019-03-20 11:21:21 +080077}
TTornblomc640e072019-06-14 14:33:51 +020078#elif defined(__ICCARM__)
79int putchar(int ch)
80{
81 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080082 (void)stdio_output_string((const unsigned char *)&ch, 1);
TTornblomc640e072019-06-14 14:33:51 +020083
84 /* Return character written */
85 return ch;
86}
Edison Ai1c266ae2019-03-20 11:21:21 +080087#endif
88
89void stdio_init(void)
90{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080091 int32_t ret;
Kevin Peng524e3ec2020-05-29 10:28:26 +080092 ret = STDIO_DRIVER.Initialize(NULL);
Edison Ai1c266ae2019-03-20 11:21:21 +080093 ASSERT_HIGH(ret);
94
Kevin Peng524e3ec2020-05-29 10:28:26 +080095 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080096 ASSERT_HIGH(ret);
97
Joakim Anderssonbfeb3b92023-04-03 16:35:04 +020098 ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS,
Kevin Peng524e3ec2020-05-29 10:28:26 +080099 DEFAULT_UART_BAUDRATE);
Edison Ai1c266ae2019-03-20 11:21:21 +0800100 ASSERT_HIGH(ret);
TTornblomae979882020-04-24 13:39:23 +0200101 (void)ret;
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800102
Kevin Peng524e3ec2020-05-29 10:28:26 +0800103 (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +0800104}
105
106void stdio_uninit(void)
107{
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800108 int32_t ret;
109
Kevin Peng524e3ec2020-05-29 10:28:26 +0800110 (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800111
Kevin Peng524e3ec2020-05-29 10:28:26 +0800112 ret = STDIO_DRIVER.Uninitialize();
Edison Ai1c266ae2019-03-20 11:21:21 +0800113 ASSERT_HIGH(ret);
TTornblomae979882020-04-24 13:39:23 +0200114 (void)ret;
Edison Ai1c266ae2019-03-20 11:21:21 +0800115}