blob: 186008ce16affe370aa1550460493c761fb45357 [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
Raef Coles91261542024-03-04 10:09:59 +000052uint32_t stdio_is_initialized = 0;
53
Kevin Peng524e3ec2020-05-29 10:28:26 +080054/* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
Edison Ai1c266ae2019-03-20 11:21:21 +080055#if defined(__ARMCC_VERSION)
TTornblomc640e072019-06-14 14:33:51 +020056/* Struct FILE is implemented in stdio.h. Used to redirect printf to
Kevin Peng524e3ec2020-05-29 10:28:26 +080057 * STDIO_DRIVER
TTornblomc640e072019-06-14 14:33:51 +020058 */
59FILE __stdout;
Gabor Abonyibe220c92023-11-22 11:50:01 +010060FILE __stderr;
Edison Ai1c266ae2019-03-20 11:21:21 +080061/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
62int fputc(int ch, FILE *f)
63{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080064 (void)f;
65
Edison Ai1c266ae2019-03-20 11:21:21 +080066 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080067 (void)stdio_output_string((const unsigned char *)&ch, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +080068
69 /* Return character written */
70 return ch;
71}
72#elif defined(__GNUC__)
Kevin Peng524e3ec2020-05-29 10:28:26 +080073/* Redirects printf to STDIO_DRIVER in case of GNUARM */
Edison Ai1c266ae2019-03-20 11:21:21 +080074int _write(int fd, char *str, int len)
75{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080076 (void)fd;
Edison Ai1c266ae2019-03-20 11:21:21 +080077
Mingyang Sun1ec6e262019-10-23 22:24:22 +080078 /* Send string and return the number of characters written */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080079 return stdio_output_string((const unsigned char *)str, (uint32_t)len);
Edison Ai1c266ae2019-03-20 11:21:21 +080080}
TTornblomc640e072019-06-14 14:33:51 +020081#elif defined(__ICCARM__)
82int putchar(int ch)
83{
84 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080085 (void)stdio_output_string((const unsigned char *)&ch, 1);
TTornblomc640e072019-06-14 14:33:51 +020086
87 /* Return character written */
88 return ch;
89}
Edison Ai1c266ae2019-03-20 11:21:21 +080090#endif
91
92void stdio_init(void)
93{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080094 int32_t ret;
Kevin Peng524e3ec2020-05-29 10:28:26 +080095 ret = STDIO_DRIVER.Initialize(NULL);
Edison Ai1c266ae2019-03-20 11:21:21 +080096 ASSERT_HIGH(ret);
97
Kevin Peng524e3ec2020-05-29 10:28:26 +080098 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080099 ASSERT_HIGH(ret);
100
Joakim Anderssonbfeb3b92023-04-03 16:35:04 +0200101 ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS,
Kevin Peng524e3ec2020-05-29 10:28:26 +0800102 DEFAULT_UART_BAUDRATE);
Edison Ai1c266ae2019-03-20 11:21:21 +0800103 ASSERT_HIGH(ret);
TTornblomae979882020-04-24 13:39:23 +0200104 (void)ret;
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800105
Kevin Peng524e3ec2020-05-29 10:28:26 +0800106 (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
Raef Coles91261542024-03-04 10:09:59 +0000107
108 stdio_is_initialized = true;
Edison Ai1c266ae2019-03-20 11:21:21 +0800109}
110
111void stdio_uninit(void)
112{
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800113 int32_t ret;
114
Kevin Peng524e3ec2020-05-29 10:28:26 +0800115 (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800116
Kevin Peng524e3ec2020-05-29 10:28:26 +0800117 ret = STDIO_DRIVER.Uninitialize();
Edison Ai1c266ae2019-03-20 11:21:21 +0800118 ASSERT_HIGH(ret);
TTornblomae979882020-04-24 13:39:23 +0200119 (void)ret;
Raef Coles91261542024-03-04 10:09:59 +0000120
121 stdio_is_initialized = false;
Edison Ai1c266ae2019-03-20 11:21:21 +0800122}