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 /* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
53 #if defined(__ARMCC_VERSION)
54 /* Struct FILE is implemented in stdio.h. Used to redirect printf to
55  * STDIO_DRIVER
56  */
57 FILE __stdout;
58 FILE __stderr;
59 /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
fputc(int ch,FILE * f)60 int fputc(int ch, FILE *f)
61 {
62     (void)f;
63 
64     /* Send byte to USART */
65     (void)stdio_output_string((const unsigned char *)&ch, 1);
66 
67     /* Return character written */
68     return ch;
69 }
70 #elif defined(__GNUC__)
71 /* Redirects printf to STDIO_DRIVER in case of GNUARM */
_write(int fd,char * str,int len)72 int _write(int fd, char *str, int len)
73 {
74     (void)fd;
75 
76     /* Send string and return the number of characters written */
77     return stdio_output_string((const unsigned char *)str, (uint32_t)len);
78 }
79 #elif defined(__ICCARM__)
putchar(int ch)80 int putchar(int ch)
81 {
82     /* Send byte to USART */
83     (void)stdio_output_string((const unsigned char *)&ch, 1);
84 
85     /* Return character written */
86     return ch;
87 }
88 #endif
89 
stdio_init(void)90 void stdio_init(void)
91 {
92     int32_t ret;
93     ret = STDIO_DRIVER.Initialize(NULL);
94     ASSERT_HIGH(ret);
95 
96     ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
97     ASSERT_HIGH(ret);
98 
99     ret = STDIO_DRIVER.Control(DEFAULT_UART_CONTROL | ARM_USART_MODE_ASYNCHRONOUS,
100                                DEFAULT_UART_BAUDRATE);
101     ASSERT_HIGH(ret);
102     (void)ret;
103 
104     (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
105 }
106 
stdio_uninit(void)107 void stdio_uninit(void)
108 {
109     int32_t ret;
110 
111     (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
112 
113     ret = STDIO_DRIVER.Uninitialize();
114     ASSERT_HIGH(ret);
115     (void)ret;
116 }
117