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 <assert.h>
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include "Driver_USART.h"
22 #include "target_cfg.h"
23 #include "device_cfg.h"
24 
25 /* Imports USART driver */
26 #if DOMAIN_NS == 1U
27 extern ARM_DRIVER_USART NS_DRIVER_STDIO;
28 #define STDIO_DRIVER    NS_DRIVER_STDIO
29 #else
30 extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
31 #define STDIO_DRIVER    TFM_DRIVER_STDIO
32 #endif
33 
34 static bool is_initialized = false;
35 
stdio_output_string(const char * str,uint32_t len)36 int stdio_output_string(const char *str, uint32_t len)
37 {
38     int32_t ret;
39 
40     if (!is_initialized) {
41         return 0;
42     }
43 
44     ret = STDIO_DRIVER.Send(str, len);
45     if (ret != ARM_DRIVER_OK) {
46         return 0;
47     }
48 
49     /* Busy wait after Send(). CMSIS mandates the Send() to be non-blocking,
50      * while TF-M's current implementation expects to block on Send(), i.e.
51      * polling the tx_busy itself in driver code. For this reason the below
52      * busy wait does not have any practical effect, but we keep it in place
53      * for those platforms which might decide to implement IRQ-based UART
54      */
55     while (STDIO_DRIVER.GetStatus().tx_busy);
56 
57     return STDIO_DRIVER.GetTxCount();
58 }
59 
stdio_is_initialized_reset(void)60 void stdio_is_initialized_reset(void)
61 {
62     is_initialized = false;
63 }
64 
stdio_is_initialized(void)65 bool stdio_is_initialized(void)
66 {
67     return is_initialized;
68 }
69 
70 /* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
71 #if defined(__ARMCC_VERSION)
72 /* Struct FILE is implemented in stdio.h. Used to redirect printf to
73  * STDIO_DRIVER
74  */
75 FILE __stdout;
76 FILE __stderr;
77 /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
fputc(int ch,FILE * f)78 int fputc(int ch, FILE *f)
79 {
80     (void)f;
81 
82     /* Send byte to USART */
83     (void)stdio_output_string((const char *)&ch, 1);
84 
85     /* Return character written */
86     return ch;
87 }
88 
89 /* Redirect sdtio for PicoLib in LLVM toolchain
90    as per https://github.com/picolibc/picolibc/blob/main/doc/os.md
91    'fputch()' named intentionally different from 'fputc()' from picolib */
92 #elif defined(__clang_major__)
93 
fputch(char ch,struct __file * f)94 int fputch(char ch, struct __file *f)
95 {
96     (void)f;
97 
98     /* Send byte to USART */
99     (void)stdio_output_string((const char *)&ch, 1);
100 
101     /* Return character written */
102     return ch;
103 }
104 
105 static FILE __stdio = FDEV_SETUP_STREAM(fputch, NULL, NULL, _FDEV_SETUP_WRITE);
106 FILE *const stdin = &__stdio;
107 __strong_reference(stdin, stdout);
108 __strong_reference(stdin, stderr);
109 
110 #elif defined(__GNUC__)
111 /* Redirects printf to STDIO_DRIVER in case of GNUARM */
_write(int fd,char * str,int len)112 int _write(int fd, char *str, int len)
113 {
114     (void)fd;
115 
116     /* Send string and return the number of characters written */
117     return stdio_output_string(str, (uint32_t)len);
118 }
119 #elif defined(__ICCARM__)
putchar(int ch)120 int putchar(int ch)
121 {
122     /* Send byte to USART */
123     (void)stdio_output_string((const char *)&ch, 1);
124 
125     /* Return character written */
126     return ch;
127 }
128 #endif
129 
stdio_init(void)130 void stdio_init(void)
131 {
132     int32_t ret = ARM_DRIVER_ERROR;
133 
134     ret = STDIO_DRIVER.Initialize(NULL);
135     if (ret != ARM_DRIVER_OK) {
136         assert(0);
137         return;
138     }
139 
140     ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
141     if (ret != ARM_DRIVER_OK) {
142         assert(0);
143         return;
144     }
145 
146     ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS, DEFAULT_UART_BAUDRATE);
147     if (ret != ARM_DRIVER_OK) {
148         assert(0);
149         return;
150     }
151 
152     ret = STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
153     if (ret != ARM_DRIVER_OK) {
154         assert(0);
155         return;
156     }
157 
158     is_initialized = true;
159 }
160 
stdio_uninit(void)161 void stdio_uninit(void)
162 {
163     int32_t ret = ARM_DRIVER_ERROR;
164 
165     ret = STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
166     /* FixMe: Still allow this function not to be implemented as in blocking
167      *        mode there is not much that needs to be done when powering off
168      */
169     if ((ret != ARM_DRIVER_OK) && (ret != ARM_DRIVER_ERROR_UNSUPPORTED)) {
170         assert(0);
171         return;
172     }
173 
174     ret = STDIO_DRIVER.Uninitialize();
175     if (ret != ARM_DRIVER_OK) {
176         assert(0);
177         return;
178     }
179 
180     is_initialized = false;
181 }
182