1 /*
2 * Copyright (c) 2017-2023 ARM Limited
3 *
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>
21 #include "Driver_USART.h"
22 #include "target_cfg.h"
23 #include "device_cfg.h"
24
25 #define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
26
27 /* Imports USART driver */
28 #if DOMAIN_NS == 1U
29 extern ARM_DRIVER_USART NS_DRIVER_STDIO;
30 #define STDIO_DRIVER NS_DRIVER_STDIO
31 #else
32 extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
33 #define STDIO_DRIVER TFM_DRIVER_STDIO
34 #endif
35
stdio_output_string(const unsigned char * str,uint32_t len)36 int stdio_output_string(const unsigned char *str, uint32_t len)
37 {
38 int32_t ret;
39
40 /* Add a busy wait before sending. */
41 while (STDIO_DRIVER.GetStatus().tx_busy);
42 ret = STDIO_DRIVER.Send(str, len);
43 if (ret != ARM_DRIVER_OK) {
44 return 0;
45 }
46 /* Add a busy wait after sending. */
47 while (STDIO_DRIVER.GetStatus().tx_busy);
48
49 return STDIO_DRIVER.GetTxCount();
50 }
51
52 uint32_t stdio_is_initialized = 0;
53
54 /* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
55 #if defined(__ARMCC_VERSION)
56 /* Struct FILE is implemented in stdio.h. Used to redirect printf to
57 * STDIO_DRIVER
58 */
59 FILE __stdout;
60 FILE __stderr;
61 /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
fputc(int ch,FILE * f)62 int fputc(int ch, FILE *f)
63 {
64 (void)f;
65
66 /* Send byte to USART */
67 (void)stdio_output_string((const unsigned char *)&ch, 1);
68
69 /* Return character written */
70 return ch;
71 }
72 #elif defined(__GNUC__)
73 /* Redirects printf to STDIO_DRIVER in case of GNUARM */
_write(int fd,char * str,int len)74 int _write(int fd, char *str, int len)
75 {
76 (void)fd;
77
78 /* Send string and return the number of characters written */
79 return stdio_output_string((const unsigned char *)str, (uint32_t)len);
80 }
81 #elif defined(__ICCARM__)
putchar(int ch)82 int putchar(int ch)
83 {
84 /* Send byte to USART */
85 (void)stdio_output_string((const unsigned char *)&ch, 1);
86
87 /* Return character written */
88 return ch;
89 }
90 #endif
91
stdio_init(void)92 void stdio_init(void)
93 {
94 int32_t ret;
95 ret = STDIO_DRIVER.Initialize(NULL);
96 ASSERT_HIGH(ret);
97
98 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
99 ASSERT_HIGH(ret);
100
101 ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS,
102 DEFAULT_UART_BAUDRATE);
103 ASSERT_HIGH(ret);
104 (void)ret;
105
106 (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
107
108 stdio_is_initialized = true;
109 }
110
stdio_uninit(void)111 void stdio_uninit(void)
112 {
113 int32_t ret;
114
115 (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
116
117 ret = STDIO_DRIVER.Uninitialize();
118 ASSERT_HIGH(ret);
119 (void)ret;
120
121 stdio_is_initialized = false;
122 }
123